|
| 1 | +using System.Net; |
| 2 | +using FluentAssertions; |
| 3 | +using JsonApiDotNetCore.Serialization.Objects; |
| 4 | +using TestBuildingBlocks; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace JsonApiDotNetCoreTests.IntegrationTests.Links; |
| 8 | + |
| 9 | +public sealed class LinkInclusionIncludeTests : IClassFixture<IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext>> |
| 10 | +{ |
| 11 | + private readonly IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext> _testContext; |
| 12 | + private readonly LinksFakers _fakers = new(); |
| 13 | + |
| 14 | + public LinkInclusionIncludeTests(IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext> testContext) |
| 15 | + { |
| 16 | + _testContext = testContext; |
| 17 | + |
| 18 | + testContext.UseController<PhotoLocationsController>(); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public async Task Hides_links_for_unregistered_controllers() |
| 23 | + { |
| 24 | + // Arrange |
| 25 | + PhotoLocation location = _fakers.PhotoLocation.Generate(); |
| 26 | + location.Photo = _fakers.Photo.Generate(); |
| 27 | + location.Album = _fakers.PhotoAlbum.Generate(); |
| 28 | + |
| 29 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 30 | + { |
| 31 | + dbContext.PhotoLocations.Add(location); |
| 32 | + await dbContext.SaveChangesAsync(); |
| 33 | + }); |
| 34 | + |
| 35 | + string route = $"/photoLocations/{location.StringId}?include=photo,album"; |
| 36 | + |
| 37 | + // Act |
| 38 | + (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 39 | + |
| 40 | + // Assert |
| 41 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); |
| 42 | + |
| 43 | + responseDocument.Data.SingleValue.ShouldNotBeNull(); |
| 44 | + |
| 45 | + responseDocument.Data.SingleValue.Relationships.ShouldContainKey("photo").With(value => |
| 46 | + { |
| 47 | + value.ShouldNotBeNull(); |
| 48 | + value.Links.ShouldNotBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + responseDocument.Included.ShouldHaveCount(2); |
| 52 | + |
| 53 | + responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photos").Subject.With(resource => |
| 54 | + { |
| 55 | + resource.Links.Should().BeNull(); |
| 56 | + resource.Relationships.Should().BeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photoAlbums").Subject.With(resource => |
| 60 | + { |
| 61 | + resource.Links.Should().BeNull(); |
| 62 | + resource.Relationships.Should().BeNull(); |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
0 commit comments