-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathClientStoreTests.cs
More file actions
165 lines (142 loc) · 6.2 KB
/
Copy pathClientStoreTests.cs
File metadata and controls
165 lines (142 loc) · 6.2 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
162
163
164
165
/*
Copyright (c) 2024 HigginsSoft, Alexander Higgins - https://github.com/alexhiggins732/
Copyright (c) 2018, Brock Allen & Dominick Baier. All rights reserved.
Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
Source code and license this software can be found
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
using FluentAssertions;
using IdentityServer8.EntityFramework.DbContexts;
using IdentityServer8.EntityFramework.Mappers;
using IdentityServer8.EntityFramework.Options;
using IdentityServer8.EntityFramework.Stores;
using IdentityServer8.Models;
using Microsoft.EntityFrameworkCore;
using Xunit;
using Xunit.Sdk;
namespace IdentityServer8.EntityFramework.IntegrationTests.Stores;
public class ClientStoreTests : IntegrationTest<ClientStoreTests, ConfigurationDbContext, ConfigurationStoreOptions>
{
public ClientStoreTests(DatabaseProviderFixture<ConfigurationDbContext> fixture) : base(fixture)
{
foreach (var options in TestDatabaseProviders)
{
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
context.Database.EnsureCreated();
}
}
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task FindClientByIdAsync_WhenClientDoesNotExist_ExpectNull(DbContextOptions<ConfigurationDbContext> options)
{
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
var store = new ClientStore(context, FakeLogger<ClientStore>.Create());
var client = await store.FindClientByIdAsync(Guid.NewGuid().ToString());
client.Should().BeNull();
}
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task FindClientByIdAsync_WhenClientExists_ExpectClientRetured(DbContextOptions<ConfigurationDbContext> options)
{
var testClient = new Client
{
ClientId = "test_client",
ClientName = "Test Client"
};
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
context.Clients.Add(testClient.ToEntity());
context.SaveChanges();
}
Client client;
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
var store = new ClientStore(context, FakeLogger<ClientStore>.Create());
client = await store.FindClientByIdAsync(testClient.ClientId);
}
client.Should().NotBeNull();
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task FindClientByIdAsync_WhenClientExistsWithCollections_ExpectClientReturnedCollections(DbContextOptions<ConfigurationDbContext> options)
{
var testClient = new Client
{
ClientId = "properties_test_client",
ClientName = "Properties Test Client",
AllowedCorsOrigins = {"https://localhost"},
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
AllowedScopes = {"openid", "profile", "api1"},
Claims = {new ClientClaim("test", "value")},
ClientSecrets = {new Secret("secret".Sha256())},
IdentityProviderRestrictions = {"AD"},
PostLogoutRedirectUris = {"https://locahost/signout-callback"},
Properties = {{"foo1", "bar1"}, {"foo2", "bar2"},},
RedirectUris = {"https://locahost/signin"}
};
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
context.Clients.Add(testClient.ToEntity());
context.SaveChanges();
}
Client client;
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
var store = new ClientStore(context, FakeLogger<ClientStore>.Create());
client = await store.FindClientByIdAsync(testClient.ClientId);
}
client.Should().BeEquivalentTo(testClient);
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task FindClientByIdAsync_WhenClientsExistWithManyCollections_ExpectClientReturnedInUnderFiveSeconds(DbContextOptions<ConfigurationDbContext> options)
{
var testClient = new Client
{
ClientId = "test_client_with_uris",
ClientName = "Test client with URIs",
AllowedScopes = {"openid", "profile", "api1"},
AllowedGrantTypes = GrantTypes.CodeAndClientCredentials
};
for (int i = 0; i < 50; i++)
{
testClient.RedirectUris.Add($"https://localhost/{i}");
testClient.PostLogoutRedirectUris.Add($"https://localhost/{i}");
testClient.AllowedCorsOrigins.Add($"https://localhost:{i}");
}
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
context.Clients.Add(testClient.ToEntity());
for (int i = 0; i < 50; i++)
{
context.Clients.Add(new Client
{
ClientId = testClient.ClientId + i,
ClientName = testClient.ClientName,
AllowedScopes = testClient.AllowedScopes,
AllowedGrantTypes = testClient.AllowedGrantTypes,
RedirectUris = testClient.RedirectUris,
PostLogoutRedirectUris = testClient.PostLogoutRedirectUris,
AllowedCorsOrigins = testClient.AllowedCorsOrigins,
}.ToEntity());
}
context.SaveChanges();
}
using (var context = new ConfigurationDbContext(options, StoreOptions))
{
var store = new ClientStore(context, FakeLogger<ClientStore>.Create());
const int timeout = 5000;
var task = Task.Run(() => store.FindClientByIdAsync(testClient.ClientId));
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
{
var client = await task;
client.Should().BeEquivalentTo(testClient);
}
else
{
throw new TestTimeoutException(timeout);
}
}
}
}