'Test that tags can be set on an application'

@mock.patch(u'requests.post', mock_status_ok)
def test_tags(self):

    url = u'/v1/apps'
    response = self.client.post(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 201)
    app_id = response.data[u'id']
    url = u'/v1/apps/{app_id}/config'.format(**locals())
    response = self.client.get(url, content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    self.assertIn(u'tags', response.data)
    self.assertEqual(response.data[u'tags'], {})
    body = {u'tags': json.dumps({u'environ': u'dev'})}
    response = self.client.post(url, json.dumps(body), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 201)
    tags1 = response.data
    response = self.client.get(url, content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    self.assertIn(u'tags', response.data)
    tags = response.data[u'tags']
    self.assertIn(u'environ', tags)
    self.assertEqual(tags[u'environ'], u'dev')
    body = {u'tags': json.dumps({u'rack': u'1'})}
    response = self.client.post(url, json.dumps(body), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 201)
    tags2 = response.data
    self.assertNotEqual(tags1[u'uuid'], tags2[u'uuid'])
    tags = response.data[u'tags']
    self.assertIn(u'rack', tags)
    self.assertEqual(tags[u'rack'], u'1')
    self.assertIn(u'environ', tags)
    self.assertEqual(tags[u'environ'], u'dev')
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    tags3 = response.data
    self.assertEqual(tags2, tags3)
    tags = response.data[u'tags']
    self.assertIn(u'rack', tags)
    self.assertEqual(tags[u'rack'], u'1')
    self.assertIn(u'environ', tags)
    self.assertEqual(tags[u'environ'], u'dev')
    body = {u'tags': json.dumps({u'rack': None})}
    response = self.client.post(url, json.dumps(body), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 201)
    tags4 = response.data
    self.assertNotEqual(tags3[u'uuid'], tags4[u'uuid'])
    self.assertNotIn(u'rack', json.dumps(response.data[u'tags']))
    body = {u'tags': json.dumps({u'valid': u'in\nvalid'})}
    response = self.client.post(url, json.dumps(body), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 400)
    body = {u'tags': json.dumps({u'in.valid': u'valid'})}
    response = self.client.post(url, json.dumps(body), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 400)
    response = self.client.put(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 405)
    response = self.client.patch(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 405)
    response = self.client.delete(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 405)
