77using sparkly_server . Enum ;
88using sparkly_server . Infrastructure ;
99using sparkly_server . Services . Auth ;
10+ using sparkly_server . test . config ;
11+ using System . Net ;
1012using Xunit . Abstractions ;
13+ using Xunit . Sdk ;
1114
1215namespace sparkly_server . test
1316{
@@ -76,7 +79,7 @@ private async Task<Guid> AuthenticateAsTestUserAsync()
7679 /// </summary>
7780 /// <param name="projectName">The name of the project to create.</param>
7881 /// <returns>A <see cref="ProjectResponse"/> containing the details of the created project, or null if creation fails.</returns>
79- private async Task < ProjectResponse ? > CreateProjectAsync ( string projectName )
82+ private async Task < ProjectResponse > CreateProjectAsync ( string projectName )
8083 {
8184 var payload = new CreateProjectRequest (
8285 ProjectName : projectName ,
@@ -93,7 +96,9 @@ private async Task<Guid> AuthenticateAsTestUserAsync()
9396 response . EnsureSuccessStatusCode ( ) ;
9497
9598 var created = await response . Content . ReadFromJsonAsync < ProjectResponse > ( ) ;
96- return created ;
99+
100+ return created ?? throw new XunitException ( "CreateProjectAsync: response body deserialized to null" ) ;
101+
97102 }
98103
99104 // Tests
@@ -107,7 +112,7 @@ public async Task CreateProject_Should_Create_Project_For_Authenticated_User()
107112 var created = await CreateProjectAsync ( projectName ) ;
108113
109114 Assert . NotNull ( created ) ;
110- Assert . Equal ( projectName , created ! . ProjectName ) ;
115+ Assert . Equal ( projectName , created . ProjectName ) ;
111116
112117 using var scope = _factory . Services . CreateScope ( ) ;
113118 var db = scope . ServiceProvider . GetRequiredService < AppDbContext > ( ) ;
@@ -119,5 +124,46 @@ public async Task CreateProject_Should_Create_Project_For_Authenticated_User()
119124 Assert . Equal ( projectName , project . ProjectName ) ;
120125 Assert . Equal ( userId , project . OwnerId ) ;
121126 }
127+
128+ [ Fact ]
129+ public async Task CreateProject_Should_Fail_For_Unauthenticated_User ( )
130+ {
131+ var request = new CreateProjectRequest ( "MyTestProject" , "Test project" , ProjectVisibility . Public ) ;
132+
133+ var response = await _client . PostAsJsonAsync ( "/api/v1/projects/create" , request ) ;
134+
135+ Assert . Equal ( HttpStatusCode . Unauthorized , response . StatusCode ) ;
136+ }
137+
138+ [ Fact ]
139+ public async Task GetProjectById_Should_Return_Null_For_Nonexistent_Project ( )
140+ {
141+ var response = await _client . GetAsync ( "/api/v1/projects/1234567890abcdef" ) ;
142+
143+ Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
144+ }
145+
146+ [ Fact ]
147+ public async Task GetProjectById_Should_Return_Project ( )
148+ {
149+ await AuthenticateAsTestUserAsync ( ) ;
150+ var projectName = "MyTestProject1" ;
151+
152+ var project = await CreateProjectAsync ( projectName ) ;
153+ var projectId = project . Id ;
154+
155+ _output . WriteLine ( $ "[Test] Created projectId = { projectId } ") ;
156+
157+ using ( var scope = _factory . Services . CreateScope ( ) )
158+ {
159+ var db = scope . ServiceProvider . GetRequiredService < AppDbContext > ( ) ;
160+ var exists = await db . Projects . AnyAsync ( p => p . Id == projectId ) ;
161+ _output . WriteLine ( $ "[Test] Project exists in DB: { exists } ") ;
162+ }
163+
164+ var response = await _client . GetAsync ( $ "/api/v1/projects/{ projectId } ") ;
165+
166+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
167+ }
122168 }
123169}
0 commit comments