Example #1
0
        public async Task Can_delete_resource()
        {
            // Arrange
            var existingOffice = _fakers.PostOffice.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.PostOffice.Add(existingOffice);
                await dbContext.SaveChangesAsync();
            });

            var route = "/postOffices/" + existingOffice.StringId;

            // Act
            var(httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                var officeInDatabase = await dbContext.PostOffice
                                       .FirstOrDefaultAsync(postOffice => postOffice.Id == existingOffice.Id);

                officeInDatabase.Should().BeNull();
            });
        }
Example #2
0
        public async Task Can_delete_existing_resource()
        {
            // Arrange
            WorkItem existingWorkItem = _fakers.WorkItem.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.WorkItems.Add(existingWorkItem);
                await dbContext.SaveChangesAsync();
            });

            string route = "/workItems/" + existingWorkItem.StringId;

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                WorkItem workItemsInDatabase = await dbContext.WorkItems.FirstWithIdOrDefaultAsync(existingWorkItem.Id);

                workItemsInDatabase.Should().BeNull();
            });
        }
Example #3
0
        public async Task Can_block_deleting_primary_resource_using_BeforeImplicitUpdateRelationship_hook()
        {
            // Arrange
            var lockedPerson = _fakers.Person.Generate();

            lockedPerson.IsLocked = true;
            lockedPerson.Passport = new Passport();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.People.Add(lockedPerson);
                await dbContext.SaveChangesAsync();
            });

            var route = $"/api/v1/passports/{lockedPerson.Passport.StringId}";

            // Act
            var(httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync <ErrorDocument>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.Forbidden);

            responseDocument.Errors.Should().HaveCount(1);
            responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.Forbidden);
            responseDocument.Errors[0].Title.Should().Be("You are not allowed to update fields or relationships of locked resource of type 'people'.");
            responseDocument.Errors[0].Detail.Should().BeNull();
        }
        public async Task Can_remove_from_OneToMany_relationship()
        {
            // Arrange
            var existingDealership = new Dealership
            {
                Address   = "Dam 1, 1012JS Amsterdam, the Netherlands",
                Inventory = new HashSet <Car>
                {
                    new Car
                    {
                        RegionId     = 123,
                        LicensePlate = "AA-BB-11"
                    },
                    new Car
                    {
                        RegionId     = 456,
                        LicensePlate = "CC-DD-22"
                    }
                }
            };

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                await dbContext.ClearTableAsync <Car>();
                dbContext.Dealerships.Add(existingDealership);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "cars",
                        id   = "123:AA-BB-11"
                    }
                }
            };

            var route = $"/dealerships/{existingDealership.StringId}/relationships/inventory";

            // Act
            var(httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                var dealershipInDatabase = await dbContext.Dealerships
                                           .Include(dealership => dealership.Inventory)
                                           .FirstWithIdOrDefaultAsync(existingDealership.Id);

                dealershipInDatabase.Inventory.Should().HaveCount(1);
                dealershipInDatabase.Inventory.Should().ContainSingle(car => car.Id == existingDealership.Inventory.ElementAt(1).Id);
            });
        }
Example #5
0
        public async Task Deleting_principal_side_of_required_OneToMany_relationship_triggers_cascading_delete()
        {
            // Arrange
            Order existingOrder = _fakers.Orders.Generate();

            existingOrder.Customer = _fakers.Customers.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Orders.Add(existingOrder);
                await dbContext.SaveChangesAsync();
            });

            string route = $"/customers/{existingOrder.Customer.Id}";

            // Act
            (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteDeleteAsync <Document>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeNull();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                Customer existingCustomerInDatabase = await dbContext.Customers.FirstWithIdOrDefaultAsync(existingOrder.Customer.Id);
                existingCustomerInDatabase.Should().BeNull();

                Order existingOrderInDatabase = await dbContext.Orders.FirstWithIdOrDefaultAsync(existingOrder.Id);
                existingOrderInDatabase.Should().BeNull();
            });
        }
        public async Task Can_remove_from_ToMany_relationship_with_zero_ID()
        {
            // Arrange
            var existingPlayer = _fakers.Player.Generate();

            existingPlayer.RecentlyPlayed = _fakers.Game.Generate(2);
            existingPlayer.RecentlyPlayed.ElementAt(0).Id = 0;

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                await dbContext.ClearTableAsync <Game>();
                dbContext.Players.Add(existingPlayer);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "games",
                        id   = 0
                    }
                }
            };

            var route = $"/players/{existingPlayer.StringId}/relationships/recentlyPlayed";

            // Act
            var(httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                var playerInDatabase = await dbContext.Players
                                       .Include(player => player.RecentlyPlayed)
                                       .FirstAsync(player => player.Id == existingPlayer.Id);

                playerInDatabase.Should().NotBeNull();
                playerInDatabase.RecentlyPlayed.Should().HaveCount(1);
                playerInDatabase.RecentlyPlayed.Should().ContainSingle(game => game.Id != 0);
            });
        }
        public async Task Can_remove_from_ToMany_relationship_with_empty_ID()
        {
            // Arrange
            var existingGame = _fakers.Game.Generate();

            existingGame.Maps = _fakers.Map.Generate(2);
            existingGame.Maps.ElementAt(0).Id = Guid.Empty;

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                await dbContext.ClearTableAsync <Map>();
                dbContext.Games.Add(existingGame);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "maps",
                        id   = "00000000-0000-0000-0000-000000000000"
                    }
                }
            };

            var route = $"/games/{existingGame.StringId}/relationships/maps";

            // Act
            var(httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                var gameInDatabase = await dbContext.Games
                                     .Include(game => game.Maps)
                                     .FirstWithIdAsync(existingGame.Id);

                gameInDatabase.Should().NotBeNull();
                gameInDatabase.Maps.Should().HaveCount(1);
                gameInDatabase.Maps.Should().ContainSingle(map => map.Id != Guid.Empty);
            });
        }
        public async Task Can_delete_archived_resource()
        {
            // Arrange
            TelevisionBroadcast broadcast = _fakers.TelevisionBroadcast.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Broadcasts.Add(broadcast);
                await dbContext.SaveChangesAsync();
            });

            string route = "/televisionBroadcasts/" + broadcast.StringId;

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                TelevisionBroadcast broadcastInDatabase = await dbContext.Broadcasts.FirstWithIdOrDefaultAsync(broadcast.Id);

                broadcastInDatabase.Should().BeNull();
            });
        }
Example #9
0
        public async Task Can_delete_resource()
        {
            // Arrange
            Building existingBuilding = _fakers.Building.Generate();

            existingBuilding.PrimaryDoor = _fakers.Door.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Buildings.Add(existingBuilding);
                await dbContext.SaveChangesAsync();
            });

            string route = "/buildings/" + existingBuilding.StringId;

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                Building buildingInDatabase = await dbContext.Buildings.FirstWithIdOrDefaultAsync(existingBuilding.Id);

                buildingInDatabase.Should().BeNull();
            });
        }
Example #10
0
        public async Task Cannot_delete_resource()
        {
            // Arrange
            var existingSofa = _fakers.Sofa.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Sofas.Add(existingSofa);
                await dbContext.SaveChangesAsync();
            });

            var route = "/sofas/" + existingSofa.StringId;

            // Act
            var(httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync <ErrorDocument>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.MethodNotAllowed);

            responseDocument.Errors.Should().HaveCount(1);

            var error = responseDocument.Errors[0];

            error.StatusCode.Should().Be(HttpStatusCode.MethodNotAllowed);
            error.Title.Should().Be("The request method is not allowed.");
            error.Detail.Should().Be("Resource does not support DELETE requests.");
        }
Example #11
0
        public async Task Can_remove_from_ToMany_relationship()
        {
            // Arrange
            var existingBankAccount = _fakers.BankAccount.Generate();

            existingBankAccount.Cards = _fakers.DebitCard.Generate(2);

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.BankAccounts.Add(existingBankAccount);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "debitCards",
                        id   = existingBankAccount.Cards[0].StringId
                    }
                }
            };

            var route = $"/bankAccounts/{existingBankAccount.StringId}/relationships/cards";

            // Act
            var(httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                var bankAccountInDatabase = await dbContext.BankAccounts
                                            .Include(bankAccount => bankAccount.Cards)
                                            .FirstAsync(bankAccount => bankAccount.Id == existingBankAccount.Id);

                bankAccountInDatabase.Cards.Should().HaveCount(1);
            });
        }
        public async Task Skips_on_remove_from_ToMany_relationship()
        {
            // Arrange
            var hitCounter = _testContext.Factory.Services.GetRequiredService <SerializationHitCounter>();

            Scholarship existingScholarship = _fakers.Scholarship.Generate();

            existingScholarship.Participants = _fakers.Student.Generate(2);

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Scholarships.Add(existingScholarship);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "students",
                        id   = existingScholarship.Participants[0].StringId
                    },
                    new
                    {
                        type = "students",
                        id   = existingScholarship.Participants[1].StringId
                    }
                }
            };

            string route = $"/scholarships/{existingScholarship.StringId}/relationships/participants";

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            hitCounter.DeserializeCount.Should().Be(0);
            hitCounter.SerializeCount.Should().Be(0);
        }
Example #13
0
        public async Task Can_delete_resource()
        {
            // Arrange
            var existingChair = _fakers.Chair.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Chairs.Add(existingChair);
                await dbContext.SaveChangesAsync();
            });

            var route = "/chairs/" + existingChair.StringId;

            // Act
            var(httpResponse, _) = await _testContext.ExecuteDeleteAsync <string>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);
        }
        public async Task Can_delete_resource()
        {
            // Arrange
            Table existingTable = _fakers.Table.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Tables.Add(existingTable);
                await dbContext.SaveChangesAsync();
            });

            string route = "/tables/" + existingTable.StringId;

            // Act
            (HttpResponseMessage httpResponse, _) = await _testContext.ExecuteDeleteAsync <string>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);
        }
        public async Task Cannot_remove_from_ToMany_relationship_for_soft_deleted_parent()
        {
            // Arrange
            Company existingCompany = _fakers.Company.Generate();

            existingCompany.SoftDeletedAt = SoftDeletionTime;
            existingCompany.Departments   = _fakers.Department.Generate(1);

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Companies.Add(existingCompany);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "departments",
                        id   = existingCompany.Departments.ElementAt(0).StringId
                    }
                }
            };

            string route = $"/companies/{existingCompany.StringId}/relationships/departments";

            // Act
            (HttpResponseMessage httpResponse, ErrorDocument responseDocument) = await _testContext.ExecuteDeleteAsync <ErrorDocument>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NotFound);

            responseDocument.Errors.Should().HaveCount(1);

            Error error = responseDocument.Errors[0];

            error.StatusCode.Should().Be(HttpStatusCode.NotFound);
            error.Title.Should().Be("The requested resource does not exist.");
            error.Detail.Should().Be($"Resource of type 'companies' with ID '{existingCompany.StringId}' does not exist.");
        }
        public async Task Cannot_remove_from_ToMany_relationship_for_other_parent_tenant()
        {
            // Arrange
            WebShop existingShop = _fakers.WebShop.Generate();

            existingShop.TenantId = OtherTenantId;
            existingShop.Products = _fakers.WebProduct.Generate(1);

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.WebShops.Add(existingShop);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "webProducts",
                        id   = existingShop.Products[0].StringId
                    }
                }
            };

            string route = $"/nld/shops/{existingShop.StringId}/relationships/products";

            // Act
            (HttpResponseMessage httpResponse, ErrorDocument responseDocument) = await _testContext.ExecuteDeleteAsync <ErrorDocument>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NotFound);

            responseDocument.Errors.Should().HaveCount(1);

            Error error = responseDocument.Errors[0];

            error.StatusCode.Should().Be(HttpStatusCode.NotFound);
            error.Title.Should().Be("The requested resource does not exist.");
            error.Detail.Should().Be($"Resource of type 'webShops' with ID '{existingShop.StringId}' does not exist.");
        }
Example #17
0
        public async Task Can_remove_from_annotated_ToMany_relationship()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = true,
                Files           = new List <SystemFile>
                {
                    new SystemFile
                    {
                        FileName    = "Main.cs",
                        SizeInBytes = 100
                    }
                }
            };

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.AddRange(directory);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new object[0]
            };

            string route = $"/systemDirectories/{directory.StringId}/relationships/files";

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecuteDeleteAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();
        }
Example #18
0
        public async Task Cannot_remove_from_HasOne_relationship()
        {
            // Arrange
            WorkItem existingWorkItem = _fakers.WorkItem.Generate();

            existingWorkItem.Assignee = _fakers.UserAccount.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.WorkItems.Add(existingWorkItem);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new
                {
                    type = "userAccounts",
                    id   = existingWorkItem.Assignee.StringId
                }
            };

            string route = $"/workItems/{existingWorkItem.StringId}/relationships/assignee";

            // Act
            (HttpResponseMessage httpResponse, ErrorDocument responseDocument) = await _testContext.ExecuteDeleteAsync <ErrorDocument>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.Forbidden);

            responseDocument.Errors.Should().HaveCount(1);

            Error error = responseDocument.Errors[0];

            error.StatusCode.Should().Be(HttpStatusCode.Forbidden);
            error.Title.Should().Be("Only to-many relationships can be updated through this endpoint.");
            error.Detail.Should().Be("Relationship 'assignee' must be a to-many relationship.");
        }
        public async Task Does_not_send_message_on_write_error()
        {
            // Arrange
            string missingUserId = Guid.NewGuid().ToString();

            string route = "/domainUsers/" + missingUserId;

            // Act
            (HttpResponseMessage httpResponse, ErrorDocument responseDocument) = await _testContext.ExecuteDeleteAsync <ErrorDocument>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NotFound);

            responseDocument.Errors.Should().HaveCount(1);

            Error error = responseDocument.Errors[0];

            error.StatusCode.Should().Be(HttpStatusCode.NotFound);
            error.Title.Should().Be("The requested resource does not exist.");
            error.Detail.Should().Be($"Resource of type 'domainUsers' with ID '{missingUserId}' does not exist.");

            var messageBroker = _testContext.Factory.Services.GetRequiredService <MessageBroker>();

            messageBroker.SentMessages.Should().BeEmpty();
        }