Repository navigation
Expand file tree
/
Copy pathOpenIdConnectConfigurationTests.cs
More file actions
120 lines (106 loc) · 4.22 KB
/
Copy pathOpenIdConnectConfigurationTests.cs
File metadata and controls
120 lines (106 loc) · 4.22 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
{
public class OpenIdConnectConfigurationTests
{
[Fact]
public void MetadataAddressIsGeneratedFromAuthorityWhenMissing()
{
var options = new OpenIdConnectOptions
{
Authority = TestServerBuilder.DefaultAuthority,
ClientId = Guid.NewGuid().ToString(),
SignInScheme = Guid.NewGuid().ToString()
};
BuildTestServer(options);
Assert.Equal($"{options.Authority}/.well-known/openid-configuration", options.MetadataAddress);
}
public void ThrowsWhenSignInSchemeIsMissing()
{
TestConfigurationException<ArgumentException>(
new OpenIdConnectOptions
{
Authority = TestServerBuilder.DefaultAuthority,
ClientId = Guid.NewGuid().ToString()
},
ex => Assert.Equal("SignInScheme", ex.ParamName));
}
[Fact]
public void ThrowsWhenClientIdIsMissing()
{
TestConfigurationException<ArgumentException>(
new OpenIdConnectOptions
{
SignInScheme = "TestScheme",
Authority = TestServerBuilder.DefaultAuthority,
},
ex => Assert.Equal("ClientId", ex.ParamName));
}
[Fact]
public void ThrowsWhenAuthorityIsMissing()
{
TestConfigurationException<InvalidOperationException>(
new OpenIdConnectOptions
{
SignInScheme = "TestScheme",
ClientId = "Test Id",
},
ex => Assert.Equal("Provide Authority, MetadataAddress, Configuration, or ConfigurationManager to OpenIdConnectOptions", ex.Message)
);
}
[Fact]
public void ThrowsWhenAuthorityIsNotHttps()
{
TestConfigurationException<InvalidOperationException>(
new OpenIdConnectOptions
{
SignInScheme = "TestScheme",
ClientId = "Test Id",
Authority = "http://example.com"
},
ex => Assert.Equal("The MetadataAddress or Authority must use HTTPS unless disabled for development by setting RequireHttpsMetadata=false.", ex.Message)
);
}
[Fact]
public void ThrowsWhenMetadataAddressIsNotHttps()
{
TestConfigurationException<InvalidOperationException>(
new OpenIdConnectOptions
{
SignInScheme = "TestScheme",
ClientId = "Test Id",
MetadataAddress = "http://example.com"
},
ex => Assert.Equal("The MetadataAddress or Authority must use HTTPS unless disabled for development by setting RequireHttpsMetadata=false.", ex.Message)
);
}
private TestServer BuildTestServer(OpenIdConnectOptions options)
{
var builder = new WebHostBuilder()
.ConfigureServices(services => services.AddAuthentication())
.Configure(app => app.UseOpenIdConnectAuthentication(options));
return new TestServer(builder);
}
private void TestConfigurationException<T>(
OpenIdConnectOptions options,
Action<T> verifyException)
where T : Exception
{
var builder = new WebHostBuilder()
.ConfigureServices(services => services.AddAuthentication())
.Configure(app => app.UseOpenIdConnectAuthentication(options));
var exception = Assert.Throws<T>(() =>
{
new TestServer(builder);
});
verifyException(exception);
}
}
}