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

Update username regex pattern in routes #2566

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
68 changes: 68 additions & 0 deletions onadata/apps/main/tests/test_username_patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# import re
# from django.test import RequestFactory, TestCase
# from django.urls import resolve, reverse

# from django_digest.test import Client as DigestClient
# from django_digest.test import DigestAuth

# from onadata.apps.main.tests.test_base import TestBase
# from onadata.libs.permissions import DataEntryRole

# class TestUsernamePatterns(TestBase):
# def setUp(self):
# super().setUp()

# def test_submission_list_view(self):
# # Define test data
# usernames = ["test_user", "user-with-hyphen", "user.with.dot"]
# id_string = "sample_id_string"

# # Define the URL pattern name
# url_name = "form-list-username"

# # Iterate over usernames
# for username in usernames:
# # Generate the URL with parameters
# url = reverse(url_name, kwargs={'username': username})

# # Check if the URL matches the expected pattern and resolves to the correct view
# resolved = resolve(url)
# import ipdb; ipdb.set_trace()

# # Check if the resolved view has the correct resolver_match attribute
# self.assertEqual(resolved.url_name, "form-list-username")
# self.assertEqual(resolved.kwargs, {'username': username})


import re
from django.test import SimpleTestCase
from django.urls import resolve

class TestRoutePatternMatching(SimpleTestCase):
def test_route_pattern_matching(self):
# Define test data
urls = [
"/test_user/view/submissionList",
"/forms/example_xform_pk/view/submissionList",
"/projects/123/view/submissionList"
]

# Define the expected regular expressions for route patterns
patterns = [
r"^(?P<username>[\w.@-]+)/view/submissionList$",
r"^forms/(?P<xform_pk>\w+)/view/submissionList$",
r"^projects/(?P<project_pk>\d+)/view/submissionList$"
]

# Loop through each URL and corresponding pattern
for url, pattern in zip(urls, patterns):
# Resolve the URL and get the route pattern
resolved = resolve(url)
route_pattern = resolved.route
import ipdb; ipdb.set_trace()

# Test if the route pattern matches the expected regular expression
with self.subTest(url=url, pattern=pattern):
self.assertIsNotNone(re.match(pattern, url), f"The route pattern '{route_pattern}' does not match the expected regular expression '{pattern}'")


100 changes: 50 additions & 50 deletions onadata/apps/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
),
# briefcase api urls
re_path(
r"^(?P<username>\w+)/view/submissionList$",
r"^(?P<username>[^/]+)/view/submissionList$",
BriefcaseViewset.as_view({"get": "list", "head": "list"}),
name="view-submission-list",
),
Expand All @@ -212,7 +212,7 @@
name="view-submission-list",
),
re_path(
r"^(?P<username>\w+)/view/downloadSubmission$",
r"^(?P<username>[^/]+)/view/downloadSubmission$",
BriefcaseViewset.as_view({"get": "retrieve", "head": "retrieve"}),
name="view-download-submission",
),
Expand All @@ -227,117 +227,117 @@
name="view-download-submission",
),
re_path(
r"^(?P<username>\w+)/formUpload$",
r"^(?P<username>[^/]+)/formUpload$",
BriefcaseViewset.as_view({"post": "create", "head": "create"}),
name="form-upload",
),
re_path(
r"^(?P<username>\w+)/upload$",
r"^(?P<username>[^/]+)/upload$",
BriefcaseViewset.as_view({"post": "create", "head": "create"}),
name="upload",
),
# exporting stuff
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.csv$",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/data\.csv$",
viewer_views.data_export,
name="csv_export",
kwargs={"export_type": "csv"},
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.xls",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/data\.xls",
viewer_views.data_export,
name="xlsx_export",
kwargs={"export_type": "xlsx"},
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.csv.zip",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/data\.csv.zip",
viewer_views.data_export,
name="csv_zip_export",
kwargs={"export_type": "csv_zip"},
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.sav.zip",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/data\.sav.zip",
viewer_views.data_export,
name="sav_zip_export",
kwargs={"export_type": "sav_zip"},
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.kml$",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/data\.kml$",
viewer_views.kml_export,
name="kml-export",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.zip",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/data\.zip",
viewer_views.zip_export,
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/gdocs$",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/gdocs$",
viewer_views.google_xlsx_export,
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/map_embed",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/map_embed",
viewer_views.map_embed_view,
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/map",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/map",
viewer_views.map_view,
name="map-view",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/instance",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/instance",
viewer_views.instance,
name="submission-instance",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/enter-data",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/enter-data",
logger_views.enter_data,
name="enter_data",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/add-submission-with", # noqa
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/add-submission-with", # noqa
viewer_views.add_submission_with,
name="add_submission_with",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/thank_you_submission", # noqa
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/thank_you_submission", # noqa
viewer_views.thank_you_submission,
name="thank_you_submission",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/edit-data/(?P<data_id>\d+)$", # noqa
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/edit-data/(?P<data_id>\d+)$", # noqa
logger_views.edit_data,
name="edit_data",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/view-data",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/view-data",
viewer_views.data_view,
name="data-view",
),
re_path(
r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)/new$", # noqa
r"^(?P<username>[^/]+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)/new$", # noqa
viewer_views.create_export,
name="new-export",
),
re_path(
r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
r"^(?P<username>[^/]+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
"/delete$",
viewer_views.delete_export,
name="delete-export",
),
re_path(
r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
r"^(?P<username>[^/]+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
"/progress$",
viewer_views.export_progress,
name="export-progress",
),
re_path(
r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
r"^(?P<username>[^/]+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
"/$",
viewer_views.export_list,
name="export-list",
),
re_path(
r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
r"^(?P<username>[^/]+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)" # noqa
"/(?P<filename>[^/]+)$",
viewer_views.export_download,
name="export-download",
Expand Down Expand Up @@ -366,47 +366,47 @@
name="form-list",
),
re_path(
r"^(?P<username>\w+)/formList$",
r"^(?P<username>[^/]+)/formList$",
XFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-username",
),
re_path(
r"^projects/(?P<project_pk>\d+)/formList$",
XFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-projects",
),
re_path(
r"^enketo/(?P<xform_pk>\w+)/formList$",
XFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-enketo",
),
re_path(
r"^forms/(?P<xform_pk>\w+)/formList$",
XFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-forms",
),
re_path(
r"^enketo-preview/(?P<xform_pk>\w+)/formList$",
PreviewXFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-enketo-preview",
),
re_path(
r"^(?P<username>\w+)/(?P<xform_pk>\d+)/formList$",
r"^(?P<username>[^/]+)/(?P<xform_pk>\d+)/formList$",
XFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-username-xform",
),
re_path(
r"^preview/(?P<username>\w+)/(?P<xform_pk>\d+)/formList$",
r"^preview/(?P<username>[^/]+)/(?P<xform_pk>\d+)/formList$",
PreviewXFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-preview-xform",
),
re_path(
r"^preview/(?P<username>\w+)/formList$",
r"^preview/(?P<username>[^/]+)/formList$",
PreviewXFormListViewSet.as_view({"get": "list", "head": "list"}),
name="form-list",
name="form-list-preview-username",
),
re_path(
r"^(?P<username>\w+)/xformsManifest/(?P<pk>[\d+^/]+)$",
r"^(?P<username>[^/]+)/xformsManifest/(?P<pk>[\d+^/]+)$",
XFormListViewSet.as_view({"get": "manifest", "head": "manifest"}),
name="manifest-url",
),
Expand All @@ -416,13 +416,13 @@
name="manifest-url",
),
re_path(
r"^(?P<username>\w+)/xformsMedia/(?P<pk>[\d+^/]+)/(?P<metadata>[\d+^/.]+)$", # noqa
r"^(?P<username>[^/]+)/xformsMedia/(?P<pk>[\d+^/]+)/(?P<metadata>[\d+^/.]+)$", # noqa
XFormListViewSet.as_view({"get": "media", "head": "media"}),
name="xform-media",
),
re_path(
# pylint: disable=line-too-long
r"^(?P<username>\w+)/xformsMedia/(?P<pk>[\d+^/]+)/(?P<metadata>[\d+^/.]+)\.(?P<format>([a-z]|[0-9])*)$", # noqa
r"^(?P<username>[^/]+)/xformsMedia/(?P<pk>[\d+^/]+)/(?P<metadata>[\d+^/.]+)\.(?P<format>([a-z]|[0-9])*)$", # noqa
XFormListViewSet.as_view({"get": "media", "head": "media"}),
name="xform-media",
),
Expand All @@ -438,7 +438,7 @@
name="xform-media",
),
re_path(
r"^(?P<username>\w+)/submission$",
r"^(?P<username>[^/]+)/submission$",
XFormSubmissionViewSet.as_view({"post": "create", "head": "create"}),
name="submissions",
),
Expand All @@ -458,41 +458,41 @@
name="submissions",
),
re_path(
r"^(?P<username>\w+)/(?P<xform_pk>\d+)/submission$",
r"^(?P<username>[^/]+)/(?P<xform_pk>\d+)/submission$",
XFormSubmissionViewSet.as_view({"post": "create", "head": "create"}),
name="submissions",
),
re_path(r"^(?P<username>\w+)/bulk-submission$", logger_views.bulksubmission),
re_path(r"^(?P<username>[^/]+)/bulk-submission$", logger_views.bulksubmission),
re_path(
r"^(?P<username>\w+)/bulk-submission-form$", logger_views.bulksubmission_form
r"^(?P<username>[^/]+)/bulk-submission-form$", logger_views.bulksubmission_form
),
re_path(
r"^(?P<username>\w+)/forms/(?P<pk>[\d+^/]+)/form\.xml$",
r"^(?P<username>[^/]+)/forms/(?P<pk>[\d+^/]+)/form\.xml$",
XFormListViewSet.as_view({"get": "retrieve", "head": "retrieve"}),
name="download_xform",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/form\.xml$",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/form\.xml$",
logger_views.download_xform,
name="download_xform",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/form\.xls$",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/form\.xls$",
logger_views.download_xlsform,
name="download_xlsform",
),
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/form\.json",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/form\.json",
logger_views.download_jsonform,
name="download_jsonform",
),
re_path(
r"^(?P<username>\w+)/delete/(?P<id_string>[^/]+)/$",
r"^(?P<username>[^/]+)/delete/(?P<id_string>[^/]+)/$",
logger_views.delete_xform,
name="delete-xform",
),
re_path(
r"^(?P<username>\w+)/(?P<id_string>[^/]+)/toggle_downloadable/$",
r"^(?P<username>[^/]+)/(?P<id_string>[^/]+)/toggle_downloadable/$",
logger_views.toggle_downloadable,
name="toggle-downloadable",
),
Expand Down Expand Up @@ -530,7 +530,7 @@
),
# Stats tables
re_path(
r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/tables",
r"^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/tables",
viewer_views.stats_tables,
name="stats-tables",
),
Expand Down
Loading