-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBasicOptionsTests.cs
158 lines (129 loc) · 6.15 KB
/
BasicOptionsTests.cs
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
// Copyright (c) Mihir Dilip. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using AspNetCore.Authentication.Basic.Tests.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Net;
using Xunit;
namespace AspNetCore.Authentication.Basic.Tests
{
public class BasicOptionsTests
{
[Fact]
public void Events_default_not_null()
{
var options = new BasicOptions();
Assert.NotNull(options.Events);
}
[Fact]
public void SuppressWWWAuthenticateHeader_default_false()
{
var options = new BasicOptions();
Assert.False(options.SuppressWWWAuthenticateHeader);
}
[Fact]
public async Task SuppressWWWAuthenticateHeader_verify_false()
{
var realm = TestServerBuilder.Realm;
using var server = TestServerBuilder.BuildTestServerWithService(options =>
{
options.Realm = realm;
options.SuppressWWWAuthenticateHeader = false;
});
using var client = server.CreateClient();
using var response = await client.GetAsync(TestServerBuilder.BaseUrl);
Assert.False(response.IsSuccessStatusCode);
var wwwAuthenticateHeader = response.Headers.WwwAuthenticate;
Assert.NotEmpty(wwwAuthenticateHeader);
var wwwAuthenticateHeaderToMatch = Assert.Single(wwwAuthenticateHeader);
Assert.NotNull(wwwAuthenticateHeaderToMatch);
Assert.Equal(BasicDefaults.AuthenticationScheme, wwwAuthenticateHeaderToMatch.Scheme);
Assert.Equal($"realm=\"{realm}\", charset=\"UTF-8\"", wwwAuthenticateHeaderToMatch.Parameter);
}
[Fact]
public async Task SuppressWWWAuthenticateHeader_verify_true()
{
var realm = TestServerBuilder.Realm;
using var server = TestServerBuilder.BuildTestServerWithService(options =>
{
options.Realm = realm;
options.SuppressWWWAuthenticateHeader = true;
});
using var client = server.CreateClient();
using var response = await client.GetAsync(TestServerBuilder.BaseUrl);
Assert.False(response.IsSuccessStatusCode);
Assert.Empty(response.Headers.WwwAuthenticate);
}
[Fact]
public void BasicUserValidationServiceType_default_null()
{
var options = new BasicOptions();
Assert.Null(options.BasicUserValidationServiceType);
}
[Fact]
public void BasicUserValidationServiceType_verify_null()
{
using var server = TestServerBuilder.BuildTestServer();
var services = server.Host.Services;
var apiKeyOptionsSnapshot = services.GetService<IOptionsSnapshot<BasicOptions>>();
var apiKeyOptions = apiKeyOptionsSnapshot?.Get(BasicDefaults.AuthenticationScheme);
Assert.NotNull(apiKeyOptions);
Assert.Null(apiKeyOptions.BasicUserValidationServiceType);
var apiKeyProvider = services.GetService<IBasicUserValidationService>();
Assert.Null(apiKeyProvider);
}
[Fact]
public void BasicUserValidationServiceType_verify_not_null()
{
using var server = TestServerBuilder.BuildTestServerWithService();
var services = server.Host.Services;
var apiKeyOptionsSnapshot = services.GetService<IOptionsSnapshot<BasicOptions>>();
var apiKeyOptions = apiKeyOptionsSnapshot?.Get(BasicDefaults.AuthenticationScheme);
Assert.NotNull(apiKeyOptions);
Assert.NotNull(apiKeyOptions.BasicUserValidationServiceType);
Assert.Equal(typeof(FakeBasicUserValidationService), apiKeyOptions.BasicUserValidationServiceType);
var apiKeyProvider = services.GetService<IBasicUserValidationService>();
Assert.NotNull(apiKeyProvider);
Assert.Equal(typeof(FakeBasicUserValidationService), apiKeyProvider.GetType());
}
#if !(NET461 || NETSTANDARD2_0 || NETCOREAPP2_1)
[Fact]
public void IgnoreAuthenticationIfAllowAnonymous_default_false()
{
var options = new BasicOptions();
Assert.False(options.IgnoreAuthenticationIfAllowAnonymous);
}
[Fact]
public async Task IgnoreAuthenticationIfAllowAnonymous_verify_false()
{
var realm = TestServerBuilder.Realm;
using var server = TestServerBuilder.BuildTestServerWithService(options =>
{
options.Realm = realm;
options.IgnoreAuthenticationIfAllowAnonymous = false;
});
using var client = server.CreateClient();
using var request = new HttpRequestMessage(HttpMethod.Get, TestServerBuilder.AnonymousUrl);
request.Headers.Authorization = FakeUsers.FakeUserIgnoreAuthenticationIfAllowAnonymous.ToAuthenticationHeaderValue();
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => client.SendAsync(request));
Assert.Equal(nameof(BasicOptions.IgnoreAuthenticationIfAllowAnonymous), exception.Message);
}
[Fact]
public async Task IgnoreAuthenticationIfAllowAnonymous_verify_true()
{
var realm = TestServerBuilder.Realm;
using var server = TestServerBuilder.BuildTestServerWithService(options =>
{
options.Realm = realm;
options.IgnoreAuthenticationIfAllowAnonymous = true;
});
using var client = server.CreateClient();
using var request = new HttpRequestMessage(HttpMethod.Get, TestServerBuilder.AnonymousUrl);
request.Headers.Authorization = FakeUsers.FakeUserIgnoreAuthenticationIfAllowAnonymous.ToAuthenticationHeaderValue();
using var response = await client.SendAsync(request);
Assert.True(response.IsSuccessStatusCode);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
#endif
}
}