1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
using System . Net ;
@@ -40,10 +40,43 @@ public UpdatingDataTests(TestFixture<TestStartup> fixture)
40
40
. RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
41
41
}
42
42
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
+ Assert . Equal ( 422 , Convert . ToInt32 ( response . StatusCode ) ) ;
74
+ }
75
+
43
76
[ Fact ]
44
77
public async Task Respond_404_If_EntityDoesNotExist ( )
45
78
{
46
- // arrange
79
+ // Arrange
47
80
var maxPersonId = _context . TodoItems . LastOrDefault ( ) ? . Id ?? 0 ;
48
81
var todoItem = _todoItemFaker . Generate ( ) ;
49
82
var builder = new WebHostBuilder ( )
@@ -65,13 +98,7 @@ public async Task Respond_404_If_EntityDoesNotExist()
65
98
}
66
99
}
67
100
} ;
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" ) ;
101
+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ maxPersonId + 100 } ", content ) ;
75
102
76
103
// Act
77
104
var response = await client . SendAsync ( request ) ;
@@ -109,13 +136,7 @@ public async Task Can_Patch_Entity()
109
136
}
110
137
}
111
138
} ;
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" ) ;
139
+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
119
140
120
141
// Act
121
142
var response = await client . SendAsync ( request ) ;
@@ -172,13 +193,7 @@ public async Task Patch_Entity_With_HasMany_Does_Not_Included_Relationships()
172
193
}
173
194
}
174
195
} ;
175
-
176
- var httpMethod = new HttpMethod ( "PATCH" ) ;
177
- var route = $ "/api/v1/people/{ person . Id } ";
178
- var request = new HttpRequestMessage ( httpMethod , route ) ;
179
-
180
- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
181
- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
196
+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/people/{ person . Id } ", content ) ;
182
197
183
198
// Act
184
199
var response = await client . SendAsync ( request ) ;
@@ -237,13 +252,7 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
237
252
}
238
253
}
239
254
} ;
240
-
241
- var httpMethod = new HttpMethod ( "PATCH" ) ;
242
- var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
243
- var request = new HttpRequestMessage ( httpMethod , route ) ;
244
-
245
- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
246
- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
255
+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
247
256
248
257
// Act
249
258
var response = await client . SendAsync ( request ) ;
@@ -255,5 +264,15 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
255
264
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
256
265
Assert . Equal ( person . Id , updatedTodoItem . OwnerId ) ;
257
266
}
267
+
268
+ private HttpRequestMessage PrepareRequest ( string method , string route , object content )
269
+ {
270
+ var httpMethod = new HttpMethod ( method ) ;
271
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
272
+
273
+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
274
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
275
+ return request ;
276
+ }
258
277
}
259
278
}
0 commit comments