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

Fix: adapt test for healthcheck user agents #1287

Merged
merged 57 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
d16642a
Update django from 3.2.3 to 3.2.4 (#68)
pyup-bot Jun 7, 2021
bfcc02a
Deploy into test (#85)
thekaveman Jun 16, 2021
c6c23a4
Deploy to test (#87)
thekaveman Jun 16, 2021
7ac5c38
Deploy to test (#94)
thekaveman Jun 18, 2021
9f698d2
fix: trying to hardcode ALLOWED_HOSTS
thekaveman Jun 23, 2021
0433618
revert: remove hardcoded ALLOWED_HOSTS
thekaveman Jun 23, 2021
6a98364
Deploy to test (#101)
thekaveman Jul 9, 2021
3c83a6d
Deploy to test (#106)
thekaveman Aug 20, 2021
ba5f95b
Deploy to test (#108)
thekaveman Aug 23, 2021
579cf44
Deploy to test (#139)
thekaveman Oct 14, 2021
e39b18b
Deploy to test (#145)
thekaveman Oct 19, 2021
9f49cff
fix: match requirements to dev/test
thekaveman Oct 19, 2021
2814247
Quick fix on prod requirements.txt (#147)
thekaveman Oct 19, 2021
d0a1dd5
Deploy to prod (#146)
thekaveman Oct 19, 2021
2037d86
Deploy to test (#149)
thekaveman Oct 19, 2021
e5ef3e2
Deploy to prod (#150)
thekaveman Oct 19, 2021
0b054ed
Deploy to test (#154)
thekaveman Oct 28, 2021
33545f5
Deploy to prod (#155)
thekaveman Oct 28, 2021
25bc70f
Deploy to test (#196)
thekaveman Nov 18, 2021
cca0747
Deploy to test (#215)
thekaveman Nov 19, 2021
811cadf
Deploy to test (#217)
thekaveman Nov 20, 2021
a411844
Deploy to test (#231)
thekaveman Nov 30, 2021
ec81e71
Deploy to prod (#232)
thekaveman Nov 30, 2021
6744953
Deploy to test (#249)
thekaveman Dec 5, 2021
e8f37c4
Deploy to prod (#250)
thekaveman Dec 5, 2021
9e2a390
Deploy to test (#258)
thekaveman Dec 8, 2021
0d2d2cb
Deploy to prod (#260)
thekaveman Dec 9, 2021
b13b710
Deploy 2021.12.1 to test (#271)
thekaveman Dec 17, 2021
bf4500a
Deploy 2022.01.1 to test (#289)
thekaveman Jan 18, 2022
35a2007
Release 2022.01.1 (#290)
thekaveman Jan 18, 2022
026ea61
Deploy to test (#299)
thekaveman Jan 27, 2022
288faac
Deploy 2022.01.2 to test (#301)
thekaveman Jan 27, 2022
ec9cc45
Release 2022.01.2 (#302)
thekaveman Jan 27, 2022
b601047
Deploy to test (#695)
thekaveman Jun 15, 2022
f3316cd
Merge pull request #748 from cal-itp/dev
afeld Jun 27, 2022
97f9e82
Merge pull request #770 from cal-itp/dev
afeld Jul 11, 2022
05f4006
Deploy to test (#824)
thekaveman Jul 22, 2022
a9266b4
Deploy to test (#854)
angela-tran Aug 8, 2022
65a3ad0
Deploy to test (#859)
thekaveman Aug 11, 2022
4787a42
Release: 2022.08.1 (#862)
thekaveman Aug 11, 2022
fe2d3df
Deploy to Test (#883)
thekaveman Aug 29, 2022
b781958
Deploy 2022.08.2 to test (#887)
thekaveman Aug 29, 2022
8e4cb1d
Release 2022.08.2 (#884)
thekaveman Aug 29, 2022
18de831
Deploy 2022.09.1 to test (#917)
angela-tran Sep 14, 2022
1137fb7
Release 2022.09.1 (#918)
angela-tran Sep 14, 2022
096ff5e
Deploy 2022.11.1 to test (#1131)
machikoyasuda Nov 10, 2022
0879732
Deploy 2022.11.1 to prod (#1132)
machikoyasuda Nov 14, 2022
f578ec7
Deploy 2022.12.1 to test (#1169)
afeld Dec 22, 2022
0bd84ff
Deploy 2023.01.1 to test (#1178)
angela-tran Jan 4, 2023
2b95c78
Release 2023.01.1 (#1170)
afeld Jan 4, 2023
4e64e55
fix: introduce setting for user agents that only see healthcheck
angela-tran Feb 16, 2023
9a27321
chore: rename test file to reflect the middleware name
angela-tran Feb 16, 2023
f8ff0fe
docs: add detail around the new HEALTHCHECK_USER_AGENTS setting
angela-tran Feb 16, 2023
1d423d0
chore(release): bump version to 2023.02.1
angela-tran Feb 17, 2023
921b95f
Fix: healthcheck user agents (#1285)
angela-tran Feb 17, 2023
4fe68d8
Chore: get hotfix into dev (#1286)
angela-tran Feb 17, 2023
d5a4f27
fix: adapt test to changes in dev
angela-tran Feb 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions benefits/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ def __call__(self, request):
return self.get_response(request)


class HealthcheckUserAgents(MiddlewareMixin):
"""Middleware to return healthcheck for user agents specified in HEALTHCHECK_USER_AGENTS."""

def process_request(self, request):
if hasattr(request, "META"):
user_agent = request.META.get("HTTP_USER_AGENT", "")
if user_agent in settings.HEALTHCHECK_USER_AGENTS:
return HttpResponse("Healthy", content_type="text/plain")

return self.get_response(request)


class VerifierSessionRequired(MiddlewareMixin):
"""Middleware raises an exception for sessions lacking an eligibility verifier configuration."""

Expand Down
2 changes: 2 additions & 0 deletions benefits/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def _filter_empty(ls):
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.locale.LocaleMiddleware",
"benefits.core.middleware.Healthcheck",
"benefits.core.middleware.HealthcheckUserAgents",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
Expand All @@ -67,6 +68,7 @@ def _filter_empty(ls):
if DEBUG:
MIDDLEWARE.append("benefits.core.middleware.DebugSession")

HEALTHCHECK_USER_AGENTS = _filter_empty(os.environ.get("HEALTHCHECK_USER_AGENTS", "").split(","))

CSRF_COOKIE_AGE = None
CSRF_COOKIE_SAMESITE = "Strict"
Expand Down
8 changes: 8 additions & 0 deletions docs/configuration/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ Django's primary secret, keep this safe!

Comma-separated list of hosts which are trusted origins for unsafe requests (e.g. POST)

### `HEALTHCHECK_USER_AGENTS`

!!! warning "Deployment configuration"

You must change this setting when deploying the app to a non-localhost domain

Comma-separated list of User-Agent strings which, when present as an HTTP header, should only receive healthcheck responses. Used by our `HealthcheckUserAgent` middleware.

## Cypress tests

!!! tldr "Cypress docs"
Expand Down
29 changes: 29 additions & 0 deletions tests/pytest/core/test_middleware_healthcheck_user_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.test import Client
from django.urls import reverse

import pytest

from benefits.core import session
from benefits.core.views import ROUTE_INDEX, TEMPLATE_INDEX


@pytest.mark.django_db
@pytest.mark.parametrize("user_agent", ["AlwaysOn", "Edge Health Probe"])
def test_healthcheck_user_agent(mocker, user_agent):
mocker.patch.object(session.settings, "HEALTHCHECK_USER_AGENTS", [user_agent])
client = Client(HTTP_USER_AGENT=user_agent)

response = client.get(reverse(ROUTE_INDEX))

assert response.status_code == 200
assert response.content.decode("UTF-8") == "Healthy"


@pytest.mark.django_db
def test_non_healthcheck_user_agent(mocker, client):
mocker.patch.object(session.settings, "HEALTHCHECK_USER_AGENTS", ["AlwaysOn"])

response = client.get(reverse(ROUTE_INDEX))

assert response.status_code == 200
assert response.template_name == TEMPLATE_INDEX