public void CreateCommentar_WithInvalideSessionKeyData_ShouldSaveToDatabase() { var testUser = new UserModel() { Username = "******", DisplayName = "VALIDNICK", AuthCode = new string('b', 40) }; var userModel = Helpers.RegisterTestValidUser(httpServer, testUser); var testPost = new PostModel() { Title = "NEW POST created", Tags = new List<string>() { "post", "web", "api", "root", }, Text = "this is just a test post" }; var postResponse = httpServer.Post("api/posts?sessionKey=32543tfg", testPost); var postContentString = postResponse.Content.ReadAsStringAsync().Result; var postModel = JsonConvert.DeserializeObject<PostModel>(postContentString); var testComment = new CommentModel() { Text = "Abe kefi me toq post" }; var commentResponse = httpServer.Put("api/posts/" + postModel.Id + "/comment?sessionKey=dsadasda", testComment); Assert.AreEqual(HttpStatusCode.BadRequest, commentResponse.StatusCode); }
//PUT api/posts/{postId}/comment public HttpResponseMessage PutComment(int postId, CommentModel comment, string sessionKey) { var context = new BlogContext(); var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () => { if (comment != null && string.IsNullOrEmpty(comment.Text)) { throw new InvalidOperationException("Comment text cannot be empty"); } var post = context.Posts.FirstOrDefault(p => p.Id == postId); if (post != null) { post.Comments.Add(new Comment() { Text = comment.Text, User = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey), PostDate = DateTime.Now }); context.SaveChanges(); } else { throw new InvalidOperationException("Post with id " + postId + " do not exist"); } var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }); return responseMsg; }
public void CreateCommentar_WithWrongPostIdData_ShouldSaveToDatabase() { var testUser = new UserModel() { Username = "******", DisplayName = "VALIDNICK", AuthCode = new string('b', 40) }; var userModel = Helpers.RegisterTestValidUser(httpServer, testUser); var testComment = new CommentModel() { Text = "Abe kefi me toq post" }; var commentResponse = httpServer.Put("api/posts/9999/comment?sessionKey=" + userModel.SessionKey, testComment); Assert.AreEqual(HttpStatusCode.BadRequest, commentResponse.StatusCode); }
public void LeaveComment_WhenWrongSessionKey_ShouldNotSaveToDatabase() { var testUser = new UserModel() { Username = "******", DisplayName = "VALIDNICK", AuthCode = new string('b', 40) }; var testNewPost = new PostModel() { Title = "VALIDTITLE", Text = "VALIDTEXT", Tags = new string[] { "FIRST", "SECOND" } }; LoggedUserModel userModel = RegisterUser.RegisterTestUser(httpServer, testUser); var postResponse = httpServer.Post("api/posts?sessionKey=" + userModel.SessionKey, testNewPost); var contentString = postResponse.Content.ReadAsStringAsync().Result; var postModel = JsonConvert.DeserializeObject<PostModel>(contentString); var newComment = new CommentModel() { Text = "VALIDCOMMENTTEXT" }; //Try to make a comment with wrong sessionKey string invalidSessionKey = "sdasdasdasddsaijdij2jiwjijei2jiedjisjidsa"; var response = httpServer.Put("api/posts/" + postModel.Id + "/comment?sessionKey=" + invalidSessionKey, newComment); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void LeaveComment_WhenPostNotExists_ShouldNotSaveToDatabase() { var testUser = new UserModel() { Username = "******", DisplayName = "VALIDNICK", AuthCode = new string('b', 40) }; var testNewPost = new PostModel() { Title = "VALIDTITLE", Text = "VALIDTEXT", Tags = new string[] { "FIRST", "SECOND" } }; LoggedUserModel userModel = RegisterUser.RegisterTestUser(httpServer, testUser); httpServer.Post("api/posts?sessionKey=" + userModel.SessionKey, testNewPost); var newComment = new CommentModel() { Text = "VALIDCOMMENTTEXT" }; int invalidPostId = 0; var response = httpServer.Put("api/posts/" + invalidPostId + "/comment?sessionKey=" + userModel.SessionKey, newComment); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void LeaveComment_WhenCommentModelIsValid_ShouldSaveToDatabase() { var testUser = new UserModel() { Username = "******", DisplayName = "VALIDNICK", AuthCode = new string('b', 40) }; var testNewPost = new PostModel() { Title = "VALIDTITLE", Text = "VALIDTEXT", Tags = new string[] { "FIRST", "SECOND" } }; LoggedUserModel userModel = RegisterUser.RegisterTestUser(httpServer, testUser); var postResponse = httpServer.Post("api/posts?sessionKey=" + userModel.SessionKey, testNewPost); var contentString = postResponse.Content.ReadAsStringAsync().Result; var postModel = JsonConvert.DeserializeObject<PostModel>(contentString); var newComment = new CommentModel() { Text = "VALIDCOMMENTTEXT" }; var response = httpServer.Put("api/posts/" + postModel.Id + "/comment?sessionKey=" + userModel.SessionKey, newComment); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
//PUT api/posts/{postId}/comment public HttpResponseMessage PutComment(CommentModel comment, int postId, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var context = new BloggingSystemContext(); var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey( sessionKey, context, () => { var post = context.Posts.FirstOrDefault(p => p.Id == postId); if (post != null) { post.Comments.Add(new Comment() { Text = comment.Text, PostDate = DateTime.Now, }); } context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created); return response; }); return responseMsg; }