2016-03-19 08:26:44 +03:00
|
|
|
|
using System.Diagnostics;
|
2018-03-05 21:45:20 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2017-06-22 23:58:36 +03:00
|
|
|
|
using Xunit;
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
2015-09-14 22:18:02 +03:00
|
|
|
|
namespace Microsoft.Alm.Authentication.Test
|
2015-04-03 23:35:36 +03:00
|
|
|
|
{
|
|
|
|
|
public class BasicAuthTests
|
|
|
|
|
{
|
|
|
|
|
public BasicAuthTests()
|
|
|
|
|
{
|
|
|
|
|
Trace.Listeners.AddRange(Debug.Listeners);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 23:58:36 +03:00
|
|
|
|
[Fact]
|
2018-03-05 21:45:20 +03:00
|
|
|
|
public async Task BasicAuthDeleteCredentialsTest()
|
2015-04-03 23:35:36 +03:00
|
|
|
|
{
|
2016-02-26 00:00:51 +03:00
|
|
|
|
TargetUri targetUri = new TargetUri("http://localhost");
|
2018-03-06 01:42:10 +03:00
|
|
|
|
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-delete");
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
await basicAuth.CredentialStore.WriteCredentials(targetUri, new Credential("username", "password"));
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
await basicAuth.DeleteCredentials(targetUri);
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
Assert.Null(await basicAuth.CredentialStore.ReadCredentials(targetUri));
|
2015-04-03 23:35:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 23:58:36 +03:00
|
|
|
|
[Fact]
|
2018-03-05 21:45:20 +03:00
|
|
|
|
public async Task BasicAuthGetCredentialsTest()
|
2015-04-03 23:35:36 +03:00
|
|
|
|
{
|
2016-02-26 00:00:51 +03:00
|
|
|
|
TargetUri targetUri = new TargetUri("http://localhost");
|
2018-03-06 01:42:10 +03:00
|
|
|
|
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-get");
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
|
|
|
|
Credential credentials = null;
|
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
Assert.Null(credentials = await basicAuth.GetCredentials(targetUri));
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
|
|
|
|
credentials = new Credential("username", "password");
|
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
await basicAuth.CredentialStore.WriteCredentials(targetUri, credentials);
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
Assert.NotNull(credentials = await basicAuth.GetCredentials(targetUri));
|
2015-04-03 23:35:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 23:58:36 +03:00
|
|
|
|
[Fact]
|
2018-03-05 21:45:20 +03:00
|
|
|
|
public async Task BasicAuthSetCredentialsTest()
|
2015-04-03 23:35:36 +03:00
|
|
|
|
{
|
2016-02-26 00:00:51 +03:00
|
|
|
|
TargetUri targetUri = new TargetUri("http://localhost");
|
2018-03-06 01:42:10 +03:00
|
|
|
|
BasicAuthentication basicAuth = GetBasicAuthentication(RuntimeContext.Default, "basic-set");
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
|
|
|
|
Credential credentials = null;
|
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
Assert.Null(credentials = await basicAuth.GetCredentials(targetUri));
|
2017-06-22 23:58:36 +03:00
|
|
|
|
Assert.Throws<System.ArgumentNullException>(() =>
|
2015-04-03 23:35:36 +03:00
|
|
|
|
{
|
2018-03-05 21:45:20 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () => { await basicAuth.SetCredentials(targetUri, credentials); }).Wait();
|
|
|
|
|
}
|
|
|
|
|
catch (System.AggregateException exception)
|
|
|
|
|
{
|
|
|
|
|
exception = exception.Flatten();
|
|
|
|
|
throw exception.InnerException;
|
|
|
|
|
}
|
2017-06-22 23:58:36 +03:00
|
|
|
|
});
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
|
|
|
|
credentials = new Credential("username", "password");
|
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
await basicAuth.SetCredentials(targetUri, credentials);
|
2016-08-05 17:48:04 +03:00
|
|
|
|
|
2018-03-05 21:45:20 +03:00
|
|
|
|
Assert.NotNull(credentials = await basicAuth.GetCredentials(targetUri));
|
2015-04-03 23:35:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 01:42:10 +03:00
|
|
|
|
private BasicAuthentication GetBasicAuthentication(RuntimeContext context, string @namespace)
|
2015-04-03 23:35:36 +03:00
|
|
|
|
{
|
2018-03-06 01:42:10 +03:00
|
|
|
|
ICredentialStore credentialStore = new SecretCache(context, @namespace);
|
2015-04-03 23:35:36 +03:00
|
|
|
|
|
2018-03-06 01:42:10 +03:00
|
|
|
|
return new BasicAuthentication(context, credentialStore, NtlmSupport.Auto, null, null);
|
2015-04-03 23:35:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|