Skip to content

Commit c299d9c

Browse files
Fix UserStoreTest (#64019)
1 parent 0aefdae commit c299d9c

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/Identity/EntityFrameworkCore/test/EF.Test/UserStoreTest.cs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public UserStoreTest(ScratchDatabaseFixture fixture)
1919
_fixture = fixture;
2020
}
2121

22+
public class UserStoreTestDbContext : IdentityDbContext
23+
{
24+
public UserStoreTestDbContext(DbContextOptions<UserStoreTestDbContext> options) : base(options)
25+
{ }
26+
}
27+
2228
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
2329
{
2430
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
@@ -38,9 +44,9 @@ public void CanCreateUserUsingEF()
3844
}
3945
}
4046

41-
private IdentityDbContext CreateContext()
47+
private UserStoreTestDbContext CreateContext()
4248
{
43-
var db = DbUtil.Create<IdentityDbContext>(_fixture.Connection);
49+
var db = DbUtil.Create<UserStoreTestDbContext>(_fixture.Connection);
4450
db.Database.EnsureCreated();
4551
return db;
4652
}
@@ -52,18 +58,18 @@ protected override object CreateTestContext()
5258

5359
protected override void AddUserStore(IServiceCollection services, object context = null)
5460
{
55-
services.AddSingleton<IUserStore<IdentityUser>>(new UserStore<IdentityUser, IdentityRole, IdentityDbContext>((IdentityDbContext)context));
61+
services.AddSingleton<IUserStore<IdentityUser>>(new UserStore<IdentityUser, IdentityRole, UserStoreTestDbContext>((UserStoreTestDbContext)context));
5662
}
5763

5864
protected override void AddRoleStore(IServiceCollection services, object context = null)
5965
{
60-
services.AddSingleton<IRoleStore<IdentityRole>>(new RoleStore<IdentityRole, IdentityDbContext>((IdentityDbContext)context));
66+
services.AddSingleton<IRoleStore<IdentityRole>>(new RoleStore<IdentityRole, UserStoreTestDbContext>((UserStoreTestDbContext)context));
6167
}
6268

6369
[Fact]
6470
public async Task SqlUserStoreMethodsThrowWhenDisposedTest()
6571
{
66-
var store = new UserStore(new IdentityDbContext(new DbContextOptionsBuilder<IdentityDbContext>().Options));
72+
var store = new UserStore(new UserStoreTestDbContext(new DbContextOptionsBuilder<UserStoreTestDbContext>().Options));
6773
store.Dispose();
6874
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddClaimsAsync(null, null));
6975
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddLoginAsync(null, null));
@@ -97,7 +103,7 @@ await Assert.ThrowsAsync<ObjectDisposedException>(
97103
public async Task UserStorePublicNullCheckTest()
98104
{
99105
Assert.Throws<ArgumentNullException>("context", () => new UserStore(null));
100-
var store = new UserStore(new IdentityDbContext(new DbContextOptionsBuilder<IdentityDbContext>().Options));
106+
var store = new UserStore(new UserStoreTestDbContext(new DbContextOptionsBuilder<UserStoreTestDbContext>().Options));
101107
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetUserIdAsync(null));
102108
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetUserNameAsync(null));
103109
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.SetUserNameAsync(null, null));
@@ -210,17 +216,17 @@ await Assert.ThrowsAsync<InvalidOperationException>(
210216
[ConditionalFact]
211217
public async Task ConcurrentUpdatesWillFail()
212218
{
213-
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
219+
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
214220
var user = CreateTestUser();
215-
using (var db = new IdentityDbContext(options))
221+
using (var db = new UserStoreTestDbContext(options))
216222
{
217223
db.Database.EnsureCreated();
218224

219225
var manager = CreateManager(db);
220226
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
221227
}
222-
using (var db = new IdentityDbContext(options))
223-
using (var db2 = new IdentityDbContext(options))
228+
using (var db = new UserStoreTestDbContext(options))
229+
using (var db2 = new UserStoreTestDbContext(options))
224230
{
225231
var manager1 = CreateManager(db);
226232
var manager2 = CreateManager(db2);
@@ -241,17 +247,17 @@ public async Task ConcurrentUpdatesWillFail()
241247
[ConditionalFact]
242248
public async Task ConcurrentUpdatesWillFailWithDetachedUser()
243249
{
244-
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
250+
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
245251
var user = CreateTestUser();
246-
using (var db = new IdentityDbContext(options))
252+
using (var db = new UserStoreTestDbContext(options))
247253
{
248254
db.Database.EnsureCreated();
249255

250256
var manager = CreateManager(db);
251257
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
252258
}
253-
using (var db = new IdentityDbContext(options))
254-
using (var db2 = new IdentityDbContext(options))
259+
using (var db = new UserStoreTestDbContext(options))
260+
using (var db2 = new UserStoreTestDbContext(options))
255261
{
256262
var manager1 = CreateManager(db);
257263
var manager2 = CreateManager(db2);
@@ -270,17 +276,17 @@ public async Task ConcurrentUpdatesWillFailWithDetachedUser()
270276
[ConditionalFact]
271277
public async Task DeleteAModifiedUserWillFail()
272278
{
273-
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
279+
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
274280
var user = CreateTestUser();
275-
using (var db = new IdentityDbContext(options))
281+
using (var db = new UserStoreTestDbContext(options))
276282
{
277283
db.Database.EnsureCreated();
278284

279285
var manager = CreateManager(db);
280286
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
281287
}
282-
using (var db = new IdentityDbContext(options))
283-
using (var db2 = new IdentityDbContext(options))
288+
using (var db = new UserStoreTestDbContext(options))
289+
using (var db2 = new UserStoreTestDbContext(options))
284290
{
285291
var manager1 = CreateManager(db);
286292
var manager2 = CreateManager(db2);
@@ -300,17 +306,17 @@ public async Task DeleteAModifiedUserWillFail()
300306
[ConditionalFact]
301307
public async Task ConcurrentRoleUpdatesWillFail()
302308
{
303-
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
309+
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
304310
var role = new IdentityRole(Guid.NewGuid().ToString());
305-
using (var db = new IdentityDbContext(options))
311+
using (var db = new UserStoreTestDbContext(options))
306312
{
307313
db.Database.EnsureCreated();
308314

309315
var manager = CreateRoleManager(db);
310316
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
311317
}
312-
using (var db = new IdentityDbContext(options))
313-
using (var db2 = new IdentityDbContext(options))
318+
using (var db = new UserStoreTestDbContext(options))
319+
using (var db2 = new UserStoreTestDbContext(options))
314320
{
315321
var manager1 = CreateRoleManager(db);
316322
var manager2 = CreateRoleManager(db2);
@@ -331,17 +337,17 @@ public async Task ConcurrentRoleUpdatesWillFail()
331337
[ConditionalFact]
332338
public async Task ConcurrentRoleUpdatesWillFailWithDetachedRole()
333339
{
334-
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
340+
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
335341
var role = new IdentityRole(Guid.NewGuid().ToString());
336-
using (var db = new IdentityDbContext(options))
342+
using (var db = new UserStoreTestDbContext(options))
337343
{
338344
db.Database.EnsureCreated();
339345

340346
var manager = CreateRoleManager(db);
341347
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
342348
}
343-
using (var db = new IdentityDbContext(options))
344-
using (var db2 = new IdentityDbContext(options))
349+
using (var db = new UserStoreTestDbContext(options))
350+
using (var db2 = new UserStoreTestDbContext(options))
345351
{
346352
var manager1 = CreateRoleManager(db);
347353
var manager2 = CreateRoleManager(db2);
@@ -361,17 +367,17 @@ public async Task ConcurrentRoleUpdatesWillFailWithDetachedRole()
361367
[ConditionalFact]
362368
public async Task DeleteAModifiedRoleWillFail()
363369
{
364-
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
370+
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
365371
var role = new IdentityRole(Guid.NewGuid().ToString());
366-
using (var db = new IdentityDbContext(options))
372+
using (var db = new UserStoreTestDbContext(options))
367373
{
368374
db.Database.EnsureCreated();
369375

370376
var manager = CreateRoleManager(db);
371377
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
372378
}
373-
using (var db = new IdentityDbContext(options))
374-
using (var db2 = new IdentityDbContext(options))
379+
using (var db = new UserStoreTestDbContext(options))
380+
using (var db2 = new UserStoreTestDbContext(options))
375381
{
376382
var manager1 = CreateRoleManager(db);
377383
var manager2 = CreateRoleManager(db2);

0 commit comments

Comments
 (0)