-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathTokenCleanupTests.cs
More file actions
175 lines (147 loc) · 6.49 KB
/
Copy pathTokenCleanupTests.cs
File metadata and controls
175 lines (147 loc) · 6.49 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
166
167
168
169
170
171
172
173
174
175
/*
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.Entities;
using IdentityServer8.EntityFramework.Interfaces;
using IdentityServer8.EntityFramework.Options;
using IdentityServer8.EntityFramework.Stores;
using IdentityServer8.Stores;
using IdentityServer8.Test;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace IdentityServer8.EntityFramework.IntegrationTests.TokenCleanup;
public class TokenCleanupTests : IntegrationTest<TokenCleanupTests, PersistedGrantDbContext, OperationalStoreOptions>
{
public TokenCleanupTests(DatabaseProviderFixture<PersistedGrantDbContext> fixture) : base(fixture)
{
foreach (var options in TestDatabaseProviders)
{
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.Database.EnsureCreated();
}
}
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task RemoveExpiredGrantsAsync_WhenExpiredGrantsExist_ExpectExpiredGrantsRemoved(DbContextOptions<PersistedGrantDbContext> options)
{
var expiredGrant = new PersistedGrant
{
Key = Guid.NewGuid().ToString(),
ClientId = "app1",
Type = "reference",
SubjectId = "123",
Expiration = DateTime.UtcNow.AddDays(-3),
Data = "{!}"
};
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.PersistedGrants.Add(expiredGrant);
context.SaveChanges();
}
await CreateSut(options).RemoveExpiredGrantsAsync();
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.PersistedGrants.FirstOrDefault(x => x.Key == expiredGrant.Key).Should().BeNull();
}
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task RemoveExpiredGrantsAsync_WhenValidGrantsExist_ExpectValidGrantsInDb(DbContextOptions<PersistedGrantDbContext> options)
{
var validGrant = new PersistedGrant
{
Key = Guid.NewGuid().ToString(),
ClientId = "app1",
Type = "reference",
SubjectId = "123",
Expiration = DateTime.UtcNow.AddDays(3),
Data = "{!}"
};
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.PersistedGrants.Add(validGrant);
context.SaveChanges();
}
await CreateSut(options).RemoveExpiredGrantsAsync();
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.PersistedGrants.FirstOrDefault(x => x.Key == validGrant.Key).Should().NotBeNull();
}
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task RemoveExpiredGrantsAsync_WhenExpiredDeviceGrantsExist_ExpectExpiredDeviceGrantsRemoved(DbContextOptions<PersistedGrantDbContext> options)
{
var expiredGrant = new DeviceFlowCodes
{
DeviceCode = Guid.NewGuid().ToString(),
UserCode = Guid.NewGuid().ToString(),
ClientId = "app1",
SubjectId = "123",
CreationTime = DateTime.UtcNow.AddDays(-4),
Expiration = DateTime.UtcNow.AddDays(-3),
Data = "{!}"
};
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.DeviceFlowCodes.Add(expiredGrant);
context.SaveChanges();
}
await CreateSut(options).RemoveExpiredGrantsAsync();
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.DeviceFlowCodes.FirstOrDefault(x => x.DeviceCode == expiredGrant.DeviceCode).Should().BeNull();
}
}
[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task RemoveExpiredGrantsAsync_WhenValidDeviceGrantsExist_ExpectValidDeviceGrantsInDb(DbContextOptions<PersistedGrantDbContext> options)
{
var validGrant = new DeviceFlowCodes
{
DeviceCode = Guid.NewGuid().ToString(),
UserCode = "2468",
ClientId = "app1",
SubjectId = "123",
CreationTime = DateTime.UtcNow.AddDays(-4),
Expiration = DateTime.UtcNow.AddDays(3),
Data = "{!}"
};
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.DeviceFlowCodes.Add(validGrant);
context.SaveChanges();
}
await CreateSut(options).RemoveExpiredGrantsAsync();
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.DeviceFlowCodes.FirstOrDefault(x => x.DeviceCode == validGrant.DeviceCode).Should().NotBeNull();
}
}
private EntityFramework.TokenCleanupService CreateSut(DbContextOptions<PersistedGrantDbContext> options)
{
IServiceCollection services = new ServiceCollection();
services.AddIdentityServer()
.AddTestUsers(new List<TestUser>())
.AddInMemoryClients(new List<Models.Client>())
.AddInMemoryIdentityResources(new List<Models.IdentityResource>())
.AddInMemoryApiResources(new List<Models.ApiResource>());
services.AddScoped<IPersistedGrantDbContext, PersistedGrantDbContext>(_ =>
new PersistedGrantDbContext(options, StoreOptions));
services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
services.AddTransient<IDeviceFlowStore, DeviceFlowStore>();
services.AddTransient<EntityFramework.TokenCleanupService>();
services.AddSingleton(StoreOptions);
return services.BuildServiceProvider().GetRequiredService<EntityFramework.TokenCleanupService>();
//return new EntityFramework.TokenCleanupService(
// services.BuildServiceProvider(),
// new NullLogger<EntityFramework.TokenCleanup>(),
// StoreOptions);
}
}