Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions src/Identity/EntityFrameworkCore/test/EF.Test/UserStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public UserStoreTest(ScratchDatabaseFixture fixture)
_fixture = fixture;
}

public class UserStoreTestDbContext : IdentityDbContext
{
public UserStoreTestDbContext(DbContextOptions<UserStoreTestDbContext> options) : base(options)
{ }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
Expand All @@ -38,9 +44,9 @@ public void CanCreateUserUsingEF()
}
}

private IdentityDbContext CreateContext()
private UserStoreTestDbContext CreateContext()
{
var db = DbUtil.Create<IdentityDbContext>(_fixture.Connection);
var db = DbUtil.Create<UserStoreTestDbContext>(_fixture.Connection);
db.Database.EnsureCreated();
return db;
}
Expand All @@ -52,18 +58,18 @@ protected override object CreateTestContext()

protected override void AddUserStore(IServiceCollection services, object context = null)
{
services.AddSingleton<IUserStore<IdentityUser>>(new UserStore<IdentityUser, IdentityRole, IdentityDbContext>((IdentityDbContext)context));
services.AddSingleton<IUserStore<IdentityUser>>(new UserStore<IdentityUser, IdentityRole, UserStoreTestDbContext>((UserStoreTestDbContext)context));
}

protected override void AddRoleStore(IServiceCollection services, object context = null)
{
services.AddSingleton<IRoleStore<IdentityRole>>(new RoleStore<IdentityRole, IdentityDbContext>((IdentityDbContext)context));
services.AddSingleton<IRoleStore<IdentityRole>>(new RoleStore<IdentityRole, UserStoreTestDbContext>((UserStoreTestDbContext)context));
}

[Fact]
public async Task SqlUserStoreMethodsThrowWhenDisposedTest()
{
var store = new UserStore(new IdentityDbContext(new DbContextOptionsBuilder<IdentityDbContext>().Options));
var store = new UserStore(new UserStoreTestDbContext(new DbContextOptionsBuilder<UserStoreTestDbContext>().Options));
store.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddClaimsAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddLoginAsync(null, null));
Expand Down Expand Up @@ -97,7 +103,7 @@ await Assert.ThrowsAsync<ObjectDisposedException>(
public async Task UserStorePublicNullCheckTest()
{
Assert.Throws<ArgumentNullException>("context", () => new UserStore(null));
var store = new UserStore(new IdentityDbContext(new DbContextOptionsBuilder<IdentityDbContext>().Options));
var store = new UserStore(new UserStoreTestDbContext(new DbContextOptionsBuilder<UserStoreTestDbContext>().Options));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetUserIdAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetUserNameAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.SetUserNameAsync(null, null));
Expand Down Expand Up @@ -210,17 +216,17 @@ await Assert.ThrowsAsync<InvalidOperationException>(
[ConditionalFact]
public async Task ConcurrentUpdatesWillFail()
{
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var user = CreateTestUser();
using (var db = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
{
db.Database.EnsureCreated();

var manager = CreateManager(db);
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
}
using (var db = new IdentityDbContext(options))
using (var db2 = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
using (var db2 = new UserStoreTestDbContext(options))
{
var manager1 = CreateManager(db);
var manager2 = CreateManager(db2);
Expand All @@ -241,17 +247,17 @@ public async Task ConcurrentUpdatesWillFail()
[ConditionalFact]
public async Task ConcurrentUpdatesWillFailWithDetachedUser()
{
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var user = CreateTestUser();
using (var db = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
{
db.Database.EnsureCreated();

var manager = CreateManager(db);
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
}
using (var db = new IdentityDbContext(options))
using (var db2 = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
using (var db2 = new UserStoreTestDbContext(options))
{
var manager1 = CreateManager(db);
var manager2 = CreateManager(db2);
Expand All @@ -270,17 +276,17 @@ public async Task ConcurrentUpdatesWillFailWithDetachedUser()
[ConditionalFact]
public async Task DeleteAModifiedUserWillFail()
{
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var user = CreateTestUser();
using (var db = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
{
db.Database.EnsureCreated();

var manager = CreateManager(db);
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
}
using (var db = new IdentityDbContext(options))
using (var db2 = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
using (var db2 = new UserStoreTestDbContext(options))
{
var manager1 = CreateManager(db);
var manager2 = CreateManager(db2);
Expand All @@ -300,17 +306,17 @@ public async Task DeleteAModifiedUserWillFail()
[ConditionalFact]
public async Task ConcurrentRoleUpdatesWillFail()
{
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var role = new IdentityRole(Guid.NewGuid().ToString());
using (var db = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
{
db.Database.EnsureCreated();

var manager = CreateRoleManager(db);
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
}
using (var db = new IdentityDbContext(options))
using (var db2 = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
using (var db2 = new UserStoreTestDbContext(options))
{
var manager1 = CreateRoleManager(db);
var manager2 = CreateRoleManager(db2);
Expand All @@ -331,17 +337,17 @@ public async Task ConcurrentRoleUpdatesWillFail()
[ConditionalFact]
public async Task ConcurrentRoleUpdatesWillFailWithDetachedRole()
{
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var role = new IdentityRole(Guid.NewGuid().ToString());
using (var db = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
{
db.Database.EnsureCreated();

var manager = CreateRoleManager(db);
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
}
using (var db = new IdentityDbContext(options))
using (var db2 = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
using (var db2 = new UserStoreTestDbContext(options))
{
var manager1 = CreateRoleManager(db);
var manager2 = CreateRoleManager(db2);
Expand All @@ -361,17 +367,17 @@ public async Task ConcurrentRoleUpdatesWillFailWithDetachedRole()
[ConditionalFact]
public async Task DeleteAModifiedRoleWillFail()
{
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var options = new DbContextOptionsBuilder<UserStoreTestDbContext>().UseSqlite($"Data Source=D{Guid.NewGuid()}.db").Options;
var role = new IdentityRole(Guid.NewGuid().ToString());
using (var db = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
{
db.Database.EnsureCreated();

var manager = CreateRoleManager(db);
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
}
using (var db = new IdentityDbContext(options))
using (var db2 = new IdentityDbContext(options))
using (var db = new UserStoreTestDbContext(options))
using (var db2 = new UserStoreTestDbContext(options))
{
var manager1 = CreateRoleManager(db);
var manager2 = CreateRoleManager(db2);
Expand Down
Loading