66using System . Text . Json ;
77using System . Text . Json . Nodes ;
88using exercise . tests . Helpers ;
9+ using exercise . wwwapi . DTOs ;
910
1011namespace exercise . tests . IntegrationTests
1112{
1213 /// <summary>
1314 /// Integration tests exercising the user management endpoints end-to-end via the API surface.
1415 /// </summary>
1516 [ TestFixture ]
16- public class UserTests
17+ public class UserTests : BaseIntegrationTest
1718 {
18- private WebApplicationFactory < Program > _factory ;
19- private HttpClient _client ;
20-
21- [ SetUp ]
22- public void SetUp ( )
23- {
24- // Arrange
25- _factory = new WebApplicationFactory < Program > ( ) ;
26- _client = _factory . CreateClient ( ) ;
27- }
28-
29- [ TearDown ]
30- public void TearDown ( )
31- {
32- _client . Dispose ( ) ;
33- _factory . Dispose ( ) ;
34- }
35-
3619 /// <summary>
3720 /// Confirms that valid registration payloads yield an HTTP 201 Created response.
3821 /// </summary>
@@ -193,19 +176,18 @@ public async Task Login_failure(string email, string password)
193176 [ Test ]
194177 public async Task UpdateUserSuccess ( )
195178 {
196- var fieldsToUpdate = new Dictionary < string , object ? >
179+ var fieldsToUpdate = new UserPatchDTO ( )
197180 {
198- { "username" , "roman-olsen13" } ,
199- { "email" , "roman.olsen13@example.com" } ,
200- { "password" , "aGoodPassword!200" } ,
201- { "role" , 0 }
181+ Username = "roman-olsen13" ,
182+ Email = "roman.olsen13@example.com" ,
183+ Password = "aGoodPassword!200" ,
184+ Role = 0
202185 } ;
203186
204- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
205- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
206-
187+ string token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
207188 int userId = 13 ;
208- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
189+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
190+
209191
210192 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
211193 }
@@ -216,14 +198,12 @@ public async Task UpdateUserSuccess()
216198 [ Test ]
217199 public async Task UpdateUserNullFieldsOnly ( )
218200 {
219- var fieldsToUpdate = new Dictionary < string , object ? > { } ;
220-
221- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
222- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
223-
201+ var fieldsToUpdate = new UserPatchDTO ( ) ;
202+
203+ string token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
224204 int userId = 1 ;
225- var response = await _client . PatchAsync ( $ "/users/ { userId } " , content ) ;
226-
205+
206+ var response = await SendAuthenticatedPatchAsync ( $ "/users/ { userId } " , token , fieldsToUpdate ) ;
227207 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
228208 }
229209
@@ -233,17 +213,15 @@ public async Task UpdateUserNullFieldsOnly()
233213 [ Test ]
234214 public async Task UpdateUserInvalidUsername ( )
235215 {
236- var fieldsToUpdate = new Dictionary < string , object ? >
216+ var fieldsToUpdate = new UserPatchDTO ( )
237217 {
238- { "username" , "roman--olsen13" }
218+ Username = "roman--olsen13"
239219 } ;
240220
241- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
242- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
243-
244221 int userId = 13 ;
245- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
246-
222+ string token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
223+
224+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
247225 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
248226 }
249227
@@ -253,17 +231,15 @@ public async Task UpdateUserInvalidUsername()
253231 [ Test ]
254232 public async Task UpdateUserInvalidGitHubUsername ( )
255233 {
256- var fieldsToUpdate = new Dictionary < string , object ? >
234+ var fieldsToUpdate = new UserPatchDTO ( )
257235 {
258- { "gitHubUsername" , "roman--olsen13" }
236+ GithubUsername = "roman--olsen13"
259237 } ;
260238
261- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
262- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
263-
264239 int userId = 13 ;
265- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
266-
240+ string token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
241+
242+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
267243 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
268244 }
269245
@@ -273,16 +249,15 @@ public async Task UpdateUserInvalidGitHubUsername()
273249 [ Test ]
274250 public async Task UpdateUserInvalidEmail ( )
275251 {
276- var fieldsToUpdate = new Dictionary < string , object ? >
252+ var fieldsToUpdate = new UserPatchDTO ( )
277253 {
278- { "email" , "roman.olsen13@.e.com" }
254+ Email = "roman.olsen13@.e.com"
279255 } ;
280256
281- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
282- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
257+ var token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
283258
284259 int userId = 13 ;
285- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
260+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
286261
287262 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
288263 }
@@ -298,12 +273,11 @@ public async Task UpdateUserInvalidPassword()
298273 { "password" , "nope!" }
299274 } ;
300275
301- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
302- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
276+ var token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
303277
304278 int userId = 13 ;
305- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
306-
279+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
280+
307281 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
308282 }
309283
@@ -318,11 +292,10 @@ public async Task UpdateUserInvalidRole()
318292 { "role" , 10 }
319293 } ;
320294
321- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
322- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
295+ var token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
323296
324297 int userId = 13 ;
325- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
298+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
326299
327300 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
328301 }
@@ -338,11 +311,10 @@ public async Task UpdateUserUsernameExists()
338311 { "username" , "nigel-nowak2" }
339312 } ;
340313
341- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
342- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
314+ var token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
343315
344316 int userId = 13 ;
345- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
317+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
346318
347319 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
348320 }
@@ -353,16 +325,15 @@ public async Task UpdateUserUsernameExists()
353325 [ Test ]
354326 public async Task UpdateUserGitHubUsernameExists ( )
355327 {
356- var fieldsToUpdate = new Dictionary < string , object ? >
328+ var fieldsToUpdate = new UserPatchDTO ( )
357329 {
358- { "gitHubUsername" , "nigel-nowak2" }
330+ GithubUsername = "nigel-nowak2"
359331 } ;
360332
361- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
362- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
363-
364333 int userId = 13 ;
365- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
334+ string token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
335+
336+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
366337
367338 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
368339 }
@@ -373,16 +344,15 @@ public async Task UpdateUserGitHubUsernameExists()
373344 [ Test ]
374345 public async Task UpdateUserEmailExists ( )
375346 {
376- var fieldsToUpdate = new Dictionary < string , object ? >
347+ var fieldsToUpdate = new UserPatchDTO ( )
377348 {
378- { "email" , " nigel.nowak2@example.com"}
349+ Email = " nigel.nowak2@example.com"
379350 } ;
380-
381- var json = JsonSerializer . Serialize ( fieldsToUpdate ) ;
382- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
383-
351+
384352 int userId = 13 ;
385- var response = await _client . PatchAsync ( $ "/users/{ userId } ", content ) ;
353+ string token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
354+
355+ var response = await SendAuthenticatedPatchAsync ( $ "/users/{ userId } ", token , fieldsToUpdate ) ;
386356
387357 Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
388358 }
@@ -396,7 +366,9 @@ public async Task UpdateUserEmailExists()
396366 [ TestCase ( "10000000" , HttpStatusCode . NotFound ) ]
397367 public async Task GetUserByIdTest ( string id , HttpStatusCode responseStatus )
398368 {
399- var response = await _client . GetAsync ( $ "/users/{ id } ") ;
369+ string token = await LoginAndGetToken ( TeacherEmail , TeacherPassword ) ;
370+
371+ var response = await SendAuthenticatedGetAsync ( $ "/users/{ id } ", token ) ;
400372 Assert . That ( response . StatusCode , Is . EqualTo ( responseStatus ) ) ;
401373
402374 }
0 commit comments