Skip to content

Commit

Permalink
Move health check tests to unit test (#1172)
Browse files Browse the repository at this point in the history
* BB2-3081 Move health check tests to unit test
  • Loading branch information
sharonfruit authored Feb 29, 2024
1 parent 3a78624 commit 0e64ece
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 76 deletions.
78 changes: 78 additions & 0 deletions apps/health/tests/test_healthchecks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import json

from django.test.client import Client
from httmock import HTTMock, all_requests
from apps.test import BaseApiTest


class TestHealthchecks(BaseApiTest):

def setUp(self):
self.client = Client()
self.url = "http://localhost"

@all_requests
def catchall(url, req, params):
return {
'status_code': 200,
'content': '{"test": 1}', # bfd check looks for json for success
}

def test_health_external(self):
self._call_health_external_endpoint(False)

def test_health_external_endpoint_v2(self):
self._call_health_external_endpoint(True)

def _call_health_external_endpoint(self, v2=False):
with HTTMock(self.catchall):
response = self.client.get(self.url + "/health/external_v2" if v2 else "/health/external")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")

def test_health_bfd_endpoint(self):
self._call_health_bfd_endpoint(False)

def test_health_bfd_endpoint_v2(self):
self._call_health_bfd_endpoint(True)

def _call_health_bfd_endpoint(self, v2=False):
with HTTMock(self.catchall):
response = self.client.get(self.url + "/health/bfd_v2" if v2 else "/health/bfd")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")

def test_health_db_endpoint(self):
response = self.client.get(self.url + "/health/db")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")

def test_health_sls_endpoint(self):
with HTTMock(self.catchall):
response = self.client.get(self.url + "/health/sls")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")
155 changes: 79 additions & 76 deletions apps/integration_tests/integration_test_fhir_resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import os
# import os

from django.conf import settings
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
Expand Down Expand Up @@ -177,81 +177,84 @@ def test_health_endpoint(self):
pass
self.assertEqual(msg, "all's well")

@override_switch('require-scopes', active=True)
def test_health_external_endpoint(self):
self._call_health_external_endpoint(False)

@override_switch('require-scopes', active=True)
def test_health_external_endpoint_v2(self):
self._call_health_external_endpoint(True)

def _call_health_external_endpoint(self, v2=False):
use_mslsx = os.environ.get('USE_MSLSX', None)
if use_mslsx is not None and not use_mslsx == 'true':
# do not ping health end point if using MSLSX
client = APIClient()
# no authenticate needed
response = client.get(self.live_server_url + "/health/external_v2" if v2 else "/health/external")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")

@override_switch('require-scopes', active=True)
def test_health_bfd_endpoint(self):
self._call_health_bfd_endpoint(False)

@override_switch('require-scopes', active=True)
def test_health_bfd_endpoint_v2(self):
self._call_health_bfd_endpoint(True)

def _call_health_bfd_endpoint(self, v2=False):
client = APIClient()
# no authenticate needed
response = client.get(self.live_server_url + "/health/bfd_v2" if v2 else "/health/bfd")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")

@override_switch('require-scopes', active=True)
def test_health_db_endpoint(self):
client = APIClient()
# no authenticate needed
response = client.get(self.live_server_url + "/health/db")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")

@override_switch('require-scopes', active=True)
def test_health_sls_endpoint(self):
use_mslsx = os.environ.get('USE_MSLSX', None)
if use_mslsx is not None and not use_mslsx == 'true':
# do not ping health end point if using MSLSX
client = APIClient()
# no authenticate needed
response = client.get(self.live_server_url + "/health/sls")
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
msg = None
try:
msg = content['message']
except KeyError:
pass
self.assertEqual(msg, "all's well")
# Commenting the following resulting some failures in the Cloudbees CI
# in contacting the test.accounts.cms.gov servers

# @override_switch('require-scopes', active=True)
# def test_health_external_endpoint(self):
# self._call_health_external_endpoint(False)

# @override_switch('require-scopes', active=True)
# def test_health_external_endpoint_v2(self):
# self._call_health_external_endpoint(True)

# def _call_health_external_endpoint(self, v2=False):
# use_mslsx = os.environ.get('USE_MSLSX', None)
# if use_mslsx is not None and not use_mslsx == 'true':
# # do not ping health end point if using MSLSX
# client = APIClient()
# # no authenticate needed
# response = client.get(self.live_server_url + "/health/external_v2" if v2 else "/health/external")
# self.assertEqual(response.status_code, 200)
# content = json.loads(response.content)
# msg = None
# try:
# msg = content['message']
# except KeyError:
# pass
# self.assertEqual(msg, "all's well")

# @override_switch('require-scopes', active=True)
# def test_health_bfd_endpoint(self):
# self._call_health_bfd_endpoint(False)

# @override_switch('require-scopes', active=True)
# def test_health_bfd_endpoint_v2(self):
# self._call_health_bfd_endpoint(True)

# def _call_health_bfd_endpoint(self, v2=False):
# client = APIClient()
# # no authenticate needed
# response = client.get(self.live_server_url + "/health/bfd_v2" if v2 else "/health/bfd")
# self.assertEqual(response.status_code, 200)
# content = json.loads(response.content)
# msg = None
# try:
# msg = content['message']
# except KeyError:
# pass
# self.assertEqual(msg, "all's well")

# @override_switch('require-scopes', active=True)
# def test_health_db_endpoint(self):
# client = APIClient()
# # no authenticate needed
# response = client.get(self.live_server_url + "/health/db")
# self.assertEqual(response.status_code, 200)
# content = json.loads(response.content)
# msg = None
# try:
# msg = content['message']
# except KeyError:
# pass
# self.assertEqual(msg, "all's well")

# @override_switch('require-scopes', active=True)
# def test_health_sls_endpoint(self):
# use_mslsx = os.environ.get('USE_MSLSX', None)
# if use_mslsx is not None and not use_mslsx == 'true':
# # do not ping health end point if using MSLSX
# client = APIClient()
# # no authenticate needed
# response = client.get(self.live_server_url + "/health/sls")
# self.assertEqual(response.status_code, 200)
# content = json.loads(response.content)
# msg = None
# try:
# msg = content['message']
# except KeyError:
# pass
# self.assertEqual(msg, "all's well")

@override_switch('require-scopes', active=True)
def test_userinfo_endpoint(self):
Expand Down

0 comments on commit 0e64ece

Please sign in to comment.