@@ -253,9 +253,10 @@ public async Task OnActionExecutionAsync_WhenValidationFailed_ContinuesAndAddsRe
253253 }
254254
255255 [ Test ]
256- public async Task OnActionExecutionAsync_WhenValidationSuccess_ContinuesAndAddsResponseToArguments ( )
256+ public async Task OnActionExecutionAsync_WhenActionDoesNotMatch_BlocksAndReturns_RecaptchaValidationFailedResult ( )
257257 {
258258 // Arrange
259+ var action = "submit" ;
259260 var httpContext = new DefaultHttpContext ( ) ;
260261 httpContext . Request . Headers . Add ( RecaptchaServiceConstants . TokenKeyName , TokenValue ) ;
261262
@@ -270,8 +271,79 @@ public async Task OnActionExecutionAsync_WhenValidationSuccess_ContinuesAndAddsR
270271 . Verifiable ( ) ;
271272
272273 _filter = new ValidateRecaptchaFilter ( _recaptchaServiceMock . Object , _optionsMock . Object , _logger ) ;
274+ _filter . Action = action ;
275+
276+ // Act
277+ await _filter . OnActionExecutionAsync ( _actionExecutingContext , _actionExecutionDelegate ) ;
278+
279+ // Assert
280+ _recaptchaServiceMock . Verify ( ) ;
281+ Assert . IsInstanceOf < IRecaptchaValidationFailedResult > ( _actionExecutingContext . Result ) ;
282+ }
283+
284+ [ Test ]
285+ public async Task OnActionExecutionAsync_WhenActionDoesNotMatch_ContinuesAndAddsResponseToArguments ( )
286+ {
287+ // Arrange
288+ var action = "submit" ;
289+ var httpContext = new DefaultHttpContext ( ) ;
290+ httpContext . Request . Headers . Add ( RecaptchaServiceConstants . TokenKeyName , TokenValue ) ;
291+
292+ _actionExecutingContext . HttpContext = httpContext ;
293+
294+ _recaptchaServiceMock = new Mock < IRecaptchaService > ( ) ;
295+ _recaptchaServiceMock . Setup ( service => service . ValidateRecaptchaResponse ( It . Is < string > ( s => s == TokenValue ) , null ) )
296+ . ReturnsAsync ( new ValidationResponse
297+ {
298+ Success = true ,
299+ ErrorMessages = new List < string > { "invalid-input-response" }
300+ } )
301+ . Verifiable ( ) ;
302+
303+ _filter = new ValidateRecaptchaFilter ( _recaptchaServiceMock . Object , _optionsMock . Object , _logger )
304+ {
305+ OnValidationFailedAction = ValidationFailedAction . ContinueRequest ,
306+ Action = action
307+ } ;
308+
309+ _actionExecutingContext . ActionArguments . Add ( "argumentName" , new ValidationResponse { Success = true } ) ;
310+
311+ // Act
312+ await _filter . OnActionExecutionAsync ( _actionExecutingContext , _actionExecutionDelegate ) ;
313+
314+ // Assert
315+ _recaptchaServiceMock . Verify ( ) ;
316+ Assert . IsInstanceOf < OkResult > ( _actionExecutingContext . Result ) ;
317+ Assert . IsTrue ( ( _actionExecutingContext . ActionArguments [ "argumentName" ] as ValidationResponse ) . Success ) ;
318+ Assert . GreaterOrEqual ( ( _actionExecutingContext . ActionArguments [ "argumentName" ] as ValidationResponse ) . Errors . Count ( ) , 1 ) ;
319+ Assert . AreEqual ( ValidationError . InvalidInputResponse , ( _actionExecutingContext . ActionArguments [ "argumentName" ] as ValidationResponse ) . Errors . First ( ) ) ;
320+ }
321+
322+ [ Test ]
323+ public async Task OnActionExecutionAsync_WhenValidationSuccess_ContinuesAndAddsResponseToArguments ( )
324+ {
325+ // Arrange
326+ var action = "submit" ;
327+ var httpContext = new DefaultHttpContext ( ) ;
328+ httpContext . Request . Headers . Add ( RecaptchaServiceConstants . TokenKeyName , TokenValue ) ;
329+
330+ _actionExecutingContext . HttpContext = httpContext ;
331+
332+ _recaptchaServiceMock = new Mock < IRecaptchaService > ( ) ;
333+ _recaptchaServiceMock . Setup ( service => service . ValidateRecaptchaResponse ( It . Is < string > ( s => s == TokenValue ) , null ) )
334+ . ReturnsAsync ( new ValidationResponse
335+ {
336+ Success = true ,
337+ Action = action
338+ } )
339+ . Verifiable ( ) ;
340+
341+ _filter = new ValidateRecaptchaFilter ( _recaptchaServiceMock . Object , _optionsMock . Object , _logger )
342+ {
343+ Action = action
344+ } ;
273345
274- _actionExecutingContext . ActionArguments . Add ( "argumentName" , new ValidationResponse { Success = false } ) ;
346+ _actionExecutingContext . ActionArguments . Add ( "argumentName" , new ValidationResponse { Success = false , Action = string . Empty } ) ;
275347
276348 // Act
277349 await _filter . OnActionExecutionAsync ( _actionExecutingContext , _actionExecutionDelegate ) ;
@@ -280,6 +352,7 @@ public async Task OnActionExecutionAsync_WhenValidationSuccess_ContinuesAndAddsR
280352 _recaptchaServiceMock . Verify ( ) ;
281353 Assert . IsInstanceOf < OkResult > ( _actionExecutingContext . Result ) ;
282354 Assert . IsTrue ( ( _actionExecutingContext . ActionArguments [ "argumentName" ] as ValidationResponse ) . Success ) ;
355+ Assert . AreEqual ( action , ( _actionExecutingContext . ActionArguments [ "argumentName" ] as ValidationResponse ) . Action ) ;
283356 Assert . AreEqual ( ( _actionExecutingContext . ActionArguments [ "argumentName" ] as ValidationResponse ) . Errors . Count ( ) , 0 ) ;
284357 }
285358
0 commit comments