1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . Linq ;
44using System . Net ;
@@ -40,10 +40,44 @@ public UpdatingDataTests(TestFixture<TestStartup> fixture)
4040 . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
4141 }
4242
43+
44+ [ Fact ]
45+ public async Task Response400IfUpdatingNotSettableAttribute ( )
46+ {
47+ // Arrange
48+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
49+ var server = new TestServer ( builder ) ;
50+ var client = server . CreateClient ( ) ;
51+
52+ var todoItem = _todoItemFaker . Generate ( ) ;
53+ _context . TodoItems . Add ( todoItem ) ;
54+ _context . SaveChanges ( ) ;
55+
56+ var content = new
57+ {
58+ datea = new
59+ {
60+ type = "todo-items" ,
61+ attributes = new
62+ {
63+ calculatedAttribute = "lol"
64+ }
65+ }
66+ } ;
67+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
68+
69+ // Act
70+ var response = await client . SendAsync ( request ) ;
71+
72+ // Assert
73+ var body = await response . Content . ReadAsStringAsync ( ) ;
74+ Assert . Equal ( 422 , Convert . ToInt32 ( response . StatusCode ) ) ;
75+ }
76+
4377 [ Fact ]
4478 public async Task Respond_404_If_EntityDoesNotExist ( )
4579 {
46- // arrange
80+ // Arrange
4781 var maxPersonId = _context . TodoItems . LastOrDefault ( ) ? . Id ?? 0 ;
4882 var todoItem = _todoItemFaker . Generate ( ) ;
4983 var builder = new WebHostBuilder ( )
@@ -65,13 +99,7 @@ public async Task Respond_404_If_EntityDoesNotExist()
6599 }
66100 }
67101 } ;
68-
69- var httpMethod = new HttpMethod ( "PATCH" ) ;
70- var route = $ "/api/v1/todo-items/{ maxPersonId + 100 } ";
71- var request = new HttpRequestMessage ( httpMethod , route ) ;
72-
73- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
74- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
102+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ maxPersonId + 100 } ", content ) ;
75103
76104 // Act
77105 var response = await client . SendAsync ( request ) ;
@@ -109,13 +137,7 @@ public async Task Can_Patch_Entity()
109137 }
110138 }
111139 } ;
112-
113- var httpMethod = new HttpMethod ( "PATCH" ) ;
114- var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
115- var request = new HttpRequestMessage ( httpMethod , route ) ;
116-
117- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
118- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
140+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
119141
120142 // Act
121143 var response = await client . SendAsync ( request ) ;
@@ -179,13 +201,7 @@ public async Task Patch_Entity_With_HasMany_Does_Not_Included_Relationships()
179201 }
180202 }
181203 } ;
182-
183- var httpMethod = new HttpMethod ( "PATCH" ) ;
184- var route = $ "/api/v1/people/{ person . Id } ";
185- var request = new HttpRequestMessage ( httpMethod , route ) ;
186-
187- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
188- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
204+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/people/{ person . Id } ", content ) ;
189205
190206 // Act
191207 var response = await client . SendAsync ( request ) ;
@@ -244,13 +260,7 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
244260 }
245261 }
246262 } ;
247-
248- var httpMethod = new HttpMethod ( "PATCH" ) ;
249- var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
250- var request = new HttpRequestMessage ( httpMethod , route ) ;
251-
252- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
253- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
263+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
254264
255265 // Act
256266 var response = await client . SendAsync ( request ) ;
@@ -262,5 +272,15 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
262272 Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
263273 Assert . Equal ( person . Id , updatedTodoItem . OwnerId ) ;
264274 }
275+
276+ private HttpRequestMessage PrepareRequest ( string method , string route , object content )
277+ {
278+ var httpMethod = new HttpMethod ( method ) ;
279+ var request = new HttpRequestMessage ( httpMethod , route ) ;
280+
281+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
282+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
283+ return request ;
284+ }
265285 }
266286}
0 commit comments