Skip to content

Commit 29b3256

Browse files
authored
fix: Trailing slash in default routes, add tests for default routes (#43)
1 parent 4760718 commit 29b3256

File tree

7 files changed

+69
-11
lines changed

7 files changed

+69
-11
lines changed

settings/dev.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
PROMETHEUS_ENABLED = True
1616

1717
# Settings required for tests
18+
SECRET_KEY = "test"
1819
DATABASES = {
1920
"default": dj_database_url.parse(
2021
env(
@@ -30,6 +31,7 @@
3031
"task_processor",
3132
]
3233
MIDDLEWARE = [
34+
"django.middleware.common.CommonMiddleware",
3335
"common.gunicorn.middleware.RouteLoggerMiddleware",
3436
]
3537
LOG_FORMAT = "json"

src/common/core/urls.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from common.core import views
55

66
urlpatterns = [
7-
path("version", views.version_info),
8-
path("health/liveness", views.liveness),
9-
path("health/readiness", include("health_check.urls", namespace="health")),
7+
path("version/", views.version_info),
8+
path("health/liveness/", views.liveness),
9+
path("health/readiness/", include("health_check.urls", namespace="health")),
1010
re_path(r"^health", include("health_check.urls", namespace="health-deprecated")),
1111
# Aptible health checks must be on /healthcheck and cannot redirect
1212
# see https://www.aptible.com/docs/core-concepts/apps/connecting-to-apps/app-endpoints/https-endpoints/health-checks
1313
re_path(r"^healthcheck", include("health_check.urls", namespace="health-aptible")),
1414
]
1515

1616
if settings.PROMETHEUS_ENABLED:
17-
urlpatterns += [path("metrics", views.metrics)]
17+
urlpatterns += [path("metrics/", views.metrics)]

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1+
import os
2+
from importlib import reload
3+
4+
import prometheus_client
5+
import prometheus_client.values
6+
import pytest
7+
18
pytest_plugins = "flagsmith-test-tools"
9+
10+
11+
@pytest.fixture(scope="session")
12+
def prometheus_multiproc_dir(tmp_path_factory: pytest.TempPathFactory) -> str:
13+
os.environ["PROMETHEUS_MULTIPROC_DIR"] = prometheus_multiproc_dir_path = str(
14+
tmp_path_factory.mktemp("prometheus_multiproc")
15+
)
16+
reload(prometheus_client.values)
17+
return prometheus_multiproc_dir_path
18+
19+
20+
@pytest.fixture(scope="session")
21+
def test_metric(prometheus_multiproc_dir: str) -> prometheus_client.Counter:
22+
return prometheus_client.Counter(
23+
"pytest_tests_run_total",
24+
"Total number of tests run by pytest",
25+
["test_name"],
26+
)

tests/integration/__init__.py

Whitespace-only changes.

tests/integration/core/__init__.py

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import prometheus_client
2+
from rest_framework.test import APIClient
3+
4+
5+
def test_liveness_probe__return_expected(
6+
client: APIClient,
7+
) -> None:
8+
response = client.get("/health/liveness/")
9+
assert response.status_code == 200
10+
assert response.json() == {"status": "ok"}
11+
12+
13+
def test_metrics__return_expected(
14+
test_metric: prometheus_client.Counter,
15+
client: APIClient,
16+
) -> None:
17+
# Arrange
18+
test_metric.labels(test_name="test_metrics__return_expected").inc()
19+
20+
# Act
21+
response = client.get("/metrics", follow=True)
22+
23+
# Assert
24+
assert response.status_code == 200
25+
assert response.content == (
26+
"\n".join(
27+
[
28+
"# HELP pytest_tests_run_total Total number of tests run by pytest",
29+
"# TYPE pytest_tests_run_total counter",
30+
'pytest_tests_run_total{test_name="test_metrics__return_expected"} 1.0',
31+
"",
32+
]
33+
).encode()
34+
)

tests/unit/common/test_tools/test_plugin.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
from common.test_tools import AssertMetricFixture
55
from common.test_tools.plugin import assert_metric_impl
66

7-
test_metric = prometheus_client.Counter(
8-
"pytest_tests_run_total",
9-
"Total number of tests run by pytest",
10-
["test_name"],
11-
)
12-
137

148
def test_assert_metrics__asserts_expected(
159
assert_metric: AssertMetricFixture,
10+
test_metric: prometheus_client.Counter,
1611
) -> None:
1712
# Given
1813
test_metric.labels(test_name="test_assert_metrics__asserts_expected").inc()
@@ -25,7 +20,9 @@ def test_assert_metrics__asserts_expected(
2520
)
2621

2722

28-
def test_assert_metrics__registry_reset_expected() -> None:
23+
def test_assert_metrics__registry_reset_expected(
24+
test_metric: prometheus_client.Counter,
25+
) -> None:
2926
# Given
3027
test_metric.labels(test_name="test_assert_metrics__registry_reset_expected").inc()
3128

0 commit comments

Comments
 (0)