66using System . Threading . Tasks ;
77using Bogus ;
88using JsonApiDotNetCore . Models ;
9+ using JsonApiDotNetCore . Models . JsonApiDocuments ;
910using JsonApiDotNetCoreExample ;
1011using JsonApiDotNetCoreExample . Data ;
1112using JsonApiDotNetCoreExample . Models ;
@@ -149,7 +150,13 @@ public async Task Unauthorized_TodoItem()
149150
150151 // Assert
151152 var body = await response . Content . ReadAsStringAsync ( ) ;
152- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
153+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
154+
155+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
156+ Assert . Single ( errorDocument . Errors ) ;
157+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
158+ Assert . Equal ( "You are not allowed to update the author of todo items." , errorDocument . Errors [ 0 ] . Title ) ;
159+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
153160 }
154161
155162 [ Fact ]
@@ -163,7 +170,13 @@ public async Task Unauthorized_Passport()
163170
164171 // Assert
165172 var body = await response . Content . ReadAsStringAsync ( ) ;
166- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
173+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
174+
175+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
176+ Assert . Single ( errorDocument . Errors ) ;
177+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
178+ Assert . Equal ( "You are not allowed to include passports on individual persons." , errorDocument . Errors [ 0 ] . Title ) ;
179+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
167180 }
168181
169182 [ Fact ]
@@ -185,8 +198,13 @@ public async Task Unauthorized_Article()
185198
186199 // Assert
187200 var body = await response . Content . ReadAsStringAsync ( ) ;
188- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ " { route } returned { response . StatusCode } status code with payload: { body } " ) ;
201+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
189202
203+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
204+ Assert . Single ( errorDocument . Errors ) ;
205+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
206+ Assert . Equal ( "You are not allowed to see this article." , errorDocument . Errors [ 0 ] . Title ) ;
207+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
190208 }
191209
192210 [ Fact ]
@@ -300,10 +318,14 @@ public async Task Cascade_Permission_Error_Create_ToOne_Relationship()
300318
301319 // Assert
302320 var body = await response . Content . ReadAsStringAsync ( ) ;
303- // should throw 403 in PersonResource implicit hook
304- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
305- }
321+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
306322
323+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
324+ Assert . Single ( errorDocument . Errors ) ;
325+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
326+ Assert . Equal ( "You are not allowed to update fields or relationships of locked todo items." , errorDocument . Errors [ 0 ] . Title ) ;
327+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
328+ }
307329
308330 [ Fact ]
309331 public async Task Cascade_Permission_Error_Updating_ToOne_Relationship ( )
@@ -348,8 +370,13 @@ public async Task Cascade_Permission_Error_Updating_ToOne_Relationship()
348370
349371 // Assert
350372 var body = await response . Content . ReadAsStringAsync ( ) ;
351- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ " { route } returned { response . StatusCode } status code with payload: { body } " ) ;
373+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
352374
375+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
376+ Assert . Single ( errorDocument . Errors ) ;
377+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
378+ Assert . Equal ( "You are not allowed to update fields or relationships of locked persons." , errorDocument . Errors [ 0 ] . Title ) ;
379+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
353380 }
354381
355382 [ Fact ]
@@ -395,12 +422,15 @@ public async Task Cascade_Permission_Error_Updating_ToOne_Relationship_Deletion(
395422
396423 // Assert
397424 var body = await response . Content . ReadAsStringAsync ( ) ;
398- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ " { route } returned { response . StatusCode } status code with payload: { body } " ) ;
425+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
399426
427+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
428+ Assert . Single ( errorDocument . Errors ) ;
429+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
430+ Assert . Equal ( "You are not allowed to update fields or relationships of locked persons." , errorDocument . Errors [ 0 ] . Title ) ;
431+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
400432 }
401433
402-
403-
404434 [ Fact ]
405435 public async Task Cascade_Permission_Error_Delete_ToOne_Relationship ( )
406436 {
@@ -422,10 +452,14 @@ public async Task Cascade_Permission_Error_Delete_ToOne_Relationship()
422452
423453 // Assert
424454 var body = await response . Content . ReadAsStringAsync ( ) ;
425- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
426- }
427-
455+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
428456
457+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
458+ Assert . Single ( errorDocument . Errors ) ;
459+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
460+ Assert . Equal ( "You are not allowed to update fields or relationships of locked todo items." , errorDocument . Errors [ 0 ] . Title ) ;
461+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
462+ }
429463
430464 [ Fact ]
431465 public async Task Cascade_Permission_Error_Create_ToMany_Relationship ( )
@@ -473,7 +507,13 @@ public async Task Cascade_Permission_Error_Create_ToMany_Relationship()
473507
474508 // Assert
475509 var body = await response . Content . ReadAsStringAsync ( ) ;
476- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
510+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
511+
512+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
513+ Assert . Single ( errorDocument . Errors ) ;
514+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
515+ Assert . Equal ( "You are not allowed to update fields or relationships of locked todo items." , errorDocument . Errors [ 0 ] . Title ) ;
516+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
477517 }
478518
479519 [ Fact ]
@@ -525,10 +565,13 @@ public async Task Cascade_Permission_Error_Updating_ToMany_Relationship()
525565
526566 // Assert
527567 var body = await response . Content . ReadAsStringAsync ( ) ;
568+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
528569
529- // were unrelating a persons from a locked todo, so this should be unauthorized
530- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
531-
570+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
571+ Assert . Single ( errorDocument . Errors ) ;
572+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
573+ Assert . Equal ( "You are not allowed to update fields or relationships of locked todo items." , errorDocument . Errors [ 0 ] . Title ) ;
574+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
532575 }
533576
534577 [ Fact ]
@@ -552,7 +595,13 @@ public async Task Cascade_Permission_Error_Delete_ToMany_Relationship()
552595
553596 // Assert
554597 var body = await response . Content . ReadAsStringAsync ( ) ;
555- Assert . True ( HttpStatusCode . Forbidden == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
598+ Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
599+
600+ var errorDocument = JsonConvert . DeserializeObject < ErrorDocument > ( body ) ;
601+ Assert . Single ( errorDocument . Errors ) ;
602+ Assert . Equal ( HttpStatusCode . Forbidden , errorDocument . Errors [ 0 ] . StatusCode ) ;
603+ Assert . Equal ( "You are not allowed to update fields or relationships of locked todo items." , errorDocument . Errors [ 0 ] . Title ) ;
604+ Assert . Null ( errorDocument . Errors [ 0 ] . Detail ) ;
556605 }
557606 }
558607}
0 commit comments