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); }
public HttpResponseMessage PostAddPost( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey, CreatePostDTO value) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { var context = new ExamContext(); IsUserLoged(sessionKey, context); Posts newPost = CreatePost(value, context); CreatePostResponse responsJson = new CreatePostResponse() { Id = newPost.Id, Title = newPost.Title }; var response = this.Request.CreateResponse(HttpStatusCode.Created, responsJson); return response; }); return responseMsg; }
public void InvalidPost_WithouthSessionKeyNull() { var headers = new Dictionary<string, string>(); headers["X-sessionKey"] = null; 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); Assert.AreEqual(HttpStatusCode.BadRequest, postResponse.StatusCode); }
private static Posts CreatePost(CreatePostDTO value, ExamContext context) { Posts newPost = new Posts() { Title = value.Title, Text = value.Text, }; newPost.PostDate = DateTime.Now; ICollection<Tags> allTags = CreateOrLoadTags(value, context); newPost.Tags = allTags; context.Posts.Add(newPost); context.SaveChanges(); return newPost; }
private static ICollection<Tags> CreateOrLoadTags(CreatePostDTO value, ExamContext context) { ICollection<Tags> allTags = new List<Tags>(); foreach (var tag in value.Tags) { var existingTag = context.Tags.FirstOrDefault(t => t.Name == tag); if (existingTag == null) { context.Tags.Add(new Tags() { Name = tag }); context.SaveChanges(); } allTags.Add(existingTag); } return allTags; }