Skip to content

Commit

Permalink
add test for urls
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwojcik committed Nov 10, 2024
1 parent 4d296cf commit 22245bb
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
Empty file.
47 changes: 47 additions & 0 deletions testproject/testapp/tests/test_urls/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json
import pathlib

import pytest
from django.urls import get_resolver


@pytest.mark.django_db
def test_urls_have_not_changed(settings):
BASE_DIR = settings.BASE_DIR
if isinstance(BASE_DIR, str):
BASE_DIR = pathlib.Path(BASE_DIR)
TEST_PATH = BASE_DIR / "testapp" / "tests" / "test_urls"
FILE_PATH = TEST_PATH / "urls_snapshot.json"
url_patterns = get_resolver().url_patterns

def get_all_urls(patterns, prefix=""):
urls = []
for pattern in patterns:
if hasattr(pattern, "url_patterns"):
urls += get_all_urls(
pattern.url_patterns, prefix + pattern.pattern.regex.pattern
)
else:
pattern_str = prefix + pattern.pattern.regex.pattern
name = pattern.name if pattern.name else None
urls.append({"pattern": pattern_str, "name": name})
return urls

current_urls = sorted(get_all_urls(url_patterns), key=lambda x: x["pattern"])

if not FILE_PATH.exists():
with open(FILE_PATH, "w") as f:
json.dump(current_urls, f, indent=2)
pytest.fail(
"URL snapshot not found. Created snapshot with current URLs. Re-run the test." # noqa: E501
)

with open(FILE_PATH) as f:
saved_urls = json.load(f)

if current_urls != saved_urls:
with open(FILE_PATH, "w") as f:
json.dump(current_urls, f, indent=2)
pytest.fail(
"URL structure has changed. Updated snapshot with new URLs and names. Review the changes." # noqa: E501
)
142 changes: 142 additions & 0 deletions testproject/testapp/tests/test_urls/urls_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
[
{
"pattern": "^auth/^(?P<format>\\.[a-z0-9]+/?)\\Z",
"name": "api-root"
},
{
"pattern": "^auth/^\\Z",
"name": "api-root"
},
{
"pattern": "^auth/^jwt/create/?",
"name": "jwt-create"
},
{
"pattern": "^auth/^jwt/refresh/?",
"name": "jwt-refresh"
},
{
"pattern": "^auth/^jwt/verify/?",
"name": "jwt-verify"
},
{
"pattern": "^auth/^o/(?P<provider>\\S+)/$",
"name": "provider-auth"
},
{
"pattern": "^auth/^token/login/?$",
"name": "login"
},
{
"pattern": "^auth/^token/logout/?$",
"name": "logout"
},
{
"pattern": "^auth/^users/$",
"name": "user-list"
},
{
"pattern": "^auth/^users/(?P<id>[^/.]+)/$",
"name": "user-detail"
},
{
"pattern": "^auth/^users/(?P<id>[^/.]+)\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-detail"
},
{
"pattern": "^auth/^users/activation/$",
"name": "user-activation"
},
{
"pattern": "^auth/^users/activation\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-activation"
},
{
"pattern": "^auth/^users/me/$",
"name": "user-me"
},
{
"pattern": "^auth/^users/me\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-me"
},
{
"pattern": "^auth/^users/resend_activation/$",
"name": "user-resend-activation"
},
{
"pattern": "^auth/^users/resend_activation\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-resend-activation"
},
{
"pattern": "^auth/^users/reset_password/$",
"name": "user-reset-password"
},
{
"pattern": "^auth/^users/reset_password\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-reset-password"
},
{
"pattern": "^auth/^users/reset_password_confirm/$",
"name": "user-reset-password-confirm"
},
{
"pattern": "^auth/^users/reset_password_confirm\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-reset-password-confirm"
},
{
"pattern": "^auth/^users/reset_username/$",
"name": "user-reset-username"
},
{
"pattern": "^auth/^users/reset_username\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-reset-username"
},
{
"pattern": "^auth/^users/reset_username_confirm/$",
"name": "user-reset-username-confirm"
},
{
"pattern": "^auth/^users/reset_username_confirm\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-reset-username-confirm"
},
{
"pattern": "^auth/^users/set_password/$",
"name": "user-set-password"
},
{
"pattern": "^auth/^users/set_password\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-set-password"
},
{
"pattern": "^auth/^users/set_username/$",
"name": "user-set-username"
},
{
"pattern": "^auth/^users/set_username\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-set-username"
},
{
"pattern": "^auth/^users\\.(?P<format>[a-z0-9]+)/?$",
"name": "user-list"
},
{
"pattern": "^webauthn-example/$",
"name": null
},
{
"pattern": "^webauthn/^login/$",
"name": "webauthn_login"
},
{
"pattern": "^webauthn/^login_request/$",
"name": "webauthn_login_request"
},
{
"pattern": "^webauthn/^signup/(?P<ukey>.+)/$",
"name": "webauthn_signup"
},
{
"pattern": "^webauthn/^signup_request/$",
"name": "webauthn_signup_request"
}
]

0 comments on commit 22245bb

Please sign in to comment.