Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 2503a0b

Browse files
Adding KeychainTests
1 parent 05c2cd3 commit 2503a0b

File tree

4 files changed

+108
-10
lines changed

4 files changed

+108
-10
lines changed

src/GitHub.Api/Authentication/Keychain.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,29 @@ class Keychain : IKeychain
3232

3333
private readonly ILogging logger = Logging.GetLogger<Keychain>();
3434

35+
private readonly IEnvironment environment;
36+
private readonly IFileSystem fileSystem;
3537
private readonly ICredentialManager credentialManager;
36-
private readonly NPath cachePath;
38+
private readonly string cachePath;
3739

3840
// loaded at the start of application from cached/serialized data
3941
private Dictionary<UriString, Connection> connectionCache = new Dictionary<UriString, Connection>();
4042

4143
// cached credentials loaded from git to pass to GitHub/ApiClient
42-
private Dictionary<UriString, KeychainAdapter> keychainAdapters =
43-
new Dictionary<UriString, KeychainAdapter>();
44+
private readonly Dictionary<UriString, KeychainAdapter> keychainAdapters
45+
= new Dictionary<UriString, KeychainAdapter>();
4446

45-
public Keychain(IEnvironment environment, ICredentialManager credentialManager)
47+
public Keychain(IEnvironment environment, IFileSystem fileSystem, ICredentialManager credentialManager)
4648
{
47-
cachePath = environment.UserCachePath.Combine(ConnectionFile);
49+
Guard.ArgumentNotNull(environment, nameof(environment));
50+
Guard.ArgumentNotNull(fileSystem, nameof(fileSystem));
51+
Guard.ArgumentNotNull(credentialManager, nameof(credentialManager));
52+
53+
Guard.NotNull(environment, environment.UserCachePath, nameof(environment.UserCachePath));
54+
55+
cachePath = environment.UserCachePath.Combine(ConnectionFile).ToString();
56+
this.environment = environment;
57+
this.fileSystem = fileSystem;
4858
this.credentialManager = credentialManager;
4959
}
5060

@@ -101,17 +111,18 @@ private void ReadCacheFromDisk()
101111
logger.Trace("ReadCacheFromDisk Path:{0}", cachePath.ToString());
102112

103113
ConnectionCacheItem[] connections = null;
104-
if (cachePath.FileExists())
114+
115+
if (fileSystem.FileExists(cachePath))
105116
{
106-
var json = cachePath.ReadAllText();
117+
var json = fileSystem.ReadAllText(cachePath);
107118
try
108119
{
109120
connections = SimpleJson.DeserializeObject<ConnectionCacheItem[]>(json);
110121
}
111122
catch (Exception ex)
112123
{
113124
logger.Error(ex, "Error deserializing connection cache: {0}", cachePath);
114-
cachePath.Delete();
125+
fileSystem.FileDelete(cachePath);
115126
}
116127
}
117128

@@ -140,7 +151,7 @@ private void WriteCacheToDisk()
140151
}).ToArray();
141152

142153
var json = SimpleJson.SerializeObject(connectionCacheItems);
143-
cachePath.WriteAllText(json);
154+
fileSystem.WriteAllText(cachePath, json);
144155
}
145156

146157
/// <summary>

src/GitHub.Api/Platform/Platform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public IPlatform Initialize(IProcessManager processManager, ITaskManager taskMan
2727
if (CredentialManager == null)
2828
{
2929
CredentialManager = new GitCredentialManager(Environment, processManager, taskManager);
30-
Keychain = new Keychain(Environment, CredentialManager);
30+
Keychain = new Keychain(Environment, Environment.FileSystem, CredentialManager);
3131
Keychain.Initialize();
3232
}
3333

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Collections.Generic;
2+
using GitHub.Unity;
3+
using NCrunch.Framework;
4+
using NSubstitute;
5+
using NUnit.Framework;
6+
using TestUtils;
7+
8+
namespace UnitTests
9+
{
10+
[TestFixture, Isolated]
11+
public class KeychainTests
12+
{
13+
private static readonly SubstituteFactory SubstituteFactory = new SubstituteFactory();
14+
15+
[Test]
16+
public void Should_Initialize_When_Cache_Does_Not_Exist()
17+
{
18+
const string connectionsCachePath = "c:\\UserCachePath\\";
19+
20+
var environment = SubstituteFactory.CreateEnvironment();
21+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
22+
23+
var fileSystem = SubstituteFactory.CreateFileSystem();
24+
var credentialManager = NSubstitute.Substitute.For<ICredentialManager>();
25+
26+
var keychain = new Keychain(environment, fileSystem, credentialManager);
27+
keychain.Initialize();
28+
29+
fileSystem.Received(1).FileExists(@"c:\UserCachePath\connections.json");
30+
fileSystem.DidNotReceive().ReadAllText(Args.String);
31+
fileSystem.DidNotReceive().ReadAllLines(Args.String);
32+
}
33+
34+
[Test]
35+
public void Should_Initialize_When_Cache_Invalid()
36+
{
37+
const string connectionsCachePath = "c:\\UserCachePath\\";
38+
const string connectionsCacheFile = @"c:\UserCachePath\connections.json";
39+
40+
var environment = SubstituteFactory.CreateEnvironment();
41+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
42+
43+
var fileSystem = SubstituteFactory.CreateFileSystem(new CreateFileSystemOptions()
44+
{
45+
FilesThatExist = new List<string> { connectionsCacheFile },
46+
FileContents = new Dictionary<string, IList<string>> {
47+
{connectionsCacheFile, new List<string> { @"invalid json" }}
48+
}
49+
});
50+
51+
var credentialManager = Substitute.For<ICredentialManager>();
52+
53+
var keychain = new Keychain(environment, fileSystem, credentialManager);
54+
keychain.Initialize();
55+
56+
fileSystem.Received(1).FileExists(connectionsCacheFile);
57+
fileSystem.Received(1).ReadAllText(connectionsCacheFile);
58+
}
59+
60+
[Test]
61+
public void Should_Initialize_When_Cache_Exists()
62+
{
63+
const string connectionsCachePath = "c:\\UserCachePath\\";
64+
const string connectionsCacheFile = @"c:\UserCachePath\connections.json";
65+
66+
var environment = SubstituteFactory.CreateEnvironment();
67+
environment.UserCachePath.Returns(info => connectionsCachePath.ToNPath());
68+
69+
var fileSystem = SubstituteFactory.CreateFileSystem(new CreateFileSystemOptions()
70+
{
71+
FilesThatExist = new List<string> { connectionsCacheFile },
72+
FileContents = new Dictionary<string, IList<string>> {
73+
{connectionsCacheFile, new List<string> { @"[{""Host"":""https://github.com/"",""Username"":""StanleyGoldman""}]" }}
74+
}
75+
});
76+
77+
var credentialManager = Substitute.For<ICredentialManager>();
78+
79+
var keychain = new Keychain(environment, fileSystem, credentialManager);
80+
keychain.Initialize();
81+
82+
fileSystem.Received(1).FileExists(connectionsCacheFile);
83+
fileSystem.Received(1).ReadAllText(connectionsCacheFile);
84+
}
85+
}
86+
}

src/tests/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
</Reference>
8181
</ItemGroup>
8282
<ItemGroup>
83+
<Compile Include="Authentication\KeychainTests.cs" />
8384
<Compile Include="Git\GitConfigTests.cs" />
8485
<Compile Include="IO\BranchListOutputProcessorTests.cs" />
8586
<Compile Include="Extensions\EnvironmentExtensionTests.cs" />

0 commit comments

Comments
 (0)