Skip to content

Commit

Permalink
Merge pull request #60 from CodebreakerApp/53-gamesapi-argumentexcept…
Browse files Browse the repository at this point in the history
…ion-to-client-error

53 gamesapi argumentexception to client error
  • Loading branch information
christiannagel authored Aug 17, 2023
2 parents a4edbf9 + 715a7a4 commit a5647a4
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 14 deletions.
25 changes: 17 additions & 8 deletions src/Codebreaker.GameApis.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "services", "services", "{0F
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gameapi", "gameapi", "{58E2F579-8F0B-424D-B965-68FDBDB30F47}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codebreaker.Data.Cosmos", "services\gameapi\Codebreaker.Data.Cosmos\Codebreaker.Data.Cosmos.csproj", "{E437B6EA-C5D9-4978-A82C-6F46FEC2A783}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codebreaker.Data.Cosmos", "services\gameapi\Codebreaker.Data.Cosmos\Codebreaker.Data.Cosmos.csproj", "{E437B6EA-C5D9-4978-A82C-6F46FEC2A783}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codebreaker.Data.SqlServer", "services\gameapi\Codebreaker.Data.SqlServer\Codebreaker.Data.SqlServer.csproj", "{E6EEB9B3-E223-4C2B-B69F-504F4038FA77}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codebreaker.Data.SqlServer", "services\gameapi\Codebreaker.Data.SqlServer\Codebreaker.Data.SqlServer.csproj", "{E6EEB9B3-E223-4C2B-B69F-504F4038FA77}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codebreaker.Data.SqlServer.Tests", "services\gameapi\Codebreaker.Data.SqlServer.Tests\Codebreaker.Data.SqlServer.Tests.csproj", "{9A381BB0-88AE-40B3-8703-7CDBDC4D7C22}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codebreaker.Data.SqlServer.Tests", "services\gameapi\Codebreaker.Data.SqlServer.Tests\Codebreaker.Data.SqlServer.Tests.csproj", "{9A381BB0-88AE-40B3-8703-7CDBDC4D7C22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codebreaker.GameAPIs", "services\gameapi\Codebreaker.GameAPIs\Codebreaker.GameAPIs.csproj", "{FE97466C-C996-4652-88E5-98B6EAA261C3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codebreaker.GameAPIs", "services\gameapi\Codebreaker.GameAPIs\Codebreaker.GameAPIs.csproj", "{FE97466C-C996-4652-88E5-98B6EAA261C3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codebreaker.GameAPIs.Models", "services\gameapi\Codebreaker.GameAPIs.Models\Codebreaker.GameAPIs.Models.csproj", "{D53C9ACA-6AF5-4404-AA85-7C03FB3ABA01}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codebreaker.GameAPIs.Models", "services\gameapi\Codebreaker.GameAPIs.Models\Codebreaker.GameAPIs.Models.csproj", "{D53C9ACA-6AF5-4404-AA85-7C03FB3ABA01}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codebreaker.GameAPIs.Tests", "services\gameapi\Codebreaker.GameAPIs.Tests\Codebreaker.GameAPIs.Tests.csproj", "{2A6EDB3B-B79E-49D3-A92B-C8A81E48F65F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E437B6EA-C5D9-4978-A82C-6F46FEC2A783}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E437B6EA-C5D9-4978-A82C-6F46FEC2A783}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -46,6 +45,13 @@ Global
{D53C9ACA-6AF5-4404-AA85-7C03FB3ABA01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D53C9ACA-6AF5-4404-AA85-7C03FB3ABA01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D53C9ACA-6AF5-4404-AA85-7C03FB3ABA01}.Release|Any CPU.Build.0 = Release|Any CPU
{2A6EDB3B-B79E-49D3-A92B-C8A81E48F65F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A6EDB3B-B79E-49D3-A92B-C8A81E48F65F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A6EDB3B-B79E-49D3-A92B-C8A81E48F65F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A6EDB3B-B79E-49D3-A92B-C8A81E48F65F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{58E2F579-8F0B-424D-B965-68FDBDB30F47} = {0FB1B986-E087-4C76-8FF8-B68B703F39B0}
Expand All @@ -55,4 +61,7 @@ Global
{FE97466C-C996-4652-88E5-98B6EAA261C3} = {58E2F579-8F0B-424D-B965-68FDBDB30F47}
{D53C9ACA-6AF5-4404-AA85-7C03FB3ABA01} = {58E2F579-8F0B-424D-B965-68FDBDB30F47}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {18EADE0D-2496-4DD6-B225-A2BC1D4EDB68}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="8.0.0-preview.6.23329.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="7.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0-preview.6.23329.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0-preview.7.23375.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CNinnovation.Codebreaker.Analyzers" Version="3.5.0-beta.47" />
<PackageReference Include="CNinnovation.Codebreaker.Analyzers" Version="3.5.0-beta.49" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Codebreaker.GameAPIs\Codebreaker.GameAPIs.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Net;
using System.Net.Http.Json;

using Codebreaker.GameAPIs.Models;

namespace Codebreaker.GameAPIs.Tests;

public class GameEndpointsTests
{
[Fact]
public async Task SetMoveWithInvalidMoveNumberShouldReturnBadRequest()
{
await using GamesApiApplication app = new();
var client = app.CreateClient();

int moveNumber = 0;
CreateGameResponse gameResponse = await StartGameFixtureAsync(app);

UpdateGameRequest updateGameRequest = new(gameResponse.GameId, gameResponse.GameType, gameResponse.PlayerName, moveNumber)
{
GuessPegs = new string[] { "Red", "Red", "Red", "Red" }
};

string uri = $"/games/{updateGameRequest.GameId}";
var updateGameResponse = await client.PatchAsJsonAsync(uri, updateGameRequest);

Assert.Equal(HttpStatusCode.BadRequest, updateGameResponse.StatusCode);
}

[Fact]
public async Task SetMoveWithInvalidGuessNumberShouldReturnBadRequest()
{
await using GamesApiApplication app = new();
var client = app.CreateClient();

int moveNumber = 1;
CreateGameResponse gameResponse = await StartGameFixtureAsync(app);

UpdateGameRequest updateGameRequest = new(gameResponse.GameId, gameResponse.GameType, gameResponse.PlayerName, moveNumber)
{
GuessPegs = new string[] { "Red", "Red", "Red", }
};

string uri = $"/games/{updateGameRequest.GameId}";
var updateGameResponse = await client.PatchAsJsonAsync(uri, updateGameRequest);

Assert.Equal(HttpStatusCode.BadRequest, updateGameResponse.StatusCode);
}

[Fact]
public async Task SetMoveWithWrongGuessesShouldReturnBadRequest()
{
await using GamesApiApplication app = new();
var client = app.CreateClient();

int moveNumber = 1;
CreateGameResponse gameResponse = await StartGameFixtureAsync(app);

UpdateGameRequest updateGameRequest = new(gameResponse.GameId, gameResponse.GameType, gameResponse.PlayerName, moveNumber)
{
GuessPegs = new string[] { "Red", "Red", "Red", "Schwarz" }
};

string uri = $"/games/{updateGameRequest.GameId}";
var updateGameResponse = await client.PatchAsJsonAsync(uri, updateGameRequest);

Assert.Equal(HttpStatusCode.BadRequest, updateGameResponse.StatusCode);
}

private async Task<CreateGameResponse> StartGameFixtureAsync(GamesApiApplication app)
{
var client = app.CreateClient();
CreateGameRequest request = new(GameType.Game6x4, "test");
var response = await client.PostAsJsonAsync("/games", request);
var gameReponse = await response.Content.ReadFromJsonAsync<CreateGameResponse>();

if (gameReponse is null)
Assert.Fail("gameResponse is null");
return gameReponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Hosting;

namespace Codebreaker.GameAPIs.Tests;

internal class GamesApiApplication : WebApplicationFactory<Program>
{
private readonly string _environment;
public GamesApiApplication(string envrionment = "Development")
{
_environment = envrionment;
}

protected override IHost CreateHost(IHostBuilder builder)
{
builder.ConfigureHostOptions(webBuilder =>
{
});

builder.UseEnvironment(_environment);

builder.ConfigureServices(services =>
{

});

Environment.SetEnvironmentVariable("DataStorage", "InMemory");
return base.CreateHost(builder);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"DataStorage": "InMemory"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
<UserSecretsId>cbc04dc5-efef-4c1d-9c2b-f5db1e2d2e5b</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Codebreaker.GameAPIs.Tests" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Endpoints\GameEndpointsPreOpenAPI.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0-preview.6.23329.11" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0-preview.7.23375.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void MapGameEndpoints(this IEndpointRouteBuilder routes)
return TypedResults.Ok(game.AsUpdateGameResponse(move.KeyPegs));
}
}
catch (ArgumentException ex) when (ex.HResult is <= 4200 and >= 400)
catch (ArgumentException ex) when (ex.HResult is <= 4400 and >= 4000)
{
string url = context.Request.GetDisplayUrl();
return ex.HResult switch
Expand Down

0 comments on commit a5647a4

Please sign in to comment.