98 lines
6.1 KiB
C#
98 lines
6.1 KiB
C#
using System.Net;
|
|||
|
|
using System.Net.Http.Json;
|
||
|
|
using System.Text;
|
||
|
|
using Helpdesk.Application.Auth;
|
||
|
|
using Helpdesk.Application.Catalogs;
|
||
|
|
using Helpdesk.Application.Common;
|
||
|
|
using Helpdesk.Application.Users;
|
||
|
|
using Helpdesk.Infrastructure.Persistence;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
|
using static Helpdesk.IntegrationTests.BackendFixture;
|
||
|
|
|
||
|
|
namespace Helpdesk.IntegrationTests;
|
||
|
|
|
||
|
|
public sealed class AdministrationTests(BackendFixture fixture) : IClassFixture<BackendFixture>
|
||
|
|
{
|
||
|
|
[Theory]
|
||
|
|
[InlineData("/api/v1/issue-categories")]
|
||
|
|
[InlineData("/api/v1/proposal-categories")]
|
||
|
|
public async Task AdministratorsCanManageCategories(string route)
|
||
|
|
{
|
||
|
|
using var anonymous = fixture.CreateClient();
|
||
|
|
using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken);
|
||
|
|
var user = await fixture.RegisterAsync(anonymous);
|
||
|
|
using var member = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
|
||
|
|
var request = new SaveCategoryRequest("Testovací kategorie", "Popis");
|
||
|
|
await ExpectAsync(member.PostAsJsonAsync(route, request, Cancellation), HttpStatusCode.Forbidden);
|
||
|
|
var category = await ReadAsync<CategoryDto>(admin.PostAsJsonAsync(route, request, Cancellation), HttpStatusCode.Created);
|
||
|
|
await ExpectAsync(admin.PostAsJsonAsync(route, request, Cancellation), HttpStatusCode.Conflict);
|
||
|
|
var path = $"{route}/{category.Id}";
|
||
|
|
var edited = await ReadAsync<CategoryDto>(admin.PutAsJsonAsync(path, request with { Name = "Upravená kategorie" }, Cancellation));
|
||
|
|
Assert.Equal("Upravená kategorie", edited.Name);
|
||
|
|
await ExpectAsync(admin.DeleteAsync(path, Cancellation), HttpStatusCode.NoContent);
|
||
|
|
|
||
|
|
var active = await ReadAsync<CategoryDto[]>(member.GetAsync(route + "?includeInactive=true", Cancellation));
|
||
|
|
var all = await ReadAsync<CategoryDto[]>(admin.GetAsync(route + "?includeInactive=true", Cancellation));
|
||
|
|
Assert.DoesNotContain(active, item => item.Id == category.Id);
|
||
|
|
Assert.False(Assert.Single(all, item => item.Id == category.Id).IsActive);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task BuildingCodesAreUniqueAndInactiveBuildingsStayAvailableToTheAdministrator()
|
||
|
|
{
|
||
|
|
using var anonymous = fixture.CreateClient();
|
||
|
|
using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken);
|
||
|
|
const string route = "/api/v1/buildings";
|
||
|
|
var request = new SaveBuildingRequest("TEST", "Testovací budova", null);
|
||
|
|
var building = await ReadAsync<BuildingDto>(admin.PostAsJsonAsync(route, request, Cancellation), HttpStatusCode.Created);
|
||
|
|
await ExpectAsync(admin.PostAsJsonAsync(route, request with { Code = " TEST " }, Cancellation), HttpStatusCode.Conflict);
|
||
|
|
var path = $"{route}/{building.Id}";
|
||
|
|
var edited = await ReadAsync<BuildingDto>(admin.PutAsJsonAsync(path, request with { Address = "Náměstí 1" }, Cancellation));
|
||
|
|
Assert.Equal("Náměstí 1", edited.Address);
|
||
|
|
await ExpectAsync(admin.DeleteAsync(path, Cancellation), HttpStatusCode.NoContent);
|
||
|
|
var active = await ReadAsync<BuildingDto[]>(admin.GetAsync(route, Cancellation));
|
||
|
|
var all = await ReadAsync<BuildingDto[]>(admin.GetAsync(route + "?includeInactive=true", Cancellation));
|
||
|
|
Assert.DoesNotContain(active, item => item.Id == building.Id);
|
||
|
|
Assert.False(Assert.Single(all, item => item.Id == building.Id).IsActive);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task ProfileDataRoundTripsAndDeactivationRevokesTheSession()
|
||
|
|
{
|
||
|
|
using var anonymous = fixture.CreateClient();
|
||
|
|
using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken);
|
||
|
|
var user = await fixture.RegisterAsync(anonymous);
|
||
|
|
using var member = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
|
||
|
|
const string profile = "/api/v1/users/me";
|
||
|
|
const string phone = "+420 123 456 789";
|
||
|
|
var request = new UpdateProfileRequest("Jana", "Nováková", phone, false);
|
||
|
|
var validation = await ReadAsync<ValidationProblemDetails>(member.PutAsJsonAsync(profile,
|
||
|
|
request with { Phone = "invalid" }, Cancellation), HttpStatusCode.BadRequest);
|
||
|
|
Assert.Contains("phone", validation.Errors.Keys);
|
||
|
|
var updated = await ReadAsync<UserDto>(member.PutAsJsonAsync(profile, request, Cancellation));
|
||
|
|
Assert.Equal(updated, await ReadAsync<UserDto>(member.GetAsync(profile, Cancellation)));
|
||
|
|
Assert.Equal((request.FirstName, request.LastName, phone, false),
|
||
|
|
(updated.FirstName, updated.LastName, updated.Phone, updated.EmailNotificationsEnabled));
|
||
|
|
|
||
|
|
await using var scope = fixture.Services.CreateAsyncScope();
|
||
|
|
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
||
|
|
var encrypted = await db.Database.SqlQuery<byte[]>($"SELECT phone_encrypted AS Value FROM app_user WHERE id = {user.Id}")
|
||
|
|
.SingleAsync(Cancellation);
|
||
|
|
Assert.NotEmpty(encrypted);
|
||
|
|
Assert.False(Encoding.UTF8.GetBytes(phone).SequenceEqual(encrypted));
|
||
|
|
|
||
|
|
var search = $"/api/v1/admin/users?q={Uri.EscapeDataString(user.Email)}&role=STUDENT&pageSize=1";
|
||
|
|
await ExpectAsync(member.GetAsync(search, Cancellation), HttpStatusCode.Forbidden);
|
||
|
|
var page = await ReadAsync<PagedResult<AdminUserDto>>(admin.GetAsync(search, Cancellation));
|
||
|
|
Assert.Equal(user.Id, Assert.Single(page.Items).Id);
|
||
|
|
var activation = $"/api/v1/admin/users/{user.Id}/active";
|
||
|
|
await ExpectAsync(admin.PutAsJsonAsync(activation, new SetActiveRequest(false), Cancellation), HttpStatusCode.NoContent);
|
||
|
|
await ExpectAsync(member.GetAsync(profile, Cancellation), HttpStatusCode.Unauthorized);
|
||
|
|
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest(user.Email, Password), Cancellation),
|
||
|
|
HttpStatusCode.Forbidden);
|
||
|
|
await ExpectAsync(admin.PutAsJsonAsync(activation, new SetActiveRequest(true), Cancellation), HttpStatusCode.NoContent);
|
||
|
|
Assert.Equal(user.Id, (await LoginAsync(anonymous, user.Email)).User.Id);
|
||
|
|
}
|
||
|
|
}
|