This repository was archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathBasicAuthTests.cs
More file actions
117 lines (87 loc) · 4.51 KB
/
BasicAuthTests.cs
File metadata and controls
117 lines (87 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System.Diagnostics;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.Alm.Authentication.Test
{
public class BasicAuthTests
{
public BasicAuthTests()
{
Trace.Listeners.AddRange(Debug.Listeners);
}
[Fact]
public async Task BasicAuthDeleteCredentialsTest()
{
TargetUri targetUri = new TargetUri("http://localhost");
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-delete");
await basicAuth.CredentialStore.WriteCredentials(targetUri, new Credential("username", "password"));
await basicAuth.DeleteCredentials(targetUri);
Assert.Null(await basicAuth.CredentialStore.ReadCredentials(targetUri));
}
[Fact]
public async Task BasicAuthUserUriDeleteCredentialsTest()
{
TargetUri targetUserUri = new TargetUri("http://username@localhost");
TargetUri targetGenericUri = new TargetUri("http://localhost");
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-delete-user");
await basicAuth.CredentialStore.WriteCredentials(targetUserUri, new Credential("username", "password"));
await basicAuth.CredentialStore.WriteCredentials(targetGenericUri, new Credential("username", "password"));
/* User-included format is what comes out of "erase" action, so that's what we want to test */
await basicAuth.DeleteCredentials(targetUserUri);
Assert.Null(await basicAuth.CredentialStore.ReadCredentials(targetUserUri));
Assert.Null(await basicAuth.CredentialStore.ReadCredentials(targetGenericUri));
}
[Fact]
public async Task BasicAuthGetCredentialsTest()
{
TargetUri targetUri = new TargetUri("http://localhost");
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-get");
Credential credentials = null;
Assert.Null(credentials = await basicAuth.GetCredentials(targetUri));
credentials = new Credential("username", "password");
await basicAuth.CredentialStore.WriteCredentials(targetUri, credentials);
Assert.NotNull(credentials = await basicAuth.GetCredentials(targetUri));
}
[Fact]
public async Task BasicAuthUserUriGetCredentialsTest()
{
TargetUri targetUserUri = new TargetUri("http://username@localhost");
TargetUri targetGenericUri = new TargetUri("http://localhost");
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-get-user");
Credential credentials = null;
Assert.Null(credentials = await basicAuth.GetCredentials(targetGenericUri));
Assert.Null(credentials = await basicAuth.GetCredentials(targetUserUri));
credentials = new Credential("username", "password");
await basicAuth.CredentialStore.WriteCredentials(targetGenericUri, credentials);
Assert.NotNull(credentials = await basicAuth.GetCredentials(targetUserUri));
}
[Fact]
public async Task BasicAuthSetCredentialsTest()
{
TargetUri targetUri = new TargetUri("http://localhost");
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-set");
Credential credentials = null;
Assert.Null(credentials = await basicAuth.GetCredentials(targetUri));
Assert.Throws<System.ArgumentNullException>(() =>
{
try
{
Task.Run(async () => { await basicAuth.SetCredentials(targetUri, credentials); }).Wait();
}
catch (System.AggregateException exception)
{
exception = exception.Flatten();
throw exception.InnerException;
}
});
credentials = new Credential("username", "password");
await basicAuth.SetCredentials(targetUri, credentials);
Assert.NotNull(credentials = await basicAuth.GetCredentials(targetUri));
}
private BasicAuthentication GetBasicAuthentication(RuntimeContext context, string @namespace)
{
ICredentialStore credentialStore = new SecretCache(context, @namespace);
return new BasicAuthentication(context, credentialStore, NtlmSupport.Auto, null, null);
}
}
}