microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Plugins.AspNetCore.Tests/Extensions/HostApplicationBuilderTests.cs
107lines · modecode
| 1 | using Microsoft.AspNetCore.Authentication; |
| 2 | using Microsoft.AspNetCore.Authorization; |
| 3 | using Microsoft.AspNetCore.Authorization.Infrastructure; |
| 4 | using Microsoft.AspNetCore.Builder; |
| 5 | using Microsoft.Extensions.Configuration; |
| 6 | using Microsoft.Extensions.DependencyInjection; |
| 7 | using Microsoft.Teams.Plugins.AspNetCore.Extensions; |
| 8 | |
| 9 | using static Microsoft.Teams.Plugins.AspNetCore.Extensions.HostApplicationBuilderExtensions; |
| 10 | |
| 11 | public class HostApplicationBuilderTests |
| 12 | { |
| 13 | [Fact] |
| 14 | public async Task AddTeamsTokenAuthentication_ShouldRegisterJwtBearerScheme() |
| 15 | { |
| 16 | var builder = WebApplication.CreateBuilder(); |
| 17 | var mockSettings = new Dictionary<string, string?> |
| 18 | { |
| 19 | ["Teams:ClientId"] = "test-client-id", |
| 20 | }; |
| 21 | builder.Configuration.AddInMemoryCollection(mockSettings); |
| 22 | builder.AddTeams(); |
| 23 | |
| 24 | var services = builder.Build().Services; |
| 25 | var schemes = services.GetRequiredService<IAuthenticationSchemeProvider>(); |
| 26 | var scheme = await schemes.GetSchemeAsync(TeamsTokenAuthConstants.AuthenticationScheme); |
| 27 | var authOptions = services.GetRequiredService<IAuthorizationPolicyProvider>(); |
| 28 | |
| 29 | var policy = await authOptions.GetPolicyAsync(TeamsTokenAuthConstants.AuthorizationPolicy); |
| 30 | var mvcBuilder = services.GetService<Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProvider>(); |
| 31 | |
| 32 | Assert.NotNull(scheme); |
| 33 | Assert.NotNull(policy); |
| 34 | Assert.Equal("JwtBearerHandler", scheme.HandlerType.Name); |
| 35 | Assert.True(policy.Requirements.OfType<RolesAuthorizationRequirement>().Any() || |
| 36 | policy.Requirements.OfType<IAuthorizationRequirement>().Any(r => r is not AssertionRequirement)); |
| 37 | Assert.NotNull(mvcBuilder); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | [Fact] |
| 42 | public async Task AddTeamsTokenAuthentication_ShouldSkipJwtAuthentication_WhenClientIdIsMissing() |
| 43 | { |
| 44 | var builder = WebApplication.CreateBuilder(); |
| 45 | var mockSettings = new Dictionary<string, string?> |
| 46 | { |
| 47 | ["Teams:ClientId"] = null, |
| 48 | }; |
| 49 | builder.Configuration.AddInMemoryCollection(mockSettings); |
| 50 | builder.AddTeams(skipAuth: false); |
| 51 | var services = builder.Build().Services; |
| 52 | var authOptions = services.GetRequiredService<IAuthorizationPolicyProvider>(); |
| 53 | |
| 54 | var policy = await authOptions.GetPolicyAsync(TeamsTokenAuthConstants.AuthorizationPolicy); |
| 55 | |
| 56 | // Should allow all requests |
| 57 | Assert.NotNull(policy); |
| 58 | Assert.True(policy.Requirements.OfType<AssertionRequirement>().Any()); |
| 59 | } |
| 60 | |
| 61 | [Fact] |
| 62 | public async Task AddTeamsTokenAuthentication_ShouldSkipJwtAuthentication_WhenWithSkipIsTrue() |
| 63 | { |
| 64 | var builder = WebApplication.CreateBuilder(); |
| 65 | var mockSettings = new Dictionary<string, string?> |
| 66 | { |
| 67 | ["Teams:ClientId"] = "test-client-id", |
| 68 | }; |
| 69 | builder.Configuration.AddInMemoryCollection(mockSettings); |
| 70 | builder.AddTeams(skipAuth: true); |
| 71 | var services = builder.Build().Services; |
| 72 | var authOptions = services.GetRequiredService<IAuthorizationPolicyProvider>(); |
| 73 | |
| 74 | var policy = await authOptions.GetPolicyAsync(TeamsTokenAuthConstants.AuthorizationPolicy); |
| 75 | |
| 76 | // Should allow all requests |
| 77 | Assert.NotNull(policy); |
| 78 | Assert.True(policy.Requirements.OfType<AssertionRequirement>().Any()); |
| 79 | } |
| 80 | |
| 81 | [Fact] |
| 82 | public async Task AddTeamsTokenAuthentication_ShouldRegisterEntraTokenValidation() |
| 83 | { |
| 84 | var builder = WebApplication.CreateBuilder(); |
| 85 | var mockSettings = new Dictionary<string, string?> |
| 86 | { |
| 87 | ["Teams:ClientId"] = "test-client-id", |
| 88 | }; |
| 89 | builder.Configuration.AddInMemoryCollection(mockSettings); |
| 90 | builder.AddTeams(); |
| 91 | |
| 92 | var services = builder.Build().Services; |
| 93 | var schemes = services.GetRequiredService<IAuthenticationSchemeProvider>(); |
| 94 | var scheme = await schemes.GetSchemeAsync(EntraTokenAuthConstants.AuthenticationScheme); |
| 95 | var authOptions = services.GetRequiredService<IAuthorizationPolicyProvider>(); |
| 96 | |
| 97 | var policy = await authOptions.GetPolicyAsync(EntraTokenAuthConstants.AuthorizationPolicy); |
| 98 | var mvcBuilder = services.GetService<Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProvider>(); |
| 99 | |
| 100 | Assert.NotNull(scheme); |
| 101 | Assert.NotNull(policy); |
| 102 | Assert.Equal("JwtBearerHandler", scheme.HandlerType.Name); |
| 103 | Assert.True(policy.Requirements.OfType<RolesAuthorizationRequirement>().Any() || |
| 104 | policy.Requirements.OfType<IAuthorizationRequirement>().Any(r => r is not AssertionRequirement)); |
| 105 | Assert.NotNull(mvcBuilder); |
| 106 | } |
| 107 | } |