-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from CodebreakerApp/53-gamesapi-argumentexcept…
…ion-to-client-error 53 gamesapi argumentexception to client error
- Loading branch information
Showing
12 changed files
with
179 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/services/gameapi/Codebreaker.GameAPIs.Tests/Codebreaker.GameAPIs.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
81 changes: 81 additions & 0 deletions
81
src/services/gameapi/Codebreaker.GameAPIs.Tests/GameEndpointsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/services/gameapi/Codebreaker.GameAPIs.Tests/GamesApiApplication.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
1 change: 1 addition & 0 deletions
1
src/services/gameapi/Codebreaker.GameAPIs.Tests/GlobalUsings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
3 changes: 3 additions & 0 deletions
3
src/services/gameapi/Codebreaker.GameAPIs.Tests/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"DataStorage": "InMemory" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters