75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Shogi.AcceptanceTests.TestSetup;
|
|
|
|
/// <summary>
|
|
/// Acceptance Test fixture for tests which assert features for Microsoft accounts.
|
|
/// </summary>
|
|
public class GuestTestFixture : IAsyncLifetime, IDisposable
|
|
{
|
|
private bool disposedValue;
|
|
|
|
public GuestTestFixture()
|
|
{
|
|
Configuration = new ConfigurationBuilder()
|
|
.AddJsonFile("appsettings.json")
|
|
.Build();
|
|
|
|
var baseUrl = Configuration["ServiceUrl"] ?? throw new InvalidOperationException("ServiceUrl configuration missing.");
|
|
var baseAddress = new Uri(baseUrl, UriKind.Absolute);
|
|
Guest1ServiceClient = new HttpClient
|
|
{
|
|
BaseAddress = baseAddress
|
|
};
|
|
Guest2ServiceClient = new HttpClient
|
|
{
|
|
BaseAddress = baseAddress
|
|
};
|
|
}
|
|
|
|
public IConfiguration Configuration { get; private set; }
|
|
public HttpClient Guest2ServiceClient { get; }
|
|
public HttpClient Guest1ServiceClient { get; }
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
// 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").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)
|
|
{
|
|
if (!disposedValue)
|
|
{
|
|
if (disposing)
|
|
{
|
|
Guest1ServiceClient.Dispose();
|
|
}
|
|
|
|
disposedValue = true;
|
|
}
|
|
}
|
|
|
|
public Task DisposeAsync()
|
|
{
|
|
Dispose(true);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Dispose(disposing: true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|