Scaffold some AAT stuff
This commit is contained in:
@@ -1,16 +1,26 @@
|
||||
using Shogi.AcceptanceTests.TestSetup;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Shogi.AcceptanceTests
|
||||
{
|
||||
public class AcceptanceTests
|
||||
public class AcceptanceTests : IClassFixture<AATFixture>
|
||||
{
|
||||
public AcceptanceTests()
|
||||
{
|
||||
private readonly AATFixture fixture;
|
||||
private readonly ITestOutputHelper console;
|
||||
|
||||
public AcceptanceTests(AATFixture fixture, ITestOutputHelper console)
|
||||
{
|
||||
this.fixture = fixture;
|
||||
this.console = console;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateAndReadSession()
|
||||
public async Task CreateAndReadSession()
|
||||
{
|
||||
|
||||
var response = await fixture.Service.GetAsync(new Uri("Game", UriKind.Relative));
|
||||
console.WriteLine(await response.Content.ReadAsStringAsync());
|
||||
console.WriteLine(response.Headers.WwwAuthenticate.ToString());
|
||||
response.IsSuccessStatusCode.Should().BeTrue(because: "AAT Client should be authorized.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
@@ -6,12 +6,31 @@
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<UserSecretsId>96d6281d-a75b-4181-b535-ea34b26dc8a2</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<None Remove="appsettings.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</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.44.0" />
|
||||
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
||||
77
Shogi.AcceptanceTests/TestSetup/AATFixture.cs
Normal file
77
Shogi.AcceptanceTests/TestSetup/AATFixture.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace Shogi.AcceptanceTests.TestSetup
|
||||
{
|
||||
public class AATFixture : IAsyncLifetime, IDisposable
|
||||
{
|
||||
private bool disposedValue;
|
||||
private readonly IConfidentialClientApplication app;
|
||||
|
||||
public AATFixture()
|
||||
{
|
||||
Configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddEnvironmentVariables()
|
||||
.AddUserSecrets<AATFixture>()
|
||||
.Build();
|
||||
|
||||
var azure = Configuration.GetSection("Auth");
|
||||
app = ConfidentialClientApplicationBuilder.Create(azure["ClientId"])
|
||||
.WithTenantId(azure["TenantId"])
|
||||
.WithClientSecret(azure["SecretValue"])
|
||||
.Build();
|
||||
|
||||
Service = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(Configuration["ServiceUrl"], UriKind.Absolute)
|
||||
};
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; private set; }
|
||||
|
||||
|
||||
public HttpClient Service { get; }
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
var authResult = await app
|
||||
.AcquireTokenForClient(Configuration.GetSection("Auth:Scopes").Get<string[]>())
|
||||
.ExecuteAsync();
|
||||
|
||||
authResult.Should().NotBeNull();
|
||||
authResult.AccessToken.Should().NotBeNullOrEmpty();
|
||||
|
||||
Service.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
|
||||
|
||||
var response = await Service.GetAsync("Socket/Token");
|
||||
response.IsSuccessStatusCode.Should().BeTrue(because: "AAT client should create an account for tests.");
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Service.Dispose();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Task DisposeAsync()
|
||||
{
|
||||
Dispose(true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
global using Xunit;
|
||||
global using Xunit;
|
||||
global using FluentAssertions;
|
||||
11
Shogi.AcceptanceTests/appsettings.json
Normal file
11
Shogi.AcceptanceTests/appsettings.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ServiceUrl": "https://localhost:5001",
|
||||
"Auth": {
|
||||
"TenantId": "d6019544-c403-415c-8e96-50009635b6aa",
|
||||
"ClientId": "78b12a47-440c-4cc7-9402-f573a2802951",
|
||||
"SecretValue": "REDACTED",
|
||||
"Scopes": [
|
||||
"api://c1e94676-cab0-42ba-8b6c-9532b8486fff/.default"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user