def test_env_whitelist_in_handler(app_config_file, handler_file, whitelist_file, headers): app = Flask("mytestname") webhook = WebhookApp(app, loadconfig=False) # Add handler for checking the environment is correct handler = os.path.abspath("tests/handler_test_env.py") add_handler(handler_file, "testevent", handler) # Add whitelist whitelist_env = {'FIRSTENV': 'foo', 'SECONDENV': 'bar'} whitelist = ' '.join(list(whitelist_env)) add_whitelist(whitelist_file, "testevent", whitelist) webhook.loadconfig() test_client = webhook.app.test_client() token = 'foo' data = '' headers['X-Hub-Signature'] = b'sha1=' + get_digest(token, data) headers['X-GitHub-Event'] = b'testevent' # patch os.environ so it actually contains the values we are whitelisting with patch.dict(os.environ, whitelist_env): # This should return 200 rv = test_client.post('/webhook', headers=headers, data=data) print(rv.status_code) print(rv.data) assert (rv.status_code == 200)
def test_arguments(flaskapp_mock): # Ensure the arguments are what is expected with pytest.raises(TypeError) as excinfo: WebhookApp() assert (("__init__() missing 1 required positional argument: " "'app'") in str(excinfo.value)) # Ensure non flask arguments fail with pytest.raises(AssertionError): WebhookApp(0)
def test_verify_digest(): token = "foo" data = "somedata" digest = b"sha1=" + get_digest(token, data) # Verify that a computed token by openssl matches one computed by # _verify_digest result = WebhookApp._verify_digest(token, data, digest) assert(result) # Verify that an invalid digest fails result = WebhookApp._verify_digest(token, data, "foo") assert(not result)
def test_verify_digest(): token = "foo" data = "somedata" digest = b"sha1=" + get_digest(token, data) # Verify that a computed token by openssl matches one computed by # _verify_digest result = WebhookApp._verify_digest(token, data, digest) assert (result) # Verify that an invalid digest fails result = WebhookApp._verify_digest(token, data, "foo") assert (not result)
def test_get_env_from_whitelist(flaskapp_mock): webhook = WebhookApp(flaskapp_mock, loadconfig=False) ############################### # Test that no whitelist_config returns the current environment webhook.whitelist_config = None env = webhook._get_env_from_whitelist('testevent') assert (env == os.environ) ############################### ############################### # Test that an event that doesn't exist returns an empty environment whitelist_config = Mock() whitelist_config.get = Mock(return_value='') webhook.whitelist_config = whitelist_config # patch os.environ so it actually contains the values we are whitelisting env = webhook._get_env_from_whitelist('testevent') assert (env == {}) ############################### ############################### # Test that the returned environment only contains the whitelisted values whitelist_env = {'FIRSTENV': 'foo', 'SECONDENV': 'bar'} whitelist_config = Mock() whitelist_config.get = Mock(return_value=' '.join(whitelist_env.keys())) webhook.whitelist_config = whitelist_config # patch os.environ so it actually contains the values we are whitelisting with patch.dict(os.environ, whitelist_env): env = webhook._get_env_from_whitelist('testevent') assert (env == whitelist_env)
def test_key_set_in_environment(request, flaskapp_mock): def fin(): os.environ.pop('CROPS_WEBHOOK_KEY') request.addfinalizer(fin) os.environ['CROPS_WEBHOOK_KEY'] = 'testkey' webhook = WebhookApp(flaskapp_mock) assert (webhook.key == 'testkey')
def test_gethandler_expected_result(flaskapp_mock, handler_file): with patch('crops_webhook.get_key') as mock_get_key: webhook = WebhookApp(flaskapp_mock) # Common mocking flaskapp_mock.config = {} config = Mock() webhook._load_handlers_config = Mock(return_value=config) webhook._handler_sane = Mock(return_value=True) webhook.app.config['HANDLERS_FILE'] = handler_file dirname = os.path.dirname(handler_file) # Check absolute paths work as expected config.get = Mock(return_value='/absolute') handler = webhook._gethandler('') assert (handler == '/absolute') # Check relative paths work as expected config.get = Mock(return_value='relative') handler = webhook._gethandler('') assert (handler == os.path.join(dirname, 'relative'))
def test_client(handler_file, app_config_file): app = Flask("mytestname") webhook = WebhookApp(app) return webhook.app.test_client()