diff --git a/api/organisations/invites/tests/__init__.py b/api/organisations/invites/tests/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/api/organisations/invites/tests/test_models.py b/api/organisations/invites/tests/test_models.py deleted file mode 100644 index 95731711823b..000000000000 --- a/api/organisations/invites/tests/test_models.py +++ /dev/null @@ -1,52 +0,0 @@ -from datetime import timedelta -from unittest import TestCase - -import pytest -from django.utils import timezone - -from organisations.invites.models import InviteLink -from organisations.models import Organisation - - -@pytest.mark.django_db -class InviteLinkTestCase(TestCase): - def setUp(self) -> None: - self.organisation = Organisation.objects.create(name="Test organisation") - - def test_is_expired_expiry_date_in_past(self): - # Given - yesterday = timezone.now() - timedelta(days=1) - expired_link = InviteLink.objects.create( - organisation=self.organisation, expires_at=yesterday - ) - - # When - is_expired = expired_link.is_expired - - # Then - assert is_expired - - def test_is_expired_expiry_date_in_future(self): - # Given - tomorrow = timezone.now() + timedelta(days=1) - expired_link = InviteLink.objects.create( - organisation=self.organisation, expires_at=tomorrow - ) - - # When - is_expired = expired_link.is_expired - - # Then - assert not is_expired - - def test_is_expired_no_expiry_date(self): - # Given - expired_link = InviteLink.objects.create( - organisation=self.organisation, expires_at=None - ) - - # When - is_expired = expired_link.is_expired - - # Then - assert not is_expired diff --git a/api/organisations/permissions/tests/__init__.py b/api/organisations/permissions/tests/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/api/organisations/tests/__init__.py b/api/organisations/tests/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/api/organisations/tests/test_tasks.py b/api/organisations/tests/test_tasks.py deleted file mode 100644 index 8ccc7dc1bd4d..000000000000 --- a/api/organisations/tests/test_tasks.py +++ /dev/null @@ -1,65 +0,0 @@ -import pytest - -from organisations.chargebee.metadata import ChargebeeObjMetadata -from organisations.subscriptions.constants import ( - FREE_PLAN_ID, - MAX_SEATS_IN_FREE_PLAN, -) -from organisations.subscriptions.xero.metadata import XeroSubscriptionMetadata -from organisations.tasks import ( - ALERT_EMAIL_MESSAGE, - ALERT_EMAIL_SUBJECT, - send_org_over_limit_alert, -) - - -def test_send_org_over_limit_alert_for_organisation_with_free_subscription( - organisation, mocker -): - # Given - mocked_ffadmin_user = mocker.patch("organisations.tasks.FFAdminUser") - - # When - send_org_over_limit_alert(organisation.id) - - # Then - args, kwargs = mocked_ffadmin_user.send_alert_to_admin_users.call_args - assert len(args) == 0 - assert len(kwargs) == 2 - assert kwargs["message"] == ALERT_EMAIL_MESSAGE % ( - organisation.name, - organisation.num_seats, - MAX_SEATS_IN_FREE_PLAN, - FREE_PLAN_ID, - ) - assert kwargs["subject"] == ALERT_EMAIL_SUBJECT - - -@pytest.mark.parametrize( - "SubscriptionMetadata", [ChargebeeObjMetadata, XeroSubscriptionMetadata] -) -def test_send_org_over_limit_alert_for_organisation_with_subscription( - organisation, subscription, mocker, SubscriptionMetadata -): - # Given - mocked_ffadmin_user = mocker.patch("organisations.tasks.FFAdminUser") - max_seats = 10 - mocker.patch( - "organisations.tasks.get_subscription_metadata", - return_value=SubscriptionMetadata(seats=max_seats), - ) - - # When - send_org_over_limit_alert(organisation.id) - - # Then - args, kwargs = mocked_ffadmin_user.send_alert_to_admin_users.call_args - assert len(args) == 0 - assert len(kwargs) == 2 - assert kwargs["message"] == ALERT_EMAIL_MESSAGE % ( - organisation.name, - organisation.num_seats, - max_seats, - subscription.plan, - ) - assert kwargs["subject"] == ALERT_EMAIL_SUBJECT diff --git a/api/organisations/invites/tests/conftest.py b/api/tests/unit/organisations/invites/conftest.py similarity index 100% rename from api/organisations/invites/tests/conftest.py rename to api/tests/unit/organisations/invites/conftest.py diff --git a/api/tests/unit/organisations/invites/test_unit_invites_models.py b/api/tests/unit/organisations/invites/test_unit_invites_models.py index 4e342f339d5b..66056761381e 100644 --- a/api/tests/unit/organisations/invites/test_unit_invites_models.py +++ b/api/tests/unit/organisations/invites/test_unit_invites_models.py @@ -1,7 +1,10 @@ import typing +from datetime import timedelta +from unittest import TestCase import pytest from django.db.utils import IntegrityError +from django.utils import timezone from organisations.invites.exceptions import InviteLinksDisabledError from organisations.invites.models import Invite, InviteLink @@ -12,6 +15,50 @@ from pytest_django.fixtures import SettingsWrapper +@pytest.mark.django_db +class InviteLinkTestCase(TestCase): + def setUp(self) -> None: + self.organisation = Organisation.objects.create(name="Test organisation") + + def test_is_expired_expiry_date_in_past(self): + # Given + yesterday = timezone.now() - timedelta(days=1) + expired_link = InviteLink.objects.create( + organisation=self.organisation, expires_at=yesterday + ) + + # When + is_expired = expired_link.is_expired + + # Then + assert is_expired + + def test_is_expired_expiry_date_in_future(self): + # Given + tomorrow = timezone.now() + timedelta(days=1) + expired_link = InviteLink.objects.create( + organisation=self.organisation, expires_at=tomorrow + ) + + # When + is_expired = expired_link.is_expired + + # Then + assert not is_expired + + def test_is_expired_no_expiry_date(self): + # Given + expired_link = InviteLink.objects.create( + organisation=self.organisation, expires_at=None + ) + + # When + is_expired = expired_link.is_expired + + # Then + assert not is_expired + + @pytest.mark.django_db def test_cannot_create_invite_link_if_disabled(settings: "SettingsWrapper") -> None: # Given diff --git a/api/organisations/invites/tests/test_views.py b/api/tests/unit/organisations/invites/test_unit_invites_views.py similarity index 100% rename from api/organisations/invites/tests/test_views.py rename to api/tests/unit/organisations/invites/test_unit_invites_views.py diff --git a/api/tests/unit/organisations/permissions/test_migrations.py b/api/tests/unit/organisations/permissions/test_unit_organisations_migrations.py similarity index 100% rename from api/tests/unit/organisations/permissions/test_migrations.py rename to api/tests/unit/organisations/permissions/test_unit_organisations_migrations.py diff --git a/api/organisations/permissions/tests/test_permissions.py b/api/tests/unit/organisations/permissions/test_unit_organisations_permissions.py similarity index 100% rename from api/organisations/permissions/tests/test_permissions.py rename to api/tests/unit/organisations/permissions/test_unit_organisations_permissions.py diff --git a/api/organisations/permissions/tests/test_views.py b/api/tests/unit/organisations/permissions/test_unit_organisations_views.py similarity index 100% rename from api/organisations/permissions/tests/test_views.py rename to api/tests/unit/organisations/permissions/test_unit_organisations_views.py diff --git a/api/organisations/tests/test_migrations.py b/api/tests/unit/organisations/test_unit_organisations_migrations.py similarity index 100% rename from api/organisations/tests/test_migrations.py rename to api/tests/unit/organisations/test_unit_organisations_migrations.py diff --git a/api/organisations/tests/test_models.py b/api/tests/unit/organisations/test_unit_organisations_models.py similarity index 100% rename from api/organisations/tests/test_models.py rename to api/tests/unit/organisations/test_unit_organisations_models.py diff --git a/api/tests/unit/organisations/test_unit_organisation_tasks.py b/api/tests/unit/organisations/test_unit_organisations_tasks.py similarity index 55% rename from api/tests/unit/organisations/test_unit_organisation_tasks.py rename to api/tests/unit/organisations/test_unit_organisations_tasks.py index 53f4082dbd8d..5d2441ddcce2 100644 --- a/api/tests/unit/organisations/test_unit_organisation_tasks.py +++ b/api/tests/unit/organisations/test_unit_organisations_tasks.py @@ -1,17 +1,81 @@ import uuid from datetime import timedelta +import pytest from django.utils import timezone +from organisations.chargebee.metadata import ChargebeeObjMetadata from organisations.models import ( Organisation, OrganisationRole, UserOrganisation, ) -from organisations.tasks import finish_subscription_cancellation +from organisations.subscriptions.constants import ( + FREE_PLAN_ID, + MAX_SEATS_IN_FREE_PLAN, +) +from organisations.subscriptions.xero.metadata import XeroSubscriptionMetadata +from organisations.tasks import ( + ALERT_EMAIL_MESSAGE, + ALERT_EMAIL_SUBJECT, + finish_subscription_cancellation, + send_org_over_limit_alert, +) from users.models import FFAdminUser +def test_send_org_over_limit_alert_for_organisation_with_free_subscription( + organisation, mocker +): + # Given + mocked_ffadmin_user = mocker.patch("organisations.tasks.FFAdminUser") + + # When + send_org_over_limit_alert(organisation.id) + + # Then + args, kwargs = mocked_ffadmin_user.send_alert_to_admin_users.call_args + assert len(args) == 0 + assert len(kwargs) == 2 + assert kwargs["message"] == ALERT_EMAIL_MESSAGE % ( + organisation.name, + organisation.num_seats, + MAX_SEATS_IN_FREE_PLAN, + FREE_PLAN_ID, + ) + assert kwargs["subject"] == ALERT_EMAIL_SUBJECT + + +@pytest.mark.parametrize( + "SubscriptionMetadata", [ChargebeeObjMetadata, XeroSubscriptionMetadata] +) +def test_send_org_over_limit_alert_for_organisation_with_subscription( + organisation, subscription, mocker, SubscriptionMetadata +): + # Given + mocked_ffadmin_user = mocker.patch("organisations.tasks.FFAdminUser") + max_seats = 10 + mocker.patch( + "organisations.tasks.get_subscription_metadata", + return_value=SubscriptionMetadata(seats=max_seats), + ) + + # When + send_org_over_limit_alert(organisation.id) + + # Then + args, kwargs = mocked_ffadmin_user.send_alert_to_admin_users.call_args + assert len(args) == 0 + assert len(kwargs) == 2 + assert kwargs["message"] == ALERT_EMAIL_MESSAGE % ( + organisation.name, + organisation.num_seats, + max_seats, + subscription.plan, + ) + assert kwargs["subject"] == ALERT_EMAIL_SUBJECT + + def test_finish_subscription_cancellation(db: None): organisation1 = Organisation.objects.create() organisation2 = Organisation.objects.create() diff --git a/api/tests/unit/organisations/test_unit_organisation_views.py b/api/tests/unit/organisations/test_unit_organisations_views.py similarity index 100% rename from api/tests/unit/organisations/test_unit_organisation_views.py rename to api/tests/unit/organisations/test_unit_organisations_views.py