public UserForListingModel Map(User user) { return new UserForListingModel() { Id = user.Id, Name = user.Name }; }
public LoggedUserModel FromUserToUserLoginApi(User userToMapp) { LoggedUserModel userLoginApi = new LoggedUserModel(); userLoginApi.Name = userToMapp.Name; userLoginApi.Id = userToMapp.Id; userLoginApi.EMail = userToMapp.Email; return userLoginApi; }
public User Map(NewUser source) { User user = new User(); user.Name = source.Login; user.Password = source.Password; user.EMail = source.Email; return user; }
[Test]//Work + public void VerifyPassword_IsFalse_Test() { User user = new User { Password = "******" }; //You can't mocked PasswordHasher becouse it's a class, not a interface //Mock<PasswordHasher> passHesher = new Mock<PasswordHasher>(); // passHesher.Setup(t => t.VerifyHashedPassword(It.IsAny<string>(), It.IsAny<string>())).Returns(PasswordVerificationResult.Success); //passHesher.Setup(t => t.HashPassword("asdasdasd")).Returns("D8A928B2043DB77E340B523547BF16CB4AA483F0645FE0A290ED1F20AAB76257"); // passHesher.CallBase = true; PasswordHasher ph = new PasswordHasher(); string userFromDb = ph.HashPassword("ajjkks"); var result = usServ.VerifyPasswords(userFromDb, user.Password); Assert.IsFalse(result); }
public bool ChangeRolesOfUser(User user) { using (var context = new WorldOfWordsDatabaseContext()) { User userToChange = context.Users.Include("Roles").Where(item => item.Id == user.Id).FirstOrDefault(); userToChange.Roles = new List<Role>(); var queryOfNeededIds = user.Roles.Select(item => item.Id); var toAdd = context.Roles.Where(item => queryOfNeededIds.Contains(item.Id)).ToList(); foreach (var a in toAdd) { userToChange.Roles.Add(a); } context.SaveChanges(); } return true; }
public void Map_FieldsAreEqual() { //Arrange var initial = new RegisterUserModel { Email = "*****@*****.**", Login = "******", Password = "******" }; var expected = new User { Email = "*****@*****.**", Name = "Roman", Password = "******" }; //Act var actual = (new UserMapper()).Map(initial); //Assert Assert.AreEqual(expected.Email, actual.Email); Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.Password, actual.Password); }
public void FromUserToUserLoginApi_FieldsAreEqual() { //Arrange var initial = new User { Id = 1, Email = "*****@*****.**", Name = "Roman", Password = "******" }; var expected = new LoggedUserModel { EMail = "*****@*****.**", Name = "Roman", Id = 1 }; //Act var actual = (new UserLoginMapper()).FromUserToUserLoginApi(initial); //Assert Assert.AreEqual(expected.EMail, actual.EMail); Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.Id, actual.Id); }
[Test] //+ public void EditUserNameIsFalse_Test() { //Arrange User user = new User(); user.Name = "Petro"; user.Id = 1; user = null; worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetById(It.IsAny<int>())).Returns(user); worldOfWordsUow.Setup(t => t.Save()).Verifiable("All is fine"); //Act var result = usServ.EditUserName("Andrew", 2); //Assert uowFactory.Verify(t => t.GetUnitOfWork(), Times.Exactly(1)); worldOfWordsUow.Verify(t => t.UserRepository, Times.Exactly(1)); userReposit.Verify(t => t.GetById(It.IsAny<int>()), Times.Exactly(1)); worldOfWordsUow.Verify(t => t.Save(), Times.Never); Assert.IsNull(user); Assert.IsFalse(result); }
[Test]//work + public void CheckUserEmailIsFalse_Test() { //Arrange User user = new User(); user.Name = "Petro"; user.Id = 1; user.Email = "*****@*****.**"; var forgotPassword = new ForgotPasswordUserModel(); forgotPassword.Id = "5"; forgotPassword.Email = "*****@*****.**"; entity.Id = 5; var reposit = new Mock<IRepository<IncomingUser>>(); var iusReposit = new Mock<IUserRepository>(); IQueryable<User> Users = new List<User> { user }.AsQueryable<User>(); worldOfWordsUow.Setup(t => t.UserRepository).Returns(iusReposit.Object); iusReposit.Setup(t => t.GetAll()).Returns(Users); //Act var result = usServ.CheckUserEmail(forgotPassword); //Assert uowFactory.Verify(t => t.GetUnitOfWork(), Times.Exactly(1)); worldOfWordsUow.Verify(t => t.UserRepository, Times.Exactly(1)); iusReposit.Verify(t => t.GetAll(), Times.Exactly(1)); Assert.IsFalse(result); }
[Test] //+ public void GetAmountOfUsersByRoleIdNotZero_Test() { //Arrange Role admin = new Role(); admin.Id = 1; IQueryable<Role> rol = new List<Role>() { admin }.AsQueryable<Role>(); User user = new User { Id = 5, Name = "Andrew" }; var user_2 = new User(); user_2.Id = 3; user.Roles = new List<Role>() { admin }; user_2.Roles = new List<Role>() { admin }; IQueryable<User> listOfUsers = new List<User> { user, user_2 }.AsQueryable<User>(); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetAll()).Returns(listOfUsers); //Act int result = usServ.GetAmountOfUsersByRoleId(1); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.AtLeastOnce()); userReposit.Verify(t => t.GetAll(), Times.AtLeastOnce()); Assert.IsTrue(result == 2); }
[Test] //+ public void SearchByNameAndRole_RoleIsZero_Test() { IRepository<WorldOfWords.Domain.Models.Role> RoleReposit = new Mock<IRepository<WorldOfWords.Domain.Models.Role>>().Object; #region user and role User user = new User(); user.Name = "Andrew"; user.Email = "*****@*****.**"; user.Id = 2; Role admin = new Role(); admin.Id = 0; user.Roles = new List<Role>() { admin }; IQueryable<User> users = new List<User>() { user }.AsQueryable<User>(); #endregion worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetAll()).Returns(users); //Act var userColl = usServ.SearchByNameAndRole("Andrew"); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.Once); userReposit.Verify(t => t.GetAll(), Times.Once); Assert.IsNotNull(userColl); }
[Test] //Work + public void ChangePassword_Test() { //Arrange //PasswordHasher pasHesh = new PasswordHasher(); //string password = pasHesh.HashPassword("hjkol68"); ForgotPasswordUserModel model = new ForgotPasswordUserModel { Id = "5", Password = "******" }; User user = new User(); user.Email = "*****@*****.**"; user.Id = 5; user.Password = "******"; IQueryable<User> users = new List<User>() { user }.AsQueryable<User>(); uowFactory.Setup(t => t.GetUnitOfWork()).Returns(worldOfWordsUow.Object); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetAll()).Returns(users); //Act usServ.ChangePassword(model); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.Once); userReposit.Verify(t => t.GetAll(), Times.Once); Assert.AreNotEqual(model.Password, user.Password); Assert.AreEqual(user.Id.ToString(), model.Id); }
[Test] //work - 100% + public void ChangeRolesOfUser_Test() { ///Arrange Mock<IRepository<WorldOfWords.Domain.Models.Role>> roleRepository = new Mock<IRepository<Role>>(); Role admin = new Role(); admin.Id = 1; IQueryable<Role> rol = new List<Role>() { admin }.AsQueryable<Role>(); User user_2 = new User() { Email = "*****@*****.**", Id = 1, Roles = new List<Role> { new Role { Id = 1 } } }; IQueryable<User> users = new List<User>() { new User { Email = "*****@*****.**", Id = 1, Roles = new List<Role> { new Role { Id = 1 } }} }.AsQueryable<User>(); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetAll()).Returns(users); worldOfWordsUow.Setup(t => t.RoleRepository).Returns(roleRepository.Object); roleRepository.Setup(t => t.GetAll()).Returns(rol); worldOfWordsUow.Setup(t => t.Save()).Verifiable(); //Act var actual = usServ.ChangeRolesOfUser(user_2); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.Once); userReposit.Verify(t => t.GetAll(), Times.Once); worldOfWordsUow.Verify(t => t.RoleRepository, Times.Once); roleRepository.Verify(t => t.GetAll(), Times.Once); worldOfWordsUow.VerifyAll(); Assert.IsTrue(actual); }
[Test] //+ public void CheckUserIdIsFalse_Test() { //Assert User user = new User { Email = "*****@*****.**", Id = 15689 }; ForgotPasswordUserModel checkEmail = new ForgotPasswordUserModel() { Id = user.Id.ToString(), Email = "*****@*****.**" }; IQueryable<User> users = new List<User>() { user }.AsQueryable<User>(); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetAll()).Returns(users); tokenVaidation.Setup(t => t.GetHashSha256(It.IsAny<string>())).Returns("Hrin"); //Act var result = usServ.CheckUserId(checkEmail); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.Once); userReposit.Verify(t => t.GetAll(), Times.Once); tokenVaidation.Verify(t => t.GetHashSha256(user.Id.ToString()), Times.Once); Assert.IsFalse(result); }
[Test] //work + public void EditUserPasswordIsTrue_Test() { User user = new User(); user.Email = "*****@*****.**"; user.Id = 1; user.Password = "******"; IQueryable<User> users = new List<User>() { user }.AsQueryable<User>(); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetById(It.IsAny<int>())).Returns(user); worldOfWordsUow.Setup(t => t.Save()).Verifiable("Saved"); //Act var result = usServ.EditUserPassword("hello", 1); //Assert uowFactory.Verify(t => t.GetUnitOfWork(), Times.Exactly(1)); worldOfWordsUow.Verify(t => t.UserRepository, Times.Exactly(1)); userReposit.Verify(t => t.GetById(It.IsAny<int>()), Times.Exactly(1)); worldOfWordsUow.VerifyAll(); Assert.IsTrue(result); }
[Test] //work + public void EditUserPasswordIsFalse_Test() { User user = new User(); user.Email = "*****@*****.**"; user.Id = 1; user.Password = "******"; worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetById(1)).Returns(user); worldOfWordsUow.Setup(t => t.Save()).Verifiable("Saved"); //Act var result = usServ.EditUserPassword("bye258456", 0); //Assert uowFactory.Verify(t => t.GetUnitOfWork(), Times.Exactly(1)); worldOfWordsUow.Verify(t => t.UserRepository, Times.Exactly(1)); userReposit.Verify(t => t.GetById(0), Times.Exactly(1)); Assert.IsFalse(result); }
[Test] // public void ConfirmUserRegistration_IsFalseTest() { //Arrange IncomingUser inUser = new IncomingUser { Email = "*****@*****.**", Id = 1, Password = "******", Name = "Stepan", Token = "jsdjsjd" }; IQueryable<IncomingUser> incomUsers = new List<IncomingUser> { inUser }.AsQueryable<IncomingUser>(); User user = new User { Email = "*****@*****.**", Id = 1, Password = "******", Name = "Stepan", HashedToken = "sknsdmsopp" }; IQueryable<Role> roles = new List<Role> { new Role(){ Id = 1, Name = "Student"} }.AsQueryable<Role>(); worldOfWordsUow.Setup(t => t.IncomingUserRepository).Returns(incUserReposit.Object); incUserReposit.Setup(t => t.GetAll()).Returns(incomUsers); incUserReposit.Setup(s => s.Delete(It.IsAny<IncomingUser>())).Verifiable("deleted"); incUserMapper.Setup(t => t.ToDomainModel(It.IsAny<IncomingUser>())).Returns(user); worldOfWordsUow.Setup(t => t.RoleRepository).Returns(roleRepository.Object); roleRepository.Setup(t => t.GetAll()).Returns(roles); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.Add(user)).Verifiable("Added user"); worldOfWordsUow.Setup(t => t.Save()).Verifiable("Saved"); //Act var result = usServ.ConfirmUserRegistration(2, "jaflkasjdl"); //Assert uowFactory.Verify(t => t.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.IncomingUserRepository, Times.Once); incUserReposit.Verify(t => t.GetAll(), Times.Once); incUserReposit.Verify(t => t.Delete(It.IsAny<IncomingUser>()), Times.Never); incUserMapper.Verify(t => t.ToDomainModel(It.IsAny<IncomingUser>()), Times.Never); worldOfWordsUow.Verify(t => t.RoleRepository, Times.Never); roleRepository.Verify(t => t.GetAll(), Times.Never); worldOfWordsUow.Verify(t => t.UserRepository, Times.Never); userReposit.Verify(t => t.Add(It.IsAny<User>()), Times.Never); worldOfWordsUow.Verify(t => t.Save(), Times.Never); Assert.IsFalse(result); }
[Test] //Work + public void GetUserNameIsNull_Test() { User user = new User(); user.Name = "Andrew"; user.Id = 5; worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetById(5)).Returns(user); var result = usServ.GetUserName(0); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(f => f.UserRepository, Times.Once); userReposit.Verify(w => w.GetById(5), Times.Never); userReposit.Verify(w => w.GetById(0), Times.Once); Assert.IsNull(result); }
[Test] //Work public void ExistUser_Test() { //Arrange User user = new User(); user.Email = entity.Email; IQueryable<User> Users = new List<User> { user }.AsQueryable(); IQueryable<IncomingUser> incUsers = new List<IncomingUser> { entity }.AsQueryable<IncomingUser>(); worldOfWordsUow.Setup(t => t.IncomingUserRepository).Returns(incUserReposit.Object); incUserReposit.Setup(t => t.GetAll()).Returns(incUsers); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetAll()).Returns(Users); //Act var result = usServ.Exists(entity); //Assert uowFactory.Verify(t => t.GetUnitOfWork(), Times.Exactly(1)); worldOfWordsUow.Verify(t => t.UserRepository, Times.Exactly(1)); userReposit.Verify(t => t.GetAll(), Times.Exactly(1)); worldOfWordsUow.Verify(t => t.IncomingUserRepository, Times.Exactly(0)); incUserReposit.Verify(t => t.GetAll(), Times.Exactly(0)); Assert.AreEqual(Users.First().Email, entity.Email); Assert.IsTrue(result, "Variable is exist"); }
[Test] //+ public void CheckUserAuthorization_IsTrue_Test() { //Arrange UserWithPasswordModel userLoginApi = new UserWithPasswordModel(); PasswordHasher passHesher = new PasswordHasher(); User user = new User(); user.Email = entity.Email; user.Id = 4; user.Roles = new List<Role> { new Role() { Name = "admin", Id = user.Id } }; userLoginApi.Password = "******"; userLoginApi.Email = user.Email; string userPassword = passHesher.HashPassword(userLoginApi.Password); user.Password = userPassword; IQueryable<User> Users = new List<User> { user }.AsQueryable<User>(); worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetAll()).Returns(Users); userReposit.Setup(t => t.AddOrUpdate(It.IsAny<User>())).Verifiable("added or updated"); worldOfWordsUow.Setup(t => t.Save()).Verifiable("Saved"); //Act var result = usServ.CheckUserAuthorization(userLoginApi); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.Exactly(2)); userReposit.Verify(t => t.AddOrUpdate(It.IsAny<User>()), Times.Once); worldOfWordsUow.VerifyAll(); Assert.IsTrue(result); }
[Test] //+ public void CheckUserNameIsTrue_Test() { //Arrange User user = new User(); user.Name = "Petro"; user.Id = 1; worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetById(It.IsAny<int>())).Returns(user); //Act var result = usServ.CheckUserName("Petro", 1); //Assert uowFactory.Verify(t => t.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.Once); userReposit.Verify(t => t.GetById(It.IsAny<int>()), Times.Once); Assert.IsTrue(result, "User is not Exist"); }
public bool ChangeRolesOfUser(User user) { using (var uow = _unitOfWorkFactory.GetUnitOfWork()) { User userToChange = uow.UserRepository.GetAll().Include("Roles").FirstOrDefault(item => item.Id == user.Id); userToChange.Roles = new List<Role>(); var queryOfNeededIds = user.Roles.Select(item => item.Id); var toAdd = uow.RoleRepository.GetAll().Where(item => queryOfNeededIds.Contains(item.Id)).ToList(); foreach (var a in toAdd) { userToChange.Roles.Add(a); } uow.Save(); } return true; }
[Test] //+ public void CheckUserNameIsFalse_NamesAreNotEqual_Test() { //Arrange User user = new User(); user.Name = "Andrew"; user.Id = 1; worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetById(It.IsAny<int>())).Returns(user); //Act var result = usServ.CheckUserName("Petro", 1); //Assert worldOfWordsUow.Verify(t => t.UserRepository, Times.Once); userReposit.Verify(t => t.GetById(It.IsAny<int>()), Times.Once); Assert.AreNotEqual(user.Name, "Petro"); Assert.IsFalse(result, "User is not Exist"); }
[Test] //+ public void CheckUserPasswordPasswordNotEqual_Test() { //Arrange PasswordHasher passHesh = new PasswordHasher(); string passsword = passHesh.HashPassword("1235sjkf"); User user = new User(); user.Name = "Stepan"; user.Id = 3; user.Password = passsword; worldOfWordsUow.Setup(t => t.UserRepository).Returns(userReposit.Object); userReposit.Setup(t => t.GetById(It.IsAny<int>())).Returns(user); //Act var result = usServ.CheckUserPassword("1ah45k7", user.Id); //Assert uowFactory.Verify(f => f.GetUnitOfWork(), Times.Once); worldOfWordsUow.Verify(t => t.UserRepository, Times.Once); userReposit.Verify(t => t.GetById(It.IsAny<int>()), Times.Once); Assert.IsFalse(result); }
public void GetUsersNotBelongingToGroupTestPositive() { // Arrange // The group, not belonging to it users must be returned by test method var group = new Group { Id = 111, Course = new Course { Id = 222 }, OwnerId = 333, CourseId = 222 }; var role1 = new Role { Name = "Student" }; var role2 = new Role { Name = "AnotherRole" }; // On the exit must be this user with this enrollment // Enrollment to return var enrollment = new Enrollment { GroupId = 100, Group = new Group { Course = new Course { Id = 100 } } }; // User to return var user = new User { Id = 100, Roles = new List<Role> { role1 }, Enrollments = new List<Enrollment> { enrollment } }; enrollment.User = user; // User to test "u.Id != group.OwnerId" condition in Service var user1 = new User { Id = 333, Roles = new List<Role> { role1 }, Enrollments = new List<Enrollment> { enrollment } }; // User to test "u.Enrollments.Select(e => e.Group.Course).FirstOrDefault(c => c.Id == course.Id) == null" // condition in Service var enrollment2 = new Enrollment { GroupId = 100, Group = new Group { Course = new Course { Id = 222 } } }; var user2 = new User { Id = 333, Roles = new List<Role> { role1 }, Enrollments = new List<Enrollment> { enrollment2 } }; // User to test "!usersCurrentlyInGroup.Any(u2 => u2.Id == u.Id)" condition var user3 = new User { Id = 333, Roles = new List<Role> { role1 }, Enrollments = new List<Enrollment> { enrollment2 } }; // User to test "u.Roles.Select(r => r.Name).Contains("Student")" condition var user4 = new User { Id = 333, Roles = new List<Role> { role2 }, Enrollments = new List<Enrollment> { enrollment2 } }; // For enrollmentRepository to return var enrollments = new List<Enrollment>(new Enrollment[] { enrollment, new Enrollment { GroupId = 111, User = user1} }).AsQueryable(); var users = (new List<User> { user, user1, user2, user3, user4 }).AsQueryable(); var groupRepository = new Mock<IRepository<Group>>(); unitOfWork.Setup(x => x.GroupRepository).Returns(groupRepository.Object); groupRepository.Setup(x => x.GetById(group.Id)).Returns(group); enrollmentRepository.Setup(x => x.GetAll()).Returns(enrollments); var userRepository = new Mock<IUserRepository>(); unitOfWork.Setup(x => x.UserRepository).Returns(userRepository.Object); userRepository.Setup(x => x.GetAll()).Returns(users); // Act var result = enrollmentService.GetUsersNotBelongingToGroup(group.Id); // Assert Assert.IsTrue(result.TrueForAll(u => u.Roles.Select(r => r.Name).Contains("Student")), "User should have the Student role"); Assert.IsTrue(result.TrueForAll(u => !u.Enrollments.Any( e => e.GroupId == group.Id)), "User shouldn't be in that group"); Assert.IsTrue(result.TrueForAll(u => !u.Enrollments.Any(e => e.Group.CourseId == group.CourseId)), "User shouldn't be in the course that contains given group"); Assert.IsTrue(result.TrueForAll(u => u.Id != group.OwnerId)); }