Skip to content

openapi: support client-generated id #1475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions test/OpenApiEndToEndTests/ClientIdGenerationModes/PostTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using FluentAssertions;
using FluentAssertions.Specialized;
using JsonApiDotNetCore.OpenApi.Client;
using Newtonsoft.Json;
using OpenApiEndToEndTests.ClientIdGenerationModes.GeneratedCode;
using OpenApiTests;
using OpenApiTests.ClientIdGenerationModes;
using TestBuildingBlocks;
using Xunit;

namespace OpenApiEndToEndTests.ClientIdGenerationModes;

public sealed class PostTests : IClassFixture<IntegrationTestContext<OpenApiStartup<ClientIdGenerationModesDbContext>, ClientIdGenerationModesDbContext>>
{
private readonly IntegrationTestContext<OpenApiStartup<ClientIdGenerationModesDbContext>, ClientIdGenerationModesDbContext> _testContext;
private readonly ClientIdGenerationModesFakers _fakers = new();

public PostTests(IntegrationTestContext<OpenApiStartup<ClientIdGenerationModesDbContext>, ClientIdGenerationModesDbContext> testContext)
{
_testContext = testContext;

testContext.UseController<PlayersController>();
testContext.UseController<GamesController>();
testContext.UseController<PlayerGroupsController>();
}

[Fact]
public async Task Cannot_create_resource_without_ID_when_mode_is_required()
{
// Arrange
Player player = _fakers.Player.Generate();

using HttpClient httpClient = _testContext.Factory.CreateClient();
ClientIdGenerationModesClient apiClient = new(httpClient);

// Act
Func<Task<PlayerPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
{
Data = new PlayerDataInPostRequest
{
Id = null!,
Attributes = new PlayerAttributesInPostRequest
{
UserName = player.UserName
}
}
}));

// Assert
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
assertion.Which.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
}

[Fact]
public async Task Can_create_resource_with_ID_when_mode_is_required()
{
// Arrange
Player player = _fakers.Player.Generate();
player.Id = Guid.NewGuid();

using HttpClient httpClient = _testContext.Factory.CreateClient();
ClientIdGenerationModesClient apiClient = new(httpClient);

// Act
Func<Task<PlayerPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
{
Data = new PlayerDataInPostRequest
{
Id = player.StringId!,
Attributes = new PlayerAttributesInPostRequest
{
UserName = player.UserName
}
}
}));

// Assert
PlayerPrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
doc.Should().BeNull();
}

[Fact]
public async Task Can_create_resource_without_ID_when_mode_is_allowed()
{
// Arrange
Game game = _fakers.Game.Generate();

using HttpClient httpClient = _testContext.Factory.CreateClient();
ClientIdGenerationModesClient apiClient = new(httpClient);

// Act
Func<Task<GamePrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGameAsync(null, new GamePostRequestDocument
{
Data = new GameDataInPostRequest
{
Id = null!,
Attributes = new GameAttributesInPostRequest
{
Title = game.Title,
PurchasePrice = (double)game.PurchasePrice
}
}
}));

// Assert
GamePrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
doc?.Data.Id.Should().NotBeNullOrEmpty();
}

[Fact]
public async Task Can_create_resource_with_ID_when_mode_is_allowed()
{
// Arrange
Game game = _fakers.Game.Generate();
game.Id = Guid.NewGuid();

using HttpClient httpClient = _testContext.Factory.CreateClient();
ClientIdGenerationModesClient apiClient = new(httpClient);

// Act
Func<Task<GamePrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGameAsync(null, new GamePostRequestDocument
{
Data = new GameDataInPostRequest
{
Id = game.StringId!,
Attributes = new GameAttributesInPostRequest
{
Title = game.Title,
PurchasePrice = (double)game.PurchasePrice
}
}
}));

// Assert
GamePrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
doc.Should().BeNull();
}

[Fact]
public async Task Can_create_resource_without_ID_when_mode_is_forbidden()
{
// Arrange
PlayerGroup playerGroup = _fakers.Group.Generate();

using HttpClient httpClient = _testContext.Factory.CreateClient();
ClientIdGenerationModesClient apiClient = new(httpClient);

// Act
Func<Task<PlayerGroupPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostPlayerGroupAsync(null,
new PlayerGroupPostRequestDocument
{
Data = new PlayerGroupDataInPostRequest
{
Attributes = new PlayerGroupAttributesInPostRequest
{
Name = playerGroup.Name
}
}
}));

// Assert
PlayerGroupPrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
doc?.Data.Id.Should().NotBeNullOrEmpty();
}
}
Loading