Build and Publish / Build Yale Access Frontend (pull_request) Successful in 1m17s
Build and Publish / Push Yale Access Frontend Docker Image (pull_request) Skipped
Build and Publish / Build Yale Access Backend (pull_request) Successful in 2m12s
Build and Publish / Push Yale Access Backend Docker Image (pull_request) Skipped
- Backend: xUnit integration tests (auth, rate limiting, people, lock codes, validation, error handling) using WebApplicationFactory with a fake lock - Frontend: Vitest + Nuxt test utils for useApi, the auth middleware, LoginForm, UserCodeList and PeopleList - Run both suites in CI before building
108 lines
3.7 KiB
C#
108 lines
3.7 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.AspNetCore.TestHost;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using YaleAccess.Models;
|
|
using YaleAccess.Services.Interfaces;
|
|
|
|
namespace YaleAccess.Tests;
|
|
|
|
public static class TestConstants
|
|
{
|
|
public const string Password = "test-password";
|
|
public const int HomeCode = 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// In-memory stand-in for the Z-Wave lock.
|
|
/// </summary>
|
|
public class FakeYaleAccessor : IYaleAccessor
|
|
{
|
|
public Dictionary<int, YaleUserCode> Codes { get; } = new()
|
|
{
|
|
[1] = new() { Id = 1, Code = "1111", Status = UserCodeStatus.ENABLED },
|
|
[2] = new() { Id = 2, Code = "2222", Status = UserCodeStatus.ENABLED },
|
|
[3] = new() { Id = 3, Code = "", Status = UserCodeStatus.AVAILABLE },
|
|
};
|
|
|
|
public bool FailWrites { get; set; }
|
|
public bool ThrowOnRead { get; set; }
|
|
|
|
public Task<YaleUserCode> GetCodeInformationAsync(int userCodeId)
|
|
{
|
|
if (ThrowOnRead) throw new InvalidOperationException("Lock unavailable");
|
|
var c = Codes[userCodeId];
|
|
return Task.FromResult(new YaleUserCode { Id = c.Id, Code = c.Code, Status = c.Status });
|
|
}
|
|
|
|
public Task<bool> SetUserCode(int userCodeId, string code)
|
|
{
|
|
if (FailWrites) return Task.FromResult(false);
|
|
Codes[userCodeId].Code = code;
|
|
Codes[userCodeId].Status = UserCodeStatus.ENABLED;
|
|
return Task.FromResult(true);
|
|
}
|
|
|
|
public Task<bool> SetCodeAsAvailable(int userCode)
|
|
{
|
|
if (FailWrites) return Task.FromResult(false);
|
|
Codes[userCode].Code = "";
|
|
Codes[userCode].Status = UserCodeStatus.AVAILABLE;
|
|
return Task.FromResult(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Boots the real app with a throwaway SQLite database and a fake lock.
|
|
/// Use a fresh instance per test: the login rate limiter is per app instance.
|
|
/// </summary>
|
|
public class TestApp : WebApplicationFactory<Program>
|
|
{
|
|
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"yale-test-{Guid.NewGuid():N}.db");
|
|
private readonly string _password;
|
|
|
|
public FakeYaleAccessor Lock { get; } = new();
|
|
|
|
public TestApp(string password = TestConstants.Password) => _password = password;
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
// Development keeps the auth cookie non-Secure, so it works over the test server's plain HTTP
|
|
builder.UseEnvironment("Development");
|
|
builder.UseSetting("UseMockDevelopmentMode", "false");
|
|
builder.UseSetting("ConnectionStrings:YaleContext", $"Data Source={_dbPath}");
|
|
builder.UseSetting("Authentication:Password", _password);
|
|
builder.UseSetting("Codes:Home", TestConstants.HomeCode.ToString());
|
|
builder.UseSetting("Codes:GuestCodeRangeStart", "2");
|
|
builder.UseSetting("Codes:GuestCodeRangeCount", "2");
|
|
|
|
builder.ConfigureTestServices(services =>
|
|
{
|
|
services.AddSingleton<IYaleAccessor>(Lock);
|
|
});
|
|
}
|
|
|
|
public async Task<HttpClient> LoggedInClientAsync()
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsJsonAsync("/api/Authentication/login", TestConstants.Password);
|
|
response.EnsureSuccessStatusCode();
|
|
return client;
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
|
|
try { File.Delete(_dbPath); } catch { /* best effort */ }
|
|
}
|
|
}
|
|
|
|
public static class HttpExtensions
|
|
{
|
|
public static async Task<JsonElement> ReadApiAsync(this HttpResponseMessage response)
|
|
=> await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
}
|