Skip to content

Commit

Permalink
Added unit tests for WebApi ServiceCollection and AuthenticationBuild…
Browse files Browse the repository at this point in the history
…er extensions. Minor fixes. (#93)

Added unit tests for
- WebApiAuthenticationBuilderExtensions
- WebApiServiceCollectionExtensions
  • Loading branch information
pmaytak authored Apr 10, 2020
1 parent 5598aa7 commit 7293921
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 111 deletions.
1 change: 0 additions & 1 deletion src/Microsoft.Identity.Web/AuthorityHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ internal static string BuildAuthority(MicrosoftIdentityOptions options)
var userFlow = options.DefaultUserFlow;
return new Uri(baseUri, new PathString($"{pathBase}/{domain}/{userFlow}/v2.0")).ToString();
}

else
{
// Cannot build AAD authority without tenant id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public static IServiceCollection AddProtectedWebApiCallsProtectedWebApi(
options => configuration.Bind(configSectionName, options),
jwtBearerScheme);
}

/// <summary>
/// Protects the Web API with Microsoft identity platform (formerly Azure AD v2.0)
/// This supposes that the configuration files have a section named configSectionName (typically "AzureAD")
Expand All @@ -121,6 +122,8 @@ public static IServiceCollection AddProtectedWebApiCallsProtectedWebApi(

services.Configure<JwtBearerOptions>(jwtBearerScheme, options =>
{
options.Events ??= new JwtBearerEvents();

options.Events.OnTokenValidated = async context =>
{
context.HttpContext.StoreTokenUsedToCallWebAPI(context.SecurityToken as JwtSecurityToken);
Expand Down
16 changes: 14 additions & 2 deletions tests/Microsoft.Identity.Web.Test.Common/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class TestConstants
public const string ApiAudience = "api://" + ApiClientId;
public const string ApiClientId = "1EE5A092-0DFD-42B6-88E5-C517C0141321";

public const string ClientId = "87f0ee88-8251-48b3-8825-e0c9563f5234";
public const string TenantId = "some-tenant-id";
public const string TenantIdAsGuid = "da41245a5-11b3-996c-00a8-4d99re19f292";
public const string Domain = "contoso.onmicrosoft.com";
Expand All @@ -25,11 +26,11 @@ public static class TestConstants
public const string AuthorityOrganizationsTenant = AadInstance + "/organizations/";
public const string AuthorityOrganizationsUSTenant = "https://" + ProductionPrefNetworkUSEnvironment + "/organizations";

public const string AuthorityWithTenantSpecified = AadInstance + "/" + TenantId;
public const string AuthorityWithTenantSpecified = AadInstance + "/" + TenantIdAsGuid;
public const string AuthorityCommonTenantWithV2 = AadInstance + "/common/v2.0";
public const string AuthorityOrganizationsWithV2 = AadInstance + "/organizations/v2.0";
public const string AuthorityOrganizationsUSWithV2 = AuthorityOrganizationsUSTenant + "/v2.0";
public const string AuthorityWithTenantSpecifiedWithV2 = AadInstance + "/" + TenantId + "/v2.0";
public const string AuthorityWithTenantSpecifiedWithV2 = AadInstance + "/" + TenantIdAsGuid + "/v2.0";
public const string AadIssuer = AadInstance + "/" + TenantIdAsGuid + "/v2.0";

// B2C
Expand Down Expand Up @@ -81,5 +82,16 @@ public static class TestConstants
public const string ConfidentialClientKeyVaultUri = "https://buildautomation.vault.azure.net/secrets/AzureADIdentityDivisionTestAgentSecret/";
public const string ConfidentialClientId = "16dab2ba-145d-4b1b-8569-bf4b9aed4dc8";
public const string ConfidentialClientLabTenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";

//This value is only for testing purposes. It is for a certificate that is not used for anything other than running tests
public const string certificateX5c = @"MIIDHzCCAgegAwIBAgIQM6NFYNBJ9rdOiK+C91ZzFDANBgkqhkiG9w0BAQsFADAgMR4wHAYDVQQDExVBQ1MyQ2xpZW50Q2VydGlmaWNhdGUwHhcNMTIwNTIyMj
IxMTIyWhcNMzAwNTIyMDcwMDAwWjAgMR4wHAYDVQQDExVBQ1MyQ2xpZW50Q2VydGlmaWNhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCh7HjK
YyVMDZDT64OgtcGKWxHmK2wqzi2LJb65KxGdNfObWGxh5HQtjzrgHDkACPsgyYseqxhGxHh8I/TR6wBKx/AAKuPHE8jB4hJ1W6FczPfb7FaMV9xP0qNQrbNGZU
YbCdy7U5zIw4XrGq22l6yTqpCAh59DLufd4d7x8fCgUDV3l1ZwrncF0QrBRzns/O9Ex9pXsi2DzMa1S1PKR81D9q5QSW7LZkCgSSqI6W0b5iodx/a3RBvW3l7d
noW2fPqkZ4iMcntGNqgsSGtbXPvUR3fFdjmg+xq9FfqWyNxShlZg4U+wE1v4+kzTJxd9sgD1V0PKgW57zyzdOmTyFPJFAgMBAAGjVTBTMFEGA1UdAQRKMEiAEM
9qihCt+12P5FrjVMAEYjShIjAgMR4wHAYDVQQDExVBQ1MyQ2xpZW50Q2VydGlmaWNhdGWCEDOjRWDQSfa3ToivgvdWcxQwDQYJKoZIhvcNAQELBQADggEBAIm6
gBOkSdYjXgOvcJGgE4FJkKAMQzAhkdYq5+stfUotG6vZNL3nVOOA6aELMq/ENhrJLC3rTwLOIgj4Cy+B7BxUS9GxTPphneuZCBzjvqhzP5DmLBs8l8qu10XAsh
y1NFZmB24rMoq8C+HPOpuVLzkwBr+qcCq7ry2326auogvVMGaxhHlwSLR4Q1OhRjKs8JctCk2+5Qs1NHfawa7jWHxdAK6cLm7Rv/c0ig2Jow7wRaI5ciAcEjX7
m1t9gRT1mNeeluL4cZa6WyVXqXc6U2wfR5DY6GOMUubN5Nr1n8Czew8TPfab4OG37BuEMNmBpqoRrRgFnDzVtItOnhuFTa0=";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public static HttpContext CreateHttpContext(string[] userScopes)
httpContext.User = new ClaimsPrincipal(
new ClaimsIdentity(new Claim[]
{
new Claim("http://schemas.microsoft.com/identity/claims/scope", string.Join(' ', userScopes))
new Claim(ClaimConstants.Scope, string.Join(' ', userScopes))
})
);

return httpContext;
}
}
}
}
Loading

0 comments on commit 7293921

Please sign in to comment.