-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_domain.py
More file actions
356 lines (294 loc) · 14.4 KB
/
test_domain.py
File metadata and controls
356 lines (294 loc) · 14.4 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"""
Unit tests for the Deis api app.
Run the tests with "./manage.py test api"
"""
from unittest import mock
from django.contrib.auth.models import User
from django.core.cache import cache
from rest_framework.test import APITestCase
from rest_framework.authtoken.models import Token
from api.models import Domain
from scheduler import KubeException
import idna
class DomainTest(APITestCase):
"""Tests creation of domains"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = Token.objects.get(user=self.user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = '/v2/apps'
response = self.client.post(url)
self.assertEqual(response.status_code, 201, response.data)
self.app_id = response.data['id'] # noqa
def tearDown(self):
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_response_data(self):
"""Test that the serialized response contains only relevant data."""
response = self.client.post(
'/v2/apps',
{'id': 'test'}
)
response = self.client.post(
'/v2/apps/test/domains',
{'domain': 'test-domain.example.com'}
)
for key in response.data:
self.assertIn(key, ['uuid', 'owner', 'created', 'updated', 'app', 'domain'])
expected = {
'owner': self.user.username,
'app': 'test',
'domain': 'test-domain.example.com'
}
self.assertDictContainsSubset(expected, response.data)
def test_strip_dot(self):
"""Test that a dot on the right side of the domain gets stripped"""
domain = 'autotest.127.0.0.1.xip.io.'
msg = "failed on '{}'".format(domain)
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
# Create
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 201, msg)
# Fetch
domain = 'autotest.127.0.0.1.xip.io' # stripped version
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id, domain], expected, msg)
def test_manage_idn_domain(self):
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
test_domains = [
'ドメイン.テスト',
'xn--eckwd4c7c.xn--zckzah',
'xn--80ahd1agd.ru',
'домена.ru',
'*.домена.испытание',
'täst.königsgäßchen.de',
'xn--tst-qla.xn--knigsgsschen-lcb0w.de',
'ドメイン.xn--zckzah',
'xn--eckwd4c7c.テスト',
'täst.xn--knigsgsschen-lcb0w.de',
'*.xn--tst-qla.königsgäßchen.de'
]
for domain in test_domains:
msg = "failed on '{}'".format(domain)
# Generate ACE and Unicode variant for domain
if domain.startswith("*."):
ace_domain = "*." + idna.encode(domain[2:]).decode("utf-8", "strict")
unicode_domain = "*." + idna.decode(ace_domain[2:])
else:
ace_domain = idna.encode(domain).decode("utf-8", "strict")
unicode_domain = idna.decode(ace_domain)
# Create
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 201, msg)
# Fetch
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id, ace_domain], expected, msg)
# Verify creation failure for same domain with different encoding
if ace_domain != domain:
response = self.client.post(url, {'domain': ace_domain})
self.assertEqual(response.status_code, 400, msg)
# Verify creation failure for same domain with different encoding
if unicode_domain != domain:
response = self.client.post(url, {'domain': unicode_domain})
self.assertEqual(response.status_code, 400, msg)
# Delete
url = '/v2/apps/{app_id}/domains/{hostname}'.format(hostname=domain,
app_id=self.app_id)
response = self.client.delete(url)
self.assertEqual(response.status_code, 204, msg)
# Verify removal
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
self.assertEqual(1, response.data['count'], msg)
# verify only app domain is left
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id], expected, msg)
# Use different encoding for creating and deleting (ACE)
if ace_domain != domain:
# Create
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 201, msg)
# Fetch
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id, ace_domain], expected, msg)
# Delete
url = '/v2/apps/{app_id}/domains/{hostname}'.format(hostname=ace_domain,
app_id=self.app_id)
response = self.client.delete(url)
self.assertEqual(response.status_code, 204, msg)
# Verify removal
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
self.assertEqual(1, response.data['count'], msg)
# verify only app domain is left
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id], expected, msg)
# Use different encoding for creating and deleting (Unicode)
if unicode_domain != domain:
# Create
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 201, msg)
# Fetch
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id, ace_domain], expected, msg)
# Delete
url = '/v2/apps/{app_id}/domains/{hostname}'.format(hostname=unicode_domain,
app_id=self.app_id)
response = self.client.delete(url)
self.assertEqual(response.status_code, 204, msg)
# Verify removal
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
self.assertEqual(1, response.data['count'], msg)
# verify only app domain is left
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id], expected, msg)
def test_manage_domain(self):
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
test_domains = [
'test-domain.example.com',
'django.paas-sandbox',
'django.paas--sandbox',
'domain',
'not.too.loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
'3com.com',
'domain1',
'3333.xyz',
'w3.example.com',
'MYDOMAIN.NET',
'autotest.127.0.0.1.xip.io',
'*.deis.example.com'
]
for domain in test_domains:
msg = "failed on '{}'".format(domain)
# Create
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 201, msg)
# Fetch
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id, domain], expected, msg)
# Delete
url = '/v2/apps/{app_id}/domains/{hostname}'.format(hostname=domain,
app_id=self.app_id)
response = self.client.delete(url)
self.assertEqual(response.status_code, 204, msg)
# Verify removal
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
response = self.client.get(url)
self.assertEqual(1, response.data['count'], msg)
# verify only app domain is left
expected = [data['domain'] for data in response.data['results']]
self.assertEqual([self.app_id], expected, msg)
def test_delete_domain_does_not_exist(self):
"""Remove a domain that does not exist"""
url = '/v2/apps/{app_id}/domains/{domain}'.format(domain='test-domain.example.com',
app_id=self.app_id)
response = self.client.delete(url)
self.assertEqual(response.status_code, 404)
def test_delete_domain_does_not_remove_latest(self):
"""https://github.com/deis/deis/issues/3239"""
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
test_domains = [
'test-domain.example.com',
'django.paas-sandbox',
]
for domain in test_domains:
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 201, response.data)
url = '/v2/apps/{app_id}/domains/{domain}'.format(domain=test_domains[0],
app_id=self.app_id)
response = self.client.delete(url)
self.assertEqual(response.status_code, 204, response.data)
with self.assertRaises(Domain.DoesNotExist):
Domain.objects.get(domain=test_domains[0])
def test_delete_domain_does_not_remove_others(self):
"""https://github.com/deis/deis/issues/3475"""
self.test_delete_domain_does_not_remove_latest()
self.assertEqual(Domain.objects.all().count(), 2)
def test_manage_domain_invalid_app(self):
# Create domain
url = '/v2/apps/{app_id}/domains'.format(app_id="this-app-does-not-exist")
response = self.client.post(url, {'domain': 'test-domain.example.com'})
self.assertEqual(response.status_code, 404)
# verify
url = '/v2/apps/{app_id}/domains'.format(app_id='this-app-does-not-exist')
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_manage_domain_invalid_domain(self):
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
test_domains = [
'this_is_an.invalid.domain',
'this-is-an.invalid.1',
'django.pa--assandbox',
'too.looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
'foo.*.bar.com',
'*',
'a' * 300,
'.'.join(['a'] * 128)
]
for domain in test_domains:
msg = "failed on \"{}\"".format(domain)
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 400, msg)
def test_admin_can_add_domains_to_other_apps(self):
"""If a non-admin user creates an app, an administrator should be able to add
domains to it.
"""
user = User.objects.get(username='autotest2')
token = Token.objects.get(user=user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
url = '/v2/apps'
response = self.client.post(url)
self.assertEqual(response.status_code, 201, response.data)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = '/v2/apps/{}/domains'.format(self.app_id)
response = self.client.post(url, {'domain': 'example.deis.example.com'})
self.assertEqual(response.status_code, 201, response.data)
def test_unauthorized_user_cannot_modify_domain(self):
"""
An unauthorized user should not be able to modify other domains.
Since an unauthorized user should not know about the application at all, these
requests should return a 404.
"""
app_id = 'autotest'
url = '/v2/apps'
response = self.client.post(url, {'id': app_id})
unauthorized_user = User.objects.get(username='autotest2')
unauthorized_token = Token.objects.get(user=unauthorized_user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
url = '{}/{}/domains'.format(url, app_id)
response = self.client.post(url, {'domain': 'example.com'})
self.assertEqual(response.status_code, 403)
def test_kubernetes_service_failure(self):
"""
Cause an Exception in kubernetes services
"""
self.client.post('/v2/apps', {'id': 'test'})
# scheduler.get_service exception
with mock.patch('scheduler.KubeHTTPClient.get_service') as mock_kube:
mock_kube.side_effect = KubeException('Boom!')
domain = 'foo.com'
url = '/v2/apps/test/domains'
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 503, response.data)
# scheduler.update_service exception
with mock.patch('scheduler.KubeHTTPClient.update_service') as mock_kube:
domain = 'foo.com'
url = '/v2/apps/test/domains'
response = self.client.post(url, {'domain': domain})
self.assertEqual(response.status_code, 201, response.data)
mock_kube.side_effect = KubeException('Boom!')
url = '/v2/apps/test/domains/{domain}'.format(domain=domain)
response = self.client.delete(url)
self.assertEqual(response.status_code, 503, response.data)