public async Task CheckPasswordSignIn(UserDto dto, string password) { using (var mock = AutoMock.GetLoose()) { var user = new Ssuser { UserName = dto.UserName }; var sr = SignInResult.Success; // Expression<Func<Ssuser, bool>> ex = u => u.UserName == dto.UserName; // // would like to test with ^^this^^ but don't think I'm able to // mock.Mock<IUserDataRepository>().Setup(x => x.Find(ex)).Returns(Task.FromResult(user)); mock.Mock <IUserData>().Setup(x => x.FindAsync(It.IsAny <Expression <Func <Ssuser, bool> > >())) .Returns(Task.FromResult(user)); mock.Mock <IUserData>().Setup(x => x.CheckPasswordSignInAsync(user, password)) .Returns(Task.FromResult(sr)); var cls = mock.Create <AuthRepo>(); var expected = SignInResult.Success; var actual = await cls.CheckPasswordSignInAsync(dto, password); Assert.True(actual != null); Assert.Equal(expected, actual); } }
public async Task RegisterUser(UserForRegisterDto toRegister) { using (var mock = AutoMock.GetLoose()) { var user = new Ssuser() { FirstName = toRegister.FirstName, LastName = toRegister.LastName, Email = toRegister.Email, UserName = toRegister.UserName, }; var ir = IdentityResult.Success; mock.Mock <IMap>().Setup(x => x.MapToSsuser(toRegister)).Returns(user); mock.Mock <IUserData>().Setup(x => x.CreateUserAsync(user, toRegister.Password)) .Returns(Task.FromResult(ir)); mock.Mock <IUserData>().Setup(x => x.AddUserRoleOnRegisterAsync(user)) .Returns(Task.FromResult(ir)); var cls = mock.Create <UserRepo>(); var expected = IdentityResult.Success; var actual = await cls.RegisterUserAsync(toRegister); Assert.True(actual != null); Assert.Equal(expected, actual); Assert.Equal(expected, actual); } }
public async Task GetUserAsyncByUserName(string userName) { using (var mock = AutoMock.GetLoose()) { var user = new Ssuser { UserName = userName }; var userDto = new UserDto { UserName = userName }; // Expression<Func<Ssuser, bool>> ex = u => u.UserName == userName; // // would like to test with ^^this^^ but don't think I'm able to // mock.Mock<IUserDataRepository>().Setup(x => x.Find(ex)).Returns(Task.FromResult(user)); mock.Mock <IUserData>().Setup(x => x.FindAsync(It.IsAny <Expression <Func <Ssuser, bool> > >())) .Returns(Task.FromResult(user)); mock.Mock <IMap>().Setup(x => x.MapToUserForDetailDto(user)).Returns(userDto); var cls = mock.Create <UserRepo>(); var expected = new UserDto { UserName = userName }; var actual = await cls.GetUserAsync(userName); Assert.True(actual != null); Assert.Equal(expected.UserName, actual.UserName); } }
public async Task GetRolesFromUser(string userName) { using (var mock = AutoMock.GetLoose()) { var user = new Ssuser { UserName = userName }; var roles = new List <string> { "role1", "role2" }; // Expression<Func<Ssuser, bool>> ex = u => u.UserName == userName; // // would like to test with ^^this^^ but don't think I'm able to // mock.Mock<IUserDataRepository>().Setup(x => x.Find(ex)).Returns(Task.FromResult(user)); mock.Mock <IUserData>().Setup(x => x.FindAsync(It.IsAny <Expression <Func <Ssuser, bool> > >())) .Returns(Task.FromResult(user)); var cls = mock.Create <AdminRepo>(); var expected = It.IsAny <IEnumerable <string> >(); var actual = await cls.GetRolesAsync(userName); Assert.True(actual != null); // More Tests needed } }
public async Task UpdateRolesForUser(string userName, string[] rolesToUpdate) { using (var mock = AutoMock.GetLoose()) { var user = new Ssuser { UserName = userName }; var ir = IdentityResult.Success; // Expression<Func<Ssuser, bool>> ex = u => u.UserName == userName; // // would like to test with ^^this^^ but don't think I'm able to // mock.Mock<IUserDataRepository>().Setup(x => x.Find(ex)).Returns(Task.FromResult(user)); mock.Mock <IUserData>().Setup(x => x.FindAsync(It.IsAny <Expression <Func <Ssuser, bool> > >())) .Returns(Task.FromResult(user)); mock.Mock <IUserData>().Setup(x => x.AddRolesToUserAsync(user, rolesToUpdate)).Returns(Task.FromResult(ir)); var cls = mock.Create <AdminRepo>(); var expected = ir; var actual = await cls.UpdateRolesAsync(userName, rolesToUpdate); Assert.True(actual != null); // Assert.Equal(expected, actual); // More Tests needed } }
public Ssuser MapToSsuser(UserForRegisterDto u) { var user = new Ssuser() { FirstName = u.FirstName, LastName = u.LastName, Email = u.Email, DisplayName = u.UserName, CreatedDate = u.Created, LastActive = u.LastActive, //does this break? 062820 UserName = u.UserName }; return(user); }
// User public UserDto MapToUserForDetailDto(Ssuser u) { var user = new UserDto() { Id = u.Id, // Id = u.UserId, // "u.UserId is always 0" -z 062820 FirstName = u.FirstName, LastName = u.LastName, Email = u.Email, DateOfBirth = u.DateOfBirth, CreatedDate = u.CreatedDate, LastActive = u.LastActive, UserName = u.UserName, DisplayName = u.DisplayName, }; return(user); }
public async Task GetUserAsyncById(int userId) { using (var mock = AutoMock.GetLoose()) { var user = new Ssuser { Id = userId }; var userDto = new UserDto { Id = userId }; mock.Mock <IUserData>().Setup(x => x.GetByIdAsync(userId)).Returns(Task.FromResult(user)); mock.Mock <IMap>().Setup(x => x.MapToUserForDetailDto(user)).Returns(userDto); var cls = mock.Create <UserRepo>(); var expected = new UserDto { Id = userId }; var actual = await cls.GetUserAsync(userId); Assert.True(actual != null); Assert.Equal(expected.Id, actual.Id); } }