github.com/blystad/deis@v0.11.0/controller/api/tests/test_domain.py (about) 1 """ 2 Unit tests for the Deis api app. 3 4 Run the tests with "./manage.py test api" 5 """ 6 7 from __future__ import unicode_literals 8 9 import json 10 11 from django.test import TestCase 12 from django.test.utils import override_settings 13 14 15 @override_settings(CELERY_ALWAYS_EAGER=True) 16 class DomainTest(TestCase): 17 18 """Tests creation of domains""" 19 20 fixtures = ['tests.json'] 21 22 def setUp(self): 23 self.assertTrue( 24 self.client.login(username='autotest', password='password')) 25 body = { 26 'id': 'autotest', 27 'domain': 'autotest.local', 28 'type': 'mock', 29 'hosts': 'host1,host2', 30 'auth': 'base64string', 31 'options': {}, 32 } 33 response = self.client.post('/api/clusters', json.dumps(body), 34 content_type='application/json') 35 self.assertEqual(response.status_code, 201) 36 url = '/api/apps' 37 body = {'cluster': 'autotest'} 38 response = self.client.post(url, json.dumps(body), content_type='application/json') 39 self.assertEqual(response.status_code, 201) 40 self.app_id = response.data['id'] # noqa 41 42 def test_manage_domain(self): 43 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 44 body = {'domain': 'test-domain.example.com'} 45 response = self.client.post(url, json.dumps(body), content_type='application/json') 46 self.assertEqual(response.status_code, 201) 47 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 48 response = self.client.get(url, content_type='application/json') 49 result = response.data['results'][0] 50 self.assertEqual('test-domain.example.com', result['domain']) 51 url = '/api/apps/{app_id}/domains/{hostname}'.format(hostname='test-domain.example.com', 52 app_id=self.app_id) 53 response = self.client.delete(url, content_type='application/json') 54 self.assertEqual(response.status_code, 204) 55 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 56 response = self.client.get(url, content_type='application/json') 57 self.assertEqual(0, response.data['count']) 58 59 def test_manage_domain_invalid_app(self): 60 url = '/api/apps/{app_id}/domains'.format(app_id="this-app-does-not-exist") 61 body = {'domain': 'test-domain.example.com'} 62 response = self.client.post(url, json.dumps(body), content_type='application/json') 63 self.assertEqual(response.status_code, 404) 64 url = '/api/apps/{app_id}/domains'.format(app_id='this-app-does-not-exist') 65 response = self.client.get(url, content_type='application/json') 66 self.assertEqual(response.status_code, 404) 67 68 def test_manage_domain_perms_on_app(self): 69 self.client.logout() 70 self.assertTrue( 71 self.client.login(username='autotest2', password='password')) 72 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 73 body = {'domain': 'test-domain2.example.com'} 74 response = self.client.post(url, json.dumps(body), content_type='application/json') 75 self.assertEqual(response.status_code, 201) 76 77 def test_manage_domain_invalid_domain(self): 78 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 79 body = {'domain': 'this_is_an.invalid.domain'} 80 response = self.client.post(url, json.dumps(body), content_type='application/json') 81 self.assertEqual(response.status_code, 400) 82 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 83 body = {'domain': 'this-is-an.invalid.a'} 84 response = self.client.post(url, json.dumps(body), content_type='application/json') 85 self.assertEqual(response.status_code, 400) 86 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 87 body = {'domain': 'domain'} 88 response = self.client.post(url, json.dumps(body), content_type='application/json') 89 self.assertEqual(response.status_code, 400) 90 91 def test_manage_domain_wildcard(self): 92 # Wildcards are not allowed for now. 93 url = '/api/apps/{app_id}/domains'.format(app_id=self.app_id) 94 body = {'domain': '*.deis.example.com'} 95 response = self.client.post(url, json.dumps(body), content_type='application/json') 96 self.assertEqual(response.status_code, 400)