'Test that a release is created when an app is created, and
that updating config or build or triggers a new release'

@mock.patch(u'requests.post', mock_status_ok)
def test_release(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())
    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)
    self.assertIn(u'NEW_URL1', response.data[u'values'])
    url = u'/v1/apps/{app_id}/releases'.format(**locals())
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    self.assertEqual(response.data[u'count'], 2)
    url = u'/v1/apps/{app_id}/releases/v1'.format(**locals())
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    release1 = response.data
    self.assertIn(u'config', response.data)
    self.assertIn(u'build', response.data)
    self.assertEquals(release1[u'version'], 1)
    url = u'/v1/apps/{app_id}/releases/v2'.format(**locals())
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    release2 = response.data
    self.assertNotEqual(release1[u'uuid'], release2[u'uuid'])
    self.assertNotEqual(release1[u'config'], release2[u'config'])
    self.assertEqual(release1[u'build'], release2[u'build'])
    self.assertEquals(release2[u'version'], 2)
    url = u'/v1/apps/{app_id}/builds'.format(**locals())
    build_config = json.dumps({u'PATH': u'bin:/usr/local/bin:/usr/bin:/bin'})
    body = {u'image': u'autotest/example'}
    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.assertEqual(response.data[u'image'], body[u'image'])
    url = u'/v1/apps/{app_id}/releases/v3'.format(**locals())
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    release3 = response.data
    self.assertNotEqual(release2[u'uuid'], release3[u'uuid'])
    self.assertNotEqual(release2[u'build'], release3[u'build'])
    self.assertEquals(release3[u'version'], 3)
    url = u'/v1/apps/{app_id}/releases/v2'.format(**locals())
    response = self.client.get(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 200)
    release2 = response.data
    self.assertNotEqual(release2[u'uuid'], release3[u'uuid'])
    self.assertNotEqual(release2[u'build'], release3[u'build'])
    self.assertEquals(release2[u'version'], 2)
    url = u'/v1/apps/{app_id}/releases'.format(**locals())
    response = self.client.post(url, HTTP_AUTHORIZATION=u'token    {}'.format(self.token))
    self.assertEqual(response.status_code, 405)
    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 release3
