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

Feature: removing unused tasks #899

Merged
merged 5 commits into from
Oct 16, 2024
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
211 changes: 77 additions & 134 deletions connect/api/v1/tests/test_v2_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
ProjectAuthorization,
RequestPermissionOrganization,
BillingPlan,
Invoice
Invoice,
)
from connect.common.mocks import StripeMockGateway
from unittest.mock import patch
import pendulum
from freezegun import freeze_time
from connect.billing.tasks import end_trial_plan, check_organization_plans, daily_contact_count
from connect.billing.tasks import (
end_trial_plan,
daily_contact_count,
)
from rest_framework import status
from connect.api.v1.billing.views import BillingViewSet
from connect.common.tasks import generate_project_invoice


class CreateOrganizationAPITestCase(TestCase):
Expand Down Expand Up @@ -64,19 +66,14 @@ def test_create(self, mock_get_gateway, mock_permission):
"name": "name",
"description": "desc",
"plan": "plan",
"authorizations": [
{
"user_email": "e@mail.com",
"role": 3
}
]
"authorizations": [{"user_email": "e@mail.com", "role": 3}],
},
"project": {
"date_format": "D",
"name": "Test Project",
"organization": "2575d1f9-f7f8-4a5d-ac99-91972e309511",
"timezone": "America/Argentina/Buenos_Aires",
}
},
}

response, content_data = self.request(data, self.owner_token)
Expand All @@ -93,19 +90,14 @@ def test_create_org_with_customer(self, mock_get_gateway, mock_permission):
"description": "Customer",
"plan": BillingPlan.PLAN_SCALE,
"customer": "cus_tomer",
"authorizations": [
{
"user_email": "e@mail.com",
"role": 3
}
]
"authorizations": [{"user_email": "e@mail.com", "role": 3}],
},
"project": {
"date_format": "D",
"name": "Test Project",
"timezone": "America/Argentina/Buenos_Aires",
"template": True
}
"template": True,
},
}
response, content_data = self.request(data, self.owner_token)
org = Organization.objects.get(uuid=content_data["organization"]["uuid"])
Expand All @@ -121,58 +113,57 @@ def test_create_template_project(self, mock_get_gateway, mock_permission):
"name": "name",
"description": "desc",
"plan": "plan",
"authorizations": [
{
"user_email": "e@mail.com",
"role": 3
}
]
"authorizations": [{"user_email": "e@mail.com", "role": 3}],
},
"project": {
"date_format": "D",
"name": "Test Project",
"organization": "2575d1f9-f7f8-4a5d-ac99-91972e309511",
"timezone": "America/Argentina/Buenos_Aires",
"template": True,
"template_type": "support"
}
"template_type": "support",
},
}
response, content_data = self.request(data, self.owner_token)
self.assertEquals(response.status_code, 201)
self.assertEquals(content_data.get("project").get("first_access"), True)
self.assertEquals(content_data.get("project").get("wa_demo_token"), "wa-demo-12345")
self.assertEquals(content_data.get("project").get("project_type"), "template:support")
self.assertEquals(content_data.get("project").get("redirect_url"), "https://wa.me/5582123456?text=wa-demo-12345")
self.assertEquals(
content_data.get("project").get("wa_demo_token"), "wa-demo-12345"
)
self.assertEquals(
content_data.get("project").get("project_type"), "template:support"
)
self.assertEquals(
content_data.get("project").get("redirect_url"),
"https://wa.me/5582123456?text=wa-demo-12345",
)
self.assertEquals(OrganizationAuthorization.objects.count(), 1)
self.assertEquals(RequestPermissionOrganization.objects.count(), 1)
self.assertEquals(Project.objects.count(), 1)
self.assertEquals(ProjectAuthorization.objects.count(), 1)

@patch("connect.common.signals.update_user_permission_project")
@patch("connect.billing.get_gateway")
def test_create_template_project_type_support(self, mock_get_gateway, mock_permission):
def test_create_template_project_type_support(
self, mock_get_gateway, mock_permission
):
mock_get_gateway.return_value = StripeMockGateway()
mock_permission.return_value = True
data = {
"organization": {
"name": "name",
"description": "desc",
"plan": "plan",
"authorizations": [
{
"user_email": "e@mail.com",
"role": 3
}
]
"authorizations": [{"user_email": "e@mail.com", "role": 3}],
},
"project": {
"date_format": "D",
"name": "Test Project",
"organization": "2575d1f9-f7f8-4a5d-ac99-91972e309511",
"timezone": "America/Argentina/Buenos_Aires",
"template": True,
"template_type": Project.TYPE_SUPPORT
}
"template_type": Project.TYPE_SUPPORT,
},
}
response, content_data = self.request(data, self.owner_token)
self.assertEquals(response.status_code, 201)
Expand Down Expand Up @@ -201,9 +192,7 @@ def setUp(self, mock_get_gateway, mock_permission) -> None:
)

self.project = self.organization.project.create(
name="will fail",
flow_organization=uuid.uuid4(),
is_template=True
name="will fail", flow_organization=uuid.uuid4(), is_template=True
)

def request(self, project_uuid, token=None):
Expand Down Expand Up @@ -242,7 +231,7 @@ def setUp(self, mock_get_gateway, mock_permission) -> None:
organization_billing__cycle=BillingPlan.BILLING_CYCLE_MONTHLY,
organization_billing__plan="free",
inteligence_organization=1,
organization_billing__stripe_customer="cus_MYOrndkgpPHGK9"
organization_billing__stripe_customer="cus_MYOrndkgpPHGK9",
)
self.trial = Organization.objects.create(
name="Trial org",
Expand Down Expand Up @@ -306,19 +295,26 @@ def request_upgrade_plan(self, organization_uuid=None, data=None, token=None):
format="json",
**authorization_header,
)
response = OrganizationViewSet.as_view({"patch": "upgrade_plan"})(request, organization_uuid)
response = OrganizationViewSet.as_view({"patch": "upgrade_plan"})(
request, organization_uuid
)
content_data = json.loads(response.content)
return response, content_data

def test_stripe_customer_kwarg(self):
"""Test new kwarg organization_billing__stripe_customer at organization create."""
self.assertEqual(self.organization.organization_billing.stripe_customer, "cus_MYOrndkgpPHGK9")
self.assertEqual(
self.organization.organization_billing.stripe_customer, "cus_MYOrndkgpPHGK9"
)

def test_assert_plans(self):
"""Test trial plan creation. Check if BillingPlan save method sets a end date to trial"""

self.assertEqual(self.trial.organization_billing.plan, BillingPlan.PLAN_TRIAL)
self.assertEqual(self.trial.organization_billing.trial_end_date, pendulum.now().end_of("day").add(months=1))
self.assertEqual(
self.trial.organization_billing.trial_end_date,
pendulum.now().end_of("day").add(months=1),
)

def test_end_trial_period(self):
"""Test BillingPlan method end_trial_period.
Expand Down Expand Up @@ -351,13 +347,9 @@ def test_upgrade_plan(self, mock_get_gateway):
mock_get_gateway.return_value = StripeMockGateway()
"""Test upgrade plan view"""
self.assertEqual(self.trial.organization_billing.plan, BillingPlan.PLAN_TRIAL)
data = {
"organization_billing_plan": BillingPlan.PLAN_START
}
data = {"organization_billing_plan": BillingPlan.PLAN_START}
response, content_data = self.request_upgrade_plan(
organization_uuid=self.trial.uuid,
data=data,
token=self.owner_token
organization_uuid=self.trial.uuid, data=data, token=self.owner_token
)

upgraded_org = Organization.objects.get(uuid=self.trial.uuid)
Expand All @@ -370,13 +362,11 @@ def test_upgrade_plan_stripe_failure(self):
"""Test response if stripe charge fails"""
data = {
"organization_billing_plan": BillingPlan.PLAN_START,
"stripe_failure": True
"stripe_failure": True,
}

response, content_data = self.request_upgrade_plan(
organization_uuid=self.trial.uuid,
data=data,
token=self.owner_token
organization_uuid=self.trial.uuid, data=data, token=self.owner_token
)

self.assertEqual(content_data["status"], "FAILURE")
Expand All @@ -389,9 +379,7 @@ def test_upgrade_plan_change_failure(self):
"organization_billing_plan": "baic",
}
response, content_data = self.request_upgrade_plan(
organization_uuid=self.trial.uuid,
data=data,
token=self.owner_token
organization_uuid=self.trial.uuid, data=data, token=self.owner_token
)
self.assertEqual(content_data["status"], "FAILURE")
self.assertEqual(content_data["message"], "Invalid plan choice")
Expand All @@ -403,9 +391,7 @@ def test_upgrade_plan_empty_failure(self):
"organization_billing_plan": "plus",
}
response, content_data = self.request_upgrade_plan(
organization_uuid=self.basic.uuid,
data=data,
token=self.owner_token
organization_uuid=self.basic.uuid, data=data, token=self.owner_token
)
self.assertEqual(content_data["status"], "FAILURE")
self.assertEqual(content_data["message"], "Empty customer")
Expand Down Expand Up @@ -458,7 +444,9 @@ def test_setup_plan(self, mock_get_gateway, mock_permission):
"plan": BillingPlan.PLAN_START,
"customer": "cus_MYOrndkgpPHGK9",
}
response, content_data = self.request(data=data, path="setup-plan", method="setup_plan")
response, content_data = self.request(
data=data, path="setup-plan", method="setup_plan"
)

customer = content_data["customer"]
self.assertEqual(content_data["status"], "SUCCESS")
Expand All @@ -473,26 +461,36 @@ def test_setup_plan(self, mock_get_gateway, mock_permission):
"description": "basic",
"plan": BillingPlan.PLAN_START,
"customer": customer,
"authorizations": [
{
"user_email": "e@mail.com",
"role": 3
}
]
"authorizations": [{"user_email": "e@mail.com", "role": 3}],
},
"project": {
"date_format": "D",
"name": "Test Project basic",
"organization": "2575d1f9-f7f8-4a5d-ac99-91972e309511",
"timezone": "America/Argentina/Buenos_Aires",
}
},
}
response, content_data = self.request_create_org(create_org_data, self.owner_token)
self.assertEqual(content_data["organization"]["organization_billing"]["plan"], BillingPlan.PLAN_START)
self.assertEqual(content_data["organization"]["organization_billing"]["final_card_number"], '42')
organization = Organization.objects.get(uuid=content_data["organization"]["uuid"])
self.assertEqual(organization.organization_billing_invoice.first().payment_status, Invoice.PAYMENT_STATUS_PAID)
self.assertEqual(organization.organization_billing_invoice.first().stripe_charge, "ch_teste")
response, content_data = self.request_create_org(
create_org_data, self.owner_token
)
self.assertEqual(
content_data["organization"]["organization_billing"]["plan"],
BillingPlan.PLAN_START,
)
self.assertEqual(
content_data["organization"]["organization_billing"]["final_card_number"],
"42",
)
organization = Organization.objects.get(
uuid=content_data["organization"]["uuid"]
)
self.assertEqual(
organization.organization_billing_invoice.first().payment_status,
Invoice.PAYMENT_STATUS_PAID,
)
self.assertEqual(
organization.organization_billing_invoice.first().stripe_charge, "ch_teste"
)

self.tearDown(organization)

Expand All @@ -518,7 +516,7 @@ def setUp(self, mock_get_gateway, mock_permission):
organization_billing__cycle=BillingPlan.BILLING_CYCLE_MONTHLY,
organization_billing__plan=BillingPlan.PLAN_START,
inteligence_organization=1,
organization_billing__stripe_customer="cus_MYOrndkgpPHGK9"
organization_billing__stripe_customer="cus_MYOrndkgpPHGK9",
)

self.billing = self.organization.organization_billing
Expand All @@ -545,64 +543,9 @@ def request_upgrade_plan(self, organization_uuid=None, data=None, token=None):
format="json",
**authorization_header,
)
response = OrganizationViewSet.as_view({"patch": "upgrade_plan"})(request, organization_uuid)
response = OrganizationViewSet.as_view({"patch": "upgrade_plan"})(
request, organization_uuid
)

content_data = json.loads(response.content)
return response, content_data

@patch("connect.billing.get_gateway")
def test_plan_limits(self, mock_get_gateway):
mock_get_gateway.return_value = StripeMockGateway()
# Creates more contacts than the plan limit allows
num_contacts = BillingPlan.plan_info(self.organization.organization_billing.plan)["limit"] * 5 + 1
self.assertTrue(self.organization.organization_billing.is_active)
self.assertFalse(self.organization.is_suspended)
create_contacts(num_contacts)

daily_contact_count()
# Verify if the organizations have more contacts than the plan limit
check_organization_plans()
organization = Organization.objects.get(uuid=self.organization.uuid)
self.assertFalse(organization.organization_billing.is_active)
self.assertTrue(organization.is_suspended)

self.assertEqual(organization.organization_billing.plan, BillingPlan.PLAN_START)
self.assertEqual(self.billing.contract_on, pendulum.now().date())
self.assertEqual(self.billing.next_due_date, pendulum.now().add(months=1).date())

# Upgrade plan, request made in a diferent day to validade
# changes in BillingPlan.contract_on and next_due_date
data = {
"organization_billing_plan": BillingPlan.PLAN_SCALE
}
freezer = freeze_time(f"{pendulum.now().add(days=5)}")
freezer.start()
response, content_data = self.request_upgrade_plan(
organization_uuid=self.organization.uuid,
data=data,
token=self.owner_token
)

self.assertEqual(content_data["status"], "SUCCESS")
self.assertEqual(content_data["old_plan"], BillingPlan.PLAN_START)
self.assertEqual(content_data["plan"], BillingPlan.PLAN_SCALE)

organization = Organization.objects.get(uuid=self.organization.uuid)

# New due date, contract on and plan
self.assertEqual(organization.organization_billing.contract_on, pendulum.now().date())
self.assertEqual(organization.organization_billing.next_due_date, pendulum.now().add(months=1).date())
self.assertEqual(organization.organization_billing.plan, BillingPlan.PLAN_SCALE)

# Check if the org is active again
self.assertTrue(organization.organization_billing.is_active)
self.assertFalse(organization.is_suspended)
check_organization_plans()
freezer.stop()
freezer = freeze_time(organization.organization_billing.next_due_date)
freezer.start()

generate_project_invoice()
invoice = organization.organization_billing_invoice.last()
self.assertEqual(invoice.due_date, pendulum.now().date())
freezer.stop()
Loading
Loading