public User Create(string login, string password, string nickname) { try { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(nickname)) { throw new ArgumentException("Invalid parameters"); } User user = new User { Login = login, Nickname = nickname, Password = Criptography.GetSHA1(password) }; UserRepository.Save(user); return(user); } catch (ArgumentException ex) { Logger.LogWarning("Error while saving user to database", ex); throw; } catch (Exception ex) { Logger.LogError("Unknown error while saving user to database", ex); throw new Exception("Unknown error while saving user to database"); } }
public void CreateWithNoHome_ReturnsCreatedUserWithId() { var userRepositoryMock = new Mock <IUserRepository>(); var homeRepositoryMock = new Mock <IHomeRepository>(); var loggingStub = new Mock <ILogger <UsersService> >(); UsersService userService = new UsersService(userRepositoryMock.Object, homeRepositoryMock.Object, loggingStub.Object); userRepositoryMock.Setup(o => o.Save(It.IsAny <User>())) .Callback((User u) => { u.UserId = 1; }) .Verifiable(); User userExpected = new User { UserId = 1, Login = "******", Password = Criptography.GetSHA1("a"), Nickname = "a" }; string login = "******"; string password = "******"; string nickname = "a"; User userSaved = userService.Create(login, password, nickname); string userExpectedString = JsonConvert.SerializeObject(userExpected); string userSavedString = JsonConvert.SerializeObject(userSaved); Assert.Equal(userExpectedString, userSavedString); }
public void Login_InvalidPassword_ReturnsApplicationException() { var userRepositoryMock = new Mock <IUserRepository>(); var homeRepositoryStub = new Mock <IHomeRepository>(); var loggingStub = new Mock <ILogger <UsersService> >(); UsersService userService = new UsersService(userRepositoryMock.Object, homeRepositoryStub.Object, loggingStub.Object); userRepositoryMock.Setup(o => o.GetUserByLogin(It.IsAny <string>())).Returns(new User { Login = "******", Password = Criptography.GetSHA1("b") }); string login = "******"; string password = "******"; Assert.Throws <ApplicationException>(() => userService.Login(login, password)); }
public User Login(string login, string password) { try { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { throw new ArgumentException("Login or Password is null or empty"); } User user = UserRepository.GetUserByLogin(login); if (user != null) { string passwordCrip = Criptography.GetSHA1(password); if (login != user.Login || passwordCrip != user.Password) { throw new ApplicationException("Invalid login or password"); } } else { throw new ApplicationException("User not found"); } return(user); } catch (ArgumentException ex) { Logger.LogWarning("Error while loggin in user", ex); throw; } catch (ApplicationException ex) { Logger.LogWarning("Error while loggin in user", ex); throw; } catch (Exception ex) { Logger.LogError("Unknown error while loggin in user", ex); throw new Exception("Unknown error while loggin in user"); } }
public void Login_ValidCredentials_ReturnsUser() { var userRepositoryMock = new Mock <IUserRepository>(); var homeRepositoryStub = new Mock <IHomeRepository>(); var loggingStub = new Mock <ILogger <UsersService> >(); UsersService userService = new UsersService(userRepositoryMock.Object, homeRepositoryStub.Object, loggingStub.Object); userRepositoryMock.Setup(o => o.GetUserByLogin(It.IsAny <string>())).Returns(new User { UserId = 1, Login = "******", Password = Criptography.GetSHA1("a") }); string login = "******"; string password = "******"; User user = userService.Login(login, password); Assert.Equal(1, user.UserId); }