class TestInviteActivation(APITestCase):

    def setUp(self):
        self.invite = InviteFactory(first_name='Peter', last_name='Pan')
        self.activation_url = self.invite.get_activation_url()
        self.password = '******'
        self.post_data = {
            "user": {
                "password1": self.password,
                "password2": self.password
            }
        }
        self.response = self.client.patch(self.activation_url, data=self.post_data, format='json')

    def test_activation(self):
        assert self.response.status_code == status.HTTP_200_OK

    def test_user_created(self):
        User.objects.get(email=self.invite.email)

    def test_user_tenant_relationship_created(self):
        user = User.objects.get(email=self.invite.email)
        tenant = self.invite.tenant
        assert user.usertenantrelationship_set.filter(tenant=tenant)

    def test_user_can_login(self):
        login_path = reverse('rest_login')
        post_data = {"email": self.invite.email, "password": self.password}
        response = self.client.post(login_path, post_data)
        assert response.status_code == status.HTTP_200_OK

    def test_invite_already_used(self):
        response = self.client.patch(self.activation_url, data=self.post_data, format='json')
        assert response.data['error'] == 'The invitation was already used.'

    def test_existing_user(self):
        existing_user = UserFactory()
        existing_user_invite = InviteFactory(email=existing_user.email)
        existing_user_activation_url = existing_user_invite.get_activation_url()
        response = self.client.patch(existing_user_activation_url, data=self.post_data, format='json')
        assert response.data['error'] == 'You are already a user, please use the link from the email.'

    def test_inactive_invite(self):
        expired_creation_date = timezone.now() - timedelta(days=settings.TENANT_INVITE_EXPIRATION_IN_DAYS, hours=1)
        old_invite = InviteFactory(time_created=expired_creation_date)
        old_invite_activation_url = old_invite.get_activation_url()
        response = self.client.patch(old_invite_activation_url, data=self.post_data, format='json')
        assert response.data['error'] == 'The invitation was not used while it was active.'
 def test_inactive_invite(self):
     expired_creation_date = timezone.now() - timedelta(days=settings.TENANT_INVITE_EXPIRATION_IN_DAYS, hours=1)
     old_invite = InviteFactory(time_created=expired_creation_date)
     old_invite_activation_url = old_invite.get_activation_url()
     response = self.client.patch(old_invite_activation_url, data=self.post_data, format='json')
     assert response.data['error'] == 'The invitation was not used while it was active.'
 def test_existing_user(self):
     existing_user = UserFactory()
     existing_user_invite = InviteFactory(email=existing_user.email)
     existing_user_activation_url = existing_user_invite.get_activation_url()
     response = self.client.patch(existing_user_activation_url, data=self.post_data, format='json')
     assert response.data['error'] == 'You are already a user, please use the link from the email.'