Esempio n. 1
0
        public void InvalidPost_TextNull()
        {
            //create new user
            var testUser = new UserRegisterDTO()
            {
                Username = "******",
                DisplayName = "TestDisplayName",
                AuthCode = new string('b', 40),
            };
            //make the reques with httpServer
            var response = httpServer.Post("api/users/register", testUser);

            string content = response.Content.ReadAsStringAsync().Result;
            UserRegisterResponseDTO answer = JsonConvert.DeserializeObject<UserRegisterResponseDTO>(content);

            string sessionKey = answer.Sessionkey;

            var headers = new Dictionary<string, string>();
            headers["X-sessionKey"] = sessionKey;

            CreatePostDTO newPost = new CreatePostDTO()
            {
                Title = "some text",
                Text = null,
                Tags = new string[] { "tag1", "tag2" },
            };

            var postResponse = httpServer.Post("api/posts", newPost, headers);
            string contentResult = postResponse.Content.ReadAsStringAsync().Result;
            CreatePostResponse post = JsonConvert.DeserializeObject<CreatePostResponse>(contentResult);

            Assert.AreEqual(HttpStatusCode.BadRequest, postResponse.StatusCode);
        }
Esempio n. 2
0
        public void RegisterInvalidUser_displayNameNull()
        {
            //create new user
            var testUser = new UserRegisterDTO()
            {
                Username = "******",
                DisplayName = null,
                AuthCode = new string('b', 40),
            };
            //make the reques with httpServer
            var response = httpServer.Post("api/users/register", testUser);

            Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest);
        }
Esempio n. 3
0
        public HttpResponseMessage PostRegisterUser(UserRegisterDTO userJSON)
        {
            var responseMsg = base.PerformOperationAndHandleExceptions(
             () =>
             {
                 if (ModelState.IsValid && userJSON != null)
                 {
                     var context = new ExamContext();

                     using (context)
                     {
                         this.ValidateUsername(userJSON.Username);
                         this.ValidateDisplayName(userJSON.DisplayName);
                         this.ValidAuthCode(userJSON.AuthCode);

                         IsUserExisting(userJSON, context);

                         Users createdUser = CreateNewUSer(userJSON, context);

                         string sessionKey = this.GenerateSessionKey(createdUser.Id);

                         createdUser.SessionKey = sessionKey;
                         context.Entry(createdUser).State = System.Data.EntityState.Modified;
                         context.SaveChanges();

                         UserRegisterResponseDTO user = new UserRegisterResponseDTO()
                         {
                             DisplaName = userJSON.DisplayName,
                             Sessionkey = sessionKey,
                         };

                         var response = this.Request.CreateResponse(HttpStatusCode.Created, user);
                         return response;
                     }
                 }
                 else
                 {
                     var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
                     var errorMessage = string.Format("User input validation failed. Errors: {0}", errors);
                     throw new ArgumentException(errorMessage);
                 }
             });

            return responseMsg;
        }
Esempio n. 4
0
        public void TestMethod2()
        {
            //create new user
            var testUser = new UserRegisterDTO()
            {
                Username = "******",
                DisplayName = "VALIDNICK",
                AuthCode = new string('b', 40)
            };
            //make the reques with httpServer
            httpServer.Post("api/users/register/", testUser);
            //username

            var testLogin = new LoginDTO()
            {
                Username = "******",
                AuthCode = new string('b', 40)
            };

            var response = httpServer.Post("api/users/login/", testLogin);

            string content = response.Content.ReadAsStringAsync().Result;
            LoginResponseDTO answer = JsonConvert.DeserializeObject<LoginResponseDTO>(content);

            string sessionKey = answer.Sessionkey;
            var headers = new Dictionary<string, string>();
            headers["X-sessionKey"] = sessionKey;

            //error from .Put request
            var threadsResponse = httpServer.Post("api/users/logout", headers);

            Assert.IsNotNull(answer.Sessionkey);
            Assert.IsTrue(HttpStatusCode.OK == threadsResponse.StatusCode);
        }
Esempio n. 5
0
        public void RegisterValidUser()
        {
            //create new user
            var testUser = new UserRegisterDTO()
            {
                Username = "******",
                DisplayName = "TestDisplayName",
                AuthCode = new string('b', 40),
            };
            //make the reques with httpServer
            var response = httpServer.Post("api/users/register", testUser);

            string content = response.Content.ReadAsStringAsync().Result;
            UserRegisterResponseDTO answer = JsonConvert.DeserializeObject<UserRegisterResponseDTO>(content);

            Assert.AreEqual(testUser.DisplayName, answer.DisplaName);
            Assert.IsTrue(response.StatusCode == HttpStatusCode.Created);
        }
Esempio n. 6
0
        public void RegisterInvalidUser_usernameInvalidSmaller()
        {
            //create new user
            var testUser = new UserRegisterDTO()
            {
                Username = "******",
                DisplayName = "TestDisplayName",
                AuthCode = new string('b', 40),
            };
            //make the reques with httpServer
            var response = httpServer.Post("api/users/register", testUser);

            Assert.IsTrue(response.IsSuccessStatusCode == false);
        }
Esempio n. 7
0
        private static void IsUserExisting(UserRegisterDTO userJSON, ExamContext context)
        {
            //if there is already such user
            Users foundUser = context.Users.FirstOrDefault(x =>
                x.Username == userJSON.Username ||
                x.DisplayName == userJSON.DisplayName);

            if (foundUser != null)
            {
                throw new ArgumentException("User already exists");
            }
        }
Esempio n. 8
0
        private static Users CreateNewUSer(UserRegisterDTO userJSON, ExamContext context)
        {
            // when the user is created
            Users createdUser = new Users()
            {
                DisplayName = userJSON.DisplayName,
                Username = userJSON.Username,
                AuthCode = userJSON.AuthCode,
            };
            //we can make sessionkey

            context.Users.Add(createdUser);
            context.SaveChanges();
            return createdUser;
        }