-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_tls.py
More file actions
74 lines (60 loc) · 2.77 KB
/
test_tls.py
File metadata and controls
74 lines (60 loc) · 2.77 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
import requests_mock
from django.core.cache import cache
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from api.models import App
from api.tests import adapter, DryccTransactionTestCase
@requests_mock.Mocker(real_http=True, adapter=adapter)
class TestTLS(DryccTransactionTestCase):
"""Tests setting and updating config values"""
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)
def tearDown(self):
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_tls_enforced(self, mock_requests):
"""
Test that tls redirection can be enforced
"""
app_id = self.create_app()
app = App.objects.get(id=app_id)
data = {'https_enforced': True}
response = self.client.post(
'/v2/apps/{app_id}/tls'.format(**locals()),
data)
self.assertEqual(response.status_code, 201, response.data)
self.assertTrue(response.data.get('https_enforced'), response.data)
self.assertTrue(app.tls_set.latest().https_enforced)
data = {'https_enforced': False}
response = self.client.post(
'/v2/apps/{app_id}/tls'.format(**locals()),
data)
self.assertEqual(response.status_code, 201, response.data)
self.assertFalse(app.tls_set.latest().https_enforced)
# when the same data is sent again, a 409 is returned
conflict_response = self.client.post(
'/v2/apps/{app_id}/tls'.format(**locals()),
data)
self.assertEqual(conflict_response.status_code, 409, conflict_response.data)
self.assertFalse(app.tls_set.latest().https_enforced)
# also ensure that the previous tls UUID matches the latest,
# confirming this conflicting TLS object was deleted
self.assertEqual(response.data['uuid'], str(app.tls_set.latest().uuid))
# sending bad data returns a 400
data['https_enforced'] = "test"
response = self.client.post(
'/v2/apps/{app_id}/tls'.format(**locals()),
data)
self.assertEqual(response.status_code, 400, response.data)
def test_tls_created_on_app_create(self, mock_requests):
"""
Ensure that a TLS object is created for an App with default values.
See https://github.com/drycc/controller/issues/1042
"""
app_id = self.create_app()
response = self.client.get('/v2/apps/{}/tls'.format(app_id))
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data['https_enforced'], None)