88using CodeBeam . UltimateAuth . Core . Contracts ;
99using CodeBeam . UltimateAuth . Core . Defaults ;
1010using CodeBeam . UltimateAuth . Core . Domain ;
11+ using CodeBeam . UltimateAuth . Core . MultiTenancy ;
1112using FluentAssertions ;
1213using Microsoft . Extensions . Options ;
1314using Moq ;
@@ -145,4 +146,176 @@ public async Task QueryRoles_Should_Return_Data()
145146 result . IsSuccess . Should ( ) . BeTrue ( ) ;
146147 result . Value . Should ( ) . NotBeNull ( ) ;
147148 }
148- }
149+
150+ [ Fact ]
151+ public async Task GetMyRoles_Should_Call_Correct_Endpoint_And_Return_Data ( )
152+ {
153+ var userKey = UserKey . FromString ( "user-1" ) ;
154+
155+ var response = new UserRolesResponse
156+ {
157+ UserKey = userKey ,
158+ Roles = new PagedResult < UserRoleInfo > (
159+ new List < UserRoleInfo >
160+ {
161+ new UserRoleInfo
162+ {
163+ Tenant = TenantKeys . Single ,
164+ UserKey = userKey ,
165+ RoleId = RoleId . From ( Guid . NewGuid ( ) ) ,
166+ Name = "admin" ,
167+ AssignedAt = DateTimeOffset . UtcNow
168+ }
169+ } ,
170+ totalCount : 1 ,
171+ pageNumber : 1 ,
172+ pageSize : 10 ,
173+ sortBy : null ,
174+ descending : false )
175+ } ;
176+
177+ _request . Setup ( x => x . SendJsonAsync ( It . IsAny < string > ( ) , It . IsAny < object > ( ) ) )
178+ . ReturnsAsync ( SuccessJson ( response ) ) ;
179+
180+ var client = CreateClient ( ) ;
181+
182+ var result = await client . Authorization . GetMyRolesAsync ( ) ;
183+
184+ _request . Verify ( x =>
185+ x . SendJsonAsync ( "/auth/me/authorization/roles/get" , It . IsAny < object > ( ) ) ,
186+ Times . Once ) ;
187+
188+ result . IsSuccess . Should ( ) . BeTrue ( ) ;
189+ result . Value . Should ( ) . NotBeNull ( ) ;
190+ result . Value ! . UserKey . Should ( ) . Be ( userKey ) ;
191+ result . Value . Roles . Items . Should ( ) . HaveCount ( 1 ) ;
192+ result . Value . Roles . Items [ 0 ] . Name . Should ( ) . Be ( "admin" ) ;
193+ }
194+
195+ [ Fact ]
196+ public async Task GetUserRoles_Should_Call_Admin_Endpoint ( )
197+ {
198+ var userKey = UserKey . FromString ( "user-1" ) ;
199+
200+ var response = new UserRolesResponse
201+ {
202+ UserKey = userKey ,
203+ Roles = new PagedResult < UserRoleInfo > (
204+ new List < UserRoleInfo >
205+ {
206+ new UserRoleInfo
207+ {
208+ Tenant = TenantKeys . Single ,
209+ UserKey = userKey ,
210+ RoleId = RoleId . From ( Guid . NewGuid ( ) ) ,
211+ Name = "admin" ,
212+ AssignedAt = DateTimeOffset . UtcNow
213+ }
214+ } ,
215+ totalCount : 1 ,
216+ pageNumber : 1 ,
217+ pageSize : 10 ,
218+ sortBy : null ,
219+ descending : false )
220+ } ;
221+
222+ _request . Setup ( x => x . SendJsonAsync ( It . IsAny < string > ( ) , It . IsAny < object > ( ) ) )
223+ . ReturnsAsync ( SuccessJson ( response ) ) ;
224+
225+ var client = CreateClient ( ) ;
226+ var result = await client . Authorization . GetUserRolesAsync ( userKey ) ;
227+ _request . Verify ( x => x . SendJsonAsync ( $ "/auth/admin/authorization/users/{ userKey . Value } /roles/get", It . IsAny < object > ( ) ) , Times . Once ) ;
228+
229+ result . IsSuccess . Should ( ) . BeTrue ( ) ;
230+ result . Value . Should ( ) . NotBeNull ( ) ;
231+ result . Value ! . UserKey . Should ( ) . Be ( userKey ) ;
232+ result . Value . Roles . Items . Should ( ) . HaveCount ( 1 ) ;
233+ result . Value . Roles . Items [ 0 ] . Name . Should ( ) . Be ( "admin" ) ;
234+ }
235+
236+ [ Fact ]
237+ public async Task CreateRole_Should_Return_Result ( )
238+ {
239+ _request . Setup ( x => x . SendJsonAsync ( It . IsAny < string > ( ) , It . IsAny < object > ( ) ) )
240+ . ReturnsAsync ( SuccessJson ( new RoleInfo ( ) { Name = "admin" } ) ) ;
241+
242+ var client = CreateClient ( ) ;
243+
244+ var result = await client . Authorization . CreateRoleAsync ( new CreateRoleRequest ( ) { Name = "admin" } ) ;
245+
246+ result . IsSuccess . Should ( ) . BeTrue ( ) ;
247+ result . Value . Should ( ) . NotBeNull ( ) ;
248+ }
249+
250+ [ Fact ]
251+ public async Task RenameRole_Should_Publish_Event_On_Success ( )
252+ {
253+ _request . Setup ( x => x . SendJsonAsync ( It . IsAny < string > ( ) , It . IsAny < object > ( ) ) )
254+ . ReturnsAsync ( new UAuthTransportResult { Ok = true , Status = 200 } ) ;
255+
256+ var client = CreateClient ( ) ;
257+
258+ var request = new RenameRoleRequest
259+ {
260+ Id = RoleId . From ( Guid . NewGuid ( ) ) ,
261+ Name = "new-role"
262+ } ;
263+
264+ await client . Authorization . RenameRoleAsync ( request ) ;
265+
266+ _events . Verify ( x => x . PublishAsync ( It . Is < UAuthStateEventArgs > ( e => e . Type == UAuthStateEvent . AuthorizationChanged ) ) , Times . Once ) ;
267+ }
268+
269+ [ Fact ]
270+ public async Task RenameRole_Should_NOT_Publish_Event_On_Failure ( )
271+ {
272+ _request . Setup ( x => x . SendJsonAsync ( It . IsAny < string > ( ) , It . IsAny < object > ( ) ) )
273+ . ReturnsAsync ( new UAuthTransportResult { Ok = false , Status = 400 } ) ;
274+
275+ var client = CreateClient ( ) ;
276+
277+ await client . Authorization . RenameRoleAsync ( new RenameRoleRequest
278+ {
279+ Id = RoleId . From ( Guid . NewGuid ( ) ) ,
280+ Name = "fail"
281+ } ) ;
282+
283+ _events . Verify ( x => x . PublishAsync ( It . IsAny < UAuthStateEventArgs > ( ) ) , Times . Never ) ;
284+ }
285+
286+ [ Fact ]
287+ public async Task SetRolePermissions_Should_Publish_Event_On_Success ( )
288+ {
289+ _request . Setup ( x => x . SendJsonAsync ( It . IsAny < string > ( ) , It . IsAny < object > ( ) ) )
290+ . ReturnsAsync ( new UAuthTransportResult { Ok = true , Status = 200 } ) ;
291+
292+ var client = CreateClient ( ) ;
293+
294+ var request = new SetRolePermissionsRequest
295+ {
296+ RoleId = RoleId . From ( Guid . NewGuid ( ) ) ,
297+ Permissions = new List < Permission > { Permission . From ( "read" ) , Permission . From ( "write" ) }
298+ } ;
299+
300+ await client . Authorization . SetRolePermissionsAsync ( request ) ;
301+
302+ _events . Verify ( x => x . PublishAsync ( It . Is < UAuthStateEventArgs > ( e => e . Type == UAuthStateEvent . AuthorizationChanged ) ) , Times . Once ) ;
303+ }
304+
305+ [ Fact ]
306+ public async Task DeleteRole_Should_Publish_Event_On_Success ( )
307+ {
308+ _request . Setup ( x => x . SendJsonAsync ( It . IsAny < string > ( ) , It . IsAny < object > ( ) ) )
309+ . ReturnsAsync ( SuccessJson ( new DeleteRoleResult ( ) ) ) ;
310+
311+ var client = CreateClient ( ) ;
312+
313+ var request = new DeleteRoleRequest
314+ {
315+ Id = RoleId . From ( Guid . NewGuid ( ) )
316+ } ;
317+
318+ await client . Authorization . DeleteRoleAsync ( request ) ;
319+ _events . Verify ( x => x . PublishAsync ( It . Is < UAuthStateEventArgs > ( e => e . Type == UAuthStateEvent . AuthorizationChanged ) ) , Times . Once ) ;
320+ }
321+ }
0 commit comments