'Test that a registered user can cancel her account.'

def test_cancel(self):

    (username, password) = (u'newuser', u'password')
    submit = {u'username': username, u'password': password, u'first_name': u'Otto', u'last_name': u'Test', u'email': u'autotest@deis.io', u'is_superuser': True, u'is_staff': True}
    (other_username, other_password) = (u'newuser2', u'password')
    other_submit = {u'username': other_username, u'password': other_password, u'first_name': u'Test', u'last_name': u'Tester', u'email': u'autotest-2@deis.io', u'is_superuser': False, u'is_staff': False}
    url = u'/v1/auth/register'
    response = self.client.post(url, json.dumps(submit), content_type=u'application/json')
    self.assertEqual(response.status_code, 201)
    url = u'/v1/auth/cancel'
    user = User.objects.get(username=username)
    token = Token.objects.get(user=user).key
    response = self.client.delete(url, HTTP_AUTHORIZATION=u'token    {}'.format(token))
    self.assertEqual(response.status_code, 204)
    url = u'/v1/auth/register'
    response = self.client.post(url, json.dumps(other_submit), content_type=u'application/json')
    self.assertEqual(response.status_code, 201)
    url = u'/v1/auth/cancel'
    other_user = User.objects.get(username=other_username)
    other_token = Token.objects.get(user=other_user).key
    response = self.client.delete(url, json.dumps({u'username': self.admin.username}), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(other_token))
    self.assertEqual(response.status_code, 403)
    response = self.client.delete(url, json.dumps({u'username': other_username}), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.admin_token))
    self.assertEqual(response.status_code, 204)
    response = self.client.post(u'/v1/apps', HTTP_AUTHORIZATION=u'token    {}'.format(self.admin_token))
    self.assertEqual(response.status_code, 201)
    app_id = response.data[u'id']
    self.assertIn(u'id', response.data)
    response = self.client.delete(url, json.dumps({u'username': str(self.admin)}), content_type=u'application/json', HTTP_AUTHORIZATION=u'token    {}'.format(self.admin_token))
    self.assertEqual(response.status_code, 409)
