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
78 lines
2.9 KiB
C#
78 lines
2.9 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
|
|
namespace YaleAccess.Tests;
|
|
|
|
public class PeopleTests
|
|
{
|
|
[Fact]
|
|
public async Task Create_list_and_delete_a_person()
|
|
{
|
|
using var app = new TestApp();
|
|
var client = await app.LoggedInClientAsync();
|
|
|
|
var created = await client.PostAsJsonAsync("/api/People", new { name = " Alex ", phoneNumber = "+61400000001" });
|
|
Assert.Equal(HttpStatusCode.OK, created.StatusCode);
|
|
var person = (await created.ReadApiAsync()).GetProperty("data");
|
|
Assert.Equal("Alex", person.GetProperty("name").GetString()); // trimmed
|
|
int id = person.GetProperty("id").GetInt32();
|
|
|
|
var list = (await (await client.GetAsync("/api/People")).ReadApiAsync()).GetProperty("data");
|
|
Assert.Single(list.EnumerateArray());
|
|
|
|
Assert.Equal(HttpStatusCode.OK, (await client.DeleteAsync($"/api/People/{id}")).StatusCode);
|
|
list = (await (await client.GetAsync("/api/People")).ReadApiAsync()).GetProperty("data");
|
|
Assert.Empty(list.EnumerateArray());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("0400000000")] // no country code
|
|
[InlineData("+61 400 000 001")] // spaces
|
|
[InlineData("+0123456789")] // leading zero after +
|
|
[InlineData("+12345")] // too short
|
|
[InlineData("")]
|
|
public async Task Invalid_phone_numbers_are_rejected(string phone)
|
|
{
|
|
using var app = new TestApp();
|
|
var client = await app.LoggedInClientAsync();
|
|
|
|
var response = await client.PostAsJsonAsync("/api/People", new { name = "Alex", phoneNumber = phone });
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
Assert.Contains("international format", (await response.ReadApiAsync()).GetProperty("error").GetString());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
public async Task A_name_is_required(string name)
|
|
{
|
|
using var app = new TestApp();
|
|
var client = await app.LoggedInClientAsync();
|
|
|
|
var response = await client.PostAsJsonAsync("/api/People", new { name, phoneNumber = "+61400000001" });
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Names_over_100_characters_are_rejected()
|
|
{
|
|
using var app = new TestApp();
|
|
var client = await app.LoggedInClientAsync();
|
|
|
|
var response = await client.PostAsJsonAsync("/api/People", new { name = new string('a', 101), phoneNumber = "+61400000001" });
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Deleting_a_missing_person_returns_404()
|
|
{
|
|
using var app = new TestApp();
|
|
var client = await app.LoggedInClientAsync();
|
|
|
|
var response = await client.DeleteAsync("/api/People/9999");
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
Assert.Equal("Person not found.", (await response.ReadApiAsync()).GetProperty("error").GetString());
|
|
}
|
|
}
|