Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
readd adfs with tenantid tests...they were committed but not pushed :( (
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyf19 authored Feb 14, 2020
1 parent eee9ddb commit af10dd1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 48 deletions.
1 change: 1 addition & 0 deletions tests/Test.ADAL.NET.Common/AdalTestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class AdalTestConstants
public static readonly string DefaultResource = "resource1";
public static readonly string AnotherResource = "resource2";
public static readonly string DefaultAdfsAuthorityTenant = "https://#.contoso.com/adfs/";
public static readonly string AdfsAuthorityWithTenant = "https://#.contoso.com/adfs/" + SomeTenantId + "/";
public static readonly string DefaultAuthorityHomeTenant = "https://#.microsoftonline.com/home/";
public static readonly string SomeTenantId = "some-tenant-id";
public static readonly string TenantSpecificAuthority = "https://#.microsoftonline.com/" + SomeTenantId + "/";
Expand Down
109 changes: 65 additions & 44 deletions tests/Test.ADAL.NET.Unit.net45/DeviceCodeFlowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,61 @@ public void TestDeviceCodeCancel()

[TestMethod]
public async Task AdfsPositiveTestAsync()
{
await CreateAdfsDeviceCodeTestAsync(AdalTestConstants.DefaultAdfsAuthorityTenant).ConfigureAwait(false);
}

[TestMethod]
public async Task AdfsWithTenantIdPositiveTestAsync()
{
await CreateAdfsDeviceCodeTestAsync(AdalTestConstants.AdfsAuthorityWithTenant).ConfigureAwait(false); // adfs should never have a tenantId
}

[TestMethod]
public async Task AdfsPostMethodTestAsync()
{
await CreateAdfsPostMethodTestAsync(AdalTestConstants.DefaultAdfsAuthorityTenant).ConfigureAwait(false);
}

[TestMethod]
public async Task AdfsWithTenantIdPostMethodTestAsync()
{
await CreateAdfsPostMethodTestAsync(AdalTestConstants.AdfsAuthorityWithTenant).ConfigureAwait(false); // adfs should never have a tenantId
}

private async Task CreateAdfsPostMethodTestAsync(string authority)
{
using (var httpManager = new MockHttpManager())
{
var serviceBundle = ServiceBundle.CreateWithCustomHttpManager(httpManager);

httpManager.AddMockHandler(new MockHttpMessageHandler()
{
Method = HttpMethod.Post,
Url = "https://#.contoso.com/adfs/oauth2/devicecode",
ResponseMessage = MockHelpers.CreateSuccessDeviceCodeResponseMessage()
});

AuthenticationContext context = new AuthenticationContext(
serviceBundle,
authority,
AuthorityValidationType.False,
null);

DeviceCodeResult dcr = await context.AcquireDeviceCodeAsync(
AdalTestConstants.DefaultResource,
AdalTestConstants.DefaultClientId)
.ConfigureAwait(false);

Assert.IsNotNull(dcr);
Assert.AreEqual("some-user-code", dcr.UserCode);

Assert.AreEqual(authority, context.Authority);
CheckAdfsEndpoints(authority, context.Authenticator);
}
}

private async Task CreateAdfsDeviceCodeTestAsync(string authority)
{
using (var httpManager = new MockHttpManager())
{
Expand Down Expand Up @@ -197,59 +252,25 @@ public async Task AdfsPositiveTestAsync()
TokenCache cache = new TokenCache();
AuthenticationContext context = new AuthenticationContext(
serviceBundle,
AdalTestConstants.DefaultAdfsAuthorityTenant,
authority,
AuthorityValidationType.False,
cache);
AuthenticationResult result = await context.AcquireTokenByDeviceCodeAsync(dcr).ConfigureAwait(false);
Assert.IsNotNull(result);
Assert.AreEqual("some-access-token", result.AccessToken);

Assert.AreEqual("https://#.contoso.com/adfs/", context.Authority);
Assert.AreEqual("https://#.contoso.com/adfs/", context.Authenticator.Authority);
Assert.AreEqual(AuthorityType.ADFS, context.Authenticator.AuthorityType);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/authorize", context.Authenticator.AuthorizationUri);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/devicecode", context.Authenticator.DeviceCodeUri);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", context.Authenticator.SelfSignedJwtAudience);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", context.Authenticator.TokenUri);
CheckAdfsEndpoints(authority, context.Authenticator);
}
}

[TestMethod]
public async Task AdfsPostMethodTestAsync()
private void CheckAdfsEndpoints(string authority, Authenticator authenticator)
{
using (var httpManager = new MockHttpManager())
{
var serviceBundle = ServiceBundle.CreateWithCustomHttpManager(httpManager);

httpManager.AddMockHandler(new MockHttpMessageHandler()
{
Method = HttpMethod.Post,
Url = "https://#.contoso.com/adfs/oauth2/devicecode",
ResponseMessage = MockHelpers.CreateSuccessDeviceCodeResponseMessage()
});

AuthenticationContext context = new AuthenticationContext(
serviceBundle,
AdalTestConstants.DefaultAdfsAuthorityTenant,
AuthorityValidationType.False,
null);

DeviceCodeResult dcr = await context.AcquireDeviceCodeAsync(
AdalTestConstants.DefaultResource,
AdalTestConstants.DefaultClientId)
.ConfigureAwait(false);

Assert.IsNotNull(dcr);
Assert.AreEqual("some-user-code", dcr.UserCode);

Assert.AreEqual("https://#.contoso.com/adfs/", context.Authority);
Assert.AreEqual("https://#.contoso.com/adfs/", context.Authenticator.Authority);
Assert.AreEqual(AuthorityType.ADFS, context.Authenticator.AuthorityType);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/authorize", context.Authenticator.AuthorizationUri);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/devicecode", context.Authenticator.DeviceCodeUri);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", context.Authenticator.SelfSignedJwtAudience);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", context.Authenticator.TokenUri);
}
Assert.AreEqual(authority, authenticator.Authority);
Assert.AreEqual(AuthorityType.ADFS, authenticator.AuthorityType);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/authorize", authenticator.AuthorizationUri);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/devicecode", authenticator.DeviceCodeUri);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", authenticator.SelfSignedJwtAudience);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", authenticator.TokenUri);
}
}
}
}
24 changes: 20 additions & 4 deletions tests/Test.ADAL.NET.Unit.net45/InstanceDiscoveryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
using Microsoft.Identity.Core;
using Microsoft.Identity.Core.Cache;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Http;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Instance;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.ClientCreds;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Platform;
using Test.ADAL.NET.Common;
using Test.ADAL.NET.Common.Mocks;
using MockHttpMessageHandler = Test.ADAL.NET.Common.Mocks.MockHttpMessageHandler;
using AuthorityType = Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Instance.AuthorityType;

namespace Test.ADAL.NET.Unit
{
Expand Down Expand Up @@ -169,12 +169,28 @@ public async Task TestInstanceDiscovery_WhenAuthorityIsValidAndMetadataIsReturne

[TestMethod]
public async Task TestInstanceDiscovery_WhenAuthorityIsAdfs_ShouldNotDoInstanceDiscoveryAsync()
{
await BasicAdfsTestAsync(AdalTestConstants.DefaultAdfsAuthorityTenant).ConfigureAwait(false);
}

[TestMethod]
public async Task TestInstanceDiscovery_WhenAuthorityIsAdfsWithTenantSpecified_ShouldNotDoInstanceDiscoveryAsync()
{
await BasicAdfsTestAsync(AdalTestConstants.AdfsAuthorityWithTenant).ConfigureAwait(false);
}

private async Task BasicAdfsTestAsync(string authority)
{
using (var httpManager = new MockHttpManager())
{
var serviceBundle = ServiceBundle.CreateWithCustomHttpManager(httpManager);
var authenticator = new Authenticator(serviceBundle, "https://#.contoso.com/adfs", false);
var authenticator = new Authenticator(serviceBundle, authority, false);
await authenticator.UpdateFromTemplateAsync(new RequestContext(null, new AdalLogger(new Guid()))).ConfigureAwait(false);
Assert.AreEqual(authority, authenticator.Authority);
Assert.AreEqual(AuthorityType.ADFS, authenticator.AuthorityType);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/authorize", authenticator.AuthorizationUri);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", authenticator.SelfSignedJwtAudience);
Assert.AreEqual("https://#.contoso.com/adfs/oauth2/token", authenticator.TokenUri);
}
}

Expand Down Expand Up @@ -214,7 +230,7 @@ public void TestInstanceDiscovery_WhenEndpointTimesOut_ShouldThrowCorrectErrorMe
CreateFailureMockHandler(httpManager);
CreateFailureMockHandler(httpManager);

RequestContext requestContext = new RequestContext(null, new AdalLogger(new Guid()));
RequestContext requestContext = new RequestContext(null, new AdalLogger(new Guid()));
string givenHost = "sts.microsoft.com";

// ADAL still behaves correctly using developer provided authority
Expand Down Expand Up @@ -481,4 +497,4 @@ public async Task TestInstanceDiscovery_WhenMetadataIsReturned_ShouldUsePreferre
}
}
}
}
}

0 comments on commit af10dd1

Please # to comment.