Skip to content

Commit b77b068

Browse files
Add tests for Apple DI registrations
Add unit tests for service registration for the Apple provider to ensure that existing registrations are not overwritten.
1 parent 42201e8 commit b77b068

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="3.1.5" />
1111
<PackageVersion Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0" />
1212
<PackageVersion Include="Microsoft.NetCore.Analyzers" Version="3.0.0" />
13+
<PackageVersion Include="Moq" Version="4.14.5" />
1314
<PackageVersion Include="Shouldly" Version="3.0.2" />
1415
<PackageVersion Include="StyleCop.Analyzers" Version="1.1.118" />
1516
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="5.5.0" />
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

test/AspNet.Security.OAuth.Providers.Tests/AspNet.Security.OAuth.Providers.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
2526
<PackageReference Include="JustEat.HttpClientInterception" />
2627
<PackageReference Include="MartinCostello.Logging.XUnit" />
2728
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
2829
<PackageReference Include="Microsoft.AspNetCore.TestHost" />
30+
<PackageReference Include="Moq" />
2931
<PackageReference Include="Shouldly" />
3032
</ItemGroup>
3133

0 commit comments

Comments
 (0)