'Verify that posting the form creates an application if the user is
approved, and does not otherwise. Also ensure that if the user
already has an application, it is deleted before a new
application is created.'

@ddt.data((ApiAccessRequest.APPROVED, True, True), (ApiAccessRequest.DENIED, True, False), (ApiAccessRequest.PENDING, True, False), (ApiAccessRequest.APPROVED, False, True), (ApiAccessRequest.DENIED, False, False), (ApiAccessRequest.PENDING, False, False))
@ddt.unpack
def test_post(self, status, application_exists, new_application_created):

    if application_exists:
        old_application = ApplicationFactory(user=self.user)
    ApiAccessRequestFactory(user=self.user, status=status)
    self.client.post(self.url, {'name': 'test.com', 'redirect_uris': 'http://example.com'})
    applications = Application.objects.filter(user=self.user)
    if (application_exists and new_application_created):
        self.assertEqual(applications.count(), 1)
        self.assertNotEqual(old_application, applications[0])
    elif application_exists:
        self.assertEqual(applications.count(), 1)
        self.assertEqual(old_application, applications[0])
    elif new_application_created:
        self.assertEqual(applications.count(), 1)
    else:
        self.assertEqual(applications.count(), 0)
