def test_v2_should_be_ok(self, recaptcha_response):
     token = generate_fake_token()
     recaptcha_response.return_value = {"success": True}
     # No exception means it worked
     check_recaptcha_token_is_valid(token,
                                    secret=settings.RECAPTCHA_SECRET,
                                    version=ReCaptchaVersion.V2)
    def test_should_raise_when_score_is_too_low(self, recaptcha_response):
        # Given
        token = generate_fake_token()
        recaptcha_response.return_value = {"success": True, "score": 0.2}

        # When
        with pytest.raises(InvalidRecaptchaTokenException):
            check_recaptcha_token_is_valid(
                token,
                secret=settings.RECAPTCHA_SECRET,
                version=ReCaptchaVersion.V3,
                original_action=ORIGINAL_ACTION,
                minimal_score=0.5,
            )
 def test_v3_should_be_ok(self, recaptcha_response):
     token = generate_fake_token()
     recaptcha_response.return_value = {
         "success": True,
         "score": 0.7,
         "action": ORIGINAL_ACTION
     }
     # No exception means it worked
     check_recaptcha_token_is_valid(
         token,
         secret=settings.RECAPTCHA_SECRET,
         version=ReCaptchaVersion.V3,
         original_action=ORIGINAL_ACTION,
         minimal_score=0.5,
     )
    def test_v2_should_raise_exception_with_details(self, recaptcha_response):
        # Given
        token = generate_fake_token()
        recaptcha_response.return_value = {
            "success": False,
            "error-codes": ["first-error", "second-error"],
        }

        # When
        with pytest.raises(ReCaptchaException) as exception:
            check_recaptcha_token_is_valid(token,
                                           secret=settings.RECAPTCHA_SECRET,
                                           version=ReCaptchaVersion.V2)

        assert str(
            exception.value
        ) == "Encountered the following error(s): ['first-error', 'second-error']"
    def test_should_raise_exception_for_any_other_error_code(
            self, recaptcha_response, error_code):
        # Given
        token = generate_fake_token()
        recaptcha_response.return_value = {
            "success": False,
            "error-codes": [error_code],
        }

        # When
        with pytest.raises(ReCaptchaException):
            check_recaptcha_token_is_valid(
                token,
                secret=settings.RECAPTCHA_SECRET,
                version=ReCaptchaVersion.V3,
                original_action=ORIGINAL_ACTION,
                minimal_score=0.5,
            )
    def test_should_raise_when_token_is_too_old_or_already_used(
            self, recaptcha_response):
        # Given
        token = generate_fake_token()
        recaptcha_response.return_value = {
            "success": False,
            "error-codes": ["timeout-or-duplicate"],
        }

        # When
        with pytest.raises(InvalidRecaptchaTokenException):
            check_recaptcha_token_is_valid(
                token,
                secret=settings.RECAPTCHA_SECRET,
                version=ReCaptchaVersion.V3,
                original_action=ORIGINAL_ACTION,
                minimal_score=0.5,
            )
    def test_should_raise_when_action_is_not_matching_the_original_action(
            self, recaptcha_response):
        # Given
        token = generate_fake_token()
        recaptcha_response.return_value = {
            "success": True,
            "score": 0.9,
            "action": "fake-action"
        }

        # When
        with pytest.raises(ReCaptchaException) as exception:
            check_recaptcha_token_is_valid(
                token,
                secret=settings.RECAPTCHA_SECRET,
                version=ReCaptchaVersion.V3,
                original_action=ORIGINAL_ACTION,
                minimal_score=0.5,
            )

        # Then
        assert str(
            exception.value
        ) == "The action 'fake-action' does not match 'submit' from the form"