public void Register_WhenUserModelValid_ShouldSaveToDatabase() { var testUser = new UserModel() { Username = "******", Nickname = "VALIDNICK", AuthCode = new string('b', 40) }; //var httpServer = new InMemoryHttpServer("http://localhost/"); var model = this.RegisterTestUser(httpServer, testUser); Assert.AreEqual(testUser.Nickname, model.Nickname); Assert.IsNotNull(model.SessionKey); }
public HttpResponseMessage PostRegisterUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new ForumContext(); using (context) { this.ValidateUsername(model.Username); this.ValidateNickname(model.Nickname); this.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var nicknameToLower = model.Nickname.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower || usr.Nickname.ToLower() == nicknameToLower); if (user != null) { throw new InvalidOperationException("Users exists"); } user = new User() { Username = usernameToLower, Nickname = model.Nickname, AuthCode = model.AuthCode }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); var loggedModel = new LoggedUserModel() { Nickname = user.Nickname, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
public void GetAll_WhenDataInDatabase_ShouldReturnData() { var testUser = new UserModel() { Username = "******", Nickname = "VALIDNICK", AuthCode = new string('b', 40) }; LoggedUserModel userModel = RegisterTestUser(httpServer, testUser); var headers = new Dictionary<string, string>(); headers["X-sessionKey"] = userModel.SessionKey; var response = httpServer.Get("api/threads", headers); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.IsNotNull(response.Content); }
public void Register_WhenUserModelValid_ShouldSaveToDatabase() { var testUser = new UserModel() { Username = "******", Nickname = "VALIDNICK", AuthCode = new string('b', 40) }; var httpServer = new InMemoryHttpServer("http://localhost/"); var response = httpServer.CreatePostRequest("api/users/register", testUser); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.IsNotNull(response.Content); var contentString = response.Content.ReadAsStringAsync().Result; var model = JsonConvert.DeserializeObject<LoggedUserModel>(contentString); Assert.AreEqual(testUser.Nickname, model.Nickname); Assert.IsNotNull(model.SessionKey); }
public HttpResponseMessage PostLoginUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new ForumContext(); using (context) { this.ValidateUsername(model.Username); this.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower && usr.AuthCode == model.AuthCode); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } if (user.SessionKey == null) { user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); } var loggedModel = new LoggedUserModel() { Nickname = user.Nickname, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
private LoggedUserModel RegisterTestUser(InMemoryHttpServer httpServer, UserModel testUser) { var response = httpServer.Post("api/users/register", testUser); var contentString = response.Content.ReadAsStringAsync().Result; var userModel = JsonConvert.DeserializeObject<LoggedUserModel>(contentString); return userModel; }