23
23
using Microsoft . AspNetCore . Authentication . Twitter ;
24
24
using Microsoft . AspNetCore . Authentication . WsFederation ;
25
25
using Microsoft . IdentityModel . Protocols . WsFederation ;
26
+ using Xunit . Sdk ;
26
27
27
28
namespace Aguacongas . AspNetCore . Authentication . Test
28
29
{
@@ -52,7 +53,7 @@ Task onSignId(CookieSignedInContext context)
52
53
} ) ;
53
54
} ) ;
54
55
55
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
56
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
56
57
var cookieOptions = new CookieAuthenticationOptions
57
58
{
58
59
Cookie = new CookieBuilder
@@ -61,7 +62,7 @@ Task onSignId(CookieSignedInContext context)
61
62
}
62
63
} ;
63
64
64
- await sut . AddAsync ( CookieAuthenticationDefaults . AuthenticationScheme , "test" , typeof ( CookieAuthenticationHandler ) , ( AuthenticationSchemeOptions ) cookieOptions ) ;
65
+ await sut . AddAsync ( CookieAuthenticationDefaults . AuthenticationScheme , "test" , typeof ( CookieAuthenticationHandler ) , cookieOptions ) ;
65
66
var state = await VerifyAddedAsync < CookieAuthenticationOptions > ( CookieAuthenticationDefaults . AuthenticationScheme , provider ) ;
66
67
67
68
var storedOptions = JsonConvert . DeserializeObject < CookieAuthenticationOptions > ( state . definition . SerializedOptions ) ;
@@ -97,7 +98,7 @@ Task onCreatingTicket(OAuthCreatingTicketContext context)
97
98
} ) ;
98
99
} ) ;
99
100
100
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
101
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
101
102
var facebookOptions = new FacebookOptions
102
103
{
103
104
AppId = "test" ,
@@ -142,7 +143,7 @@ Task onCreatingTicket(OAuthCreatingTicketContext context)
142
143
} ) ;
143
144
} ) ;
144
145
145
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
146
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
146
147
var googleOptions = new GoogleOptions
147
148
{
148
149
ClientId = "test" ,
@@ -188,7 +189,7 @@ Task onMessageReceived(Microsoft.AspNetCore.Authentication.JwtBearer.MessageRece
188
189
} ) ;
189
190
} ) ;
190
191
191
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
192
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
192
193
var jwtBearerOptions = new JwtBearerOptions
193
194
{
194
195
Authority = "test"
@@ -228,7 +229,7 @@ Task onCreatingTicket(OAuthCreatingTicketContext context)
228
229
} ) ;
229
230
} ) ;
230
231
231
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
232
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
232
233
var msAccountOptions = new MicrosoftAccountOptions
233
234
{
234
235
ClientId = "test" ,
@@ -273,7 +274,7 @@ Task onTicketReceived(TicketReceivedContext context)
273
274
} ) ;
274
275
} ) ;
275
276
276
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
277
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
277
278
var msAccountOptions = new OpenIdConnectOptions
278
279
{
279
280
ClientId = "test" ,
@@ -315,7 +316,7 @@ Task onTicketReceived(TicketReceivedContext context)
315
316
} ) ;
316
317
} ) ;
317
318
318
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
319
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
319
320
var twittertOptions = new TwitterOptions
320
321
{
321
322
ConsumerKey = "test" ,
@@ -357,7 +358,7 @@ Task onTicketReceived(TicketReceivedContext context)
357
358
} ) ;
358
359
} ) ;
359
360
360
- var sut = provider . GetRequiredService < DynamicManager > ( ) ;
361
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
361
362
var wsFederationOptions = new WsFederationOptions
362
363
{
363
364
Configuration = new WsFederationConfiguration
@@ -382,7 +383,122 @@ Task onTicketReceived(TicketReceivedContext context)
382
383
383
384
Assert . True ( eventCalled ) ;
384
385
}
386
+ [ Fact ]
387
+ public async Task UpdateAsync_should_update_handler ( )
388
+ {
389
+ var eventCalled = false ;
390
+ Task onTicketReceived ( TicketReceivedContext context )
391
+ {
392
+ eventCalled = true ;
393
+ return Task . CompletedTask ;
394
+ }
395
+
396
+ var provider = CreateServiceProvider ( options =>
397
+ {
398
+ options . AddCookie ( )
399
+ . AddWsFederation ( configure =>
400
+ {
401
+ configure . Events . OnTicketReceived = onTicketReceived ;
402
+ } ) ;
403
+ } ) ;
404
+
405
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
406
+
407
+ var cookieOptions = new CookieAuthenticationOptions
408
+ {
409
+ Cookie = new CookieBuilder
410
+ {
411
+ Domain = "test"
412
+ }
413
+ } ;
414
+
415
+ await sut . AddAsync < CookieAuthenticationHandler , CookieAuthenticationOptions > ( "test" , "test" , cookieOptions ) ;
416
+ await VerifyAddedAsync < CookieAuthenticationOptions > ( "test" , provider ) ;
417
+
418
+ var wsFederationOptions = new WsFederationOptions
419
+ {
420
+ Configuration = new WsFederationConfiguration
421
+ {
422
+ Issuer = "test"
423
+ }
424
+ } ;
425
+
426
+ await sut . UpdateAsync < WsFederationHandler , WsFederationOptions > ( "test" , "new name" , wsFederationOptions ) ;
427
+ var state = await VerifyAddedAsync < WsFederationOptions > ( "test" , provider ) ;
428
+
429
+ var storedOptions = JsonConvert . DeserializeObject < WsFederationOptions > ( state . definition . SerializedOptions ) ;
430
+ _output . WriteLine ( state . definition . SerializedOptions ) ;
431
+
432
+ Assert . Equal ( wsFederationOptions . Configuration . Issuer , storedOptions . Configuration . Issuer ) ;
433
+
434
+ var httpContext = new Mock < HttpContext > ( ) . Object ;
435
+ state . options . Events . OnTicketReceived ( new TicketReceivedContext (
436
+ httpContext ,
437
+ state . scheme as AuthenticationScheme ,
438
+ state . options as WsFederationOptions ,
439
+ new AuthenticationTicket ( new ClaimsPrincipal ( ) , "test" ) ) ) ;
385
440
441
+ Assert . True ( eventCalled ) ;
442
+ }
443
+
444
+ [ Fact ]
445
+ public async Task RemoveAsync_should_remove_handler ( )
446
+ {
447
+ var provider = CreateServiceProvider ( options =>
448
+ {
449
+ options . AddCookie ( ) ;
450
+ } ) ;
451
+
452
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
453
+
454
+ var cookieOptions = new CookieAuthenticationOptions
455
+ {
456
+ Cookie = new CookieBuilder
457
+ {
458
+ Domain = "test"
459
+ }
460
+ } ;
461
+
462
+ await sut . AddAsync < CookieAuthenticationHandler , CookieAuthenticationOptions > ( "test" , "test" , cookieOptions ) ;
463
+ await VerifyAddedAsync < CookieAuthenticationOptions > ( "test" , provider ) ;
464
+
465
+ await sut . RemoveAsync ( "test" ) ;
466
+ await Assert . ThrowsAsync < NotNullException > ( ( ) => VerifyAddedAsync < WsFederationOptions > ( "test" , provider ) ) ;
467
+ }
468
+
469
+ [ Fact ]
470
+ public async Task Load_should_load_configuration ( )
471
+ {
472
+ var provider = CreateServiceProvider ( options =>
473
+ {
474
+ options . AddCookie ( ) ;
475
+ } ) ;
476
+
477
+ var store = provider . GetRequiredService < IDynamicProviderStore < ProviderDefinition > > ( ) ;
478
+
479
+ var cookieOptions = new CookieAuthenticationOptions
480
+ {
481
+ Cookie = new CookieBuilder
482
+ {
483
+ Domain = "test"
484
+ }
485
+ } ;
486
+
487
+ var definition = new ProviderDefinition
488
+ {
489
+ Id = "test" ,
490
+ HandlerTypeName = typeof ( CookieAuthenticationHandler ) . FullName ,
491
+ SerializedOptions = JsonConvert . SerializeObject ( cookieOptions , DynamicManager < ProviderDefinition > . JsonSerializerSettings )
492
+ } ;
493
+
494
+ await store . AddAsync ( definition ) ;
495
+
496
+ var sut = provider . GetRequiredService < DynamicManager < ProviderDefinition > > ( ) ;
497
+
498
+ sut . Load ( ) ;
499
+
500
+ await VerifyAddedAsync < CookieAuthenticationOptions > ( "test" , provider ) ;
501
+ }
386
502
protected virtual IServiceCollection AddStore ( IServiceCollection services )
387
503
{
388
504
return services . AddEntityFrameworkStore < ProviderDbContext > ( options =>
@@ -399,7 +515,6 @@ private static async Task<dynamic> VerifyAddedAsync<TOptions>(string schemeName,
399
515
var schemeProvider = provider . GetRequiredService < IAuthenticationSchemeProvider > ( ) ;
400
516
var scheme = await schemeProvider . GetSchemeAsync ( schemeName ) ;
401
517
Assert . NotNull ( scheme ) ;
402
- Assert . Equal ( "test" , scheme . DisplayName ) ;
403
518
404
519
var optionsMonitorCache = provider . GetRequiredService < IOptionsMonitorCache < TOptions > > ( ) ;
405
520
var options = optionsMonitorCache . GetOrAdd ( schemeName , ( ) => default ( TOptions ) ) ;
0 commit comments