Exemplo n.º 1
0
        public async Task Can_update_resource_with_multiple_self_references()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = false
            };

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

            var requestBody = new
            {
                data = new
                {
                    type       = "systemDirectories",
                    id         = directory.StringId,
                    attributes = new
                    {
                        name = "Project files"
                    },
                    relationships = new
                    {
                        self = new
                        {
                            data = new
                            {
                                type = "systemDirectories",
                                id   = directory.StringId
                            }
                        },
                        alsoSelf = new
                        {
                            data = new
                            {
                                type = "systemDirectories",
                                id   = directory.StringId
                            }
                        }
                    }
                }
            };

            string route = "/systemDirectories/" + directory.StringId;

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

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

            responseDocument.Should().BeEmpty();
        }
Exemplo n.º 2
0
        public async Task Can_replace_annotated_ToMany_relationship()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = true,
                Files           = new List <SystemFile>
                {
                    new SystemFile
                    {
                        FileName = "Main.cs"
                    },
                    new SystemFile
                    {
                        FileName = "Program.cs"
                    }
                }
            };

            var otherFile = new SystemFile
            {
                FileName = "EntryPoint.cs"
            };

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Directories.Add(directory);
                dbContext.Files.Add(otherFile);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "systemFiles",
                        id   = otherFile.StringId
                    }
                }
            };

            string route = "/systemDirectories/" + directory.StringId + "/relationships/files";

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

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

            responseDocument.Should().BeEmpty();
        }
Exemplo n.º 3
0
        public async Task Can_replace_annotated_ToOne_relationship()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = true,
                Parent          = new SystemDirectory
                {
                    Name            = "Data",
                    IsCaseSensitive = true
                }
            };

            var otherParent = new SystemDirectory
            {
                Name            = "Data files",
                IsCaseSensitive = true
            };

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

            var requestBody = new
            {
                data = new
                {
                    type = "systemDirectories",
                    id   = otherParent.StringId
                }
            };

            string route = "/systemDirectories/" + directory.StringId + "/relationships/parent";

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

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

            responseDocument.Should().BeEmpty();
        }
Exemplo n.º 4
0
        public async Task Cannot_update_resource_with_invalid_attribute_value()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = true
            };

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

            var requestBody = new
            {
                data = new
                {
                    type       = "systemDirectories",
                    id         = directory.StringId,
                    attributes = new
                    {
                        name = "!@#$%^&*().-"
                    }
                }
            };

            string route = "/systemDirectories/" + directory.StringId;

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

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

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

            Error error = responseDocument.Errors[0];

            error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
            error.Title.Should().Be("Input validation failed.");
            error.Detail.Should().Be("The field Name must match the regular expression '^[\\w\\s]+$'.");
            error.Source.Pointer.Should().Be("/data/attributes/name");
        }
Exemplo n.º 5
0
        public async Task Can_add_to_annotated_ToMany_relationship()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = true
            };

            var file = new SystemFile
            {
                FileName    = "Main.cs",
                SizeInBytes = 100
            };

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

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "systemFiles",
                        id   = file.StringId
                    }
                }
            };

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

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

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

            responseDocument.Should().BeEmpty();
        }
Exemplo n.º 6
0
        public async Task Can_update_resource_with_omitted_required_attribute_value()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = true
            };

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

            var requestBody = new
            {
                data = new
                {
                    type       = "systemDirectories",
                    id         = directory.StringId,
                    attributes = new
                    {
                        sizeInBytes = 100
                    }
                }
            };

            string route = "/systemDirectories/" + directory.StringId;

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

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

            responseDocument.Should().BeEmpty();
        }
Exemplo n.º 7
0
        public async Task Can_update_resource_with_annotated_relationships()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = false,
                Subdirectories  = new List <SystemDirectory>
                {
                    new SystemDirectory
                    {
                        Name            = "C#",
                        IsCaseSensitive = false
                    }
                },
                Files = new List <SystemFile>
                {
                    new SystemFile
                    {
                        FileName = "readme.txt"
                    }
                },
                Parent = new SystemDirectory
                {
                    Name            = "Data",
                    IsCaseSensitive = false
                }
            };

            var otherParent = new SystemDirectory
            {
                Name            = "Shared",
                IsCaseSensitive = false
            };

            var otherSubdirectory = new SystemDirectory
            {
                Name            = "Shared",
                IsCaseSensitive = false
            };

            var otherFile = new SystemFile
            {
                FileName = "readme.md"
            };

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Directories.AddRange(directory, otherParent, otherSubdirectory);
                dbContext.Files.Add(otherFile);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new
                {
                    type       = "systemDirectories",
                    id         = directory.StringId,
                    attributes = new
                    {
                        name = "Project Files"
                    },
                    relationships = new
                    {
                        subdirectories = new
                        {
                            data = new[]
                            {
                                new
                                {
                                    type = "systemDirectories",
                                    id   = otherSubdirectory.StringId
                                }
                            }
                        },
                        files = new
                        {
                            data = new[]
                            {
                                new
                                {
                                    type = "systemFiles",
                                    id   = otherFile.StringId
                                }
                            }
                        },
                        parent = new
                        {
                            data = new
                            {
                                type = "systemDirectories",
                                id   = otherParent.StringId
                            }
                        }
                    }
                }
            };

            string route = "/systemDirectories/" + directory.StringId;

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

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

            responseDocument.Should().BeEmpty();
        }
Exemplo n.º 8
0
        public async Task Cannot_update_resource_with_invalid_ID()
        {
            // Arrange
            var directory = new SystemDirectory
            {
                Name            = "Projects",
                IsCaseSensitive = true
            };

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

            var requestBody = new
            {
                data = new
                {
                    type       = "systemDirectories",
                    id         = -1,
                    attributes = new
                    {
                        name = "Repositories"
                    },
                    relationships = new
                    {
                        subdirectories = new
                        {
                            data = new[]
                            {
                                new
                                {
                                    type = "systemDirectories",
                                    id   = -1
                                }
                            }
                        }
                    }
                }
            };

            const string route = "/systemDirectories/-1";

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

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

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

            Error error1 = responseDocument.Errors[0];

            error1.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
            error1.Title.Should().Be("Input validation failed.");
            error1.Detail.Should().Be("The field Id must match the regular expression '^[0-9]+$'.");
            error1.Source.Pointer.Should().Be("/data/attributes/id");

            Error error2 = responseDocument.Errors[1];

            error2.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
            error2.Title.Should().Be("Input validation failed.");
            error2.Detail.Should().Be("The field Id must match the regular expression '^[0-9]+$'.");
            error2.Source.Pointer.Should().Be("/data/attributes/Subdirectories[0].Id");
        }
Exemplo n.º 9
0
        public async Task Can_create_resource_with_annotated_relationships()
        {
            // Arrange
            var parentDirectory = new SystemDirectory
            {
                Name            = "Shared",
                IsCaseSensitive = true
            };

            var subdirectory = new SystemDirectory
            {
                Name            = "Open Source",
                IsCaseSensitive = true
            };

            var file = new SystemFile
            {
                FileName    = "Main.cs",
                SizeInBytes = 100
            };

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.Directories.AddRange(parentDirectory, subdirectory);
                dbContext.Files.Add(file);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new
                {
                    type       = "systemDirectories",
                    attributes = new
                    {
                        name            = "Projects",
                        isCaseSensitive = true
                    },
                    relationships = new
                    {
                        subdirectories = new
                        {
                            data = new[]
                            {
                                new
                                {
                                    type = "systemDirectories",
                                    id   = subdirectory.StringId
                                }
                            }
                        },
                        files = new
                        {
                            data = new[]
                            {
                                new
                                {
                                    type = "systemFiles",
                                    id   = file.StringId
                                }
                            }
                        },
                        parent = new
                        {
                            data = new
                            {
                                type = "systemDirectories",
                                id   = parentDirectory.StringId
                            }
                        }
                    }
                }
            };

            const string route = "/systemDirectories";

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

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

            responseDocument.SingleData.Should().NotBeNull();
            responseDocument.SingleData.Attributes["name"].Should().Be("Projects");
            responseDocument.SingleData.Attributes["isCaseSensitive"].Should().Be(true);
        }