-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBasicPostConfigureOptionsTests.cs
90 lines (77 loc) · 2.86 KB
/
BasicPostConfigureOptionsTests.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
// 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 Xunit;
namespace AspNetCore.Authentication.Basic.Tests
{
public class BasicPostConfigureOptionsTests
{
[Fact]
public async Task PostConfigure_no_option_set_throws_exception()
{
await Assert.ThrowsAsync<InvalidOperationException>(() => RunAuthInitAsync(_ => { }));
}
[Fact]
public async Task PostConfigure_Realm_or_SuppressWWWAuthenticateHeader_not_set_throws_exception()
{
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
RunAuthInitWithServiceAsync(_ => { })
);
Assert.Contains($"{nameof(BasicOptions.Realm)} must be set in {typeof(BasicOptions).Name} when setting up the authentication.", exception.Message);
}
[Fact]
public async Task PostConfigure_Realm_not_set_but_SuppressWWWAuthenticateHeader_set_no_exception_thrown()
{
await RunAuthInitWithServiceAsync(options =>
{
options.SuppressWWWAuthenticateHeader = true;
});
}
[Fact]
public async Task PostConfigure_Realm_set_but_SuppressWWWAuthenticateHeader_not_set_no_exception_thrown()
{
await RunAuthInitWithServiceAsync(options =>
{
options.Realm = "Test";
});
}
[Fact]
public async Task PostConfigure_Events_OnValidateKey_or_IBasicProvider_not_set_throws_exception()
{
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
RunAuthInitAsync(options =>
{
options.SuppressWWWAuthenticateHeader = true;
})
);
Assert.Contains($"Either {nameof(BasicOptions.Events.OnValidateCredentials)} delegate on configure options {nameof(BasicOptions.Events)} should be set or use an extention method with type parameter of type {nameof(IBasicUserValidationService)}.", exception.Message);
}
[Fact]
public async Task PostConfigure_Events_OnValidateKey_set_but_IBasicProvider_not_set_no_exception_thrown()
{
await RunAuthInitAsync(options =>
{
options.Events.OnValidateCredentials = _ => Task.CompletedTask;
options.SuppressWWWAuthenticateHeader = true;
});
}
[Fact]
public async Task PostConfigure_Events_OnValidateKey_not_set_but_IBasicProvider_set_no_exception_thrown()
{
await RunAuthInitWithServiceAsync(options =>
{
options.SuppressWWWAuthenticateHeader = true;
});
}
private static async Task RunAuthInitAsync(Action<BasicOptions> configureOptions)
{
var server = TestServerBuilder.BuildTestServer(configureOptions);
await server.CreateClient().GetAsync(TestServerBuilder.BaseUrl);
}
private static async Task RunAuthInitWithServiceAsync(Action<BasicOptions> configureOptions)
{
var server = TestServerBuilder.BuildTestServerWithService(configureOptions);
await server.CreateClient().GetAsync(TestServerBuilder.BaseUrl);
}
}
}