Skip to content

Commit

Permalink
use roles instead of claims
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed May 24, 2024
1 parent 8b3fcc4 commit 631324d
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task CreateEmployee_IsAdded()
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesClaim).CreateEmployee(createEmployeeRequest);
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesRole).CreateEmployee(createEmployeeRequest);

//Assert
await response.AssertStatusCode(HttpStatusCode.Created);
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task CreateEmployee_InvalidRequest_ReturnsBadRequest(TestScenario<(
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesClaim).CreateEmployee(createEmployeeRequest);
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesRole).CreateEmployee(createEmployeeRequest);

//Assert
await response.AssertBadRequest(scenario.Input.expectedErrors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task DeleteEmployeeById_IsDeleted()
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesClaim).DeleteEmployeeById(employee.Id);
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesRole).DeleteEmployeeById(employee.Id);

//Assert
await response.AssertStatusCode(HttpStatusCode.NoContent);
Expand All @@ -29,7 +29,7 @@ public async Task DeleteEmployeeById_DoesNotExist_ReturnsNotFound()
var id = Guid.NewGuid();

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesClaim).DeleteEmployeeById(id);
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesRole).DeleteEmployeeById(id);

//Assert
await response.AssertStatusCode(HttpStatusCode.NotFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task GetEmployeeById_ReturnsExpectedEmployee()
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.ReadEmployeesClaim).GetEmployeeById(employee.Id);
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.ReadEmployeesRole).GetEmployeeById(employee.Id);

//Assert
await response.AssertStatusCode(HttpStatusCode.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public interface IEmployeeApiClient
[Get("/employees/{id}")]
Task<HttpResponseMessage> GetEmployeeById(Guid id);

[Get("/employees")]
Task<HttpResponseMessage> GetEmployees();

[Post("/employees")]
Task<HttpResponseMessage> CreateEmployee(CreateEmployeeRequest createEmployeeRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task UpdateEmployeeById_IsUpdated()
};

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesClaim).UpdateEmployeeById(employee.Id, updateEmployeeRequest);
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesRole).UpdateEmployeeById(employee.Id, updateEmployeeRequest);

//Assert
await response.AssertStatusCode(HttpStatusCode.NoContent);
Expand Down Expand Up @@ -47,7 +47,7 @@ public async Task UpdateEmployeeById_DoesNotExist_ReturnsNotFound()
};

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesClaim).UpdateEmployeeById(employee.Id, updateEmployeeRequest);
var response = await Sut.CreateClientFor<IEmployeeApiClient>(ClaimConstants.WriteEmployeesRole).UpdateEmployeeById(employee.Id, updateEmployeeRequest);

//Assert
await response.AssertStatusCode(HttpStatusCode.NotFound);
Expand Down
4 changes: 2 additions & 2 deletions CleanAspCore.Api.Tests/TestSetup/ClaimConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace CleanAspCore.Api.Tests.TestSetup;

public static class ClaimConstants
{
public static readonly Claim ReadEmployeesClaim = new("reademployees", string.Empty);
public static readonly Claim WriteEmployeesClaim = new("writeemployees", string.Empty);
public static readonly Claim ReadEmployeesRole = new(ClaimTypes.Role, "reademployees");
public static readonly Claim WriteEmployeesRole = new(ClaimTypes.Role, "writeemployees");
}
4 changes: 2 additions & 2 deletions CleanAspCore/Endpoints/Employees/EmployeeEndpointConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal static class EmployeeEndpointConfig
internal static void AddEmployeeServices(this WebApplicationBuilder builder)
{
builder.Services.AddAuthorizationBuilder()
.AddPolicy(ReadEmployeesPolicy, policy => policy.RequireClaim("reademployees"))
.AddPolicy(WriteEmployeesPolicy, policy => policy.RequireClaim("writeemployees"));
.AddPolicy(ReadEmployeesPolicy, policy => policy.RequireRole("reademployees"))
.AddPolicy(WriteEmployeesPolicy, policy => policy.RequireRole("writeemployees"));
}

internal static void AddEmployeesRoutes(this IEndpointRouteBuilder host)
Expand Down
5 changes: 2 additions & 3 deletions CleanAspCore/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
"Schemes": {
"Bearer": {
"ValidAudiences": [
"https://localhost:7162",
"http://localhost:5015"
"https://localhost:7162"
],
"ValidIssuer": "dotnet-user-jwts"
}
}
}
}
}
5 changes: 4 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Clean ASP.NET api template

This is a template repository showing how one can implement a clean api with ASP.NET using minimal apis.

Some 'features' in this template:

- Vertical Slice architecture (grouping based on features instead of technical layers)
- An easy to use and fast to run integration tests setup with the only dependency being docker with the use of [TestExamplesDotnet](https://github.com/Barsonax/TestExamplesDotnet)
- Authentication and authorization using jwt tokens (also in the tests)
Expand All @@ -20,8 +22,9 @@ dotnet test
1. First generate a jwt that you can use for local testing:

```cmd
dotnet user-jwts create --claim "reademployees=" --claim "writeemployees="
dotnet user-jwts create --role "reademployees" --role "writeemployees"
```

NOTE: The jobs and department endpoints only require authentication but the employee endpoints require that you have the correct claims in the jwt token.

2. Run the database using the provided docker-compose.yaml.
Expand Down

0 comments on commit 631324d

Please sign in to comment.