|
| 1 | +using System.Text.Json; |
| 2 | +using JsonApiDotNetCore.Resources.Annotations; |
| 3 | +using TestBuildingBlocks; |
| 4 | +using Xunit; |
| 5 | +using Xunit.Abstractions; |
| 6 | + |
| 7 | +namespace OpenApiTests.AttributeTypes; |
| 8 | + |
| 9 | +public sealed class AttributeCapabilitiesTests : IClassFixture<OpenApiTestContext<AttributeTypesStartup, AttributeTypesDbContext>> |
| 10 | +{ |
| 11 | + private enum RequestType{ Response, Create, Update } |
| 12 | + |
| 13 | + private static readonly Dictionary<RequestType, AttrCapabilities> CapabilitiesByRequestType = new() |
| 14 | + { |
| 15 | + { RequestType.Response, AttrCapabilities.AllowView }, |
| 16 | + { RequestType.Create, AttrCapabilities.AllowCreate }, |
| 17 | + { RequestType.Update, AttrCapabilities.AllowChange } |
| 18 | + }; |
| 19 | + |
| 20 | + private static readonly Dictionary<RequestType, string> SchemaPathByRequestType = new() |
| 21 | + { |
| 22 | + { RequestType.Response, "components.schemas.attributesInBookResponse.allOf[1].properties" }, |
| 23 | + { RequestType.Create, "components.schemas.attributesInCreateBookRequest.allOf[1].properties" }, |
| 24 | + { RequestType.Update, "components.schemas.attributesInUpdateBookRequest.allOf[1].properties" } |
| 25 | + }; |
| 26 | + |
| 27 | + private static readonly Dictionary<AttrCapabilities, List<string>> BookModelAttrsByCapability = new() |
| 28 | + { |
| 29 | + { AttrCapabilities.AllowView, ["title", "isbn", "publishedOn"] }, |
| 30 | + { AttrCapabilities.AllowChange, ["title", "internalNotes"] }, |
| 31 | + { AttrCapabilities.AllowCreate, ["draftContent"] }, |
| 32 | + { AttrCapabilities.None, ["secretCode"] } |
| 33 | + }; |
| 34 | + |
| 35 | + private readonly OpenApiTestContext<AttributeTypesStartup, AttributeTypesDbContext> _testContext; |
| 36 | + |
| 37 | + public AttributeCapabilitiesTests( |
| 38 | + OpenApiTestContext<AttributeTypesStartup, AttributeTypesDbContext> testContext, |
| 39 | + ITestOutputHelper output) |
| 40 | + { |
| 41 | + _testContext = testContext; |
| 42 | + _testContext.UseController<BooksController>(); |
| 43 | + _testContext.SetTestOutputHelper(output); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory] |
| 47 | + [InlineData(RequestType.Response)] |
| 48 | + [InlineData(RequestType.Create)] |
| 49 | + [InlineData(RequestType.Update)] |
| 50 | + private async Task Generated_Schema_Includes_Only_Expected_Attrs_For_Request_Type_Async(RequestType requestType) |
| 51 | + { |
| 52 | + // Arrange |
| 53 | + string path = SchemaPathByRequestType[requestType]; |
| 54 | + JsonElement document = await _testContext.GetSwaggerDocumentAsync(); |
| 55 | + JsonElement attrs = document.Should().ContainPath(path); |
| 56 | + |
| 57 | + IList<string> bookExpectedAttrs = BookModelAttrsByCapability[CapabilitiesByRequestType[requestType]]; |
| 58 | + IList<string> bookUnexpectedAttrs = BookModelAttrsByCapability.SelectMany(kvPair => kvPair.Value) |
| 59 | + .Except(bookExpectedAttrs).ToList(); |
| 60 | + |
| 61 | + // Assert |
| 62 | + foreach (string attrName in bookExpectedAttrs) |
| 63 | + { |
| 64 | + attrs.Should().ContainProperty(attrName); |
| 65 | + } |
| 66 | + foreach (string attrName in bookUnexpectedAttrs) |
| 67 | + { |
| 68 | + attrs.Should().NotContainPath($"properties.{attrName}"); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments