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

@mock.patch(u'requests.post', mock_status_ok)
def test_limit_memory(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'memory', response.data)
    self.assertEqual(response.data[u'memory'], {})
    self.assertNotIn(u'"', response.data[u'memory'])
    mem = {u'web': u'1G'}
    body = {u'memory': json.dumps(mem)}
    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)
    limit1 = 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'memory', response.data)
    memory = response.data[u'memory']
    self.assertIn(u'web', memory)
    self.assertEqual(memory[u'web'], u'1G')
    body = {u'memory': json.dumps({u'worker': u'512M'})}
    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)
    limit2 = response.data
    self.assertNotEqual(limit1[u'uuid'], limit2[u'uuid'])
    memory = response.data[u'memory']
    self.assertIn(u'worker', memory)
    self.assertEqual(memory[u'worker'], u'512M')
    self.assertIn(u'web', memory)
    self.assertEqual(memory[u'web'], u'1G')
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    limit3 = response.data
    self.assertEqual(limit2, limit3)
    memory = response.data[u'memory']
    self.assertIn(u'worker', memory)
    self.assertEqual(memory[u'worker'], u'512M')
    self.assertIn(u'web', memory)
    self.assertEqual(memory[u'web'], u'1G')
    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)
    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)
    memory = response.data[u'memory']
    self.assertIn(u'worker', memory)
    self.assertEqual(memory[u'worker'], u'512M')
    self.assertIn(u'web', memory)
    self.assertEqual(memory[u'web'], u'1G')
    body = {u'memory': json.dumps({u'worker': 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)
    limit4 = response.data
    self.assertNotEqual(limit3[u'uuid'], limit4[u'uuid'])
    self.assertNotIn(u'worker', json.dumps(response.data[u'memory']))
    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 limit4
