Skip to content

Commit 303feae

Browse files
Added Role Management endpoint and Client
1 parent 8fb7066 commit 303feae

22 files changed

Lines changed: 1179 additions & 62 deletions

File tree

SecurityService.Client/ISecurityServiceClient.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ Task<CreateApiResourceResponse> CreateApiResource(CreateApiResourceRequest creat
3333
Task<CreateClientResponse> CreateClient(CreateClientRequest createClientRequest,
3434
CancellationToken cancellationToken);
3535

36+
/// <summary>
37+
/// Creates the role.
38+
/// </summary>
39+
/// <param name="createRoleRequest">The create role request.</param>
40+
/// <param name="cancellationToken">The cancellation token.</param>
41+
/// <returns></returns>
42+
Task<CreateRoleResponse> CreateRole(CreateRoleRequest createRoleRequest,
43+
CancellationToken cancellationToken);
44+
3645
/// <summary>
3746
/// Creates the user.
3847
/// </summary>
@@ -74,6 +83,22 @@ Task<ClientDetails> GetClient(String clientId,
7483
/// <returns></returns>
7584
Task<List<ClientDetails>> GetClients(CancellationToken cancellationToken);
7685

86+
/// <summary>
87+
/// Gets the role.
88+
/// </summary>
89+
/// <param name="roleId">The role identifier.</param>
90+
/// <param name="cancellationToken">The cancellation token.</param>
91+
/// <returns></returns>
92+
Task<RoleDetails> GetRole(Guid roleId,
93+
CancellationToken cancellationToken);
94+
95+
/// <summary>
96+
/// Gets the roles.
97+
/// </summary>
98+
/// <param name="cancellationToken">The cancellation token.</param>
99+
/// <returns></returns>
100+
Task<List<RoleDetails>> GetRoles(CancellationToken cancellationToken);
101+
77102
/// <summary>
78103
/// Gets the token.
79104
/// </summary>

SecurityService.Client/SecurityServiceClient.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,47 @@ public async Task<CreateClientResponse> CreateClient(CreateClientRequest createC
130130
return response;
131131
}
132132

133+
/// <summary>
134+
/// Creates the role.
135+
/// </summary>
136+
/// <param name="createRoleRequest">The create role request.</param>
137+
/// <param name="cancellationToken">The cancellation token.</param>
138+
/// <returns></returns>
139+
public async Task<CreateRoleResponse> CreateRole(CreateRoleRequest createRoleRequest,
140+
CancellationToken cancellationToken)
141+
{
142+
CreateRoleResponse response = null;
143+
String requestUri = $"{this.BaseAddress}/api/roles";
144+
145+
try
146+
{
147+
String requestSerialised = JsonConvert.SerializeObject(createRoleRequest);
148+
149+
StringContent httpContent = new StringContent(requestSerialised, Encoding.UTF8, "application/json");
150+
151+
// Add the access token to the client headers
152+
//this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
153+
154+
// Make the Http Call here
155+
HttpResponseMessage httpResponse = await this.HttpClient.PostAsync(requestUri, httpContent, cancellationToken);
156+
157+
// Process the response
158+
String content = await this.HandleResponse(httpResponse, cancellationToken);
159+
160+
// call was successful so now deserialise the body to the response object
161+
response = JsonConvert.DeserializeObject<CreateRoleResponse>(content);
162+
}
163+
catch(Exception ex)
164+
{
165+
// An exception has occurred, add some additional information to the message
166+
Exception exception = new Exception($"Error creating role {createRoleRequest.RoleName}.", ex);
167+
168+
throw exception;
169+
}
170+
171+
return response;
172+
}
173+
133174
/// <summary>
134175
/// Creates the user.
135176
/// </summary>
@@ -315,6 +356,78 @@ public async Task<List<ClientDetails>> GetClients(CancellationToken cancellation
315356
return response;
316357
}
317358

359+
/// <summary>
360+
/// Gets the role.
361+
/// </summary>
362+
/// <param name="roleId">The role identifier.</param>
363+
/// <param name="cancellationToken">The cancellation token.</param>
364+
/// <returns></returns>
365+
public async Task<RoleDetails> GetRole(Guid roleId,
366+
CancellationToken cancellationToken)
367+
{
368+
RoleDetails response = null;
369+
String requestUri = $"{this.BaseAddress}/api/roles/{roleId}";
370+
371+
try
372+
{
373+
// Add the access token to the client headers
374+
//this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
375+
376+
// Make the Http Call here
377+
HttpResponseMessage httpResponse = await this.HttpClient.GetAsync(requestUri, cancellationToken);
378+
379+
// Process the response
380+
String content = await this.HandleResponse(httpResponse, cancellationToken);
381+
382+
// call was successful so now deserialise the body to the response object
383+
response = JsonConvert.DeserializeObject<RoleDetails>(content);
384+
}
385+
catch(Exception ex)
386+
{
387+
// An exception has occurred, add some additional information to the message
388+
Exception exception = new Exception($"Error get role with Id {roleId}.", ex);
389+
390+
throw exception;
391+
}
392+
393+
return response;
394+
}
395+
396+
/// <summary>
397+
/// Gets the roles.
398+
/// </summary>
399+
/// <param name="cancellationToken">The cancellation token.</param>
400+
/// <returns></returns>
401+
public async Task<List<RoleDetails>> GetRoles(CancellationToken cancellationToken)
402+
{
403+
List<RoleDetails> response = null;
404+
String requestUri = $"{this.BaseAddress}/api/roles";
405+
406+
try
407+
{
408+
// Add the access token to the client headers
409+
//this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
410+
411+
// Make the Http Call here
412+
HttpResponseMessage httpResponse = await this.HttpClient.GetAsync(requestUri, cancellationToken);
413+
414+
// Process the response
415+
String content = await this.HandleResponse(httpResponse, cancellationToken);
416+
417+
// call was successful so now deserialise the body to the response object
418+
response = JsonConvert.DeserializeObject<List<RoleDetails>>(content);
419+
}
420+
catch(Exception ex)
421+
{
422+
// An exception has occurred, add some additional information to the message
423+
Exception exception = new Exception("Error get roles.", ex);
424+
425+
throw exception;
426+
}
427+
428+
return response;
429+
}
430+
318431
/// <summary>
319432
/// Gets the token.
320433
/// </summary>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SecurityService.DataTransferObjects.Requests
6+
{
7+
using System.ComponentModel.DataAnnotations;
8+
using Newtonsoft.Json;
9+
10+
public class CreateRoleRequest
11+
{
12+
/// <summary>
13+
/// Gets or sets the name of the role.
14+
/// </summary>
15+
/// <value>
16+
/// The name of the role.
17+
/// </value>
18+
[Required]
19+
[JsonProperty("role_name")]
20+
public String RoleName { get; set; }
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace SecurityService.DataTransferObjects.Responses
2+
{
3+
using System;
4+
using Newtonsoft.Json;
5+
6+
public class CreateRoleResponse
7+
{
8+
/// <summary>
9+
/// Gets or sets the role identifier.
10+
/// </summary>
11+
/// <value>
12+
/// The role identifier.
13+
/// </value>
14+
[JsonProperty("role_id")]
15+
public Guid RoleId { get; set; }
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace SecurityService.DataTransferObjects.Responses
2+
{
3+
using System;
4+
using System.Diagnostics.CodeAnalysis;
5+
6+
[ExcludeFromCodeCoverage]
7+
public class RoleDetails
8+
{
9+
/// <summary>
10+
/// Gets or sets the role identifier.
11+
/// </summary>
12+
/// <value>
13+
/// The role identifier.
14+
/// </value>
15+
public Guid RoleId { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the name of the role.
19+
/// </summary>
20+
/// <value>
21+
/// The name of the role.
22+
/// </value>
23+
public String RoleName { get; set; }
24+
}
25+
}

SecurityService.DataTransferObjects/Responses/TokenResponse.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace SecurityService.DataTransferObjects.Responses
66
{
7+
using System.Diagnostics.CodeAnalysis;
78
using Newtonsoft.Json;
89

10+
[ExcludeFromCodeCoverage]
911
public class TokenResponse
1012
{
1113
/// <summary>

SecurityService.IntegrationTests/Common/TestingContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class TestingContext
88
public DockerHelper DockerHelper { get; set; }
99

1010
public Dictionary<String, Guid> Users;
11+
public Dictionary<String, Guid> Roles;
1112

1213
public List<String> Clients;
1314

@@ -16,6 +17,7 @@ public class TestingContext
1617
public TestingContext()
1718
{
1819
this.Users = new Dictionary<String, Guid>();
20+
this.Roles= new Dictionary<String, Guid>();
1921
this.Clients=new List<String>();
2022
this.ApiResources=new List<String>();
2123
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@base @roles
2+
Feature: Roles
3+
4+
Scenario: Create Role
5+
Given I create the following roles
6+
| Role Name |
7+
| TestRole1 |
8+
9+
Scenario: Get Role
10+
Given I create the following roles
11+
| Role Name |
12+
| TestRole1 |
13+
When I get the role with name 'TestRole1' the role details are returned as follows
14+
| Role Name |
15+
| TestRole1 |
16+
17+
@PRTest
18+
Scenario: Get Roles
19+
Given I create the following roles
20+
| Role Name |
21+
| TestRole1 |
22+
| TestRole2 |
23+
| TestRole3 |
24+
When I get the roles 3 roles details are returned as follows
25+
| Role Name |
26+
| TestRole1 |
27+
| TestRole2 |
28+
| TestRole3 |

0 commit comments

Comments
 (0)