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
102 lines
3.8 KiB
C#
102 lines
3.8 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
|
|
namespace YaleAccess.Tests;
|
|
|
|
public class AuthenticationTests
|
|
{
|
|
[Fact]
|
|
public async Task Protected_endpoints_return_401_without_a_login()
|
|
{
|
|
using var app = new TestApp();
|
|
var client = app.CreateClient();
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, (await client.GetAsync("/api/People")).StatusCode);
|
|
Assert.Equal(HttpStatusCode.Unauthorized, (await client.GetAsync("/api/Yale/codes")).StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Health_endpoint_is_anonymous()
|
|
{
|
|
using var app = new TestApp();
|
|
var response = await app.CreateClient().GetAsync("/api/Health");
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Wrong_password_is_rejected_and_sets_no_cookie()
|
|
{
|
|
using var app = new TestApp();
|
|
var response = await app.CreateClient().PostAsJsonAsync("/api/Authentication/login", "wrong");
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
Assert.False(response.Headers.Contains("Set-Cookie"));
|
|
var body = await response.ReadApiAsync();
|
|
Assert.False(body.GetProperty("success").GetBoolean());
|
|
Assert.Equal("Incorrect password.", body.GetProperty("error").GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Correct_password_sets_a_strict_httponly_cookie_and_grants_access()
|
|
{
|
|
using var app = new TestApp();
|
|
var client = app.CreateClient();
|
|
|
|
var login = await client.PostAsJsonAsync("/api/Authentication/login", TestConstants.Password);
|
|
Assert.Equal(HttpStatusCode.OK, login.StatusCode);
|
|
|
|
var cookie = Assert.Single(login.Headers.GetValues("Set-Cookie"));
|
|
Assert.Contains("samesite=strict", cookie, StringComparison.OrdinalIgnoreCase);
|
|
Assert.Contains("httponly", cookie, StringComparison.OrdinalIgnoreCase);
|
|
|
|
Assert.Equal(HttpStatusCode.OK, (await client.GetAsync("/api/People")).StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Passwords_containing_quotes_and_backslashes_work()
|
|
{
|
|
const string password = "pa\"ss\\word";
|
|
using var app = new TestApp(password);
|
|
var response = await app.CreateClient().PostAsJsonAsync("/api/Authentication/login", password);
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Logout_ends_the_session()
|
|
{
|
|
using var app = new TestApp();
|
|
var client = await app.LoggedInClientAsync();
|
|
|
|
Assert.Equal(HttpStatusCode.OK, (await client.PostAsync("/api/Authentication/logout", null)).StatusCode);
|
|
Assert.Equal(HttpStatusCode.Unauthorized, (await client.GetAsync("/api/People")).StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Login_is_refused_when_no_password_is_configured()
|
|
{
|
|
using var app = new TestApp(password: "");
|
|
var response = await app.CreateClient().PostAsJsonAsync("/api/Authentication/login", "");
|
|
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Login_attempts_are_rate_limited()
|
|
{
|
|
using var app = new TestApp();
|
|
var client = app.CreateClient();
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
{
|
|
var attempt = await client.PostAsJsonAsync("/api/Authentication/login", "wrong");
|
|
Assert.Equal(HttpStatusCode.Unauthorized, attempt.StatusCode);
|
|
}
|
|
|
|
// Even the right password is blocked once the limit is hit
|
|
var limited = await client.PostAsJsonAsync("/api/Authentication/login", TestConstants.Password);
|
|
Assert.Equal(HttpStatusCode.TooManyRequests, limited.StatusCode);
|
|
var body = await limited.ReadApiAsync();
|
|
Assert.False(body.GetProperty("success").GetBoolean());
|
|
Assert.Contains("Too many", body.GetProperty("error").GetString());
|
|
}
|
|
}
|