public void PostCreate_WhenPostModelIsValidButMissingTags_ShouldSaveToDB() { var userModel = RegisterValidUserAndReturnModel(); var headers = new Dictionary<string, string>(); headers["X-sessionKey"] = userModel.SessionKey; var postModel = new PostCreateRequestModel { Title = "TestPost1", Text = "This is just a test post. It's labeled 'TestPostOne'" }; var response = httpServer.Post("api/posts", postModel, headers); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); }
public void PostCreate_WhenTitleOrTextIsNullOrEmpty_StatusCode400() { var userModel = RegisterValidUserAndReturnModel(); var headers = new Dictionary<string, string>(); headers["X-sessionKey"] = userModel.SessionKey; var postModel = new PostCreateRequestModel { Tags = new List<string> { "tag1", "tag2", "tag3" }, Text = "This is just a test post. It's labeled 'TestPostOne'" }; // title postModel.Title = null; var response = httpServer.Post("api/posts", postModel, headers); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); postModel.Title = " "; response = httpServer.Post("api/posts", postModel, headers); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); postModel.Title = "ValidTitle"; // text postModel.Text = null; response = httpServer.Post("api/posts", postModel, headers); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); postModel.Text = " "; response = httpServer.Post("api/posts", postModel, headers); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void PostsFind_WhenSearchingByMultipleTags_ReturnsThePostsThatMatchAllTags() { // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!! for some reason I am unable to deserialize the json correctly... // that is why this test goes RED -> FindPostsByTags var userModel = RegisterValidUserAndReturnModel(); var headers = new Dictionary<string, string>(); headers["X-sessionKey"] = userModel.SessionKey; // create a few valid posts var postModel1 = new PostCreateRequestModel { Tags = new List<string> { "tag1", "tag2", "tag3" }, Title = "TestPost1", Text = "This is just a test post. It's labeled 'TestPostOne'" }; var postModel2 = new PostCreateRequestModel { Tags = new List<string> { "tag2", "tag3" }, Title = "TestPost2", Text = "This is just a test post. It's labeled 'TestPostTwo'" }; var postModel3 = new PostCreateRequestModel { Tags = new List<string> { "tag1", "tag3" }, Title = "TestPost3", Text = "This is just a test post. It's labeled 'TestPostThree'" }; // post all the posts from above httpServer.Post("api/posts", postModel1, headers); httpServer.Post("api/posts", postModel2, headers); httpServer.Post("api/posts", postModel3, headers); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!! for some reason I am unable to deserialize the json correctly... // that is why this test goes RED // find the posts matching by 'tag1' and 'tag2' var postModel = FindPostsByTags("tag1, TAg2", headers); Assert.AreEqual(1, postModel.Posts.Count); // find the posts matching by 'tag2' and 'tag3' postModel = FindPostsByTags("taG2, tag3", headers); Assert.AreEqual(1, postModel.Posts.Count); // find the posts matching by 'tag1' and 'tag3' postModel = FindPostsByTags("tag1, tag3", headers); Assert.AreEqual(1, postModel.Posts.Count); // find the posts matching by only by 'tag1' postModel = FindPostsByTags("tag1", headers); Assert.AreEqual(2, postModel.Posts.Count); // find the posts matching by only by 'tag2' postModel = FindPostsByTags("tag2", headers); Assert.AreEqual(2, postModel.Posts.Count); // find the posts matching by only by 'tag3' postModel = FindPostsByTags("tag3", headers); Assert.AreEqual(2, postModel.Posts.Count); }
public void PostLeaveComment_WhenCommentIsValidAndPostExists_ShouldSaveToDB() { var userModel = RegisterValidUserAndReturnModel(); var headers = new Dictionary<string, string>(); headers["X-sessionKey"] = userModel.SessionKey; var postModel = new PostCreateRequestModel { Tags = new List<string> { "tag1", "tag2", "tag3" }, Title = "TestPost1", Text = "This is just a test post. It's labeled 'TestPostOne'" }; // post a post var response = httpServer.Post("api/posts/", postModel, headers); var commentModel = new CommentPostModel { Text = "This is just a test post. It's labeled 'TestPostOne'" }; var contentString = response.Content.ReadAsStringAsync().Result; var postModelAfterSavingToDB = JsonConvert.DeserializeObject<PostModel>(contentString); int postId = postModelAfterSavingToDB.Id; // post a comment response = httpServer.Post("api/posts/" + postId + "/comment", commentModel, headers); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); }
private void ValidatePostCreateModel(PostCreateRequestModel model) { if (model == null) { throw new ArgumentNullException("Post create model is null."); } if (String.IsNullOrEmpty(model.Text)) { throw new ArgumentNullException("Post content is null or empty."); } if (String.IsNullOrEmpty(model.Title)) { throw new ArgumentNullException("Post title is null or empty."); } }
public HttpResponseMessage PostCreatePost(PostCreateRequestModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerfromOperationAndHandleException(() => { this.ValidatePostCreateModel(model); using (var dbContext = new AcadBlogContext()) { var user = this.ValidateAndGetUserBySessionKey(dbContext, sessionKey); Post newPost = new Post { Title = model.Title, Content = model.Text, Tags = this.FindOrCreateTagsByTitle(dbContext, model.Tags), PostDate = DateTime.Now, User = user, }; dbContext.Posts.Add(newPost); dbContext.SaveChanges(); var responseModel = new PostCreateResponseModel() { Id = newPost.Id, Title = model.Title, }; var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel); return response; } }); return responseMsg; }