private int CreateNews(LoginData loginData) { var news = new AddNewsBindingModel { Title = "News title", Content = "some news content", PublishDate = DateTime.Now }; var newsContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", news.Title), new KeyValuePair<string, string>("content", news.Content), new KeyValuePair<string, string>("publishDate", news.PublishDate.ToString()) }); // Act this.httpClient.DefaultRequestHeaders.Add( "Authorization", "Bearer " + loginData.Access_Token); var httpResponse = this.httpClient.PostAsync("api/news", newsContent).Result; this.httpClient.DefaultRequestHeaders.Remove("Authorization"); if (!httpResponse.IsSuccessStatusCode) { // Nothing to return, throw a proper exception + message throw new HttpRequestException( httpResponse.Content.ReadAsStringAsync().Result); } var dbNews = httpResponse.Content.ReadAsAsync<News>().Result; return dbNews.Id; }
public void ModifyExistingNews_ValidData_DuplicateTitle_ValidAccessToken_ShouldReturn_409Conflict() { // Arrange this.Register(); var loginData = this.Login(); this.CreateNews(loginData); var secondNews = new AddNewsBindingModel { Title = "Second news title", Content = "second news content", PublishDate = DateTime.Now }; var secondNewsContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", secondNews.Title), new KeyValuePair<string, string>("content", secondNews.Content), new KeyValuePair<string, string>("publishDate", secondNews.PublishDate.ToString()) }); // Act this.httpClient.DefaultRequestHeaders.Add( "Authorization", "Bearer " + loginData.Access_Token); var secondNewsHttpResponce = this.httpClient.PostAsync("api/news", secondNewsContent).Result; Assert.AreEqual(HttpStatusCode.Created, secondNewsHttpResponce.StatusCode); var newsId = secondNewsHttpResponce.Content.ReadAsAsync<NewsViewModel>().Result.Id; var editedNews = new EditNewsBindingModel { Title = "News title", Content = "Edited content", PublishDate = DateTime.Now }; var editedNewsContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", editedNews.Title), new KeyValuePair<string, string>("content", editedNews.Content), new KeyValuePair<string, string>("publishDate", editedNews.PublishDate.ToString()) }); // Act var httpResponse = this.httpClient.PutAsync("api/news/" + newsId, editedNewsContent).Result; // Assert Assert.AreEqual(HttpStatusCode.Conflict, httpResponse.StatusCode); }
public void CreateNews_CorrectData_DuplicateTitle_ValidAccessToken_ShouldReturn_409Conflict() { this.Register(); var loginData = this.Login(); this.CreateNews(loginData); // Arrange var news = new AddNewsBindingModel { Title = "News title", Content = "some news content", PublishDate = DateTime.Now }; var newsContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", news.Title), new KeyValuePair<string, string>("content", news.Content), new KeyValuePair<string, string>("publishDate", news.PublishDate.ToString()) }); // Act this.httpClient.DefaultRequestHeaders.Add( "Authorization", "Bearer " + loginData.Access_Token); var httpResponse = this.httpClient.PostAsync("api/news", newsContent).Result; // Assert Assert.AreEqual(HttpStatusCode.Conflict, httpResponse.StatusCode); }
public void CreateNews_InvalidData_ValidAccessToken_ShouldReturn_400BadRequest() { // Arrange this.Register(); var loginData = this.Login(); var news = new AddNewsBindingModel { Title = "News title", Content = "some news content", PublishDate = DateTime.Now }; // Missing Content var newsContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", news.Title), new KeyValuePair<string, string>("publishDate", news.PublishDate.ToString()) }); // Act this.httpClient.DefaultRequestHeaders.Add( "Authorization", "Bearer " + loginData.Access_Token); var httpResponse = this.httpClient.PostAsync("api/news", newsContent).Result; // Assert Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode); Assert.AreEqual(httpResponse.Content.Headers.ContentType.MediaType, "application/json"); }
public void CreateNews_CorrectData_NoAccessToken_ShouldReturn_401Unauthorized() { // Arrange var news = new AddNewsBindingModel { Title = "News title", Content = "some news content", PublishDate = DateTime.Now }; var newsContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", news.Title), new KeyValuePair<string, string>("content", news.Content), new KeyValuePair<string, string>("publishDate", news.PublishDate.ToString()) }); // Act var httpResponse = this.httpClient.PostAsync("api/news", newsContent).Result; // Assert Assert.AreEqual(HttpStatusCode.Unauthorized, httpResponse.StatusCode); }