|
| 1 | +using System.Security.Claims; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Api.Database.Context; |
| 4 | +using Api.Database.Models; |
| 5 | +using Api.Services; |
| 6 | +using Api.Test.Database; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Testcontainers.PostgreSql; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Api.Test.Services; |
| 13 | + |
| 14 | +public class AccessRoleServiceTest : IAsyncLifetime |
| 15 | +{ |
| 16 | + public required DatabaseUtilities DatabaseUtilities; |
| 17 | + public required PostgreSqlContainer Container; |
| 18 | + public required IAccessRoleService AccessRoleService; |
| 19 | + public required FlotillaDbContext Context; |
| 20 | + |
| 21 | + public async Task InitializeAsync() |
| 22 | + { |
| 23 | + (Container, string cs, var _) = await TestSetupHelpers.ConfigurePostgreSqlDatabase(); |
| 24 | + |
| 25 | + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( |
| 26 | + postgreSqlConnectionString: cs |
| 27 | + ); |
| 28 | + var sp = TestSetupHelpers.ConfigureServiceProvider(factory); |
| 29 | + |
| 30 | + Context = TestSetupHelpers.ConfigurePostgreSqlContext(cs); |
| 31 | + DatabaseUtilities = new DatabaseUtilities(Context); |
| 32 | + AccessRoleService = sp.GetRequiredService<IAccessRoleService>(); |
| 33 | + |
| 34 | + var http = sp.GetRequiredService<IHttpContextAccessor>(); |
| 35 | + http.HttpContext = new DefaultHttpContext |
| 36 | + { |
| 37 | + User = new ClaimsPrincipal( |
| 38 | + new ClaimsIdentity([new Claim(ClaimTypes.Role, "Role.Admin")], "TestAuth") |
| 39 | + ), |
| 40 | + }; |
| 41 | + |
| 42 | + var installationHUA = await DatabaseUtilities.NewInstallation("HUA"); |
| 43 | + await AccessRoleService.Create( |
| 44 | + installationHUA, |
| 45 | + "Role.ReadOnly.HUA", |
| 46 | + RoleAccessLevel.READ_ONLY |
| 47 | + ); |
| 48 | + await AccessRoleService.Create(installationHUA, "Role.User.HUA", RoleAccessLevel.USER); |
| 49 | + } |
| 50 | + |
| 51 | + public Task DisposeAsync() => Task.CompletedTask; |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task GetAllowedInstallationCodes_ReadMode_WithReadOnlyRole_ReturnsHUA() |
| 55 | + { |
| 56 | + var identity = new ClaimsIdentity( |
| 57 | + [new Claim(ClaimTypes.Role, "Role.ReadOnly.HUA")], |
| 58 | + "TestAuth" |
| 59 | + ); |
| 60 | + |
| 61 | + var user = new ClaimsPrincipal(identity); |
| 62 | + |
| 63 | + var result = await AccessRoleService.GetAllowedInstallationCodes(user, AccessMode.Read); |
| 64 | + |
| 65 | + Assert.Single(result); |
| 66 | + Assert.Equal("HUA", result[0]); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task GetAllowedInstallationCodes_ReadMode_WithUserRole_ReturnsHUA() |
| 71 | + { |
| 72 | + var identity = new ClaimsIdentity( |
| 73 | + [new Claim(ClaimTypes.Role, "Role.User.HUA")], |
| 74 | + "TestAuth" |
| 75 | + ); |
| 76 | + |
| 77 | + var user = new ClaimsPrincipal(identity); |
| 78 | + |
| 79 | + var result = await AccessRoleService.GetAllowedInstallationCodes(user, AccessMode.Read); |
| 80 | + |
| 81 | + Assert.Single(result); |
| 82 | + Assert.Equal("HUA", result[0]); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task GetAllowedInstallationCodes_WriteMode_WithUserRole_ReturnsHUA() |
| 87 | + { |
| 88 | + var identity = new ClaimsIdentity( |
| 89 | + [new Claim(ClaimTypes.Role, "Role.User.HUA")], |
| 90 | + "TestAuth" |
| 91 | + ); |
| 92 | + |
| 93 | + var user = new ClaimsPrincipal(identity); |
| 94 | + |
| 95 | + var result = await AccessRoleService.GetAllowedInstallationCodes(user, AccessMode.Write); |
| 96 | + |
| 97 | + Assert.Single(result); |
| 98 | + Assert.Equal("HUA", result[0]); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public async Task GetAllowedInstallationCodes_WriteMode_WithReadOnlyRole_ReturnsEmpty() |
| 103 | + { |
| 104 | + var identity = new ClaimsIdentity( |
| 105 | + [new Claim(ClaimTypes.Role, "Role.ReadOnly.HUA")], |
| 106 | + "TestAuth" |
| 107 | + ); |
| 108 | + var user = new ClaimsPrincipal(identity); |
| 109 | + |
| 110 | + var result = await AccessRoleService.GetAllowedInstallationCodes(user, AccessMode.Write); |
| 111 | + |
| 112 | + Assert.Empty(result); |
| 113 | + } |
| 114 | +} |
0 commit comments