def test_name_change_does_not_change_generated_key(mock_reverse): healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=True) healthcheck = healthchecks.Healthcheck() healthcheck.resolve() devKey = healthcheck.key healthcheck = healthchecks.Healthcheck( name="Something better than the default name") healthcheck.resolve() prodKey = healthcheck.key assert devKey == prodKey, "Name change should not affect produced job key"
def test_dev_mode_changes_generated_key(mock_reverse): healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=True) healthcheck = healthchecks.Healthcheck() healthcheck.resolve() devKey = healthcheck.key healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=False) healthcheck = healthchecks.Healthcheck() healthcheck.resolve() prodKey = healthcheck.key assert devKey != prodKey, "Expected DEBUG flag to change generated job key"
def test_dev_mode_false_when_debug_false(mock_reverse): healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=False) healthcheck = healthchecks.Healthcheck() healthcheck.resolve() payload = healthcheck.serialize() assert mock_reverse.call_count == 1, "Expected reverse() call once" assert 'dev' in payload, "Serialized payload missing 'dev' field" assert not payload['dev'], "Expected dev to be False"
def test_route_name_added_to_healthcheck(mock_url, mock_enqueue): healthchecks.settings = MockSettings(DEBUG=True) route_name = 'test-route' healthchecks.url('regex', 'view', healthchecks.Healthcheck(), name=route_name) assert mock_enqueue.call_count == 1, "Mock Client.enqueue not called once" assert mock_enqueue.call_args[0][ 0].route == route_name, "Mock Client.enqueue not called once"
def test_url_with_include_fails_silently_when_not_in_debug_mode(): healthchecks.settings = MockSettings(DEBUG=False) raised = False try: healthchecks.url('regex', ('included', 'url', 'tuple'), healthchecks.Healthcheck()) except healthchecks.HealthcheckError: raised = True finally: assert not raised, "Unexpected HealthcheckError exception raised"
def test_url_with_include_raises_exception_when_in_debug_mode(): healthchecks.settings = MockSettings(DEBUG=True) raised = False try: healthchecks.url('regex', ('included', 'url', 'tuple'), healthchecks.Healthcheck()) except healthchecks.HealthcheckError: raised = True finally: assert raised, "Expected HealthcheckError exception not raised"
def test_url_strips_healthcheck_before_calling_django_url( mock_url, mock_enqueue): healthchecks.settings = MockSettings(DEBUG=True) url_args = ('regex', 'view', healthchecks.Healthcheck()) expected_django_args = ('regex', 'view') healthchecks.url(*url_args) assert mock_enqueue.call_count == 1, "Mock Client.enqueue not called once" assert mock_url.call_count == 1, "Mock django_url not called once" assert mock_url.call_args == ( expected_django_args, ), "Expected args not passed to mock django_url"
def test_url_resolve_call(mock_reverse): route_name = 'example-route-name' healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=True) healthcheck = healthchecks.Healthcheck(route=route_name) healthcheck.resolve() payload = healthcheck.serialize() assert mock_reverse.call_args == (( route_name, ), ), "Args not passed to mock django_url" assert '/path/to/endpoint' in payload['request'][ 'url'], "Expected reverse() return path in generated request URL"
def test_generated_key_contains_only_valid_chars(mock_reverse): healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=True) healthcheck = healthchecks.Healthcheck() healthcheck.resolve() healthcheck = healthcheck.serialize() healthcheck = json.loads(json.dumps(healthcheck)) assert re.compile(r'[A-Za-z0-9]').match( healthcheck['key']), "Invalid healthcheck key: {}".format( healthcheck['key'])
def test_serialized_payload_includes_default_parts(mock_reverse): healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=False) healthcheck = healthchecks.Healthcheck() healthcheck.resolve() payload_keys = healthcheck.serialize().keys() assert 'dev' in payload_keys, "Request payload should have 'dev' field" assert 'request' in payload_keys, "Request payload should have 'request' field" assert 'key' in payload_keys, "Request payload should have 'key' field" assert 'defaultName' in payload_keys, "Request payload should have 'defaultName' field" assert 'type' in payload_keys, "Request payload should have 'type' field"
def test_url_resolve_call_includes_current_app_when_provided(mock_reverse): route_name = 'example-route-name' current_app = 'example-app-name' healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=True) healthcheck = healthchecks.Healthcheck(route=route_name, current_app=current_app) healthcheck.resolve() expected_args = ((route_name, ), {'current_app': 'example-app-name'}) assert mock_reverse.call_args == expected_args, "Args not passed to mock django_url"
def healthcheck_instance(): def mock_resolve(self): self._url = healthchecks.HealthcheckUrl( path='/path/to', querystring={} ) self._defaultName = self._create_name() self.key = self.key if self.key else self._create_key() healthchecks.settings = MockSettings(HEALTHCHECKS={'API_KEY': 'this is a key'}, DEBUG=True) instance = healthchecks.Healthcheck() instance.resolve = lambda: mock_resolve(instance) return instance
def test_monitor_tags_merged_with_settings_tags(mock_reverse): first_tags = ['Django', 'ExampleApp'] second_tags = ['LandingPages', 'Django'] merged_tags = ['Django', 'ExampleApp', 'LandingPages'] healthchecks.settings = MockSettings(HEALTHCHECKS={ 'HOSTNAME': 'cronitor.io', 'TAGS': first_tags }, DEBUG=False) healthcheck = healthchecks.Healthcheck(tags=second_tags) healthcheck.resolve() payload = healthcheck.serialize() assert set( payload['tags']) == set(merged_tags), "Tags not merged as expected"
def test_serialized_payload_includes_optional_parts(mock_reverse): healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=False) healthcheck = healthchecks.Healthcheck(name='Example Name', tags=['tag1'], note='Example Note', assertions=[{ 'rule_type': 'response_body', 'operator': 'contains', 'value': 'Cosmo', }]) healthcheck.resolve() payload_keys = healthcheck.serialize().keys() assert 'rules' in payload_keys, "Request payload should have 'rules' field" assert 'tags' in payload_keys, "Request payload should have 'tags' field" assert 'note' in payload_keys, "Request payload should have 'note' field" assert 'name' in payload_keys, "Request payload should have 'name' field"
def test_url_resolve_call_raises_error_with_both_args_and_kwargs(mock_reverse): route_name = 'example-route-name' healthchecks.settings = MockSettings( HEALTHCHECKS={'HOSTNAME': 'cronitor.io'}, DEBUG=True) healthcheck = healthchecks.Healthcheck(route=route_name, args=('a', 'b'), kwargs={ 'a': 1, 'b': 2 }) raised = False try: healthcheck.resolve() except healthchecks.HealthcheckError: raised = True finally: assert raised, "Expected HealthcheckError for using args and kwargs"
def test_url_enqueues_healthcheck(mock_url, mock_enqueue): healthchecks.settings = MockSettings(DEBUG=True) url_args = ('regex', 'view', healthchecks.Healthcheck()) healthchecks.url(*url_args) assert mock_enqueue.call_count == 1, "Mock Client.enqueue not called once" assert mock_url.call_count == 1, "Mock django_url not called once"