Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into DST-861
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-pettinga committed Jan 7, 2025
2 parents 31474cc + 4cbaef6 commit cb5f359
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ jobs:
name: Check missing migrations (changes to models that haven't been added to migrations)
command: pipenv run python django_app/manage.py makemigrations --check --dry-run --settings=config.settings.local

- run:
name: Apply migrations
command: pipenv run python django_app/manage.py migrate --settings=config.settings.local

- run:
name: Run tests and save coverage report
command: |
Expand Down
18 changes: 11 additions & 7 deletions django_app/healthcheck/checks/s3.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import boto3
import sentry_sdk
from core.document_storage import PermanentDocumentStorage, TemporaryDocumentStorage
from django.conf import settings


def s3_check() -> bool:
"""
Performs a check on the S3 connection
Check if the S3 bucket exists and ensure the app can access it.
https://boto3.amazonaws.com/v1/documentation/api/1.35.9/reference/services/s3/client/head_bucket.html
"""
temporary_document_bucket = TemporaryDocumentStorage().bucket
permanent_document_bucket = PermanentDocumentStorage().bucket
client = boto3.client("s3")

bucket_names = [settings.TEMPORARY_S3_BUCKET_NAME, settings.PERMANENT_S3_BUCKET_NAME]

try:
assert temporary_document_bucket.creation_date
assert permanent_document_bucket.creation_date
return True
for bucket_name in bucket_names:
client.head_bucket(Bucket=bucket_name)
except Exception as e:
sentry_sdk.capture_exception(e)
return False

return True
6 changes: 3 additions & 3 deletions django_app/healthcheck/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.views.generic import View
from healthcheck.checks import db_check
from healthcheck.checks import db_check, s3_check


class HealthCheckView(View):
Expand All @@ -20,8 +20,8 @@ def get(self, request: HttpRequest, *args: object, **kwargs: object) -> HttpResp

start = time.time()
is_db_good = db_check()
# is_s3_good = s3_check()
all_good = is_db_good and True
is_s3_good = s3_check()
all_good = is_db_good and is_s3_good

end = time.time()
time_taken = round(end - start, 3)
Expand Down
14 changes: 8 additions & 6 deletions tests/test_frontend/test_feedback/test_collect_feedback.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""class TestCollectFeedback(PlaywrightTestBase):
from feedback.models import FeedbackItem

from tests.test_frontend.conftest import PlaywrightTestBase


class TestCollectFeedback(PlaywrightTestBase):
def test_collect_full_feedback(self):
assert FeedbackItem.objects.count() == 0

Expand All @@ -9,14 +14,11 @@ def test_collect_full_feedback(self):

new_page = new_page_info.value
new_page.get_by_label("Very dissatisfied").click()
conditional_field = new_page.get_by_label("I did not experience any")
assert conditional_field.is_visible()
conditional_field.click()
new_page.get_by_role("checkbox", name="I did not find what I was looking for").click()
new_page.get_by_role("button", name="Submit").click()

assert FeedbackItem.objects.count() == 1
feedback_item = FeedbackItem.objects.first()
assert feedback_item.rating == 1
assert feedback_item.did_you_experience_any_issues == ["no"]
assert feedback_item.did_you_experience_any_issues == ["not_found"]
assert feedback_item.url == "/apply/"
"""
38 changes: 38 additions & 0 deletions tests/test_unit/test_healthcheck/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from unittest.mock import patch

import pytest
from django.urls import reverse

from tests.helpers import get_response_content


@pytest.fixture(autouse=True)
def setup():
"""Need to fix the Sites context processor as healthcheck views don't have a site."""
with patch("core.sites.context_processors.sites", return_value={}):
yield


@patch("healthcheck.checks.s3.boto3")
def test_successful_healthcheck(mock_s3_client, al_client):
mock_s3_client.head_bucket.return_value = {"test": True}
response = al_client.get(reverse("healthcheck:healthcheck_ping"))
content = get_response_content(response)
assert "OK" in content
assert response.status_code == 200


@patch("healthcheck.views.s3_check", return_value=False)
def test_s3_broken_healthcheck(mock_s3_check, al_client):
response = al_client.get(reverse("healthcheck:healthcheck_ping"))
content = get_response_content(response)
assert "FAIL" in content
assert response.status_code == 200


@patch("healthcheck.views.db_check", return_value=False)
def test_db_broken_healthcheck(mock_db_check, al_client):
response = al_client.get(reverse("healthcheck:healthcheck_ping"))
content = get_response_content(response)
assert "FAIL" in content
assert response.status_code == 200

0 comments on commit cb5f359

Please sign in to comment.