Skip to content
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

add test for urls #855

Merged
merged 10 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pytest-django = "^4.5.2"
tox = "^4.4.8"
babel = "^2.12.1"
pytest-mock = "^3.14.0"
deepdiff = "^8.0.1"

[tool.poetry.group.code-quality.dependencies]
black = "^23.1.0"
Expand Down
Empty file.
52 changes: 52 additions & 0 deletions testproject/testapp/tests/test_urls/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import json
import pathlib

import pytest
from deepdiff import DeepDiff
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"])
# api-root generates different regex pattern locally vs in CI
current_urls = [el for el in current_urls if el["name"] != "api-root"]

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)

diff = DeepDiff(current_urls, saved_urls)
if diff:
with open(FILE_PATH, "w") as f:
json.dump(current_urls, f, indent=2)
pytest.fail(
f"URL structure has changed. Updated snapshot with new URLs and names. Diff:\n\n{diff}" # noqa: E501
)
134 changes: 134 additions & 0 deletions testproject/testapp/tests/test_urls/urls_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
[
{
"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"
}
]