Fix Task 5 review issues: per-test ApiFactory, drop dead RoleManager param + EF Core using
This commit is contained in:
@@ -5,14 +5,13 @@ using Xunit;
|
||||
|
||||
namespace backend.tests;
|
||||
|
||||
public class AuthTests(ApiFactory factory) : IClassFixture<ApiFactory>
|
||||
public class AuthTests
|
||||
{
|
||||
private readonly ApiFactory _factory = factory;
|
||||
|
||||
[Fact]
|
||||
public async Task Register_then_login_then_me_returns_user()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
using var factory = new ApiFactory();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
var reg = await client.PostAsJsonAsync("/api/auth/register",
|
||||
new { email = "first@example.com", password = "Passw0rd!" });
|
||||
@@ -30,7 +29,8 @@ public class AuthTests(ApiFactory factory) : IClassFixture<ApiFactory>
|
||||
[Fact]
|
||||
public async Task Me_without_login_returns_401()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
using var factory = new ApiFactory();
|
||||
var client = factory.CreateClient();
|
||||
var res = await client.GetAsync("/api/auth/me");
|
||||
res.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
|
||||
}
|
||||
@@ -38,11 +38,12 @@ public class AuthTests(ApiFactory factory) : IClassFixture<ApiFactory>
|
||||
[Fact]
|
||||
public async Task First_user_is_admin()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
using var factory = new ApiFactory();
|
||||
var client = factory.CreateClient();
|
||||
await client.PostAsJsonAsync("/api/auth/register",
|
||||
new { email = "first@example.com", password = "Passw0rd!" });
|
||||
new { email = "admin@example.com", password = "Passw0rd!" });
|
||||
await client.PostAsJsonAsync("/api/auth/login",
|
||||
new { email = "first@example.com", password = "Passw0rd!" });
|
||||
new { email = "admin@example.com", password = "Passw0rd!" });
|
||||
|
||||
var me = await client.GetAsync("/api/auth/me");
|
||||
(await me.Content.ReadAsStringAsync()).Should().Contain("\"isAdmin\":true");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace backend.Auth;
|
||||
|
||||
@@ -15,8 +14,7 @@ public static class AuthEndpoints
|
||||
|
||||
grp.MapPost("/register", async (
|
||||
[FromBody] RegisterRequest req,
|
||||
UserManager<ApplicationUser> users,
|
||||
RoleManager<IdentityRole<Guid>> roles) =>
|
||||
UserManager<ApplicationUser> users) =>
|
||||
{
|
||||
var user = new ApplicationUser { UserName = req.Email, Email = req.Email };
|
||||
var create = await users.CreateAsync(user, req.Password);
|
||||
|
||||
Reference in New Issue
Block a user