Ejemplo n.º 1
0
        private async Task <TodoTaskDTO> CreateToDoTaskTest(int listId
                                                            , string title, string description)
        {
            // Arrange
            CreateTodoTaskDTO createToDoTaskDTO1 = new CreateTodoTaskDTO
            {
                ToDoListId  = listId,
                Title       = title,
                Description = description
            };

            var content       = JsonConvert.SerializeObject(createToDoTaskDTO1);
            var stringContent = new StringContent(content, Encoding.UTF8, "application/json");

            // Act
            var response = await _client.PostAsync("/api/TodoTasks/", stringContent);

            // Assert
            response.EnsureSuccessStatusCode();
            var responseString = await response.Content.ReadAsStringAsync();

            TodoTaskDTO todoTaskDTO = JsonConvert.DeserializeObject <TodoTaskDTO>(responseString);

            Assert.NotNull(todoTaskDTO);

            return(todoTaskDTO);
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <TodoTaskDTO> > GetTodoTasksAsync()
        {
            var currentUser = await GetCurrentUserAsync();

            var tasks = _context.TodoTasks.Where(x => x.OwnerId == currentUser.Id);

            await EnsureAuthorizedAccessAsync(tasks);

            return(TodoTaskDTO.DbSetToDtoList(tasks));
        }
Ejemplo n.º 3
0
        public async Task FullAPI_Test()
        {
            // Sign Up Test
            User user = await SignUpPostTest();

            // Sign In Test
            UserDTO userDTO = await SignInPostTest();

            var token = userDTO.Token;

            // Set authorization header for authorization
            _client.DefaultRequestHeaders.Authorization =
                new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            // Create Todo List Test
            var todoList = await CreateToDoListTest("New List 3");

            var todoList2 = await CreateToDoListTest("New List 4");

            // Create Todo Task Test
            var todoTask1 = await CreateToDoTaskTest(todoList.Id, "Title1", "Description1");

            var todoTask2 = await CreateToDoTaskTest(todoList.Id, "Title2", "Description2");

            var todoTask3 = await CreateToDoTaskTest(todoList.Id, "Title3", "Description3");

            // Set Todo Task status Test
            await SetTodoTaskStatusTest(todoTask1.Id, TodoTask.Status.DONE);
            await SetTodoTaskStatusTest(todoTask2.Id, TodoTask.Status.CANCELED);
            await SetTodoTaskStatusTest(todoTask2.Id, TodoTask.Status.AWAIT);

            // Update Todo Task Test
            TodoTaskDTO updatedToDoTask =
                await UpdateTodoTaskTest(todoTask1, todoList2);

            // Get Todo Lists For User Test
            await GetListsForUserTest(2);

            // Delete Todo List Test
            await DeleteTodoListTest(todoList2.Id);

            // Get Todo Lists For User Test
            await GetListsForUserTest(1);

            // Set List Title Test
            await SetListTitleTest(todoList.Id, "Updated Title");

            // Delete User Tess
            await DeleteUserTest();
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PostTodoTask([FromBody] TodoTaskDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var task        = TodoTaskDTO.UpdateDbObjectWithDTO(new TodoTask(), dto);
            var currentUser = await GetCurrentUserAsync();

            task.OwnerId = currentUser.Id;

            _context.TodoTasks.Add(task);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetTodoTask", new { id = task.Id }, task));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> GetTodoTask([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUser = await GetCurrentUserAsync();

            var todoTask = await _context.TodoTasks.SingleOrDefaultAsync(m => m.Id == id);

            if (todoTask == null)
            {
                return(NotFound());
            }

            await EnsureAuthorizedAccessAsync(todoTask);

            var result = TodoTaskDTO.DbObjectToDto(todoTask);

            return(Ok(result));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> PutTodoTask([FromRoute] int id, [FromBody] TodoTaskDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != dto.Id)
            {
                return(BadRequest());
            }

            var task = await _context.TodoTasks.SingleOrDefaultAsync(x => x.Id == dto.Id);

            await EnsureAuthorizedAccessAsync(task);

            task = TodoTaskDTO.UpdateDbObjectWithDTO(task, dto);

            _context.Entry(task).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TodoTaskExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 7
0
        private async Task <TodoTaskDTO> UpdateTodoTaskTest(TodoTaskDTO todoTaskDTO
                                                            , TodoList todoList)
        {
            // Arrange
            UpdateTodoTaskDTO updateToDoTaskDTO = new UpdateTodoTaskDTO
            {
                TaskId      = todoTaskDTO.Id,
                ToDoListId  = todoTaskDTO.ToDoListId,
                Title       = todoTaskDTO.Title,
                Description = todoTaskDTO.Description
            };

            updateToDoTaskDTO.Description = "New Description";
            updateToDoTaskDTO.ToDoListId  = todoList.Id;
            updateToDoTaskDTO.Title       = "New Title";

            var content       = JsonConvert.SerializeObject(updateToDoTaskDTO);
            var stringContent = new StringContent(content, Encoding.UTF8, "application/json");

            // Act
            var response = await _client.PutAsync("/api/TodoTasks/", stringContent);

            // Assert
            response.EnsureSuccessStatusCode();
            var responseString = await response.Content.ReadAsStringAsync();

            TodoTaskDTO updatedTodoTaskDTO
                = JsonConvert.DeserializeObject <TodoTaskDTO>(responseString);

            Assert.NotNull(updatedTodoTaskDTO);
            Assert.Equal(updateToDoTaskDTO.TaskId, updatedTodoTaskDTO.Id);
            Assert.Equal(updateToDoTaskDTO.Title, updatedTodoTaskDTO.Title);
            Assert.Equal(updateToDoTaskDTO.Description, updatedTodoTaskDTO.Description);
            Assert.Equal(updateToDoTaskDTO.ToDoListId, updatedTodoTaskDTO.ToDoListId);

            return(updatedTodoTaskDTO);
        }
Ejemplo n.º 8
0
        private async Task <TodoTaskDTO> SetTodoTaskStatusTest(int todoTaskId
                                                               , TodoTask.Status taskStatus)
        {
            // Arrange
            string url = "/api/TodoTasks/" + todoTaskId + "/setstatus/" + taskStatus;

            // Act
            var method   = new HttpMethod("PATCH");
            var request  = new HttpRequestMessage(method, url);
            var response = await _client.SendAsync(request);

            // Assert
            response.EnsureSuccessStatusCode();
            var responseString = await response.Content.ReadAsStringAsync();

            TodoTaskDTO updatedTodoTaskDTO
                = JsonConvert.DeserializeObject <TodoTaskDTO>(responseString);

            Assert.NotNull(updatedTodoTaskDTO);
            Assert.Equal(updatedTodoTaskDTO.TaskStatus, taskStatus);
            Assert.Equal(updatedTodoTaskDTO.Id, todoTaskId);

            return(updatedTodoTaskDTO);
        }