def test_validate_ssl(cert, server_hostname, expected_error, error_message,
                      app):
    with NamedTemporaryFile(delete=False) as cert_file:
        cert_file.write(Bytes.for_string_or_unicode(cert[0]).as_encoded_str())
        cert_file.seek(0)

        with NamedTemporaryFile(delete=False) as key_file:
            key_file.write(
                Bytes.for_string_or_unicode(cert[1]).as_encoded_str())
            key_file.seek(0)

        def return_true(filename):
            return True

        def get_volume_file(filename, mode="r"):
            if filename == SSL_FILENAMES[0]:
                return open(cert_file.name, mode=mode)

            if filename == SSL_FILENAMES[1]:
                return open(key_file.name, mode=mode)

            return None

        config = {
            "PREFERRED_URL_SCHEME": "https",
            "SERVER_HOSTNAME": server_hostname,
        }

        with patch("app.config_provider.volume_file_exists", return_true):
            with patch("app.config_provider.get_volume_file", get_volume_file):
                validator = SSLValidator()
                config = ValidatorContext(config)
                config.config_provider = config_provider

                if expected_error is not None:
                    with pytest.raises(expected_error) as ipe:
                        validator.validate(config)

                    assert str(ipe.value) == error_message
                else:
                    validator.validate(config)
Exemple #2
0
def test_validated_jwt(username, password, expected_exception, app):
    with fake_jwt() as jwt_auth:
        config = {}
        config['AUTHENTICATION_TYPE'] = 'JWT'
        config['JWT_AUTH_ISSUER'] = jwt_auth.issuer
        config['JWT_VERIFY_ENDPOINT'] = jwt_auth.verify_url
        config['JWT_QUERY_ENDPOINT'] = jwt_auth.query_url
        config['JWT_GETUSER_ENDPOINT'] = jwt_auth.getuser_url

        unvalidated_config = ValidatorContext(config)
        unvalidated_config.user = AttrDict(dict(username=username))
        unvalidated_config.user_password = password
        unvalidated_config.config_provider = config_provider

        unvalidated_config.http_client = build_requests_session()

        if expected_exception is not None:
            with pytest.raises(ConfigValidationException):
                JWTAuthValidator.validate(
                    unvalidated_config,
                    public_key_path=jwt_auth.public_key_path)
        else:
            JWTAuthValidator.validate(unvalidated_config,
                                      public_key_path=jwt_auth.public_key_path)
Exemple #3
0
def test_invalid_config(unvalidated_config, app):
    with pytest.raises(ConfigValidationException):
        config = ValidatorContext(unvalidated_config)
        config.config_provider = config_provider
        JWTAuthValidator.validate(config)
Exemple #4
0
def test_validate_noop(unvalidated_config, app):
    config = ValidatorContext(unvalidated_config)
    config.config_provider = config_provider
    JWTAuthValidator.validate(config)