-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Convert invites views TestCase to normal test function (#3321)
- Loading branch information
Showing
1 changed file
with
118 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,125 +4,136 @@ | |
|
||
import pytest | ||
from chargebee import APIError as ChargebeeAPIError | ||
from django.conf import settings | ||
from django.urls import reverse | ||
from django.utils import timezone | ||
from pytest_django.fixtures import SettingsWrapper | ||
from pytest_lazyfixture import lazy_fixture | ||
from pytest_mock.plugin import MockerFixture | ||
from rest_framework import status | ||
from rest_framework.test import APIClient, APITestCase | ||
from rest_framework.test import APIClient | ||
|
||
from organisations.invites.models import Invite, InviteLink | ||
from organisations.models import Organisation, OrganisationRole, Subscription | ||
from users.models import FFAdminUser | ||
|
||
|
||
class InviteLinkViewSetTestCase(APITestCase): | ||
def setUp(self) -> None: | ||
self.organisation = Organisation.objects.create(name="Test organisation") | ||
self.organisation_admin = FFAdminUser.objects.create(email="[email protected]") | ||
self.organisation_admin.add_organisation( | ||
self.organisation, role=OrganisationRole.ADMIN | ||
) | ||
self.client.force_authenticate(user=self.organisation_admin) | ||
|
||
def set_subscription_max_seats(self, max_seats): | ||
self.organisation.subscription.max_seats = max_seats | ||
self.organisation.subscription.save() | ||
|
||
def test_create_invite_link(self): | ||
# Given | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-list", | ||
args=[self.organisation.pk], | ||
) | ||
tomorrow = timezone.now() + timedelta(days=1) | ||
data = {"expires_at": tomorrow.strftime("%Y-%m-%d %H:%M:%S")} | ||
|
||
# When | ||
response = self.client.post(url, data=data) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_201_CREATED | ||
|
||
response_json = response.json() | ||
expected_attributes = ("hash", "id", "expires_at", "role") | ||
assert all(attr in response_json for attr in expected_attributes) | ||
|
||
def test_get_invite_links_for_organisation(self): | ||
# Given | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-list", | ||
args=[self.organisation.pk], | ||
) | ||
for role in OrganisationRole: | ||
InviteLink.objects.create(organisation=self.organisation, role=role.name) | ||
|
||
# update subscription to add another seat | ||
self.set_subscription_max_seats(2) | ||
|
||
# When | ||
response = self.client.get(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
|
||
response_json = response.json() | ||
assert len(response_json) == 2 | ||
expected_attributes = ("hash", "id", "expires_at") | ||
for invite_link in response_json: | ||
assert all(attr in invite_link for attr in expected_attributes) | ||
|
||
def test_get_invite_links_for_organisation_returns_400_if_seats_are_over(self): | ||
# Given | ||
settings.ENABLE_CHARGEBEE = True | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-list", | ||
args=[self.organisation.pk], | ||
) | ||
|
||
for role in OrganisationRole: | ||
InviteLink.objects.create(organisation=self.organisation, role=role.name) | ||
|
||
# When | ||
response = self.client.get(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
|
||
def test_delete_invite_link_for_organisation(self): | ||
# Given | ||
settings.ENABLE_CHARGEBEE = True | ||
invite = InviteLink.objects.create(organisation=self.organisation) | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-detail", | ||
args=[self.organisation.pk, invite.pk], | ||
) | ||
|
||
# update subscription to add another seat | ||
self.set_subscription_max_seats(2) | ||
|
||
# When | ||
response = self.client.delete(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
|
||
def test_delete_invite_link_for_organisation_return_400_if_seats_are_over(self): | ||
# Given | ||
settings.ENABLE_CHARGEBEE = True | ||
invite = InviteLink.objects.create(organisation=self.organisation) | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-detail", | ||
args=[self.organisation.pk, invite.pk], | ||
) | ||
|
||
# When | ||
response = self.client.delete(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
def test_create_invite_link( | ||
organisation: Organisation, | ||
admin_client: APIClient, | ||
) -> None: | ||
# Given | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-list", | ||
args=[organisation.pk], | ||
) | ||
tomorrow = timezone.now() + timedelta(days=1) | ||
expires_at = tomorrow.strftime("%Y-%m-%dT%H:%M:%SZ") | ||
data = {"expires_at": expires_at} | ||
|
||
# When | ||
response = admin_client.post(url, data=data) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_201_CREATED | ||
|
||
assert response.data["hash"] | ||
assert response.data["id"] | ||
assert response.data["expires_at"] == expires_at | ||
assert response.data["role"] == "USER" | ||
|
||
|
||
def test_get_invite_links_for_organisation( | ||
organisation: Organisation, | ||
admin_client: APIClient, | ||
) -> None: | ||
# Given | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-list", | ||
args=[organisation.pk], | ||
) | ||
for role in OrganisationRole: | ||
InviteLink.objects.create(organisation=organisation, role=role.name) | ||
|
||
# update subscription to add another seat | ||
organisation.subscription.max_seats = 3 | ||
organisation.subscription.save() | ||
|
||
# When | ||
response = admin_client.get(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
|
||
assert len(response.data) == 2 | ||
expected_attributes = ("hash", "id", "expires_at") | ||
for invite_link in response.data: | ||
assert all(attr in invite_link for attr in expected_attributes) | ||
|
||
|
||
def test_get_invite_links_for_organisation_returns_400_if_seats_are_over( | ||
settings: SettingsWrapper, | ||
organisation: Organisation, | ||
admin_client: APIClient, | ||
) -> None: | ||
# Given | ||
settings.ENABLE_CHARGEBEE = True | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-list", | ||
args=[organisation.pk], | ||
) | ||
|
||
for role in OrganisationRole: | ||
InviteLink.objects.create(organisation=organisation, role=role.name) | ||
|
||
# When | ||
response = admin_client.get(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
|
||
|
||
def test_delete_invite_link_for_organisation( | ||
settings: SettingsWrapper, | ||
organisation: Organisation, | ||
admin_client: APIClient, | ||
) -> None: | ||
# Given | ||
settings.ENABLE_CHARGEBEE = True | ||
invite = InviteLink.objects.create(organisation=organisation) | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-detail", | ||
args=[organisation.pk, invite.pk], | ||
) | ||
|
||
# update subscription to add another seat | ||
organisation.subscription.max_seats = 3 | ||
organisation.subscription.save() | ||
|
||
# When | ||
response = admin_client.delete(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
|
||
|
||
def test_delete_invite_link_for_organisation_return_400_if_seats_are_over( | ||
organisation: Organisation, | ||
admin_client: APIClient, | ||
settings: SettingsWrapper, | ||
) -> None: | ||
# Given | ||
settings.ENABLE_CHARGEBEE = True | ||
invite = InviteLink.objects.create(organisation=organisation) | ||
url = reverse( | ||
"api-v1:organisations:organisation-invite-links-detail", | ||
args=[organisation.pk, invite.pk], | ||
) | ||
|
||
# When | ||
response = admin_client.delete(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
|
||
|
||
def test_update_invite_link_returns_405(invite_link, admin_client, organisation): | ||
|
a329f6e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-preview – ./frontend
flagsmith-frontend-production-native.vercel.app
flagsmith-frontend-preview-git-main-flagsmith.vercel.app
flagsmith-frontend-preview-flagsmith.vercel.app
a329f6e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-staging – ./frontend
flagsmith-frontend-staging-flagsmith.vercel.app
flagsmith-staging-frontend.vercel.app
flagsmith-frontend-staging-git-main-flagsmith.vercel.app
staging.flagsmith.com
a329f6e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-flagsmith.vercel.app
docs.flagsmith.com
docs-git-main-flagsmith.vercel.app
docs.bullet-train.io