Working on "Join Game" feature.
This commit is contained in:
@@ -12,16 +12,62 @@ namespace Shogi.AcceptanceTests;
|
||||
public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
#pragma warning restore xUnit1033
|
||||
{
|
||||
private readonly GuestTestFixture fixture;
|
||||
private readonly HttpClient guest1HttpClient;
|
||||
private readonly HttpClient guest2HttpClient;
|
||||
private readonly ITestOutputHelper console;
|
||||
|
||||
public AcceptanceTests(GuestTestFixture fixture, ITestOutputHelper console)
|
||||
{
|
||||
this.fixture = fixture;
|
||||
this.guest1HttpClient = fixture.Guest1ServiceClient;
|
||||
this.guest2HttpClient = fixture.Guest2ServiceClient;
|
||||
this.console = console;
|
||||
}
|
||||
|
||||
private HttpClient Service => fixture.Service;
|
||||
[Fact]
|
||||
public async Task JoinSession_Player2IsNotSet_SetsPlayer2()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Arrange
|
||||
await SetupTestSession();
|
||||
|
||||
// Act
|
||||
var joinResponse = await guest2HttpClient.PatchAsync(new Uri("Sessions/Acceptance Tests/Join", UriKind.Relative), null);
|
||||
|
||||
// Assert
|
||||
joinResponse.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var readSessionResponse = await ReadTestSession();
|
||||
readSessionResponse.Session.Player2.Should().NotBeNullOrEmpty();
|
||||
}
|
||||
finally
|
||||
{
|
||||
await DeleteTestSession();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JoinSession_SessionIsFull_Conflict()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Arrange
|
||||
await SetupTestSession();
|
||||
var joinResponse = await guest2HttpClient.PatchAsync(new Uri("Sessions/Acceptance Tests/Join", UriKind.Relative), null);
|
||||
joinResponse.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var readSessionResponse = await ReadTestSession();
|
||||
readSessionResponse.Session.Player2.Should().NotBeNullOrEmpty();
|
||||
|
||||
// Act
|
||||
joinResponse = await guest2HttpClient.PatchAsync(new Uri("Sessions/Acceptance Tests/Join", UriKind.Relative), null);
|
||||
|
||||
// Assert
|
||||
joinResponse.StatusCode.Should().Be(HttpStatusCode.Conflict);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await DeleteTestSession();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadSessionsPlayerCount()
|
||||
@@ -32,7 +78,7 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
await SetupTestSession();
|
||||
|
||||
// Act
|
||||
var readAllResponse = await Service
|
||||
var readAllResponse = await guest1HttpClient
|
||||
.GetFromJsonAsync<ReadSessionsPlayerCountResponse>(new Uri("Sessions/PlayerCount", UriKind.Relative),
|
||||
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
|
||||
|
||||
@@ -42,7 +88,7 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
.PlayerHasJoinedSessions
|
||||
.Should()
|
||||
.ContainSingle(session => session.Name == "Acceptance Tests" && session.PlayerCount == 1);
|
||||
readAllResponse.AllOtherSessions.Should().BeEmpty();
|
||||
readAllResponse.AllOtherSessions.Should().NotBeNull();
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -60,7 +106,7 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
await SetupTestSession();
|
||||
|
||||
// Act
|
||||
var response = await Service.GetFromJsonAsync<ReadSessionResponse>(
|
||||
var response = await guest1HttpClient.GetFromJsonAsync<ReadSessionResponse>(
|
||||
new Uri("Sessions/Acceptance Tests", UriKind.Relative),
|
||||
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
|
||||
|
||||
@@ -273,7 +319,7 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
};
|
||||
|
||||
// Act
|
||||
var response = await Service.PatchAsync(new Uri("Sessions/Acceptance Tests/Move", UriKind.Relative), JsonContent.Create(movePawnCommand));
|
||||
var response = await guest1HttpClient.PatchAsync(new Uri("Sessions/Acceptance Tests/Move", UriKind.Relative), JsonContent.Create(movePawnCommand));
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NoContent, because: await response.Content.ReadAsStringAsync());
|
||||
|
||||
// Assert
|
||||
@@ -293,7 +339,7 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
|
||||
private async Task SetupTestSession()
|
||||
{
|
||||
var createResponse = await Service.PostAsJsonAsync(
|
||||
var createResponse = await guest1HttpClient.PostAsJsonAsync(
|
||||
new Uri("Sessions", UriKind.Relative),
|
||||
new CreateSessionCommand { Name = "Acceptance Tests" },
|
||||
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
|
||||
@@ -302,12 +348,12 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
|
||||
|
||||
private Task<ReadSessionResponse> ReadTestSession()
|
||||
{
|
||||
return Service.GetFromJsonAsync<ReadSessionResponse>(new Uri("Sessions/Acceptance Tests", UriKind.Relative))!;
|
||||
return guest1HttpClient.GetFromJsonAsync<ReadSessionResponse>(new Uri("Sessions/Acceptance Tests", UriKind.Relative))!;
|
||||
}
|
||||
|
||||
private async Task DeleteTestSession()
|
||||
{
|
||||
var response = await Service.DeleteAsync(new Uri("Sessions/Acceptance Tests", UriKind.Relative));
|
||||
var response = await guest1HttpClient.DeleteAsync(new Uri("Sessions/Acceptance Tests", UriKind.Relative));
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NoContent, because: await response.Content.ReadAsStringAsync());
|
||||
|
||||
}
|
||||
|
||||
@@ -15,22 +15,36 @@ public class GuestTestFixture : IAsyncLifetime, IDisposable
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
Service = new HttpClient
|
||||
var baseUrl = Configuration["ServiceUrl"] ?? throw new InvalidOperationException("ServiceUrl configuration missing.");
|
||||
var baseAddress = new Uri(baseUrl, UriKind.Absolute);
|
||||
Guest1ServiceClient = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(Configuration["ServiceUrl"], UriKind.Absolute)
|
||||
BaseAddress = baseAddress
|
||||
};
|
||||
Guest2ServiceClient = new HttpClient
|
||||
{
|
||||
BaseAddress = baseAddress
|
||||
};
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; private set; }
|
||||
public HttpClient Service { get; }
|
||||
public HttpClient Guest2ServiceClient { get; }
|
||||
public HttpClient Guest1ServiceClient { get; }
|
||||
|
||||
public async Task InitializeAsync()
|
||||
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));
|
||||
// Log in as some guest accounts and retain the session cookie for future requests.
|
||||
var guestLoginUri = new Uri("User/LoginAsGuest", UriKind.Relative);
|
||||
|
||||
var loginResponse = await Guest1ServiceClient.GetAsync(guestLoginUri);
|
||||
loginResponse.IsSuccessStatusCode.Should().BeTrue(because: "Guest accounts should work");
|
||||
var guestSessionCookie = loginResponse.Headers.GetValues("Set-Cookie").SingleOrDefault();
|
||||
Service.DefaultRequestHeaders.Add("Set-Cookie", guestSessionCookie);
|
||||
var guestSessionCookie = loginResponse.Headers.GetValues("Set-Cookie").Single();
|
||||
Guest1ServiceClient.DefaultRequestHeaders.Add("Set-Cookie", guestSessionCookie);
|
||||
|
||||
loginResponse = await Guest2ServiceClient.GetAsync(guestLoginUri);
|
||||
loginResponse.IsSuccessStatusCode.Should().BeTrue(because: "Guest accounts should work twice");
|
||||
guestSessionCookie = loginResponse.Headers.GetValues("Set-Cookie").Single();
|
||||
Guest2ServiceClient.DefaultRequestHeaders.Add("Set-Cookie", guestSessionCookie);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
@@ -39,7 +53,7 @@ public class GuestTestFixture : IAsyncLifetime, IDisposable
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Service.Dispose();
|
||||
Guest1ServiceClient.Dispose();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
|
||||
Reference in New Issue
Block a user