public void AddUser_UsernameAlreadyExists_ShouldNotThrowException() { using (DbCommand inserTestData = memoryDbConnection.CreateCommand()) { inserTestData.CommandText = "INSERT INTO Users('Username', 'PasswordHash', 'RoleID') VALUES ('BarUser', 'pw', 1);"; inserTestData.ExecuteNonQuery(); } using (IAddUnitOfWork <User> uow = repository.Add()) { uow.Entity.UserName = "******"; uow.Entity.NormalizedUserName = uow.Entity.UserName.Normalize(); uow.Entity.PasswordHash = "pw"; uow.Entity.Role = "User"; uow.Complete(); } }
public void AddUser_ShouldSucceed() { using (DbCommand insertTestData = memoryDbConnection.CreateCommand()) { insertTestData.CommandText = "INSERT INTO Users ('Username', 'PasswordHash', 'RoleID') VALUES ('BarUser', 'pw', 1);"; insertTestData.ExecuteNonQuery(); } using (IAddUnitOfWork <User> uow = repository.Add()) { uow.Entity.UserName = "******"; uow.Entity.NormalizedUserName = uow.Entity.UserName.Normalize(); uow.Entity.PasswordHash = "SecretPassword"; uow.Entity.Role = "User"; uow.Entity.PublicKeys.Add(new Key() { KeyData = Encoding.Default.GetBytes("keydata1"), Flag = Key.KeyFlag.ACTIVE }); uow.Entity.PublicKeys.Add(new Key() { KeyData = Encoding.Default.GetBytes("keydata2"), Flag = Key.KeyFlag.OBSOLET }); uow.Complete(); } User fooUser = repository.GetById(2); Assert.AreEqual(2, fooUser.Id); Assert.AreEqual("FooUser", fooUser.UserName); Assert.AreEqual("FooUser".ToUpper(), fooUser.NormalizedUserName); Assert.AreEqual("SecretPassword", fooUser.PasswordHash); Assert.AreEqual("User", fooUser.Role); Assert.AreEqual(2, fooUser.PublicKeys.Count); Assert.AreEqual(0, fooUser.PublicKeys[0].Id); Assert.AreEqual(Key.KeyFlag.ACTIVE, fooUser.PublicKeys[0].Flag); Assert.AreEqual(Convert.ToBase64String(Encoding.Default.GetBytes("keydata1")), Convert.ToBase64String(fooUser.PublicKeys[0].KeyData)); Assert.AreEqual(1, fooUser.PublicKeys[1].Id); Assert.AreEqual(Key.KeyFlag.OBSOLET, fooUser.PublicKeys[1].Flag); Assert.AreEqual(Convert.ToBase64String(Encoding.Default.GetBytes("keydata2")), Convert.ToBase64String(fooUser.PublicKeys[1].KeyData)); }
public Task <IdentityResult> CreateAsync(User user, CancellationToken cancellationToken) { try { using (IAddUnitOfWork <User> unitOfWork = UserRepository.Add()) { unitOfWork.Entity.UserName = user.UserName; unitOfWork.Entity.NormalizedUserName = user.NormalizedUserName; unitOfWork.Entity.PasswordHash = user.PasswordHash; unitOfWork.Entity.Role = user.Role; unitOfWork.Entity.PublicKeys.AddRange(user.PublicKeys); unitOfWork.Complete(); } } catch (IOException exception) { return(GetResultFromUnitCompleteException(exception)); } return(Task.FromResult(IdentityResult.Success)); }