Пример #1
0
        public void LeaveACommentValid()
        {
            //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 = "Ather text",
                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);

            var comment = new AddCommentDTO()
            {
                Text = "textttttttttttttt"
            };

            var commentResponse = httpServer.Post("api/posts/"+ post.Id +"/comment", comment, headers);
            string commentString = commentResponse.Content.ReadAsStringAsync().Result;

            Assert.AreEqual(HttpStatusCode.OK, commentResponse.StatusCode);
        }
Пример #2
0
        private Comments CreateComment(ExamContext context, AddCommentDTO commentJson, Users user)
        {
            Comments newComment = new Comments()
            {
                Text = commentJson.Text,
            };

            newComment.PostDate = DateTime.Now;
            newComment.CommentedBy = user;
            context.Comments.Add(newComment);
            context.SaveChanges();
            return newComment;
        }
Пример #3
0
        public HttpResponseMessage PutComment(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey,
            int postId, AddCommentDTO commentJson)
        {
            var responseMsg = base.PerformOperationAndHandleExceptions(() =>
            {
                var context = new ExamContext();

                Users user = FindUsernameForComment(sessionKey, context);

                var post = context.Posts.FirstOrDefault(x => x.Id == postId);
                if (post == null)
                {
                    throw new ArgumentNullException("Invalid post");
                }

                Comments newComment = CreateComment(context, commentJson, user);
                post.Comments.Add(newComment);
                context.SaveChanges();

                var response = this.Request.CreateResponse(HttpStatusCode.OK);
                return response;
            });

            return responseMsg;
        }