Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make OrganisationSubscriptionInformationCache.allowed_projects nullable #2716

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 3.2.20 on 2023-08-31 11:31
from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def update_allowed_projects_for_all_paid_subscriptions(
apps: Apps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
organisation_subscription_information_cache_model = apps.get_model(
"organisations", "organisationsubscriptioninformationcache"
)
organisation_subscription_information_cache_model.objects.exclude(
organisation__subscription__plan="free"
).update(allowed_projects=None)


def reverse(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
"""
Set the values for the OrganisationSubscriptionInformationCache objects back to 1 (which
is incorrect for paid subscriptions, but necessary to avoid IntegrityError when reversing
migrations)
"""

organisation_subscription_information_cache_model = apps.get_model(
"organisations", "organisationsubscriptioninformationcache"
)
organisation_subscription_information_cache_model.objects.exclude(
organisation__subscription__plan="free"
).update(allowed_projects=1)


class Migration(migrations.Migration):
dependencies = [
("organisations", "0045_auto_20230802_1956"),
]

operations = [
migrations.AlterField(
model_name="organisationsubscriptioninformationcache",
name="allowed_projects",
field=models.IntegerField(default=1, blank=True, null=True),
),
migrations.RunPython(
update_allowed_projects_for_all_paid_subscriptions, reverse_code=reverse
),
]
2 changes: 1 addition & 1 deletion api/organisations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,6 @@ class OrganisationSubscriptionInformationCache(models.Model):

allowed_seats = models.IntegerField(default=1)
allowed_30d_api_calls = models.IntegerField(default=50000)
allowed_projects = models.IntegerField(default=1)
allowed_projects = models.IntegerField(default=1, blank=True, null=True)

chargebee_email = models.EmailField(blank=True, max_length=254, null=True)
40 changes: 40 additions & 0 deletions api/organisations/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from datetime import datetime, timedelta
from unittest import TestCase, mock
from unittest.mock import MagicMock

import pytest
from django.conf import settings
Expand Down Expand Up @@ -582,6 +583,45 @@ def setUp(self) -> None:
)
self.subscription = Subscription.objects.get(organisation=self.organisation)

@mock.patch("organisations.views.extract_subscription_metadata")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not happy about having to continue to use these class based tests but ripping these up into functional pytest tests would be a fairly large effort that I don't think this PR warrants right now.

def test_chargebee_webhook(
self, mock_extract_subscription_metadata: MagicMock
) -> None:
# Given
seats = 3
api_calls = 100
mock_extract_subscription_metadata.return_value = ChargebeeObjMetadata(
seats=seats,
api_calls=api_calls,
projects=None,
chargebee_email=self.cb_user.email,
)
data = {
"content": {
"subscription": {
"status": "active",
"id": self.subscription_id,
},
"customer": {"email": self.cb_user.email},
}
}

# When
response = self.client.post(
self.url, data=json.dumps(data), content_type="application/json"
)

# Then
assert response.status_code == status.HTTP_200_OK

self.subscription.refresh_from_db()
subscription_cache = OrganisationSubscriptionInformationCache.objects.get(
organisation=self.subscription.organisation
)
assert subscription_cache.allowed_projects is None
assert subscription_cache.allowed_30d_api_calls == api_calls
assert subscription_cache.allowed_seats == seats

@mock.patch("organisations.models.cancel_chargebee_subscription")
def test_when_subscription_is_set_to_non_renewing_then_cancellation_date_set_and_alert_sent(
self, mocked_cancel_chargebee_subscription
Expand Down