11using System . Net ;
2- using System . Net . Http . Json ;
32using FluentAssertions ;
43using FluentAssertions . Extensions ;
5- using Turnierplan . App . Models ;
4+ using Microsoft . Kiota . Abstractions ;
5+ using Turnierplan . App . Test . Functional . Client . Models ;
66using Turnierplan . Core . ApiKey ;
77using Turnierplan . Core . Extensions ;
88using Turnierplan . Core . Organization ;
9- using Turnierplan . Core . RoleAssignment ;
109using Turnierplan . Core . Tournament ;
1110using Turnierplan . Core . User ;
1211using Xunit ;
12+ using Role = Turnierplan . Core . RoleAssignment . Role ;
13+ using Visibility = Turnierplan . Core . Tournament . Visibility ;
1314
1415namespace Turnierplan . App . Test . Functional ;
1516
@@ -46,14 +47,12 @@ public async Task When_ApiKey_And_User_Are_Deleted_The_Role_Assignments_Are_Also
4647 _testServer . ExecuteContextAction ( db => db . OrganizationRoleAssignments . Count ( ) ) . Should ( ) . Be ( 1 ) ;
4748 _testServer . ExecuteContextAction ( db => db . TournamentRoleAssignments . Count ( ) ) . Should ( ) . Be ( 2 ) ;
4849
49- var resp = await _testServer . Client . DeleteAsync ( Routes . ApiKeys . Delete ( apiKeyId ) , TestContext . Current . CancellationToken ) ;
50- resp . EnsureSuccessStatusCode ( ) ;
50+ await _testServer . Client . ApiKeys [ apiKeyId ] . DeleteAsync ( cancellationToken : TestContext . Current . CancellationToken ) ;
5151
5252 _testServer . ExecuteContextAction ( db => db . OrganizationRoleAssignments . Count ( ) ) . Should ( ) . Be ( 1 ) ;
5353 _testServer . ExecuteContextAction ( db => db . TournamentRoleAssignments . Count ( ) ) . Should ( ) . Be ( 1 ) ;
5454
55- resp = await _testServer . Client . DeleteAsync ( Routes . Users . Delete ( userId ) , TestContext . Current . CancellationToken ) ;
56- resp . EnsureSuccessStatusCode ( ) ;
55+ await _testServer . Client . Users [ userId ] . DeleteAsync ( cancellationToken : TestContext . Current . CancellationToken ) ;
5756
5857 _testServer . ExecuteContextAction ( db => db . OrganizationRoleAssignments . Count ( ) ) . Should ( ) . Be ( 0 ) ;
5958 _testServer . ExecuteContextAction ( db => db . TournamentRoleAssignments . Count ( ) ) . Should ( ) . Be ( 0 ) ;
@@ -65,36 +64,54 @@ public async Task New_User_Can_Not_Create_Organization_Unless_Explicitly_Granted
6564 const string newUserName = "test_user" ;
6665 const string newUserPassword = "test123" ;
6766
68- var resp = await _testServer . Client . PostAsJsonAsync (
69- Routes . Users . Create ( ) ,
70- new { UserName = newUserName , Password = newUserPassword } ,
71- TestContext . Current . CancellationToken ) ;
72- resp . EnsureSuccessStatusCode ( ) ;
73-
74- var userClient = _testServer . CreateNewClientAndLogIn ( newUserName , newUserPassword ) ;
75- resp = await userClient . PostAsJsonAsync (
76- Routes . Organizations . Create ( ) ,
77- new { Name = "test_org" } ,
78- TestContext . Current . CancellationToken ) ;
79- resp . StatusCode . Should ( ) . Be ( HttpStatusCode . Forbidden ) ;
80-
81- // extra step required to get ID of new user
82- resp = await _testServer . Client . GetAsync ( Routes . Users . List ( ) , TestContext . Current . CancellationToken ) ;
83- resp . EnsureSuccessStatusCode ( ) ;
84- var allUsers = await resp . Content . ReadFromJsonAsync < UserDto [ ] > ( TestContext . Current . CancellationToken ) ;
85- var newUserId = allUsers ! . Single ( x => x . UserName . Equals ( newUserName ) ) . Id ;
86-
87- resp = await _testServer . Client . PutAsJsonAsync (
88- Routes . Users . Update ( newUserId ) ,
89- new { UserName = newUserName , IsAdministrator = false , AllowCreateOrganization = true , UpdatePassword = false } ,
90- TestContext . Current . CancellationToken ) ;
91- resp . EnsureSuccessStatusCode ( ) ;
92-
93- userClient = _testServer . CreateNewClientAndLogIn ( newUserName , newUserPassword ) ;
94- resp = await userClient . PostAsJsonAsync (
95- Routes . Organizations . Create ( ) ,
96- new { Name = "test_org" } ,
97- TestContext . Current . CancellationToken ) ;
98- resp . EnsureSuccessStatusCode ( ) ;
67+ await _testServer . Client . Users . PostAsync (
68+ new CreateUserEndpointRequest { UserName = newUserName , Password = newUserPassword } ,
69+ cancellationToken : TestContext . Current . CancellationToken ) ;
70+
71+ {
72+ var userClient = await _testServer . CreateClientForUserAsync ( newUserName , newUserPassword ) ;
73+
74+ await ExpectApiErrorAsync ( ( ) => userClient . Organizations . PostAsync (
75+ new CreateOrganizationEndpointRequest { Name = "test_org" } ,
76+ cancellationToken : TestContext . Current . CancellationToken ) , HttpStatusCode . Forbidden ) ;
77+ }
78+
79+ // Extra step is required to get the ID of the created user
80+ var allUsers = await _testServer . Client . Users . GetAsync ( cancellationToken : TestContext . Current . CancellationToken ) ;
81+ var newUserId = allUsers ! . Single ( x => x . UserName ! . Equals ( newUserName ) ) . Id ! . Value ;
82+
83+ await _testServer . Client . Users [ newUserId ] . PutAsync ( new UpdateUserEndpointRequest
84+ {
85+ UserName = newUserName ,
86+ IsAdministrator = false ,
87+ AllowCreateOrganization = true ,
88+ UpdatePassword = false
89+ } , cancellationToken : TestContext . Current . CancellationToken ) ;
90+
91+ {
92+ // We need to create a new client because a fresh login is required to get the new claims in the token
93+ var userClient = await _testServer . CreateClientForUserAsync ( newUserName , newUserPassword ) ;
94+
95+ await userClient . Organizations . PostAsync (
96+ new CreateOrganizationEndpointRequest { Name = "test_org" } ,
97+ cancellationToken : TestContext . Current . CancellationToken ) ;
98+ }
99+ }
100+
101+ private static async Task ExpectApiErrorAsync ( Func < Task > func , HttpStatusCode code )
102+ {
103+ ApiException ? exception = null ;
104+
105+ try
106+ {
107+ await func ( ) ;
108+ }
109+ catch ( ApiException ex )
110+ {
111+ exception = ex ;
112+ }
113+
114+ exception . Should ( ) . NotBeNull ( ) ;
115+ exception . ResponseStatusCode . Should ( ) . Be ( ( int ) code ) ;
99116 }
100117}
0 commit comments