Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 8eabcf0

Browse files
Added extension methods for AuthenticationInfo
1 parent 75242fa commit 8eabcf0

File tree

7 files changed

+123
-1
lines changed

7 files changed

+123
-1
lines changed

src/GitTools.Core.Shared/Git/AuthenticationInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public class AuthenticationInfo
44
{
55
public string Username { get; set; }
66
public string Password { get; set; }
7+
public string Token { get; set; }
78
}
89
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace GitTools.Git
2+
{
3+
using Logging;
4+
5+
public static class AuthenticationInfoExtensions
6+
{
7+
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
8+
9+
public static bool IsEmpty(this AuthenticationInfo authenticationInfo)
10+
{
11+
if (authenticationInfo == null)
12+
{
13+
return true;
14+
}
15+
16+
if (IsUsernameAndPasswordAuthentication(authenticationInfo))
17+
{
18+
return false;
19+
}
20+
21+
if (IsTokenAuthentication(authenticationInfo))
22+
{
23+
return false;
24+
}
25+
26+
return true;
27+
}
28+
29+
public static bool IsUsernameAndPasswordAuthentication(this AuthenticationInfo authenticationInfo)
30+
{
31+
if (authenticationInfo == null)
32+
{
33+
return false;
34+
}
35+
36+
if (string.IsNullOrWhiteSpace(authenticationInfo.Username))
37+
{
38+
return false;
39+
}
40+
41+
if (string.IsNullOrWhiteSpace(authenticationInfo.Password))
42+
{
43+
return false;
44+
}
45+
46+
return true;
47+
}
48+
49+
public static bool IsTokenAuthentication(this AuthenticationInfo authenticationInfo)
50+
{
51+
if (authenticationInfo == null)
52+
{
53+
return false;
54+
}
55+
56+
if (string.IsNullOrWhiteSpace(authenticationInfo.Token))
57+
{
58+
return false;
59+
}
60+
61+
return true;
62+
}
63+
}
64+
}

src/GitTools.Core.Shared/GitTools.Core.Shared.projitems

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
<Compile Include="$(MSBuildThisFileDirectory)App_Packages\LibLog.4.2\LibLog.cs" />
1313
<Compile Include="$(MSBuildThisFileDirectory)Exceptions\GitToolsException.cs" />
1414
<Compile Include="$(MSBuildThisFileDirectory)Git\AuthenticationInfo.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)Git\Extensions\AuthenticationInfoExtensions.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)Git\Extensions\IRepositoryExtensions.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)Git\Extensions\LibGitExtensions.cs" />
1618
<Compile Include="$(MSBuildThisFileDirectory)Git\GitRepository.cs" />
17-
<Compile Include="$(MSBuildThisFileDirectory)Git\LibGitExtensions.cs" />
1819
<Compile Include="$(MSBuildThisFileDirectory)Git\RepositoryLoader.cs" />
1920
<Compile Include="$(MSBuildThisFileDirectory)Git\TaggedCommit.cs" />
2021
<Compile Include="$(MSBuildThisFileDirectory)IO\TemporaryFilesContext.cs" />
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace GitTools.Tests.Git.Extensions
2+
{
3+
using GitTools.Git;
4+
using NUnit.Framework;
5+
6+
[TestFixture]
7+
public class AuthenticationInfoExtensionsTests
8+
{
9+
[TestCase("user", "password", null, false)]
10+
[TestCase(null, null, "token", false)]
11+
[TestCase(null, null, null, true)]
12+
public void TheIsEmptyMethod(string username, string password, string token, bool expectedValue)
13+
{
14+
var authenticationInfo = new AuthenticationInfo
15+
{
16+
Username = username,
17+
Password = password,
18+
Token = token
19+
};
20+
21+
Assert.AreEqual(expectedValue, authenticationInfo.IsEmpty());
22+
}
23+
24+
[TestCase("user", "password", null, true)]
25+
[TestCase(null, null, "token", false)]
26+
[TestCase(null, null, null, false)]
27+
public void TheIsUsernameAndPasswordAuthenticationMethod(string username, string password, string token, bool expectedValue)
28+
{
29+
var authenticationInfo = new AuthenticationInfo
30+
{
31+
Username = username,
32+
Password = password,
33+
Token = token
34+
};
35+
36+
Assert.AreEqual(expectedValue, authenticationInfo.IsUsernameAndPasswordAuthentication());
37+
}
38+
39+
[TestCase("user", "password", null, false)]
40+
[TestCase(null, null, "token", true)]
41+
[TestCase(null, null, null, false)]
42+
public void TheIsTokenAuthenticationMethod(string username, string password, string token, bool expectedValue)
43+
{
44+
var authenticationInfo = new AuthenticationInfo
45+
{
46+
Username = username,
47+
Password = password,
48+
Token = token
49+
};
50+
51+
Assert.AreEqual(expectedValue, authenticationInfo.IsTokenAuthentication());
52+
}
53+
}
54+
}

src/GitTools.Core.Tests/GitRepositoryTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace GitTools.Tests
22
{
33
using Git;
4+
using GitTools.Git;
45
using LibGit2Sharp;
56
using NUnit.Framework;
67
using Shouldly;

src/GitTools.Core.Tests/GitTools.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Link>Properties\SolutionAssemblyInfo.cs</Link>
7777
</Compile>
7878
<Compile Include="GitRepositoryTests.cs" />
79+
<Compile Include="Git\Extensions\AuthenticationInfoExtensionsTests.cs" />
7980
<Compile Include="GlobalInitialization.cs" />
8081
<Compile Include="ModuleInitializer.cs" />
8182
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)