Skip to content

Commit

Permalink
Make test output friendlier when there's no spec file yet
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFisher committed Jul 14, 2023
1 parent fa5000c commit 840ef7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions django_url_security/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
DEFAULT_URL_SECURITY_SPEC_FILENAME = 'url_security_spec.csv'


def get_spec_file_path():
def get_spec_file_path() -> Path:
from django.conf import settings

base_dir = Path(settings.BASE_DIR)
default_spec_file_path = base_dir / DEFAULT_URL_SECURITY_SPEC_FILENAME

spec_file_path = getattr(settings, 'URL_SECURITY_SPEC_FILE_PATH', default_spec_file_path)
spec_file_path = Path(getattr(settings, 'URL_SECURITY_SPEC_FILE_PATH', default_spec_file_path))
if not spec_file_path.is_absolute():
spec_file_path = base_dir / spec_file_path

Expand Down
21 changes: 21 additions & 0 deletions django_url_security/url_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
figure_out_view_kwargs,
find_permission_spec_for_view_info,
get_all_view_infos,
get_spec_file_path,
get_view_permission_specs,
view_should_be_public,
)
Expand Down Expand Up @@ -125,9 +126,12 @@ def get(self, view_func, url_pattern, **kwargs):
@parameterized.expand(
lambda: ((info,) for info in get_all_view_infos() if view_should_be_public(info)),
name_func=get_test_name,
skip_on_empty=True,
)
def test_public_views_are_public(self, view_info: ViewInfo):
permission_spec = find_permission_spec_for_view_info(view_info)
if not permission_spec: # sourcery skip: no-conditionals-in-tests
self.fail(f'No permission spec found for view {view_info}')
self.assert_outcome_matches_status(
permission_spec,
lambda: self.assert_view_is_public(view_info.view_func, view_info.url_pattern),
Expand All @@ -136,9 +140,12 @@ def test_public_views_are_public(self, view_info: ViewInfo):
@parameterized.expand(
lambda: ((info,) for info in get_all_view_infos() if not view_should_be_public(info)),
name_func=get_test_name,
skip_on_empty=True,
)
def test_non_public_views_are_not_public(self, view_info: ViewInfo):
permission_spec = find_permission_spec_for_view_info(view_info)
if not permission_spec: # sourcery skip: no-conditionals-in-tests
self.fail(f'No permission spec found for view {view_info}')
self.assert_outcome_matches_status(
permission_spec,
lambda: self.assert_view_is_not_public(view_info.view_func, view_info.url_pattern),
Expand Down Expand Up @@ -194,6 +201,20 @@ def assert_view_is_not_public(self, view_func, url_pattern):
response = self.get(view_func, url_pattern=url_pattern)
self.assertIn(response.status_code, acceptable_status_codes)

def test__specification_file_exists(self):
"""
Test that the permission specification file exists.
(Named with __ to ensure it runs first)
"""
spec_file_path = get_spec_file_path()
self.assertTrue(
spec_file_path.exists(),
(
f'No permission specification file found at {spec_file_path}, please run '
'`manage.py export_url_security_file` to generate it.'
),
)

def test_all_url_entries_are_specified(self):
"""
Test that all url entries have a corresponding permission specification,
Expand Down

0 comments on commit 840ef7e

Please sign in to comment.