public string LoginUser(UserLoginModel userLogin, out string nickname) { if (userLogin == null) { throw new ArgumentNullException("UserLoginModel is null"); } ValidateUsername(userLogin.Username); ValidateAuthCode(userLogin.AuthCode); var userNameToLower = userLogin.Username.ToLower(); var user = this.Data.Users.GetAll().Where(u => u.Username.ToLower() == userNameToLower && u.AuthCode == userLogin.AuthCode).FirstOrDefault(); if (user == null) { throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH"); } nickname = user.Nickname; var sessionKay = GenerateSessionKey((int)user.Id); user.SessionKey = sessionKay; user.ConnectionId = userLogin.ConnectionId; Data.Users.Update(user); this.Data.SaveChanges(); return sessionKay; }
public void LoginUser_WhenAuthCodeIsNotMatch_ShouldThrowException() { mock.Setup(g => g.Users.GetAll()).Returns(new User[] { new User { Username = "******", Nickname = "nickname", ConnectionId = "75ccd4c3-fd0f-4a1d-80bb-885fb1bb5296", SessionKey = "100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL", AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5" }, }.AsQueryable()); UserLoginModel loginModel = new UserLoginModel() { AuthCode = "11111648010756ed51eecccf94c01bc0015048c5", Username = "******", ConnectionId = "newConnectionId" }; UserService userService = new UserService(mock.Object); string nickname; userService.LoginUser(loginModel, out nickname); }
public void LoginUser_WhenUserLoginModelIsValid_ShouldReturnSessionKeyAndNickname() { User updatedUser = new User(); UserLoginModel loginModel = new UserLoginModel() { AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5", Username = "******", ConnectionId = "newConnectionId" }; UserService userService = new UserService(mock.Object); mock.Setup(g => g.Users.GetAll()).Returns(new User[] { new User { Username = "******", Nickname = "nickname", ConnectionId = "75ccd4c3-fd0f-4a1d-80bb-885fb1bb5296", SessionKey = "100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL", AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5" }, }.AsQueryable()); mock.Setup(u => u.Users.Update(It.IsAny<User>())).Callback((User user) => updatedUser = user); string nickname; string sessionKey = userService.LoginUser(loginModel, out nickname); Assert.AreEqual("nickname",nickname); Assert.IsNotNull(sessionKey); Assert.AreEqual(loginModel.ConnectionId, updatedUser.ConnectionId); }
public UserLoggedModel Login(UserLoginModel user) { UserLoggedModel userLogged = new UserLoggedModel(); try { string nickName; userLogged.SessionKey = UserServise.LoginUser(new UserLoginModel() { AuthCode = user.AuthCode, Username = user.Username, ConnectionId = Context.ConnectionId, }, out nickName); userLogged.Nickname = nickName; return userLogged; } catch (ServerErrorException ex) { Clients.Caller.serverErrorException(ex.Message); return null; } }