5
5
using Moq ;
6
6
using Xunit ;
7
7
using System . Threading . Tasks ;
8
+ using JsonApiDotNetCore . Configuration ;
8
9
using JsonApiDotNetCore . Internal ;
10
+ using Microsoft . AspNetCore . Http ;
11
+ using Microsoft . AspNetCore . Mvc ;
9
12
10
13
namespace UnitTests
11
14
{
@@ -143,6 +146,8 @@ public async Task PatchAsync_Calls_Service()
143
146
const int id = 0 ;
144
147
var resource = new Resource ( ) ;
145
148
var serviceMock = new Mock < IUpdateService < Resource > > ( ) ;
149
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
150
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions ( ) ) ;
146
151
var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , update : serviceMock . Object ) ;
147
152
148
153
// act
@@ -153,6 +158,47 @@ public async Task PatchAsync_Calls_Service()
153
158
VerifyApplyContext ( ) ;
154
159
}
155
160
161
+ [ Fact ]
162
+ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateDisbled ( )
163
+ {
164
+ // arrange
165
+ const int id = 0 ;
166
+ var resource = new Resource ( ) ;
167
+ var serviceMock = new Mock < IUpdateService < Resource > > ( ) ;
168
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
169
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = false } ) ;
170
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , update : serviceMock . Object ) ;
171
+
172
+ // act
173
+ var response = await controller . PatchAsync ( id , resource ) ;
174
+
175
+ // assert
176
+ serviceMock . Verify ( m => m . UpdateAsync ( id , It . IsAny < Resource > ( ) ) , Times . Once ) ;
177
+ VerifyApplyContext ( ) ;
178
+ Assert . IsNotType < BadRequestObjectResult > ( response ) ;
179
+ }
180
+
181
+ [ Fact ]
182
+ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateEnabled ( )
183
+ {
184
+ // arrange
185
+ const int id = 0 ;
186
+ var resource = new Resource ( ) ;
187
+ var serviceMock = new Mock < IUpdateService < Resource > > ( ) ;
188
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
189
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = true } ) ;
190
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , update : serviceMock . Object ) ;
191
+ controller . ModelState . AddModelError ( "Id" , "Failed Validation" ) ;
192
+
193
+ // act
194
+ var response = await controller . PatchAsync ( id , resource ) ;
195
+
196
+ // assert
197
+ serviceMock . Verify ( m => m . UpdateAsync ( id , It . IsAny < Resource > ( ) ) , Times . Never ) ;
198
+ Assert . IsType < BadRequestObjectResult > ( response ) ;
199
+ Assert . IsType < ErrorCollection > ( ( ( BadRequestObjectResult ) response ) . Value ) ;
200
+ }
201
+
156
202
[ Fact ]
157
203
public async Task PatchAsync_Throws_405_If_No_Service ( )
158
204
{
@@ -168,6 +214,67 @@ public async Task PatchAsync_Throws_405_If_No_Service()
168
214
Assert . Equal ( 405 , exception . GetStatusCode ( ) ) ;
169
215
}
170
216
217
+ [ Fact ]
218
+ public async Task PostAsync_Calls_Service ( )
219
+ {
220
+ // arrange
221
+ var resource = new Resource ( ) ;
222
+ var serviceMock = new Mock < ICreateService < Resource > > ( ) ;
223
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
224
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions ( ) ) ;
225
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , create : serviceMock . Object ) ;
226
+ serviceMock . Setup ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) ) . ReturnsAsync ( resource ) ;
227
+ controller . ControllerContext = new Microsoft . AspNetCore . Mvc . ControllerContext { HttpContext = new DefaultHttpContext ( ) } ;
228
+
229
+ // act
230
+ await controller . PostAsync ( resource ) ;
231
+
232
+ // assert
233
+ serviceMock . Verify ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) , Times . Once ) ;
234
+ VerifyApplyContext ( ) ;
235
+ }
236
+
237
+ [ Fact ]
238
+ public async Task PostAsync_ModelStateInvalid_ValidateModelStateDisabled ( )
239
+ {
240
+ // arrange
241
+ var resource = new Resource ( ) ;
242
+ var serviceMock = new Mock < ICreateService < Resource > > ( ) ;
243
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
244
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = false } ) ;
245
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , create : serviceMock . Object ) ;
246
+ serviceMock . Setup ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) ) . ReturnsAsync ( resource ) ;
247
+ controller . ControllerContext = new Microsoft . AspNetCore . Mvc . ControllerContext { HttpContext = new DefaultHttpContext ( ) } ;
248
+
249
+ // act
250
+ var response = await controller . PostAsync ( resource ) ;
251
+
252
+ // assert
253
+ serviceMock . Verify ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) , Times . Once ) ;
254
+ VerifyApplyContext ( ) ;
255
+ Assert . IsNotType < BadRequestObjectResult > ( response ) ;
256
+ }
257
+
258
+ [ Fact ]
259
+ public async Task PostAsync_ModelStateInvalid_ValidateModelStateEnabled ( )
260
+ {
261
+ // arrange
262
+ var resource = new Resource ( ) ;
263
+ var serviceMock = new Mock < ICreateService < Resource > > ( ) ;
264
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
265
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = true } ) ;
266
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , create : serviceMock . Object ) ;
267
+ controller . ModelState . AddModelError ( "Id" , "Failed Validation" ) ;
268
+
269
+ // act
270
+ var response = await controller . PostAsync ( resource ) ;
271
+
272
+ // assert
273
+ serviceMock . Verify ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) , Times . Never ) ;
274
+ Assert . IsType < BadRequestObjectResult > ( response ) ;
275
+ Assert . IsType < ErrorCollection > ( ( ( BadRequestObjectResult ) response ) . Value ) ;
276
+ }
277
+
171
278
[ Fact ]
172
279
public async Task PatchRelationshipsAsync_Calls_Service ( )
173
280
{
0 commit comments