|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| 3 | + * See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers |
| 4 | + * for more information concerning the license and the contributors participating to this project. |
| 5 | + */ |
| 6 | + |
| 7 | +using System.IdentityModel.Tokens.Jwt; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | +using Microsoft.IdentityModel.Tokens; |
| 11 | +using Moq; |
| 12 | +using Shouldly; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace AspNet.Security.OAuth.Apple |
| 16 | +{ |
| 17 | + public static class AppleAuthenticationOptionsExtensionsTests |
| 18 | + { |
| 19 | + [Fact] |
| 20 | + public static void AddApple_Registers_Services() |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + var services = new ServiceCollection(); |
| 24 | + |
| 25 | + services.AddLogging() |
| 26 | + .AddAuthentication() |
| 27 | + .AddApple(); |
| 28 | + |
| 29 | + // Act |
| 30 | + using var serviceProvider = services.BuildServiceProvider(); |
| 31 | + |
| 32 | + // Assert |
| 33 | + serviceProvider.GetRequiredService<AppleAuthenticationHandler>().ShouldNotBeNull(); |
| 34 | + serviceProvider.GetRequiredService<AppleClientSecretGenerator>().ShouldNotBeNull(); |
| 35 | + serviceProvider.GetRequiredService<AppleIdTokenValidator>().ShouldNotBeNull(); |
| 36 | + serviceProvider.GetRequiredService<AppleKeyStore>().ShouldNotBeNull(); |
| 37 | + serviceProvider.GetRequiredService<IOptions<AppleAuthenticationOptions>>().ShouldNotBeNull(); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public static void AddApple_Does_Not_Overwrite_Existing_Service_Registrations() |
| 42 | + { |
| 43 | + // Arrange |
| 44 | + var cryptoProviderFactory = Mock.Of<CryptoProviderFactory>(); |
| 45 | + var keyStore = Mock.Of<AppleKeyStore>(); |
| 46 | + var secretGenerator = Mock.Of<AppleClientSecretGenerator>(); |
| 47 | + var tokenHandler = Mock.Of<JwtSecurityTokenHandler>(); |
| 48 | + var tokenValidator = Mock.Of<AppleIdTokenValidator>(); |
| 49 | + |
| 50 | + var services = new ServiceCollection() |
| 51 | + .AddSingleton(cryptoProviderFactory) |
| 52 | + .AddSingleton(keyStore) |
| 53 | + .AddSingleton(secretGenerator) |
| 54 | + .AddSingleton(tokenHandler) |
| 55 | + .AddSingleton(tokenValidator); |
| 56 | + |
| 57 | + services.AddLogging() |
| 58 | + .AddAuthentication() |
| 59 | + .AddApple(); |
| 60 | + |
| 61 | + // Act |
| 62 | + using var serviceProvider = services.BuildServiceProvider(); |
| 63 | + |
| 64 | + // Assert |
| 65 | + serviceProvider.GetRequiredService<AppleAuthenticationHandler>().ShouldNotBeNull(); |
| 66 | + serviceProvider.GetRequiredService<IOptions<AppleAuthenticationOptions>>().ShouldNotBeNull(); |
| 67 | + |
| 68 | + serviceProvider.GetRequiredService<AppleClientSecretGenerator>().ShouldBeSameAs(secretGenerator); |
| 69 | + serviceProvider.GetRequiredService<AppleIdTokenValidator>().ShouldBeSameAs(tokenValidator); |
| 70 | + serviceProvider.GetRequiredService<AppleKeyStore>().ShouldBeSameAs(keyStore); |
| 71 | + serviceProvider.GetRequiredService<CryptoProviderFactory>().ShouldBeSameAs(cryptoProviderFactory); |
| 72 | + serviceProvider.GetRequiredService<JwtSecurityTokenHandler>().ShouldBeSameAs(tokenHandler); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments