-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test known paths #263
Draft
hmpf
wants to merge
1
commit into
Uninett:master
Choose a base branch
from
hmpf:test-known-paths
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Test known paths #263
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from unittest import TestCase | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ViewDoesNotExist | ||
from django.urls.resolvers import get_resolver, URLPattern, URLResolver | ||
|
||
|
||
# Only paths/views created by us. Autogenerated stuff in the admin not needed | ||
# Get entries for the list via ``manage.py show_urls`` | ||
NECESSARY_PATHS = { | ||
"/api/schema/", | ||
"/api/schema/swagger-ui/", | ||
"/api/v1/auth/logout/", | ||
"/api/v1/auth/phone-number/", | ||
"/api/v1/auth/phone-number/<pk>/", | ||
"/api/v1/auth/user/", | ||
"/api/v1/auth/users/<int:pk>/", | ||
"/api/v1/incidents/", | ||
"/api/v1/incidents/<int:incident_pk>/acks/", | ||
"/api/v1/incidents/<int:incident_pk>/acks/<int:pk>/", | ||
"/api/v1/incidents/<int:incident_pk>/events/", | ||
"/api/v1/incidents/<int:incident_pk>/events/<int:pk>/", | ||
"/api/v1/incidents/<pk>/", | ||
"/api/v1/incidents/<pk>/ticket_url/", | ||
"/api/v1/incidents/metadata/", | ||
"/api/v1/incidents/mine/", | ||
"/api/v1/incidents/open+unacked/", | ||
"/api/v1/incidents/open/", | ||
"/api/v1/incidents/source-types/", | ||
"/api/v1/incidents/source-types/<pk>/", | ||
"/api/v1/incidents/sources/", | ||
"/api/v1/incidents/sources/<pk>/", | ||
"/api/v1/notificationprofiles/", | ||
"/api/v1/notificationprofiles/<pk>/", | ||
"/api/v1/notificationprofiles/<pk>/incidents/", | ||
"/api/v1/notificationprofiles/filterpreview/", | ||
"/api/v1/notificationprofiles/filters/", | ||
"/api/v1/notificationprofiles/filters/<pk>/", | ||
"/api/v1/notificationprofiles/preview/", | ||
"/api/v1/notificationprofiles/timeslots/", | ||
"/api/v1/notificationprofiles/timeslots/<pk>/", | ||
"/api/v1/token-auth/", | ||
} | ||
|
||
|
||
class UrlpatternFlattener: | ||
def __init__(self, urlpatterns=None, base="", namespace=None): | ||
if urlpatterns is None: | ||
urlconf = __import__(settings.ROOT_URLCONF, {}, {}, [""]) | ||
urlpatterns = urlconf.urlpatterns | ||
base = "/" | ||
self.urlpatterns = urlpatterns | ||
self.base = base | ||
self.namespace = namespace | ||
self.flat_urlpatterns = self.extract_views_from_urlpatterns(urlpatterns, base, namespace) | ||
|
||
@property | ||
def paths(self): | ||
paths = [] | ||
for _, path, _ in self.flat_urlpatterns: | ||
paths.append(path) | ||
return paths | ||
|
||
def extract_views_from_urlpatterns(self, urlpatterns, base="", namespace=None): | ||
""" | ||
Return a list of views from a list of urlpatterns. | ||
|
||
Each object in the returned list is a three-tuple: (view_func, regex, name) | ||
""" | ||
views = [] | ||
for p in urlpatterns: | ||
if isinstance(p, URLPattern): | ||
try: | ||
if not p.name: | ||
name = p.name | ||
elif namespace: | ||
name = "{0}:{1}".format(namespace, p.name) | ||
else: | ||
name = p.name | ||
pattern = str(p.pattern) | ||
views.append((p.callback, base + pattern, name)) | ||
except ViewDoesNotExist: | ||
continue | ||
elif isinstance(p, URLResolver): | ||
try: | ||
patterns = p.url_patterns | ||
except ImportError: | ||
continue | ||
if namespace and p.namespace: | ||
_namespace = "{0}:{1}".format(namespace, p.namespace) | ||
else: | ||
_namespace = p.namespace or namespace | ||
pattern = str(p.pattern) | ||
views.extend(self.extract_views_from_urlpatterns(patterns, base + pattern, namespace=_namespace)) | ||
elif hasattr(p, "_get_callback"): | ||
try: | ||
views.append((p._get_callback(), base + str(p.pattern), p.name)) | ||
except ViewDoesNotExist: | ||
continue | ||
elif hasattr(p, "url_patterns") or hasattr(p, "_get_url_patterns"): | ||
try: | ||
patterns = p.url_patterns | ||
except ImportError: | ||
continue | ||
views.extend(self.extract_views_from_urlpatterns(patterns, base + str(p.pattern), namespace=namespace)) | ||
else: | ||
raise TypeError("%s does not appear to be a urlpattern object" % p) | ||
return views | ||
|
||
|
||
class UrlsTest(TestCase): | ||
def test_no_necessary_paths_should_be_missing(self): | ||
Check warning on line 112 in tests/test_urls.py GitHub Actions / Test resultsAll 4 runs failed: test_no_necessary_paths_should_be_missing (tests.test_urls.UrlsTest)
Raw output
|
||
installed_paths = set(UrlpatternFlattener().paths) | ||
missing = NECESSARY_PATHS - installed_paths | ||
self.assertEqual(missing, set()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where's the assertion this test makes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deffo needed and deffo missing.