Skip to content

Commit

Permalink
Auth fully working, not all tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
LokeSGJ committed Dec 9, 2024
1 parent 2164eed commit 6e67c50
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
15 changes: 8 additions & 7 deletions Giraf.IntegrationTests/Endpoints/CitizenEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task GetAllCitizens_ReturnsListOfCitizens()
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

TestAuthHandler.SetTestClaims(scope, seeder.Users["admin"]);
client.AttachClaimsToken(scope, seeder.Users["admin"]);

// Act
var response = await client.GetAsync("/citizens");
Expand Down Expand Up @@ -71,7 +71,7 @@ public async Task GetCitizenById_ReturnsCitizen_WhenCitizenExists()
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

TestAuthHandler.SetTestClaims(scope, seeder.Users["admin"]);
client.AttachClaimsToken(scope, seeder.Users["admin"]);

var citizenId = seeder.Citizens[0].Id;

Expand Down Expand Up @@ -117,7 +117,7 @@ public async Task UpdateCitizen_ReturnsOk_WhenCitizenExists()
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

TestAuthHandler.SetTestClaims(scope, seeder.Users["admin"]);
client.AttachClaimsToken(scope, seeder.Users["admin"]);

var citizenId = seeder.Citizens[0].Id;

Expand Down Expand Up @@ -169,6 +169,7 @@ public async Task AddCitizen_ReturnsOk_WhenOrganizationExists()
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

client.AttachClaimsToken(scope, seeder.Users["admin"]);

var createCitizenDto = new CreateCitizenDTO("New", "Citizen");
Expand Down Expand Up @@ -200,7 +201,7 @@ public async Task AddCitizen_ReturnsNotFound_WhenOrganizationDoesNotExist()
seeder.SeedUsers(scope.ServiceProvider.GetRequiredService<UserManager<GirafUser>>());
var client = factory.CreateClient();

TestAuthHandler.SetTestClaims(scope, seeder.Users["admin"]);
client.AttachClaimsToken(scope, seeder.Users["admin"]);

var createCitizenDto = new CreateCitizenDTO("New", "Citizen");

Expand All @@ -226,7 +227,7 @@ public async Task RemoveCitizen_ReturnsNoContent_WhenCitizenExistsInOrganization
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

TestAuthHandler.SetTestClaims(scope, seeder.Users["admin"]);
client.AttachClaimsToken(scope, seeder.Users["admin"]);

// Get the organization ID and citizen ID
var organizationId = seeder.Organizations.First().Id;
Expand Down Expand Up @@ -258,7 +259,7 @@ public async Task RemoveCitizen_ReturnsNotFound_WhenCitizenDoesNotExist()
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

TestAuthHandler.SetTestClaims(scope, seeder.Users["admin"]);
client.AttachClaimsToken(scope, seeder.Users["admin"]);

var organizationId = seeder.Organizations.First().Id;

Expand Down Expand Up @@ -287,7 +288,7 @@ public async Task RemoveCitizen_ReturnsBadRequest_WhenCitizenNotInOrganization()
);
var client = factory.CreateClient();

TestAuthHandler.SetTestClaims(scope, seeder.Users["owner"]);
client.AttachClaimsToken(scope, seeder.Users["admin"]);

var organizationId = seeder.Organizations[1].Id;
var citizenId = seeder.Citizens[0].Id;
Expand Down
14 changes: 13 additions & 1 deletion GirafAPI/Authorization/OrgAdminRequirement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GirafAPI.Data;
using GirafAPI.Entities.Organizations;
using GirafAPI.Entities.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -45,7 +46,18 @@ protected override async Task HandleRequirementAsync(

var httpContext = _httpContextAccessor.HttpContext;
var orgIdInUrl = httpContext.Request.RouteValues["orgId"];
var organization = await _dbContext.Organizations.FindAsync(orgIdInUrl);
Organization organization;

if (orgIdInUrl is string) // The test environment sends route values as strings
{
int orgId = Convert.ToInt32(orgIdInUrl);
organization = await _dbContext.Organizations.FindAsync(orgId);
}
else
{
organization = await _dbContext.Organizations.FindAsync(orgIdInUrl);
}

if (organization == null)
{
// Succeed and let the endpoint return NotFound
Expand Down
13 changes: 12 additions & 1 deletion GirafAPI/Authorization/OrgMemberRequirement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GirafAPI.Data;
using GirafAPI.Entities.Organizations;
using GirafAPI.Entities.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -45,8 +46,18 @@ protected override async Task HandleRequirementAsync(

var httpContext = _httpContextAccessor.HttpContext;
var orgIdInUrl = httpContext.Request.RouteValues["orgId"];
Organization organization;

if (orgIdInUrl is string) // The test environment sends route values as strings
{
int orgId = Convert.ToInt32(orgIdInUrl);
organization = await _dbContext.Organizations.FindAsync(orgId);
}
else
{
organization = await _dbContext.Organizations.FindAsync(orgIdInUrl);
}

var organization = await _dbContext.Organizations.FindAsync(orgIdInUrl);
if (organization == null)
{
// Succeed and let the endpoint return NotFound
Expand Down
26 changes: 25 additions & 1 deletion GirafAPI/Authorization/OrgOwnerRequirement.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using GirafAPI.Data;
using GirafAPI.Entities.Organizations;
using GirafAPI.Entities.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand All @@ -13,11 +15,15 @@ public class OrgOwnerAuthorizationHandler : AuthorizationHandler<OrgOwnerRequire
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly UserManager<GirafUser> _userManager;
private readonly GirafDbContext _dbContext;

public OrgOwnerAuthorizationHandler(IHttpContextAccessor httpContextAccessor, UserManager<GirafUser> userManager)
public OrgOwnerAuthorizationHandler(IHttpContextAccessor httpContextAccessor,
UserManager<GirafUser> userManager,
GirafDbContext dbContext)
{
_httpContextAccessor = httpContextAccessor;
_userManager = userManager;
_dbContext = dbContext;
}

protected override async Task HandleRequirementAsync(
Expand All @@ -43,6 +49,24 @@ protected override async Task HandleRequirementAsync(

var httpContext = _httpContextAccessor.HttpContext;
var orgIdInUrl = httpContext.Request.RouteValues["orgId"];
Organization organization;

if (orgIdInUrl is string) // The test environment sends route values as strings
{
int orgId = Convert.ToInt32(orgIdInUrl);
organization = await _dbContext.Organizations.FindAsync(orgId);
}
else
{
organization = await _dbContext.Organizations.FindAsync(orgIdInUrl);
}

if (organization == null)
{
// Succeed and let the endpoint return NotFound
context.Succeed(requirement);
return;
}

if (orgIds.Contains(orgIdInUrl))
{
Expand Down

0 comments on commit 6e67c50

Please sign in to comment.