Skip to content

Commit 9ff8009

Browse files
Add test for OnCreatingTicket with EventsType
Add a unit test to validate #600 for all providers.
1 parent 2f2a595 commit 9ff8009

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

test/AspNet.Security.OAuth.Providers.Tests/OAuthTests`1.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Security.Claims;
1414
using System.Threading.Tasks;
1515
using System.Xml.Linq;
16+
using AspNet.Security.OAuth.Apple;
1617
using AspNet.Security.OAuth.Infrastructure;
1718
using JustEat.HttpClientInterception;
1819
using MartinCostello.Logging.XUnit;
@@ -22,6 +23,7 @@
2223
using Microsoft.AspNetCore.Http;
2324
using Microsoft.AspNetCore.Mvc.Testing;
2425
using Microsoft.Extensions.DependencyInjection;
26+
using Microsoft.Extensions.DependencyInjection.Extensions;
2527
using Shouldly;
2628
using Xunit;
2729
using Xunit.Abstractions;
@@ -150,7 +152,7 @@ void ConfigureServices(IServiceCollection services)
150152
return Task.CompletedTask;
151153
};
152154

153-
if (options is Apple.AppleAuthenticationOptions appleOptions)
155+
if (options is AppleAuthenticationOptions appleOptions)
154156
{
155157
appleOptions.JwtSecurityTokenHandler = new FrozenJwtSecurityTokenHandler();
156158
}
@@ -166,6 +168,60 @@ void ConfigureServices(IServiceCollection services)
166168
onCreatingTicketEventRaised.ShouldBeTrue();
167169
}
168170

171+
[Fact]
172+
public async Task OnCreatingTicket_Is_Raised_By_Handler_Using_Custom_Events_Type()
173+
{
174+
// Arrange
175+
bool onCreatingTicketEventRaised = false;
176+
177+
void ConfigureServices(IServiceCollection services)
178+
{
179+
services.TryAddScoped((_) =>
180+
{
181+
return new CustomOAuthEvents()
182+
{
183+
OnCreatingTicket = (context) =>
184+
{
185+
onCreatingTicketEventRaised = true;
186+
return Task.CompletedTask;
187+
}
188+
};
189+
});
190+
services.TryAddScoped((_) =>
191+
{
192+
return new CustomAppleAuthenticationEvents()
193+
{
194+
OnCreatingTicket = (context) =>
195+
{
196+
onCreatingTicketEventRaised = true;
197+
return Task.CompletedTask;
198+
}
199+
};
200+
});
201+
202+
services.PostConfigureAll<TOptions>((options) =>
203+
{
204+
if (options is AppleAuthenticationOptions appleOptions)
205+
{
206+
appleOptions.EventsType = typeof(CustomAppleAuthenticationEvents);
207+
appleOptions.JwtSecurityTokenHandler = new FrozenJwtSecurityTokenHandler();
208+
}
209+
else
210+
{
211+
options.EventsType = typeof(CustomOAuthEvents);
212+
}
213+
});
214+
}
215+
216+
using var server = CreateTestServer(ConfigureServices);
217+
218+
// Act
219+
var claims = await AuthenticateUserAsync(server);
220+
221+
// Assert
222+
onCreatingTicketEventRaised.ShouldBeTrue();
223+
}
224+
169225
/// <summary>
170226
/// Run the ChannelAsync for authentication
171227
/// </summary>
@@ -252,5 +308,13 @@ protected void AssertClaim(IDictionary<string, Claim> actual, string claimType,
252308
actualValue.ShouldBe(claimValue);
253309
}
254310
}
311+
312+
private sealed class CustomOAuthEvents : OAuthEvents
313+
{
314+
}
315+
316+
private sealed class CustomAppleAuthenticationEvents : AppleAuthenticationEvents
317+
{
318+
}
255319
}
256320
}

0 commit comments

Comments
 (0)