microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
docs/update-release-process

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Tests/Microsoft.Teams.Plugins.AspNetCore.Tests/Extensions/HostApplicationBuilderTests.cs

109lines · modecode

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