52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using FluentAssertions;
|
|
using Xunit;
|
|
|
|
namespace backend.tests;
|
|
|
|
public class AuthTests
|
|
{
|
|
[Fact]
|
|
public async Task Register_then_login_then_me_returns_user()
|
|
{
|
|
using var factory = new ApiFactory();
|
|
var client = factory.CreateClient();
|
|
|
|
var reg = await client.PostAsJsonAsync("/api/auth/register",
|
|
new { email = "first@example.com", password = "Passw0rd!" });
|
|
reg.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var login = await client.PostAsJsonAsync("/api/auth/login",
|
|
new { email = "first@example.com", password = "Passw0rd!" });
|
|
login.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var me = await client.GetAsync("/api/auth/me");
|
|
me.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
(await me.Content.ReadAsStringAsync()).Should().Contain("first@example.com");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Me_without_login_returns_401()
|
|
{
|
|
using var factory = new ApiFactory();
|
|
var client = factory.CreateClient();
|
|
var res = await client.GetAsync("/api/auth/me");
|
|
res.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task First_user_is_admin()
|
|
{
|
|
using var factory = new ApiFactory();
|
|
var client = factory.CreateClient();
|
|
await client.PostAsJsonAsync("/api/auth/register",
|
|
new { email = "admin@example.com", password = "Passw0rd!" });
|
|
await client.PostAsJsonAsync("/api/auth/login",
|
|
new { email = "admin@example.com", password = "Passw0rd!" });
|
|
|
|
var me = await client.GetAsync("/api/auth/me");
|
|
(await me.Content.ReadAsStringAsync()).Should().Contain("\"isAdmin\":true");
|
|
}
|
|
}
|