From 8a87e8118d7f866cc5ad0743782600759e3d9c61 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 20 Aug 2016 19:19:18 -0300 Subject: [PATCH] Ivan - Backend - Add unit test for captcha validation [skip ci] --- server/run-tests.sh | 3 +- server/tests/__mocks__/BeanMock.php | 3 +- server/tests/__mocks__/ReCaptchaMock.php | 23 ++++++++++++ server/tests/__mocks__/RespectMock.php | 5 +++ server/tests/__mocks__/SettingMock.php | 1 + server/tests/lib/.gitkeep | 0 server/tests/libs/validations/captchaTest.php | 35 +++++++++++++++++++ 7 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 server/tests/__mocks__/ReCaptchaMock.php create mode 100644 server/tests/__mocks__/RespectMock.php delete mode 100644 server/tests/lib/.gitkeep create mode 100644 server/tests/libs/validations/captchaTest.php diff --git a/server/run-tests.sh b/server/run-tests.sh index 487b82e1..251e7164 100755 --- a/server/run-tests.sh +++ b/server/run-tests.sh @@ -1,2 +1,3 @@ phpunit --colors tests/models -phpunit --colors tests/controllers \ No newline at end of file +phpunit --colors tests/controllers +phpunit --colors tests/libs \ No newline at end of file diff --git a/server/tests/__mocks__/BeanMock.php b/server/tests/__mocks__/BeanMock.php index 56d7568a..e47421e1 100644 --- a/server/tests/__mocks__/BeanMock.php +++ b/server/tests/__mocks__/BeanMock.php @@ -1,6 +1,7 @@ returns(new \Mock([ + 'isSuccess' => \Mock::stub()->returns($value) + ])); + } + + public function __construct($privateKey) { + parent::__construct(); + + $this->privateKey = $privateKey; + $this->verify = ReCaptcha::$verify; + } + } +} \ No newline at end of file diff --git a/server/tests/__mocks__/RespectMock.php b/server/tests/__mocks__/RespectMock.php new file mode 100644 index 00000000..79d4d0eb --- /dev/null +++ b/server/tests/__mocks__/RespectMock.php @@ -0,0 +1,5 @@ +name = 'MOCK_SETTING_NAME'; $mockUserInstance->value = 'MOCK_SETTING_VALUE'; + $mockUserInstance->getValue = \Mock::stub()->returns('MOCK_SETTING_VALUE'); return $mockUserInstance; } diff --git a/server/tests/lib/.gitkeep b/server/tests/lib/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/server/tests/libs/validations/captchaTest.php b/server/tests/libs/validations/captchaTest.php new file mode 100644 index 00000000..618d0bc3 --- /dev/null +++ b/server/tests/libs/validations/captchaTest.php @@ -0,0 +1,35 @@ +validate('MOCK_RESPONSE'); + $this->assertTrue($response); + + \ReCaptcha\ReCaptcha::initVerify(false); + $response = $captchaValidation->validate('MOCK_RESPONSE'); + $this->assertFalse($response); + } + + public function testShouldPassCorrectValuesToCaptcha() { + $captchaValidation = new \CustomValidations\Captcha(); + $captchaValidation->validate('MOCK_RESPONSE'); + + $this->assertTrue(Setting::get('getSetting')->hasBeenCalledWithArgs('recaptcha-private')); + $this->assertTrue(\ReCaptcha\ReCaptcha::$verify->hasBeenCalledWithArgs('MOCK_RESPONSE', 'MOCK_REMOTE')); + } +} \ No newline at end of file