public void Register_WhenAuthcodeIsInvalidLength_StatusCode400()
        {
            var type = typeof(UsersController);
            var testUser = new UserModel()
            {
                Username = "******",
                Displayname = "VALIDNICK",
                AuthCode = "Not40SymbolsLong"
            };

            var response = httpServer.Post("api/users/register", testUser);
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerfromOperationAndHandleException(() =>
            {
                using (var dbContext = new AcadBlogContext())
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateNickname(model.Displayname);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameAsLowercase = model.Username.ToLower();
                    var nicknameAsLowercase = model.Displayname.ToLower();

                    var existingUser = dbContext.Users.FirstOrDefault(
                        u => u.Username == usernameAsLowercase ||
                            u.Displayname == nicknameAsLowercase);

                    if (existingUser != null)
                    {
                        throw new InvalidOperationException(
                            "User with the same displayname or username already exists.");
                    }

                    var newUser = new User()
                    {
                        Username = usernameAsLowercase,
                        Displayname = model.Displayname,
                        AuthCode = model.AuthCode,
                    };

                    dbContext.Users.Add(newUser);
                    dbContext.SaveChanges();

                    newUser.SessionKey = this.GenerateSessionKey(newUser.Id);
                    dbContext.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        Displayname = model.Displayname,
                        SessionKey = newUser.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created,
                        loggedModel);

                    return response;
                };
            });

            return responseMsg;
        }
        public void Logout_WhenSessionKeyIsValid_StatusCode200()
        {
            var type = typeof(UsersController);
            var testUser = new UserModel()
            {
                Username = "******",
                Displayname = "VALIDNICK",
                AuthCode = new string('b', 40)
            };

            var model = this.RegisterTestUser(this.httpServer, testUser);

            var response = this.LogoutUser(this.httpServer, model.SessionKey);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
        public void Register_WhenDisplaynameIsNullOrEmpty_StatusCode400()
        {
            var type = typeof(UsersController);
            var testUser = new UserModel()
            {
                Username = "******",
                AuthCode = new string('b', 40)
            };

            testUser.Displayname = null;

            var response = httpServer.Post("api/users/register", testUser);
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);

            testUser.Displayname = "    ";

            response = httpServer.Post("api/users/register", testUser);
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerfromOperationAndHandleException(() =>
            {
                using (var dbContext = new AcadBlogContext())
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameAsLowercase = model.Username.ToLower();
                    var existingUser = dbContext.Users.FirstOrDefault(
                        u => u.Username == usernameAsLowercase &&
                        u.AuthCode == model.AuthCode);

                    if (existingUser == null)
                    {
                        throw new InvalidOperationException(
                            "Username or password is invalid.");
                    }

                    if (existingUser.SessionKey == null)
                    {
                        existingUser.SessionKey = this.GenerateSessionKey(existingUser.Id);
                        dbContext.SaveChanges();
                    }

                    var loggedModel = new LoggedUserModel()
                    {
                        Displayname = existingUser.Displayname,
                        SessionKey = existingUser.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.OK,
                        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;
 }
        public LoggedUserModel RegisterValidUserAndReturnModel()
        {
            var type = typeof(UsersController);
            var testUser = new UserModel()
            {
                Username = "******",
                Displayname = "VALIDNICK",
                AuthCode = new string('b', 40)
            };

            var model = this.RegisterTestUser(this.httpServer, testUser);
            return model;
        }
        public void Register_WhenUserModelValid_ShouldSaveToDatabase()
        {
            var type = typeof(UsersController);
            var testUser = new UserModel()
            {
                Username = "******",
                Displayname = "VALIDNICK",
                AuthCode = new string('b', 40)
            };

            var model = this.RegisterTestUser(this.httpServer, testUser);
            Assert.AreEqual(testUser.Displayname, model.Displayname);
            Assert.IsNotNull(model.SessionKey);
        }