Last active
June 2, 2020 10:56
-
-
Save mo-mughrabi/50e6ba54bbc8b1bf5cea953dfabbbd98 to your computer and use it in GitHub Desktop.
Testing all admin django list pages
This file contains hidden or 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
| import pytest | |
| from django.conf import settings | |
| from django.urls import URLPattern, URLResolver | |
| def _admin_urls(): | |
| """ function to return all admin urls from urlconf.urlpatterns | |
| """ | |
| urlconf = __import__(settings.ROOT_URLCONF, {}, {}, [""]) | |
| def list_urls(lis, acc=None): | |
| if acc is None: | |
| acc = [] | |
| if not lis: | |
| return | |
| l = lis[0] | |
| if isinstance(l, URLPattern): | |
| yield acc + [str(l.pattern)] | |
| elif isinstance(l, URLResolver): | |
| yield from list_urls(l.url_patterns, acc + [str(l.pattern)]) | |
| yield from list_urls(lis[1:], acc) | |
| urls = [] | |
| for p in list_urls(urlconf.urlpatterns): | |
| url = "".join(p) | |
| if url.startswith("admin/"): | |
| # urls to skip in admin | |
| if "jsi18n" in url: | |
| continue | |
| if "<" in url: | |
| continue | |
| urls.append(f"/{url}") | |
| return urls | |
| @pytest.mark.parametrize("admin_url", _admin_urls()) | |
| @pytest.mark.django_db | |
| def test_admin_pages_reach(client, superuser, admin_url): | |
| client.force_login(superuser) | |
| resp = client.get(admin_url, follow=True) | |
| assert resp.status_code == 200 | |
| assert "<!DOCTYPE html".encode() in resp.content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment