-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_domain.py
More file actions
242 lines (207 loc) · 9 KB
/
test_domain.py
File metadata and controls
242 lines (207 loc) · 9 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
"""
Unit tests for the Deis api app.
Run the tests with "./manage.py test api"
"""
import json
from django.contrib.auth.models import User
from django.test import TestCase
from rest_framework.authtoken.models import Token
from api.models import Domain
class DomainTest(TestCase):
"""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
url = '/v2/apps'
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
self.assertEqual(response.status_code, 201)
self.app_id = response.data['id'] # noqa
def test_response_data(self):
"""Test that the serialized response contains only relevant data."""
response = self.client.post(
'/v2/apps',
json.dumps({'id': 'test'}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
response = self.client.post(
'/v2/apps/test/domains',
json.dumps({'domain': 'test-domain.example.com'}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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_manage_domain(self):
url = '/v2/apps/{app_id}/domains'.format(app_id=self.app_id)
test_domains = [
'test-domain.example.com',
'django.paas-sandbox',
'domain',
'not.too.loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
'3com.com',
'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,
json.dumps({'domain': domain}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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,
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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,
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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,
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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'.format(app_id=self.app_id)
url = '/v2/apps/{app_id}/domains/{domain}'.format(domain='test-domain.example.com',
app_id=self.app_id)
response = self.client.delete(url, content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token))
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,
json.dumps({'domain': domain}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
self.assertEqual(response.status_code, 201)
url = '/v2/apps/{app_id}/domains/{domain}'.format(domain=test_domains[0],
app_id=self.app_id)
response = self.client.delete(url, content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token))
self.assertEqual(response.status_code, 204)
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,
json.dumps({'domain': 'test-domain.example.com'}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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,
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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.pass--sandbox',
'domain1',
'3333.com',
'too.looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
'foo.*.bar.com',
'*',
]
for domain in test_domains:
msg = "failed on \"{}\"".format(domain)
response = self.client.post(
url,
json.dumps({'domain': domain}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
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
url = '/v2/apps'
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(token))
self.assertEqual(response.status_code, 201)
url = '/v2/apps/{}/domains'.format(self.app_id)
response = self.client.post(
url,
json.dumps({'domain': 'example.deis.example.com'}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
self.assertEqual(response.status_code, 201)
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,
json.dumps({'id': app_id}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(self.token)
)
unauthorized_user = User.objects.get(username='autotest2')
unauthorized_token = Token.objects.get(user=unauthorized_user).key
url = '{}/{}/domains'.format(url, app_id)
response = self.client.post(
url,
json.dumps({'domain': 'example.com'}),
content_type='application/json',
HTTP_AUTHORIZATION='token {}'.format(unauthorized_token)
)
self.assertEqual(response.status_code, 403)