Skip to content

Commit

Permalink
chore: Convert invites views TestCase to normal test function (#3321)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Jan 25, 2024
1 parent 21cf0b8 commit a329f6e
Showing 1 changed file with 118 additions and 107 deletions.
225 changes: 118 additions & 107 deletions api/tests/unit/organisations/invites/test_unit_invites_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

3 comments on commit a329f6e

@vercel
Copy link

@vercel vercel bot commented on a329f6e Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a329f6e Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a329f6e Jan 25, 2024

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

Please sign in to comment.