github.com/amrnt/deis@v1.3.1/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_response_data(self):
    31          """Test that the serialized response contains only relevant data."""
    32          body = {'id': 'test'}
    33          response = self.client.post('/v1/apps', json.dumps(body),
    34                                      content_type='application/json',
    35                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
    36          body = {'domain': 'test-domain.example.com'}
    37          response = self.client.post('/v1/apps/test/domains', json.dumps(body),
    38                                      content_type='application/json',
    39                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
    40          for key in response.data.keys():
    41              self.assertIn(key, ['uuid', 'owner', 'created', 'updated', 'app', 'domain'])
    42          expected = {
    43              'owner': self.user.username,
    44              'app': 'test',
    45              'domain': 'test-domain.example.com'
    46          }
    47          self.assertDictContainsSubset(expected, response.data)
    48  
    49      def test_manage_domain(self):
    50          url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id)
    51          body = {'domain': 'test-domain.example.com'}
    52          response = self.client.post(url, json.dumps(body), content_type='application/json',
    53                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
    54          self.assertEqual(response.status_code, 201)
    55          url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id)
    56          response = self.client.get(url, content_type='application/json',
    57                                     HTTP_AUTHORIZATION='token {}'.format(self.token))
    58          result = response.data['results'][0]
    59          self.assertEqual('test-domain.example.com', result['domain'])
    60          url = '/v1/apps/{app_id}/domains/{hostname}'.format(hostname='test-domain.example.com',
    61                                                              app_id=self.app_id)
    62          response = self.client.delete(url, content_type='application/json',
    63                                        HTTP_AUTHORIZATION='token {}'.format(self.token))
    64          self.assertEqual(response.status_code, 204)
    65          url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id)
    66          response = self.client.get(url, content_type='application/json',
    67                                     HTTP_AUTHORIZATION='token {}'.format(self.token))
    68          self.assertEqual(0, response.data['count'])
    69  
    70      def test_manage_domain_invalid_app(self):
    71          url = '/v1/apps/{app_id}/domains'.format(app_id="this-app-does-not-exist")
    72          body = {'domain': 'test-domain.example.com'}
    73          response = self.client.post(url, json.dumps(body), content_type='application/json',
    74                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
    75          self.assertEqual(response.status_code, 404)
    76          url = '/v1/apps/{app_id}/domains'.format(app_id='this-app-does-not-exist')
    77          response = self.client.get(url, content_type='application/json',
    78                                     HTTP_AUTHORIZATION='token {}'.format(self.token))
    79          self.assertEqual(response.status_code, 404)
    80  
    81      def test_manage_domain_invalid_domain(self):
    82          url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id)
    83          body = {'domain': 'this_is_an.invalid.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          url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id)
    88          body = {'domain': 'this-is-an.invalid.a'}
    89          response = self.client.post(url, json.dumps(body), content_type='application/json',
    90                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
    91          self.assertEqual(response.status_code, 400)
    92          url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id)
    93          body = {'domain': 'domain'}
    94          response = self.client.post(url, json.dumps(body), content_type='application/json',
    95                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
    96          self.assertEqual(response.status_code, 400)
    97  
    98      def test_manage_domain_wildcard(self):
    99          """Wildcards are not allowed for now."""
   100          url = '/v1/apps/{app_id}/domains'.format(app_id=self.app_id)
   101          body = {'domain': '*.deis.example.com'}
   102          response = self.client.post(url, json.dumps(body), content_type='application/json',
   103                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
   104          self.assertEqual(response.status_code, 400)
   105  
   106      def test_admin_can_add_domains_to_other_apps(self):
   107          """If a non-admin user creates an app, an administrator should be able to add
   108          domains to it.
   109          """
   110          user = User.objects.get(username='autotest2')
   111          token = Token.objects.get(user=user).key
   112          url = '/v1/apps'
   113          response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(token))
   114          self.assertEqual(response.status_code, 201)
   115          url = '/v1/apps/{}/domains'.format(self.app_id)
   116          body = {'domain': 'example.deis.example.com'}
   117          response = self.client.post(url, json.dumps(body), content_type='application/json',
   118                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
   119          self.assertEqual(response.status_code, 201)
   120  
   121      def test_unauthorized_user_cannot_modify_domain(self):
   122          """
   123          An unauthorized user should not be able to modify other domains.
   124  
   125          Since an unauthorized user should not know about the application at all, these
   126          requests should return a 404.
   127          """
   128          app_id = 'autotest'
   129          url = '/v1/apps'
   130          body = {'id': app_id}
   131          response = self.client.post(url, json.dumps(body), content_type='application/json',
   132                                      HTTP_AUTHORIZATION='token {}'.format(self.token))
   133          unauthorized_user = User.objects.get(username='autotest2')
   134          unauthorized_token = Token.objects.get(user=unauthorized_user).key
   135          url = '{}/{}/domains'.format(url, app_id)
   136          body = {'domain': 'example.com'}
   137          response = self.client.post(url, json.dumps(body), content_type='application/json',
   138                                      HTTP_AUTHORIZATION='token {}'.format(unauthorized_token))
   139          self.assertEqual(response.status_code, 403)