-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_ping_json.py
117 lines (105 loc) · 4.51 KB
/
test_ping_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import json
import sys
from unittest import mock
import django
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
from django.urls import reverse
from moj_irat.views import PingJsonView
from tests.utils import TestCase
class PingJsonViewTestCase(TestCase):
def test_print_django_version(self):
print(f'Testing on django {django.__version__}', file=sys.stderr)
def call_ping_json_view(self, **view_kwargs):
view = PingJsonView.as_view(**view_kwargs)
request = HttpRequest()
request.method = 'GET'
response = view(request)
return response, json.loads(response.content.decode('utf-8'))
def test_ping_json_returns_minimum_environment(self):
env = {
'DOCKER_IMAGE_DATE': '2015-12-04T10:00:00+0000',
'DOCKER_IMAGE_SHA': 'e9866935d3c5d19adc48e0be3ad3f2718b86bfe4',
}
with mock.patch.dict('os.environ', env):
response, response_json = self.call_ping_json_view(
build_date_key='DOCKER_IMAGE_DATE',
commit_id_key='DOCKER_IMAGE_SHA',
)
self.assertEqual(response.status_code, 200)
self.assertDictEqual(response_json, {
'build_date': '2015-12-04T10:00:00+0000',
'commit_id': 'e9866935d3c5d19adc48e0be3ad3f2718b86bfe4',
})
def test_ping_json_returns_complete_environment(self):
env = {
'APP_BUILD_DATE': '2015-12-04T10:00:00+0000',
'APP_GIT_COMMIT': 'e9866935d3c5d19adc48e0be3ad3f2718b86bfe4',
'APP_BUILD_TAG': 'master.latest',
'JENKINS_TAG': 'TEST Django Utils',
}
with mock.patch.dict('os.environ', env):
response, response_json = self.call_ping_json_view(
build_date_key='APP_BUILD_DATE',
commit_id_key='APP_GIT_COMMIT',
version_number_key='APP_BUILD_TAG',
build_tag_key='JENKINS_TAG',
)
self.assertEqual(response.status_code, 200)
self.assertDictEqual(response_json, {
'build_date': '2015-12-04T10:00:00+0000',
'commit_id': 'e9866935d3c5d19adc48e0be3ad3f2718b86bfe4',
'version_number': 'master.latest',
'build_tag': 'TEST Django Utils',
})
def test_ping_json_returns_incomplete_environment_as_server_error(self):
env = {
'DOCKER_IMAGE_DATE': '2015-12-04T10:00:00+0000',
}
with mock.patch.dict('os.environ', env):
response, response_json = self.call_ping_json_view(
build_date_key='DOCKER_IMAGE_DATE',
commit_id_key='DOCKER_IMAGE_SHA',
)
self.assertEqual(response.status_code, 501)
self.assertDictEqual(response_json, {
'build_date': '2015-12-04T10:00:00+0000',
'commit_id': None,
})
def test_unconfigured_ping_json_environment_fails(self):
with self.assertRaises(ImproperlyConfigured):
self.call_ping_json_view(commit_id_key='DOCKER_IMAGE_SHA')
def test_incomplete_environment_ping_json_view(self):
response = self.client.get(reverse('ping_json'))
self.assertJsonResponse(response, {
'build_date': None,
'commit_id': None,
'version_number': None,
'build_tag': None,
}, status_code=501)
def test_complete_environment_ping_json_view(self):
env = {
'APP_BUILD_DATE': '2015-12-04T10:00:00+0000',
'APP_GIT_COMMIT': 'e9866935d3c5d19adc48e0be3ad3f2718b86bfe4',
'APP_BUILD_TAG': 'master.latest',
'JENKINS_TAG': 'TEST Django Utils',
}
with mock.patch.dict('os.environ', env):
response = self.client.get(reverse('ping_json'))
self.assertJsonResponse(response, {
'build_date': '2015-12-04T10:00:00+0000',
'commit_id': 'e9866935d3c5d19adc48e0be3ad3f2718b86bfe4',
'version_number': 'master.latest',
'build_tag': 'TEST Django Utils',
})
def test_cors_header_is_set_correctly(self):
env = {
'DOCKER_IMAGE_DATE': '2015-12-04T10:00:00+0000',
'DOCKER_IMAGE_SHA': 'e9866935d3c5d19adc48e0be3ad3f2718b86bfe4',
}
with mock.patch.dict('os.environ', env):
response, response_json = self.call_ping_json_view(
build_date_key='DOCKER_IMAGE_DATE',
commit_id_key='DOCKER_IMAGE_SHA',
)
self.assertEqual('*', response['access-control-allow-origin'])