public void Login_WhenLoggedModelValid_RetrunData()
        {
            var testUser = new UserModel()
            {
                UserName = "******",
                NickName = "ValidNick",
                AuthCode = new string('b', 40)
            };

            var testUserLogin = new UserModel()
            {
                UserName = "******",
                AuthCode = new string('b', 40)
            };

            var httpServer = new InMemoryHttpServer("http://localhost/");
            httpServer.CreatePostRequest("api/users/register", testUser);
            var response = httpServer.CreatePostRequest("api/users/login", testUserLogin);

            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            Assert.IsNotNull(response.Content);

            var contentString = response.Content.ReadAsStringAsync().Result;
            var model = JsonConvert.DeserializeObject<UserLoggedModel>(contentString);
            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 == usr.NickName);

                        if (user != null)
                        {
                            throw new InvalidOperationException("User 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 UserLoggedModel()
                        {
                            Nickname = user.NickName,
                            SessionKey = user.SessionKey
                        };

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

                        return response;
                    }
                });

            return responseMsg;
            }