Add backend and frontend tests and run them in CI
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
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
This commit is contained in:
1 parent
a9bef7b50d
commit
4fe6f9d7b5
15 files changed
+968
No files matched your search
@@ -0,0 +1,143 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using YaleAccess.Models;
|
||||
using YaleAccess.Services;
|
||||
|
||||
namespace YaleAccess.Tests;
|
||||
|
||||
public class YaleCodeValidationTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("1234")]
|
||||
[InlineData("12345")]
|
||||
[InlineData("123456")]
|
||||
[InlineData("0000")]
|
||||
public void Valid_codes_pass(string code) => Assert.Equal(string.Empty, YaleAccessor.ValidateCode(code));
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("123")]
|
||||
[InlineData("1234567")]
|
||||
public void Wrong_length_is_rejected(string code) => Assert.NotEqual(string.Empty, YaleAccessor.ValidateCode(code));
|
||||
|
||||
[Theory]
|
||||
[InlineData("12a4")]
|
||||
[InlineData("+123")]
|
||||
[InlineData("-123")]
|
||||
[InlineData(" 123")]
|
||||
[InlineData("12.4")]
|
||||
[InlineData("١٢٣٤")] // Arabic-Indic digits, not ASCII
|
||||
public void Non_digit_codes_are_rejected(string code) => Assert.Equal("The code must be numeric.", YaleAccessor.ValidateCode(code));
|
||||
|
||||
[Fact]
|
||||
public void The_home_code_cannot_be_cleared()
|
||||
{
|
||||
Assert.NotEqual(string.Empty, YaleAccessor.ValidateClearCode(1, 1));
|
||||
Assert.Equal(string.Empty, YaleAccessor.ValidateClearCode(2, 1));
|
||||
}
|
||||
}
|
||||
|
||||
public class YaleEndpointTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Codes_list_includes_guest_codes_and_flags_the_home_code()
|
||||
{
|
||||
using var app = new TestApp();
|
||||
var client = await app.LoggedInClientAsync();
|
||||
|
||||
var data = (await (await client.GetAsync("/api/Yale/codes")).ReadApiAsync()).GetProperty("data");
|
||||
var codes = data.EnumerateArray().ToList();
|
||||
|
||||
Assert.Equal(3, codes.Count); // 2 guest codes + home
|
||||
Assert.Equal(new[] { 1 }, codes.Where(c => c.GetProperty("isHome").GetBoolean()).Select(c => c.GetProperty("id").GetInt32()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Setting_a_valid_code_updates_the_lock()
|
||||
{
|
||||
using var app = new TestApp();
|
||||
var client = await app.LoggedInClientAsync();
|
||||
|
||||
var response = await client.PostAsJsonAsync("/api/Yale/code/3", "4321");
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal("4321", app.Lock.Codes[3].Code);
|
||||
Assert.Equal(UserCodeStatus.ENABLED, app.Lock.Codes[3].Status);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("12a4")]
|
||||
[InlineData("+123")]
|
||||
[InlineData("12")]
|
||||
public async Task Invalid_codes_are_rejected_without_touching_the_lock(string code)
|
||||
{
|
||||
using var app = new TestApp();
|
||||
var client = await app.LoggedInClientAsync();
|
||||
|
||||
var response = await client.PostAsJsonAsync("/api/Yale/code/2", code);
|
||||
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
Assert.Equal("2222", app.Lock.Codes[2].Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_failed_lock_write_returns_502()
|
||||
{
|
||||
using var app = new TestApp();
|
||||
app.Lock.FailWrites = true;
|
||||
var client = await app.LoggedInClientAsync();
|
||||
|
||||
var response = await client.PostAsJsonAsync("/api/Yale/code/2", "4321");
|
||||
Assert.Equal(HttpStatusCode.BadGateway, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Clearing_a_guest_code_makes_it_available()
|
||||
{
|
||||
using var app = new TestApp();
|
||||
var client = await app.LoggedInClientAsync();
|
||||
|
||||
var response = await client.PostAsync("/api/Yale/code/2/status", null);
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal(UserCodeStatus.AVAILABLE, app.Lock.Codes[2].Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_home_code_cannot_be_cleared_via_the_api()
|
||||
{
|
||||
using var app = new TestApp();
|
||||
var client = await app.LoggedInClientAsync();
|
||||
|
||||
var response = await client.PostAsync("/api/Yale/code/1/status", null);
|
||||
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
Assert.Equal(UserCodeStatus.ENABLED, app.Lock.Codes[1].Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Sending_an_unset_code_is_rejected()
|
||||
{
|
||||
using var app = new TestApp();
|
||||
var client = await app.LoggedInClientAsync();
|
||||
|
||||
var response = await client.PostAsJsonAsync("/api/Yale/code/3/send", "+61400000001");
|
||||
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Unexpected_errors_become_a_json_500_without_leaking_details()
|
||||
{
|
||||
using var app = new TestApp();
|
||||
var client = await app.LoggedInClientAsync();
|
||||
app.Lock.ThrowOnRead = true;
|
||||
|
||||
var response = await client.GetAsync("/api/Yale/codes");
|
||||
|
||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
var body = await response.ReadApiAsync();
|
||||
Assert.False(body.GetProperty("success").GetBoolean());
|
||||
Assert.Equal("An unexpected error occurred.", body.GetProperty("error").GetString());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user