|
| 1 | +from django.urls import resolve, reverse |
| 2 | +from django.test import TestCase |
| 3 | + |
| 4 | + |
| 5 | +class ProviderDirectorySpaRouting(TestCase): |
| 6 | + """ |
| 7 | + Ensure that provider_directory routing is agnostic to paths. |
| 8 | + """ |
| 9 | + |
| 10 | + def test_homepage_url_resolves_to_correct_view(self): |
| 11 | + resolver = resolve("/") |
| 12 | + self.assertEqual(resolver.view_name, "provider_directory:index") |
| 13 | + self.assertEqual(resolver.func.__name__, "index") |
| 14 | + |
| 15 | + def test_detail_url_resolves_with_path(self): |
| 16 | + resolver = resolve("/search/123/") |
| 17 | + self.assertEqual(resolver.view_name, "provider_directory:index_with_path") |
| 18 | + self.assertEqual(resolver.kwargs["path"], "search/123/") |
| 19 | + |
| 20 | + def test_reverse_index_returns_path(self): |
| 21 | + path = reverse("provider_directory:index") |
| 22 | + self.assertEqual(path, "/") |
| 23 | + |
| 24 | + def test_reverse_index_with_path_returns_path(self): |
| 25 | + path = reverse("provider_directory:index_with_path", kwargs={"path": "search/123"}) |
| 26 | + self.assertEqual(path, "/search/123") |
| 27 | + |
| 28 | + |
| 29 | +class FhirApiRouting(TestCase): |
| 30 | + """ |
| 31 | + Ensure we maintain a consistent routing configuration, with or without trailing slashes. |
| 32 | + """ |
| 33 | + |
| 34 | + def assert_valid_routing(self, path_views): |
| 35 | + for path, expected_view_name in path_views: |
| 36 | + with self.subTest(f"route {path}"): |
| 37 | + resolver = resolve(path) |
| 38 | + self.assertEqual(resolver.view_name, expected_view_name) |
| 39 | + |
| 40 | + def test_fhir_accepts_optional_trailing_slash(self): |
| 41 | + path_views = [ |
| 42 | + ("/fhir", "api-root"), |
| 43 | + ("/fhir/", "api-root"), |
| 44 | + ] |
| 45 | + self.assert_valid_routing(path_views) |
| 46 | + |
| 47 | + def test_fhir_docs_accepts_optional_trailing_slash(self): |
| 48 | + path_views = [ |
| 49 | + ("/fhir/docs", "schema-swagger-ui"), |
| 50 | + ("/fhir/docs/", "schema-swagger-ui"), |
| 51 | + # fallthrough |
| 52 | + ("/fhirdocs", "provider_directory:index_with_path"), |
| 53 | + ("/fhirdocs/", "provider_directory:index_with_path"), |
| 54 | + ] |
| 55 | + self.assert_valid_routing(path_views) |
| 56 | + |
| 57 | + def test_fhir_rest_routes_accept_optional_trailing_slash(self): |
| 58 | + path_views = [ |
| 59 | + # valid paths |
| 60 | + ("/fhir/Endpoint", "fhir-endpoint-list"), |
| 61 | + ("/fhir/Endpoint/", "fhir-endpoint-list"), |
| 62 | + ("/fhir/Endpoint/12345", "fhir-endpoint-detail"), |
| 63 | + ("/fhir/Endpoint/12345/", "fhir-endpoint-detail"), |
| 64 | + # fallthrough |
| 65 | + ("/fhirEndpoint", "provider_directory:index_with_path"), |
| 66 | + ("/fhirEndpoint/", "provider_directory:index_with_path"), |
| 67 | + ("/fhirEndpoint/12345", "provider_directory:index_with_path"), |
| 68 | + ("/fhirEndpoint/12345/", "provider_directory:index_with_path"), |
| 69 | + ("/fhir/Endpoint12345", "provider_directory:index_with_path"), |
| 70 | + ("/fhir/Endpoint12345/", "provider_directory:index_with_path"), |
| 71 | + ("/fhirEndpoint12345", "provider_directory:index_with_path"), |
| 72 | + ("/fhirEndpoint12345/", "provider_directory:index_with_path"), |
| 73 | + ] |
| 74 | + self.assert_valid_routing(path_views) |
| 75 | + |
| 76 | + def test_fhir_rest_routes_reverse_without_slash(self): |
| 77 | + endpoint_list_path = reverse("fhir-endpoint-list") |
| 78 | + self.assertEqual(endpoint_list_path, "/fhir/Endpoint") |
| 79 | + |
| 80 | + endpoint_detail_path = reverse("fhir-endpoint-detail", kwargs={"pk": 12345}) |
| 81 | + self.assertEqual(endpoint_detail_path, "/fhir/Endpoint/12345") |
0 commit comments