-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathLoginEndpointTests.cs
More file actions
161 lines (137 loc) · 7.41 KB
/
LoginEndpointTests.cs
File metadata and controls
161 lines (137 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.
using System.Net;
using Duende.Bff.Tests.TestHosts;
using Microsoft.Extensions.DependencyInjection;
using Xunit.Abstractions;
namespace Duende.Bff.Tests.Endpoints.Management
{
public class LoginEndpointTests(ITestOutputHelper output) : BffIntegrationTestBase(output)
{
[Fact]
public async Task login_should_allow_anonymous()
{
BffHost.OnConfigureServices += svcs =>
{
svcs.AddAuthorization(opts =>
{
opts.FallbackPolicy =
new Microsoft.AspNetCore.Authorization.AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
};
await BffHost.InitializeAsync();
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/bff/login"));
response.StatusCode.ShouldNotBe(HttpStatusCode.Unauthorized);
}
[Fact]
public async Task login_endpoint_should_challenge_and_redirect_to_root()
{
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/bff/login"));
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(IdentityServerHost.Url("/connect/authorize"));
await IdentityServerHost.IssueSessionCookieAsync("alice");
response = await IdentityServerHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(BffHost.Url("/signin-oidc"));
response = await BffHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldBe("/");
}
[Fact]
public async Task login_endpoint_should_challenge_and_redirect_to_root_with_custom_prefix()
{
BffHost.OnConfigureServices += svcs =>
{
svcs.Configure<BffOptions>(options =>
{
options.ManagementBasePath = "/custom/bff";
});
};
await BffHost.InitializeAsync();
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/custom/bff/login"));
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(IdentityServerHost.Url("/connect/authorize"));
await IdentityServerHost.IssueSessionCookieAsync("alice");
response = await IdentityServerHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(BffHost.Url("/signin-oidc"));
response = await BffHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldBe("/");
}
[Fact]
public async Task login_endpoint_should_challenge_and_redirect_to_root_with_custom_prefix_trailing_slash()
{
BffHost.OnConfigureServices += svcs =>
{
svcs.Configure<BffOptions>(options =>
{
options.ManagementBasePath = "/custom/bff/";
});
};
await BffHost.InitializeAsync();
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/custom/bff/login"));
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(IdentityServerHost.Url("/connect/authorize"));
await IdentityServerHost.IssueSessionCookieAsync("alice");
response = await IdentityServerHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(BffHost.Url("/signin-oidc"));
response = await BffHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldBe("/");
}
[Fact]
public async Task login_endpoint_should_challenge_and_redirect_to_root_with_root_prefix()
{
BffHost.OnConfigureServices += svcs =>
{
svcs.Configure<BffOptions>(options =>
{
options.ManagementBasePath = "/";
});
};
await BffHost.InitializeAsync();
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/login"));
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(IdentityServerHost.Url("/connect/authorize"));
await IdentityServerHost.IssueSessionCookieAsync("alice");
response = await IdentityServerHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(BffHost.Url("/signin-oidc"));
response = await BffHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldBe("/");
}
[Fact]
public async Task login_endpoint_with_existing_session_should_challenge()
{
await BffHost.BffLoginAsync("alice");
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/bff/login"));
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(IdentityServerHost.Url("/connect/authorize"));
}
[Fact]
public async Task login_endpoint_should_accept_returnUrl()
{
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/bff/login") + "?returnUrl=/foo");
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(IdentityServerHost.Url("/connect/authorize"));
await IdentityServerHost.IssueSessionCookieAsync("alice");
response = await IdentityServerHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldStartWith(BffHost.Url("/signin-oidc"));
response = await BffHost.BrowserClient.GetAsync(response.Headers.Location.ToString());
response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
response.Headers.Location!.ToString().ShouldBe("/foo");
}
[Fact]
public async Task login_endpoint_should_not_accept_non_local_returnUrl()
{
var response = await BffHost.BrowserClient.GetAsync(BffHost.Url("/bff/login") + "?returnUrl=https://foo");
response.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
}
}
}