github.com/inflatablewoman/deis@v1.0.1-0.20141111034523-a4511c46a6ce/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.contrib.auth.models import User 12 from django.test import TestCase 13 from rest_framework.authtoken.models import Token 14 15 16 class DomainTest(TestCase): 17 18 """Tests creation of domains""" 19 20 fixtures = ['tests.json'] 21 22 def setUp(self): 23 self.user = User.objects.get(username='autotest') 24 self.token = Token.objects.get(user=self.user).key 25 url = '/v1/apps' 26 response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token)) 27 self.assertEqual(response.status_code, 201) 28 self.app_id = response.data['id'] # noqa 29 30 def test_manage_domain(self): 31 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 32 body = {'domain': 'test-domain.example.com'} 33 response = self.client.post(url, json.dumps(body), content_type='application/json', 34 HTTP_AUTHORIZATION='token {}'.format(self.token)) 35 self.assertEqual(response.status_code, 201) 36 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 37 response = self.client.get(url, content_type='application/json', 38 HTTP_AUTHORIZATION='token {}'.format(self.token)) 39 result = response.data['results'][0] 40 self.assertEqual('test-domain.example.com', result['domain']) 41 url = '/v1/apps/{app_id}/domains/{hostname}'.format(hostname='test-domain.example.com', 42 app_id=self.app_id) 43 response = self.client.delete(url, content_type='application/json', 44 HTTP_AUTHORIZATION='token {}'.format(self.token)) 45 self.assertEqual(response.status_code, 204) 46 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 47 response = self.client.get(url, content_type='application/json', 48 HTTP_AUTHORIZATION='token {}'.format(self.token)) 49 self.assertEqual(0, response.data['count']) 50 51 def test_manage_domain_invalid_app(self): 52 url = '/v1/apps/{app_id}/domains'.format(app_id="this-app-does-not-exist") 53 body = {'domain': 'test-domain.example.com'} 54 response = self.client.post(url, json.dumps(body), content_type='application/json', 55 HTTP_AUTHORIZATION='token {}'.format(self.token)) 56 self.assertEqual(response.status_code, 404) 57 url = '/v1/apps/{app_id}/domains'.format(app_id='this-app-does-not-exist') 58 response = self.client.get(url, content_type='application/json', 59 HTTP_AUTHORIZATION='token {}'.format(self.token)) 60 self.assertEqual(response.status_code, 404) 61 62 def test_manage_domain_perms_on_app(self): 63 user = User.objects.get(username='autotest2') 64 token = Token.objects.get(user=user).key 65 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 66 body = {'domain': 'test-domain2.example.com'} 67 response = self.client.post(url, json.dumps(body), content_type='application/json', 68 HTTP_AUTHORIZATION='token {}'.format(token)) 69 self.assertEqual(response.status_code, 201) 70 71 def test_manage_domain_invalid_domain(self): 72 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 73 body = {'domain': 'this_is_an.invalid.domain'} 74 response = self.client.post(url, json.dumps(body), content_type='application/json', 75 HTTP_AUTHORIZATION='token {}'.format(self.token)) 76 self.assertEqual(response.status_code, 400) 77 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 78 body = {'domain': 'this-is-an.invalid.a'} 79 response = self.client.post(url, json.dumps(body), content_type='application/json', 80 HTTP_AUTHORIZATION='token {}'.format(self.token)) 81 self.assertEqual(response.status_code, 400) 82 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 83 body = {'domain': 'domain'} 84 response = self.client.post(url, json.dumps(body), content_type='application/json', 85 HTTP_AUTHORIZATION='token {}'.format(self.token)) 86 self.assertEqual(response.status_code, 400) 87 88 def test_manage_domain_wildcard(self): 89 """Wildcards are not allowed for now.""" 90 url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id) 91 body = {'domain': '*.deis.example.com'} 92 response = self.client.post(url, json.dumps(body), content_type='application/json', 93 HTTP_AUTHORIZATION='token {}'.format(self.token)) 94 self.assertEqual(response.status_code, 400) 95 96 def test_admin_can_add_domains_to_other_apps(self): 97 """If a non-admin user creates an app, an administrator should be able to add 98 domains to it. 99 """ 100 user = User.objects.get(username='autotest2') 101 token = Token.objects.get(user=user).key 102 url = '/v1/apps' 103 response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(token)) 104 self.assertEqual(response.status_code, 201) 105 url = '/v1/apps/{}/domains'.format(self.app_id) 106 body = {'domain': 'example.deis.example.com'} 107 response = self.client.post(url, json.dumps(body), content_type='application/json', 108 HTTP_AUTHORIZATION='token {}'.format(self.token)) 109 self.assertEqual(response.status_code, 201)