public void UserRoleStoreInterface_AddToRoleAsync_GivesUserPermission() { using (var dbContext = new EntityDbContext()) { var user = new User { Name = Guid.NewGuid().ToString() }; var permission1 = new Permission { Name = Guid.NewGuid().ToString() }; var permission2 = new Permission { Name = Guid.NewGuid().ToString() }; var permission3 = new Permission { Name = Guid.NewGuid().ToString() }; dbContext.Create(user); dbContext.Create(permission1); dbContext.Create(permission2); dbContext.Create(permission3); dbContext.SaveChangesAsync().GetAwaiter().GetResult(); var securityStore = new SecurityStore(dbContext); securityStore.AddToRoleAsync(user, permission2.Name).Wait(); dbContext.SaveChangesAsync().Wait(); user.Permissions.ShouldContain(permission2); permission2.Users.ShouldContain(user); } }
public void Handler_ReturnsUser_ById() { using (var dbContext = new EntityDbContext()) { var userName = Guid.NewGuid().ToString(); var user = new User { Name = userName }; dbContext.Create(user); var rowsAffected = dbContext.SaveChangesAsync().Result; var handler = new HandleUserByQuery(dbContext); var result = handler.Handle(new UserBy(user.Id) { EagerLoad = new Expression <Func <User, object> >[] { x => x.Permissions, } }).Result; Assert.NotNull(result); result.ShouldEqual(user); rowsAffected.ShouldEqual(1); } }
public void Query_ReturnsData() { using (var dbContext = new EntityDbContext()) { var createdEntity = new User { Name = Guid.NewGuid().ToString() }; dbContext.Create(createdEntity); var affectedRows = dbContext.SaveChangesAsync().Result; affectedRows.ShouldEqual(1); var queriedEntity = dbContext.Query<User>().SingleOrDefaultAsync(x => x.Id == createdEntity.Id).Result; Assert.NotNull(queriedEntity); createdEntity.Id.ShouldEqual(queriedEntity.Id); } }
public void Handler_ReturnsUser_ByName_CaseInsensitively() { using (var dbContext = new EntityDbContext()) { var userName = Guid.NewGuid().ToString().ToUpper(); var user = new User { Name = userName }; dbContext.Create(user); var rowsAffected = dbContext.SaveChangesAsync().Result; var handler = new HandleUserByQuery(dbContext); var result = handler.Handle(new UserBy(userName.ToLower())).Result; Assert.NotNull(result); result.ShouldEqual(user); rowsAffected.ShouldEqual(1); } }
public void UserLoginStoreInterface_FindAsync_FindsMatchingRemoteMemberships() { using (var dbContext = new EntityDbContext()) { var user = new User { Name = Guid.NewGuid().ToString() }; var remote1 = new RemoteMembership { Id = { LoginProvider = Guid.NewGuid().ToString(), ProviderKey = Guid.NewGuid().ToString() } }; var remote2 = new RemoteMembership { Id = { LoginProvider = Guid.NewGuid().ToString(), ProviderKey = Guid.NewGuid().ToString() } }; var remote3 = new RemoteMembership { Id = { LoginProvider = Guid.NewGuid().ToString(), ProviderKey = Guid.NewGuid().ToString() } }; user.RemoteMemberships.Add(remote1); user.RemoteMemberships.Add(remote2); user.RemoteMemberships.Add(remote3); dbContext.Create(user); dbContext.SaveChangesAsync().GetAwaiter().GetResult(); var securityStore = new SecurityStore(dbContext); var result = securityStore.FindAsync(new UserLoginInfo(remote2.LoginProvider, remote2.ProviderKey)).Result; result.ShouldNotBeNull(); result.ShouldEqual(user); } }
public void EagerLoad_IncludesRelatedData() { using (var dbContext = new EntityDbContext()) { var userName = Guid.NewGuid().ToString(); var permissionName = Guid.NewGuid().ToString(); var user = new User { Name = userName, }; user.Permissions.Add(new Permission { Name = permissionName }); dbContext.Create(user); var affectedRows = dbContext.SaveChangesAsync().Result; affectedRows.ShouldEqual(3); var entity = dbContext.Query<User>() .EagerLoad(new Expression<Func<User, object>>[] { x => x.Permissions, }).Single(x => x.Name.Equals(userName)); entity.Permissions.Count.ShouldEqual(1); entity.Permissions.Single().Name.ShouldEqual(permissionName); } }
public void Handler_ReturnsUser_ById() { using (var dbContext = new EntityDbContext()) { var userName = Guid.NewGuid().ToString(); var user = new User { Name = userName }; dbContext.Create(user); var rowsAffected = dbContext.SaveChangesAsync().Result; var handler = new HandleUserByQuery(dbContext); var result = handler.Handle(new UserBy(user.Id) { EagerLoad = new Expression<Func<User, object>>[] { x => x.Permissions, } }).Result; Assert.NotNull(result); result.ShouldEqual(user); rowsAffected.ShouldEqual(1); } }
public void DiscardChangesAsync_ChangesDeletedEntityState_ToUnchanged() { using (var dbContext = new EntityDbContext()) { var entity = new User { Name = Guid.NewGuid().ToString() }; dbContext.Create(entity); var affectedRows = dbContext.SaveChangesAsync().Result; affectedRows.ShouldEqual(1); dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged); dbContext.Delete(entity); dbContext.Entry(entity).State.ShouldEqual(EntityState.Deleted); dbContext.DiscardChangesAsync().Wait(); dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged); } }
public void DiscardChangesAsync_ChangesAddedEntityState_ToDetached() { using (var dbContext = new EntityDbContext()) { var entity = new User { Name = Guid.NewGuid().ToString() }; dbContext.Create(entity); dbContext.Entry(entity).State.ShouldEqual(EntityState.Added); dbContext.DiscardChangesAsync().Wait(); dbContext.Entry(entity).State.ShouldEqual(EntityState.Detached); } }
public void ReloadAsync_ChangesModifiedEntityState_ToUnchanged() { using (var dbContext = new EntityDbContext()) { var description = Guid.NewGuid().ToString(); var entity = new Permission { Name = Guid.NewGuid().ToString(), Description = description }; dbContext.Create(entity); var affectedRows = dbContext.SaveChangesAsync().Result; affectedRows.ShouldEqual(1); dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged); entity.Description = Guid.NewGuid().ToString(); dbContext.Entry(entity).State.ShouldEqual(EntityState.Modified); dbContext.ReloadAsync(entity).Wait(); dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged); entity.Description.ShouldEqual(description); } }
public void Update_SetsEntityState_ToModified() { using (var dbContext = new EntityDbContext()) { var permissionName = Guid.NewGuid().ToString(); var entity = new Permission { Name = permissionName, Description = "d1" }; dbContext.Create(entity); var affectedRows = dbContext.SaveChangesAsync().Result; affectedRows.ShouldEqual(1); dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged); dbContext.Update(entity); dbContext.Entry(entity).State.ShouldEqual(EntityState.Modified); } }
public void Create_AddsEntityToDb_WhenChangesAreSaved() { using (var dbContext = new EntityDbContext()) { var entity = new User { Name = Guid.NewGuid().ToString() }; dbContext.Create(entity); entity.Id.ShouldEqual(0); var affectedRows = dbContext.SaveChangesAsync().Result; affectedRows.ShouldEqual(1); entity.Id.ShouldNotEqual(0); } }
public void Create_SetsEntityState_ToAdded() { using (var dbContext = new EntityDbContext()) { var entity = new User { Name = Guid.NewGuid().ToString() }; dbContext.Entry(entity).State.ShouldEqual(EntityState.Detached); dbContext.Create(entity); dbContext.Entry(entity).State.ShouldEqual(EntityState.Added); } }