'Test that config is auto-created for a new app and that
config can be updated using a PATCH'

@mock.patch(u'requests.post', mock_status_ok)
def test_config(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, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    self.assertIn(u'values', response.data)
    self.assertEqual(response.data[u'values'], {})
    config1 = response.data
    body = {u'values': json.dumps({u'NEW_URL1': u'http://localhost:8080/'})}
    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)
    config2 = response.data
    self.assertNotEqual(config1[u'uuid'], config2[u'uuid'])
    self.assertIn(u'NEW_URL1', response.data[u'values'])
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    config3 = response.data
    self.assertEqual(config2, config3)
    self.assertIn(u'NEW_URL1', response.data[u'values'])
    body = {u'values': json.dumps({u'NEW_URL2': u'http://localhost:8080/'})}
    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)
    config3 = response.data
    self.assertNotEqual(config2[u'uuid'], config3[u'uuid'])
    self.assertIn(u'NEW_URL1', response.data[u'values'])
    self.assertIn(u'NEW_URL2', response.data[u'values'])
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    config4 = response.data
    self.assertEqual(config3, config4)
    self.assertIn(u'NEW_URL1', response.data[u'values'])
    self.assertIn(u'NEW_URL2', response.data[u'values'])
    body = {u'values': json.dumps({u'NEW_URL2': 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)
    config5 = response.data
    self.assertNotEqual(config4[u'uuid'], config5[u'uuid'])
    self.assertNotIn(u'NEW_URL2', json.dumps(response.data[u'values']))
    body = {u'values': json.dumps({u'NEW_URL1': 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)
    self.assertNotIn(u'NEW_URL1', json.dumps(response.data[u'values']))
    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)
    return config5
