public async Task DeleteAsync_UsingNewlyCreatedTodoItem_MustSucceed()
        {
            // Arrange
            using HttpClient httpClient =
                      await testWebApplicationFactory.CreateClientWithJwtToken().ConfigureAwait(false);

            string name       = $"it--{nameof(DeleteAsync_UsingNewlyCreatedTodoItem_MustSucceed)}--{Guid.NewGuid():N}";
            bool   isComplete = DateTime.UtcNow.Ticks % 2 == 0;

            var newTodoItemInfo = new NewTodoItemModel
            {
                Name       = name,
                IsComplete = isComplete
            };

            HttpResponseMessage response =
                await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemInfo).ConfigureAwait(false);

            response.IsSuccessStatusCode.Should().BeTrue("a new entity has been created");
            long?id = await response.Content.ReadAsAsync <long>().ConfigureAwait(false);

            response = await httpClient.DeleteAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

            response.IsSuccessStatusCode.Should().BeTrue("existing entity must be deleted");
        }
Beispiel #2
0
        public async Task DeleteAsync_UsingNewlyCreatedTodoItem_MustSucceed()
        {
            // Arrange
            string name       = $"it--{nameof(DeleteAsync_UsingNewlyCreatedTodoItem_MustSucceed)}--{Guid.NewGuid():N}";
            bool   isComplete = DateTime.UtcNow.Ticks % 2 == 0;

            var newTodoItemInfo = new NewTodoItemModel
            {
                Name       = name,
                IsComplete = isComplete
            };

            using HttpClient httpClient = await webApplicationFactory.CreateClientWithJwtAsync();

            HttpResponseMessage response = await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemInfo);

            response.IsSuccessStatusCode.Should().BeTrue(BecauseNewEntityHasBeenCreated);
            long?id = await response.Content.ReadAsAsync <long>();

            // Act
            response = await httpClient.DeleteAsync($"{BaseUrl}/{id}");

            // Assert
            using (new AssertionScope())
            {
                response.IsSuccessStatusCode.Should().BeTrue("existing entity must be deleted");
                response = await httpClient.GetAsync($"{BaseUrl}/{id}");

                response.StatusCode.Should().Be(HttpStatusCode.NotFound, "existing entity has already been deleted");
            }
        }
Beispiel #3
0
        public async Task <IActionResult> CreateAsync(NewTodoItemModel newTodoItemModel)
        {
            var newTodoItemInfo = new NewTodoItemInfo
            {
                IsComplete = newTodoItemModel.IsComplete,
                Name       = newTodoItemModel.Name
            };

            long newlyCreatedEntityId = await addTodoItemFlow.ExecuteAsync(newTodoItemInfo, User);

            return(Created($"api/todo/{newlyCreatedEntityId}", newlyCreatedEntityId));
        }
        public async Task GetByIdAsync_UsingNewlyCreatedItem_ReturnsExpectedResult()
        {
            // Arrange
            using HttpClient httpClient =
                      await testWebApplicationFactory.CreateClientWithJwtToken().ConfigureAwait(false);

            long?id = null;

            try
            {
                string nameSuffix = Guid.NewGuid().ToString("N");
                string name       = $"it--{nameof(GetByIdAsync_UsingNewlyCreatedItem_ReturnsExpectedResult)}--{nameSuffix}";

                var newTodoItemModel = new NewTodoItemModel
                {
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0,
                    Name       = name
                };

                HttpResponseMessage response =
                    await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel).ConfigureAwait(false);

                response.IsSuccessStatusCode.Should().BeTrue();
                response.StatusCode.Should().Be(HttpStatusCode.Created);
                id = await response.Content.ReadAsAsync <long>().ConfigureAwait(false);

                // Act
                response = await httpClient.GetAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

                // Assert
                response.IsSuccessStatusCode.Should().BeTrue();
                response.StatusCode.Should().Be(HttpStatusCode.OK);

                TodoItemModel todoItemModel =
                    await response.Content.ReadAsAsync <TodoItemModel>().ConfigureAwait(false);

                todoItemModel.Should().NotBeNull();
                todoItemModel.Id.Should().Be(id);
                todoItemModel.Name.Should().Be(newTodoItemModel.Name);
                todoItemModel.IsComplete.Should().Be(newTodoItemModel.IsComplete.Value);
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response =
                        await httpClient.DeleteAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

                    response.IsSuccessStatusCode.Should().BeTrue("cleanup must succeed");
                }
            }
        }
        public async Task <IActionResult> CreateAsync(NewTodoItemModel newTodoItemModel)
        {
            var newTodoItemInfo = new NewTodoItemInfo
            {
                IsComplete = newTodoItemModel.IsComplete,
                Name       = newTodoItemModel.Name,
                User       = User
            };

            long newlyCreatedEntityId = await todoService.AddAsync(newTodoItemInfo).ConfigureAwait(false);

            return(Created($"api/todo/{newlyCreatedEntityId}", newlyCreatedEntityId));
        }
Beispiel #6
0
        public async Task GetByIdAsync_UsingNonExistingId_ReturnsNotFoundHttpStatusCode()
        {
            // Arrange
            using HttpClient httpClient = await webApplicationFactory.CreateClientWithJwtAsync();

            long?id = null;

            try
            {
                string nameSuffix = Guid.NewGuid().ToString("N");
                string name       =
                    $"it--{nameof(GetByIdAsync_UsingNonExistingId_ReturnsNotFoundHttpStatusCode)}--{nameSuffix}";

                var newTodoItemModel = new NewTodoItemModel
                {
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0,
                    Name       = name
                };

                HttpResponseMessage response = await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel);

                response.IsSuccessStatusCode.Should().BeTrue();
                response.StatusCode.Should().Be(HttpStatusCode.Created);
                id = await response.Content.ReadAsAsync <long>();

                long nonExistentId = long.MinValue;

                // Act
                response = await httpClient.GetAsync($"{BaseUrl}/{nonExistentId}");

                // Assert
                using (new AssertionScope())
                {
                    response.IsSuccessStatusCode.Should().BeFalse(BecauseMustNotFindSomethingWhichDoesNotExist);
                    response.StatusCode.Should()
                    .Be(HttpStatusCode.NotFound, BecauseMustNotFindSomethingWhichDoesNotExist);
                }
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response = await httpClient.DeleteAsync($"{BaseUrl}/{id}");

                    response.IsSuccessStatusCode.Should().BeTrue(BecauseCleanupMustSucceed);
                }
            }
        }
Beispiel #7
0
        public async Task CreateAsync_UsingNoJsonWebToken_ReturnsUnauthorizedHttpStatusCode()
        {
            // Arrange
            using HttpClient httpClient = webApplicationFactory.CreateClient();
            var newTodoItemModel = new NewTodoItemModel();

            // Act
            HttpResponseMessage response = await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel);

            // Assert
            using (new AssertionScope())
            {
                response.IsSuccessStatusCode.Should().BeFalse(BecauseCurrentRequestHasNoJwt);
                response.StatusCode.Should().Be(HttpStatusCode.Unauthorized, BecauseCurrentRequestHasNoJwt);
            }
        }
        public async Task GetByIdAsync_UsingNonExistingId_ReturnsNotFoundStatusCode()
        {
            // Arrange
            using HttpClient httpClient =
                      await testWebApplicationFactory.CreateClientWithJwtToken().ConfigureAwait(false);

            long?id = null;

            try
            {
                string nameSuffix = Guid.NewGuid().ToString("N");
                string name       = $"it--{nameof(GetByIdAsync_UsingNonExistingId_ReturnsNotFoundStatusCode)}--{nameSuffix}";

                var newTodoItemModel = new NewTodoItemModel
                {
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0,
                    Name       = name
                };

                HttpResponseMessage response =
                    await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel).ConfigureAwait(false);

                response.IsSuccessStatusCode.Should().BeTrue();
                response.StatusCode.Should().Be(HttpStatusCode.Created);
                id = await response.Content.ReadAsAsync <long>().ConfigureAwait(false);

                long nonExistentId = long.MinValue;

                // Act
                response = await httpClient.GetAsync($"{BaseUrl}/{nonExistentId}").ConfigureAwait(false);

                // Assert
                response.StatusCode.Should().Be(HttpStatusCode.NotFound,
                                                "must not find something which does not exist");
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response =
                        await httpClient.DeleteAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

                    response.IsSuccessStatusCode.Should().BeTrue("cleanup must succeed");
                }
            }
        }
Beispiel #9
0
        public async Task CreateAsync_UsingInvalidTodoItem_ReturnsBadRequestHttpStatusCode()
        {
            // Arrange
            using HttpClient httpClient = await webApplicationFactory.CreateClientWithJwtAsync();

            var invalidModel = new NewTodoItemModel();

            // Act
            HttpResponseMessage response = await httpClient.PostAsJsonAsync(BaseUrl, invalidModel);

            // Assert
            using (new AssertionScope())
            {
                response.IsSuccessStatusCode.Should().BeFalse(BecauseInputModelIsInvalid);
                response.StatusCode.Should().Be(HttpStatusCode.BadRequest, BecauseInputModelIsInvalid);
            }
        }
Beispiel #10
0
        public async Task CreateAsync_UsingValidTodoItem_ReturnsExpectedData()
        {
            // Arrange
            using HttpClient httpClient = await webApplicationFactory.CreateHttpClientWithJwtAsync();

            long?      id          = null;
            const long idThreshold = 1;

            try
            {
                var newTodoItemModel = new NewTodoItemModel
                {
                    Name       = $"it--{nameof(CreateAsync_UsingValidTodoItem_ReturnsExpectedData)}--{Guid.NewGuid():N}",
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0
                };

                // Act
                HttpResponseMessage response = await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel);

                // Assert
                using (new AssertionScope())
                {
                    response.IsSuccessStatusCode.Should().BeTrue(BecauseAnEntityHasBeenCreated);
                    response.StatusCode.Should().Be(HttpStatusCode.Created, BecauseAnEntityHasBeenCreated);
                    response.Headers.ToDictionary(x => x.Key, x => x.Value).Should().ContainKey("Location");
                    response.Headers.Location?.OriginalString
                    .Should().MatchRegex(@"api/todo/\d+", BecauseAnEntityHasBeenCreated);

                    id = await response.Content.ReadAsAsync <long>();

                    id.Should().BeGreaterOrEqualTo(idThreshold, BecauseAnEntityHasBeenCreated);
                }
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response = await httpClient.DeleteAsync($"{BaseUrl}/{id}");

                    response.IsSuccessStatusCode.Should().BeTrue(BecauseCleanupMustSucceed);
                }
            }
        }
        public async Task CreateAsync_UsingValidTodoItem_ReturnsTheSameInstance()
        {
            // Arrange
            using HttpClient httpClient =
                      await testWebApplicationFactory.CreateClientWithJwtToken().ConfigureAwait(false);

            long?id = null;

            try
            {
                var newTodoItemModel = new NewTodoItemModel
                {
                    Name       = $"it--{nameof(CreateAsync_UsingValidTodoItem_ReturnsTheSameInstance)}--{Guid.NewGuid():N}",
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0
                };

                // Act
                HttpResponseMessage response =
                    await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel).ConfigureAwait(false);

                // Assert
                response.IsSuccessStatusCode.Should().BeTrue();
                response.StatusCode.Should().Be(HttpStatusCode.Created);

                id = await response.Content.ReadAsAsync <long>().ConfigureAwait(false);

                id.Should().BeGreaterOrEqualTo(1);
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response =
                        await httpClient.DeleteAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

                    response.IsSuccessStatusCode.Should().BeTrue("cleanup must succeed");
                }
            }
        }
Beispiel #12
0
        public async Task CreateAsync_UsingValidTodoItem_ReturnsExpectedId()
        {
            // Arrange
            using HttpClient httpClient = await webApplicationFactory.CreateClientWithJwtAsync();

            long?id = null;

            try
            {
                var newTodoItemModel = new NewTodoItemModel
                {
                    Name       = $"it--{nameof(CreateAsync_UsingValidTodoItem_ReturnsExpectedId)}--{Guid.NewGuid():N}",
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0
                };

                // Act
                HttpResponseMessage response = await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel);

                // Assert
                using (new AssertionScope())
                {
                    response.IsSuccessStatusCode.Should().BeTrue(BecauseAnEntityHasBeenCreated);
                    response.StatusCode.Should().Be(HttpStatusCode.Created, BecauseAnEntityHasBeenCreated);

                    id = await response.Content.ReadAsAsync <long>();

                    id.Should().BeGreaterOrEqualTo(1, BecauseAnEntityHasBeenCreated);
                }
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response = await httpClient.DeleteAsync($"{BaseUrl}/{id}");

                    response.IsSuccessStatusCode.Should().BeTrue(BecauseCleanupMustSucceed);
                }
            }
        }
        public async Task GetByQueryAsync_UsingDefaults_ReturnsExpectedResult()
        {
            // Arrange
            using HttpClient httpClient =
                      await testWebApplicationFactory.CreateClientWithJwtToken().ConfigureAwait(false);

            long?id = null;

            try
            {
                string nameSuffix = Guid.NewGuid().ToString("N");
                string name       = $"it--{nameof(GetByQueryAsync_UsingDefaults_ReturnsExpectedResult)}--{nameSuffix}";

                var newTodoItemModel = new NewTodoItemModel
                {
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0,
                    Name       = name
                };

                HttpResponseMessage response =
                    await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel).ConfigureAwait(false);

                response.IsSuccessStatusCode.Should().BeTrue();
                response.StatusCode.Should().Be(HttpStatusCode.Created);
                id = await response.Content.ReadAsAsync <long>().ConfigureAwait(false);

                var queryString = new Dictionary <string, string>
                {
                    { nameof(TodoItemQueryModel.Id), id.ToString() },
                    { nameof(TodoItemQueryModel.IsComplete), newTodoItemModel.IsComplete.ToString() },
                    { nameof(TodoItemQueryModel.NamePattern), name },
                    { nameof(TodoItemQueryModel.PageIndex), 0.ToString() },
                    { nameof(TodoItemQueryModel.PageSize), 5.ToString() },
                    { nameof(TodoItemQueryModel.SortBy), nameof(TodoItem.CreatedOn) },
                    { nameof(TodoItemQueryModel.IsSortAscending), bool.FalseString }
                };

                string requestUri = QueryHelpers.AddQueryString(BaseUrl, queryString);

                // Act
                response = await httpClient.GetAsync(requestUri).ConfigureAwait(false);

                // Assert
                response.IsSuccessStatusCode.Should().BeTrue();
                response.StatusCode.Should().Be(HttpStatusCode.OK);

                List <TodoItemModel> todoItemModels =
                    await response.Content.ReadAsAsync <List <TodoItemModel> >().ConfigureAwait(false);

                todoItemModels.Should().HaveCount(1);

                TodoItemModel todoItemModel = todoItemModels.Single();
                todoItemModel.Should().NotBeNull();
                todoItemModel.Name.Should().Be(newTodoItemModel.Name);
                todoItemModel.IsComplete.Should().Be(newTodoItemModel.IsComplete.Value);
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response =
                        await httpClient.DeleteAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

                    response.IsSuccessStatusCode.Should().BeTrue("cleanup must succeed");
                }
            }
        }
        public async Task UpdateAsync_UsingNewlyCreatedTodoItem_MustSucceed()
        {
            // Arrange
            using HttpClient httpClient =
                      await testWebApplicationFactory.CreateClientWithJwtToken().ConfigureAwait(false);

            long?id = null;

            try
            {
                string name       = $"it--{nameof(UpdateAsync_UsingNewlyCreatedTodoItem_MustSucceed)}--{Guid.NewGuid():N}";
                bool   isComplete = DateTime.UtcNow.Ticks % 2 == 0;

                var newTodoItemInfo = new NewTodoItemModel
                {
                    Name       = name,
                    IsComplete = isComplete
                };

                HttpResponseMessage response =
                    await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemInfo).ConfigureAwait(false);

                response.IsSuccessStatusCode.Should().BeTrue("a new entity has been created");
                id = await response.Content.ReadAsAsync <long>().ConfigureAwait(false);

                var updateTodoItemModel = new UpdateTodoItemModel
                {
                    IsComplete = !newTodoItemInfo.IsComplete,
                    Name       = $"CHANGED--{newTodoItemInfo.Name}"
                };

                // Act
                response = await httpClient.PutAsJsonAsync($"{BaseUrl}/{id}", updateTodoItemModel)
                           .ConfigureAwait(false);

                // Assert
                response.IsSuccessStatusCode.Should().BeTrue("an entity has been previously created");
                response.StatusCode.Should().Be(HttpStatusCode.NoContent);

                response = await httpClient.GetAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

                response.IsSuccessStatusCode.Should().BeTrue("an entity has been previously update");
                response.StatusCode.Should().Be(HttpStatusCode.OK);

                TodoItemModel todoItemModel =
                    await response.Content.ReadAsAsync <TodoItemModel>().ConfigureAwait(false);

                todoItemModel.Should().NotBeNull("an entity has been previously created");
                todoItemModel.Id.Should().Be(id);
                todoItemModel.IsComplete.Should().Be(updateTodoItemModel.IsComplete.Value);
                todoItemModel.Name.Should().Be(updateTodoItemModel.Name);
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response =
                        await httpClient.DeleteAsync($"{BaseUrl}/{id}").ConfigureAwait(false);

                    response.IsSuccessStatusCode.Should().BeTrue("cleanup must succeed");
                }
            }
        }