-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_registry.py
More file actions
139 lines (117 loc) · 5.19 KB
/
test_registry.py
File metadata and controls
139 lines (117 loc) · 5.19 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import json
import requests_mock
from django.core.cache import cache
from django.contrib.auth import get_user_model
from api.tests import adapter, DryccTransactionTestCase
User = get_user_model()
@requests_mock.Mocker(real_http=True, adapter=adapter)
class TestRegistry(DryccTransactionTestCase):
"""Tests setting and updating config values"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = self.get_or_create_token(self.user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
def tearDown(self):
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_registry(self, mock_requests):
"""
Test that registry information can be set on an application
"""
app_id = self.create_app()
# check default
url = f'/v2/apps/{app_id}/config'
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('registry', response.data)
self.assertEqual(response.data['registry'], {})
# set some registry information without PORT
body = {'registry': {'web': {'username': 'bob'}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry1 = response.data
# set required PORT
body = {'values': [{"name": "PORT", "value": "80", "group": "global"}]}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry1 = response.data
# no change error
body = {'registry': {'web': {'username': 'bobb'}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry1 = response.data
# check registry information again
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('registry', response.data)
registry = response.data['registry']['web']
self.assertIn('username', registry)
self.assertEqual(registry['username'], 'bobb')
# set an additional value
body = {'registry': {'web': {'password': 's3cur3pw1'}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry2 = response.data
self.assertNotEqual(registry1['uuid'], registry2['uuid'])
registry = response.data['registry']['web']
self.assertIn('password', registry)
self.assertEqual(registry['password'], 's3cur3pw1')
self.assertIn('username', registry)
self.assertEqual(registry['username'], 'bobb')
# read the registry information again
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
registry3 = response.data
self.assertEqual(registry2, registry3)
registry = response.data['registry']['web']
self.assertIn('password', registry)
self.assertEqual(registry['password'], 's3cur3pw1')
self.assertIn('username', registry)
self.assertEqual(registry['username'], 'bobb')
# unset a value
body = {'registry': {'web': {'password': None}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
registry4 = response.data
self.assertNotEqual(registry3['uuid'], registry4['uuid'])
self.assertNotIn('password', json.dumps(response.data['registry']))
# key error
body = {'registry': {'web': {'pa$$w0rd': 'woop'}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
# disallow put/patch/delete
response = self.client.put(url)
self.assertEqual(response.status_code, 405, response.data)
response = self.client.patch(url)
self.assertEqual(response.status_code, 405, response.data)
def test_registry_deploy(self, mock_requests):
"""
Test that registry information can be applied
"""
app_id = self.create_app()
# Set mandatory PORT
response = self.client.post(
f'/v2/apps/{app_id}/config',
{'values': [{"name": "PORT", "value": "4999", "group": "global"}]}
)
# Set registry information
body = {
'registry': {
'web': {
'username': 'bob',
'password': 's3cur3pw1'
}
}
}
response = self.client.post(
f'/v2/apps/{app_id}/config',
body
)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('username', response.data['registry']['web'])
self.assertIn('password', response.data['registry']['web'])
self.assertEqual(response.data['registry']['web']['username'], 'bob')
self.assertEqual(response.data['registry']['web']['password'], 's3cur3pw1')