create, read, playercount
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Shogi.AcceptanceTests.TestSetup;
|
||||
using Shogi.Contracts.Api;
|
||||
using Shogi.Contracts.Types;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Xunit.Abstractions;
|
||||
@@ -21,16 +22,78 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
|
||||
private HttpClient Service => fixture.Service;
|
||||
|
||||
[Fact]
|
||||
public async Task ReadSessionsPlayerCount()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Arrange
|
||||
await CreateSession();
|
||||
|
||||
// Act
|
||||
var readAllResponse = await Service
|
||||
.GetFromJsonAsync<ReadSessionsPlayerCountResponse>(new Uri("Sessions/PlayerCount", UriKind.Relative),
|
||||
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
|
||||
|
||||
// Assert
|
||||
readAllResponse.Should().NotBeNull();
|
||||
readAllResponse!
|
||||
.AllOtherSessions
|
||||
.Should()
|
||||
.ContainSingle(session => session.Name == "Acceptance Tests" && session.PlayerCount == 1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Annul
|
||||
await DeleteSession();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateAndReadSession()
|
||||
{
|
||||
var createResponse = await Service.PostAsJsonAsync(new Uri("Session", UriKind.Relative), new CreateSessionCommand { Name = "Acceptance Tests" });
|
||||
createResponse.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||
var yep = await createResponse.Content.ReadAsStringAsync();
|
||||
console.WriteLine(yep);
|
||||
try
|
||||
{
|
||||
// Arrange
|
||||
await CreateSession();
|
||||
|
||||
// Act
|
||||
var response = await Service.GetFromJsonAsync<ReadSessionResponse>(
|
||||
new Uri("Sessions/Acceptance Tests", UriKind.Relative),
|
||||
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
|
||||
|
||||
// Assert
|
||||
response.Should().NotBeNull();
|
||||
response!.Session.Should().NotBeNull();
|
||||
response.Session.BoardState.Board.Should().NotBeEmpty();
|
||||
response.Session.BoardState.Player1Hand.Should().BeEmpty();
|
||||
response.Session.BoardState.Player2Hand.Should().BeEmpty();
|
||||
response.Session.BoardState.PlayerInCheck.Should().BeNull();
|
||||
response.Session.BoardState.WhoseTurn.Should().Be(WhichPlayer.Player1);
|
||||
response.Session.Player1.Should().NotBeNullOrEmpty();
|
||||
response.Session.Player2.Should().BeNull();
|
||||
response.Session.SessionName.Should().Be("Acceptance Tests");
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Annul
|
||||
await DeleteSession();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CreateSession()
|
||||
{
|
||||
var createResponse = await Service.PostAsJsonAsync(
|
||||
new Uri("Sessions", UriKind.Relative),
|
||||
new CreateSessionCommand { Name = "Acceptance Tests" },
|
||||
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
|
||||
createResponse.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||
}
|
||||
|
||||
private async Task DeleteSession()
|
||||
{
|
||||
var response = await Service.DeleteAsync(new Uri("Sessions/Acceptance Tests", UriKind.Relative));
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NoContent, because: "Test cleanup should succeed");
|
||||
|
||||
var readAllResponse = await Service.GetFromJsonAsync<ReadAllSessionsResponse>(new Uri("Session", UriKind.Relative));
|
||||
readAllResponse.Should().NotBeNull();
|
||||
readAllResponse!.AllOtherSessions.Should().ContainSingle(session => session.Name == "Acceptance Tests" && session.PlayerCount == 1);
|
||||
}
|
||||
}
|
||||
@@ -21,21 +21,21 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.7.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Identity.Client" Version="4.46.1" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.8.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Identity.Client" Version="4.48.0" />
|
||||
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.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="3.1.2">
|
||||
<PackageReference Include="coverlet.collector" Version="3.2.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
||||
@@ -1,62 +1,60 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Shogi.AcceptanceTests.TestSetup
|
||||
namespace Shogi.AcceptanceTests.TestSetup;
|
||||
|
||||
/// <summary>
|
||||
/// Acceptance Test fixture for tests which assert features for Microsoft accounts.
|
||||
/// </summary>
|
||||
public class GuestTestFixture : IAsyncLifetime, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Acceptance Test fixture for tests which assert features for Microsoft accounts.
|
||||
/// </summary>
|
||||
public class GuestTestFixture : IAsyncLifetime, IDisposable
|
||||
private bool disposedValue;
|
||||
|
||||
public GuestTestFixture()
|
||||
{
|
||||
private bool disposedValue;
|
||||
Configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
public GuestTestFixture()
|
||||
Service = new HttpClient
|
||||
{
|
||||
Configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
//.AddEnvironmentVariables()
|
||||
.Build();
|
||||
BaseAddress = new Uri(Configuration["ServiceUrl"], UriKind.Absolute)
|
||||
};
|
||||
}
|
||||
|
||||
Service = new HttpClient
|
||||
public IConfiguration Configuration { get; private set; }
|
||||
public HttpClient Service { get; }
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
// Log in as a guest account and retain the session cookie for future requests.
|
||||
var loginResponse = await Service.GetAsync(new Uri("User/LoginAsGuest", UriKind.Relative));
|
||||
loginResponse.IsSuccessStatusCode.Should().BeTrue(because: "Guest accounts should work");
|
||||
var guestSessionCookie = loginResponse.Headers.GetValues("Set-Cookie").SingleOrDefault();
|
||||
Service.DefaultRequestHeaders.Add("Set-Cookie", guestSessionCookie);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
BaseAddress = new Uri(Configuration["ServiceUrl"], UriKind.Absolute)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; private set; }
|
||||
|
||||
public HttpClient Service { get; }
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
// Log in as a guest account.
|
||||
var loginResponse = await Service.GetAsync(new Uri("User/LoginAsGuest", UriKind.Relative));
|
||||
loginResponse.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Service.Dispose();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
Service.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public Task DisposeAsync()
|
||||
{
|
||||
Dispose(true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Task DisposeAsync()
|
||||
{
|
||||
Dispose(true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user