public ActionResult <TodoItemResponseDTO> PutTodoItem(int id, TodoItemUpsertDTO todoItem) { try { if (todoItem == null) { return(BadRequest()); } var existingItem = _todoRepository.GetSingle(id); if (existingItem == null) { return(NotFound()); } TodoEntity item = _mapper.Map <TodoEntity>(todoItem); //todo look at me _todoRepository.Update(id, item); if (!_todoRepository.Save()) { throw new Exception("Updating a fooditem failed on save."); } TodoItemResponseDTO responseItem = _mapper.Map <TodoItemResponseDTO>(item); return(responseItem); } catch (Exception ex) { _logger.LogError(String.Concat("An unhanded error occured when updating items: ", ex.Message)); return(StatusCode(500)); } }
public async Task Update_TodoItemNotAuthorized_Returns401() { TodoItemUpsertDTO updateItem = new TodoItemUpsertDTO(); updateItem.Name = "update item fail test"; updateItem.IsComplete = false; var json = JsonConvert.SerializeObject(updateItem); var data = new StringContent(json, Encoding.UTF8, "application/json"); var response = await _notAuthorizedClient.PutAsync("/api/TodoItems/2", data); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }
public ActionResult <TodoItemResponseDTO> PostTodoItem(TodoItemUpsertDTO todoItem) { try { TodoEntity item = _mapper.Map <TodoEntity>(todoItem); item.Id = _todoRepository.Count() + 1; _todoRepository.Add(item); _todoRepository.Save(); TodoItemResponseDTO responseItem = _mapper.Map <TodoItemResponseDTO>(item); return(CreatedAtAction(nameof(GetTodoItem), new { id = item.Id }, responseItem)); } catch (Exception ex) { _logger.LogError(String.Concat("An unhandled error occured when adding items: ", ex.Message)); return(StatusCode(500)); } }
public async Task Update_TodoItemById_Returns200() { TodoItemUpsertDTO updateItem = new TodoItemUpsertDTO(); updateItem.Name = "update item test"; updateItem.IsComplete = false; var json = JsonConvert.SerializeObject(updateItem); var data = new StringContent(json, Encoding.UTF8, "application/json"); var response = await _client.PutAsync("/api/TodoItems/1", data); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var jsonString = response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <TodoItemResponseDTO>(jsonString.Result); Assert.Equal("update item test", model.Name); }
public async Task Add_TodoItem_Returns201() { TodoItemUpsertDTO newItem = new TodoItemUpsertDTO(); newItem.Name = "new item test"; newItem.IsComplete = false; var json = JsonConvert.SerializeObject(newItem); var data = new StringContent(json, Encoding.UTF8, "application/json"); var response = await _client.PostAsync("/api/TodoItems", data); var jsonString = response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <TodoItemResponseDTO>(jsonString.Result); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("new item test", model.Name); Assert.False(model.IsComplete); }
public async Task Delete_TodoItemById_Returns204() { //we want to add before we delete to not have any side effects TodoItemUpsertDTO newItem = new TodoItemUpsertDTO(); newItem.Name = "delete me"; newItem.IsComplete = false; var json = JsonConvert.SerializeObject(newItem); var data = new StringContent(json, Encoding.UTF8, "application/json"); var postResponse = await _client.PostAsync("/api/TodoItems", data); Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode); var jsonString = postResponse.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <TodoItemResponseDTO>(jsonString.Result); var deleteResponse = await _client.DeleteAsync(String.Concat("/api/TodoItems/", model.Id.ToString())); Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode); }
public TodoControllerIntegrationTests() { var server = new TestServer(new WebHostBuilder().UseStartup <TemplateAPI.Startup>()); _client = server.CreateClient(); _notAuthorizedClient = server.CreateClient(); var token = GetBearerToken(); _client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.Result); // Create an entry as setup //todo add setup method? Is that a thing with xunit? TodoItemUpsertDTO item1 = new TodoItemUpsertDTO(); item1.Name = "test"; item1.IsComplete = true; var json = JsonConvert.SerializeObject(item1); var data = new StringContent(json, Encoding.UTF8, "application/json"); _client.PostAsync("/api/TodoItems", data); }