Skip to content

Commit 6b65126

Browse files
committed
add tests for all new providers
1 parent ba756b7 commit 6b65126

1 file changed

Lines changed: 79 additions & 11 deletions

File tree

test/KK.AspNetCore.EasyAuthAuthentication.Test/Services/EasyAuthWithHeaderServiceTest.cs

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,64 @@ namespace KK.AspNetCore.EasyAuthAuthentication.Test.Services
1111
using Microsoft.Extensions.Logging.Abstractions;
1212
using Newtonsoft.Json;
1313
using Xunit;
14+
using KK.AspNetCore.EasyAuthAuthentication.Services.Base;
15+
using KK.AspNetCore.EasyAuthAuthentication.Interfaces;
16+
using System.Linq;
17+
using KK.AspNetCore.EasyAuthAuthentication.Models;
1418

1519
public class EasyAuthWithHeaderServiceTest
1620
{
1721
private readonly ILoggerFactory loggerFactory = new NullLoggerFactory();
1822

19-
[Fact]
20-
public void IfTheAADIdTokenHeaderIsSetTheCanUseMethodMustReturnTrue()
23+
[Theory]
24+
[InlineData(typeof(EasyAuthAzureAdService), "aad", AuthTokenHeaderNames.AADIdToken)]
25+
[InlineData(typeof(EasyAuthTwitterService), "twitter", AuthTokenHeaderNames.TwitterAccessToken)]
26+
[InlineData(typeof(EasyAuthFacebookService), "facebook", AuthTokenHeaderNames.FacebookAccessToken)]
27+
[InlineData(typeof(EasyAuthGoogleService), "google", AuthTokenHeaderNames.GoogleIdToken)]
28+
[InlineData(typeof(EasyAuthMicrosoftService), "microsoftaccount", AuthTokenHeaderNames.MicrosoftAccessToken)]
29+
30+
public void IfTheAADIdTokenHeaderIsSetTheCanUseMethodMustReturnTrue(Type authServiceType, string idpName, string requiredHeader)
2131
{
2232
// Arrange
23-
var handler = new EasyAuthAzureAdService(this.loggerFactory.CreateLogger<EasyAuthAzureAdService>());
33+
var handler = this.CreateServiceInstance(authServiceType);
2434
var httpcontext = new DefaultHttpContext();
25-
httpcontext.Request.Headers.Add("X-MS-TOKEN-AAD-ID-TOKEN", "blup");
26-
httpcontext.Request.Headers.Add("X-MS-CLIENT-PRINCIPAL-IDP", "aad");
35+
httpcontext.Request.Headers.Add(requiredHeader, "blup");
36+
httpcontext.Request.Headers.Add("X-MS-CLIENT-PRINCIPAL-IDP", idpName);
2737
// Act
2838
var result = handler.CanHandleAuthentification(httpcontext);
2939
// Arrange
3040
Assert.True(result);
3141
}
3242

33-
[Fact]
34-
public void IfTheAuthorizationHeaderIsNotSetTheCanUseMethodMustReturnFalse()
43+
44+
45+
[Theory]
46+
[InlineData(typeof(EasyAuthAzureAdService))]
47+
[InlineData(typeof(EasyAuthTwitterService))]
48+
[InlineData(typeof(EasyAuthFacebookService))]
49+
[InlineData(typeof(EasyAuthGoogleService))]
50+
[InlineData(typeof(EasyAuthMicrosoftService))]
51+
public void IfTheAuthorizationHeaderIsNotSetTheCanUseMethodMustReturnFalse(Type authServiceType)
3552
{
3653
// Arrange
37-
var handler = new EasyAuthAzureAdService(this.loggerFactory.CreateLogger<EasyAuthAzureAdService>());
54+
var handler = this.CreateServiceInstance(authServiceType);
3855
var httpcontext = new DefaultHttpContext();
3956
// Act
4057
var result = handler.CanHandleAuthentification(httpcontext);
4158
// Arrange
4259
Assert.False(result);
4360
}
4461

45-
[Fact]
46-
public void IfAValidJwtTokenIsInTheHeaderTheResultIsSuccsess()
62+
[Theory]
63+
[InlineData(typeof(EasyAuthAzureAdService), "aad", AuthTokenHeaderNames.AADIdToken)]
64+
[InlineData(typeof(EasyAuthTwitterService), "twitter", AuthTokenHeaderNames.TwitterAccessToken)]
65+
[InlineData(typeof(EasyAuthFacebookService), "facebook", AuthTokenHeaderNames.FacebookAccessToken)]
66+
[InlineData(typeof(EasyAuthGoogleService), "google", AuthTokenHeaderNames.GoogleIdToken)]
67+
[InlineData(typeof(EasyAuthMicrosoftService), "microsoftaccount", AuthTokenHeaderNames.MicrosoftAccessToken)]
68+
public void IfAValidJwtTokenIsInTheHeaderTheResultIsSuccsess(Type authServiceType, string idpName, string requiredHeader)
4769
{
4870
// Arrange
49-
var handler = new EasyAuthAzureAdService(this.loggerFactory.CreateLogger<EasyAuthAzureAdService>());
71+
var handler = this.CreateServiceInstance(authServiceType);
5072
var httpcontext = new DefaultHttpContext();
5173
var inputObject = new InputJson()
5274
{
@@ -69,6 +91,38 @@ public void IfAValidJwtTokenIsInTheHeaderTheResultIsSuccsess()
6991
Assert.True(result.Principal.IsInRole("Admin"));
7092
}
7193

94+
[Theory]
95+
[InlineData(typeof(EasyAuthAzureAdService), "aad", AuthTokenHeaderNames.AADIdToken)]
96+
[InlineData(typeof(EasyAuthTwitterService), "twitter", AuthTokenHeaderNames.TwitterAccessToken)]
97+
[InlineData(typeof(EasyAuthFacebookService), "facebook", AuthTokenHeaderNames.FacebookAccessToken)]
98+
[InlineData(typeof(EasyAuthGoogleService), "google", AuthTokenHeaderNames.GoogleIdToken)]
99+
[InlineData(typeof(EasyAuthMicrosoftService), "microsoftaccount", AuthTokenHeaderNames.MicrosoftAccessToken)]
100+
public void IfACustomOptionsIsInTheHeaderTheResultIsSuccsess(Type authServiceType, string idpName, string requiredHeader)
101+
{
102+
// Arrange
103+
var handler = this.CreateServiceInstance(authServiceType);
104+
var httpcontext = new DefaultHttpContext();
105+
var inputObject = new InputJson()
106+
{
107+
Claims = new List<InputClaims>()
108+
{
109+
new InputClaims() {Typ= "x", Value= "y"},
110+
new InputClaims() {Typ= "name", Value= "PrincipalName"},
111+
new InputClaims() {Typ = "hannes", Value = "Admin"}
112+
}
113+
};
114+
var json = JsonConvert.SerializeObject(inputObject);
115+
httpcontext.Request.Headers.Add("X-MS-TOKEN-AAD-ID-TOKEN", "Blup");
116+
httpcontext.Request.Headers.Add("X-MS-CLIENT-PRINCIPAL-IDP", "providername");
117+
httpcontext.Request.Headers.Add("X-MS-CLIENT-PRINCIPAL", Base64Encode(json));
118+
// Act
119+
var result = handler.AuthUser(httpcontext, new ProviderOptions(authServiceType.Name , nameClaimType: "name",roleNameClaimType: "hannes"));
120+
// Arrange
121+
Assert.True(result.Succeeded);
122+
Assert.Equal("PrincipalName", result.Principal.Identity.Name);
123+
Assert.True(result.Principal.IsInRole("Admin"));
124+
}
125+
72126
private static string Base64Encode(string plainText)
73127
{
74128
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
@@ -88,5 +142,19 @@ internal class InputClaims
88142
[JsonProperty("val")]
89143
public string Value { get; set; }
90144
}
145+
146+
private IEasyAuthAuthentificationService CreateServiceInstance(Type AuthServiceType)
147+
{
148+
var loggerMethod = typeof(LoggerFactoryExtensions).GetMethods()
149+
.Where(m => m.Name == nameof(LoggerFactoryExtensions.CreateLogger) && m.IsGenericMethod == true)
150+
.Single();
151+
152+
var logger = loggerMethod
153+
.MakeGenericMethod(AuthServiceType)
154+
.Invoke(null, new object[] { this.loggerFactory });
155+
var ctorOfAuthService = AuthServiceType.GetConstructor(new[] { logger.GetType() });
156+
var handler = Activator.CreateInstance(AuthServiceType, new object[] { logger }) as IEasyAuthAuthentificationService;
157+
return handler;
158+
}
91159
}
92160
}

0 commit comments

Comments
 (0)