def test_restore_organization(organization_plan_factory, mocker): patched = mocker.patch( "squarelet.organizations.tasks.send_cache_invalidations") mocker.patch("stripe.Plan.create") today = date.today() organization_plan = organization_plan_factory() subsc_update_cancel = SubscriptionFactory(update_on=today - timedelta(1), plan=organization_plan, cancelled=True) subsc_update_later = SubscriptionFactory(update_on=today + timedelta(1), plan=organization_plan) subsc_update = SubscriptionFactory(update_on=today - timedelta(1), plan=organization_plan) tasks.restore_organization() subsc_update_later.refresh_from_db() subsc_update.refresh_from_db() # update cancel should have been deleted assert not Subscription.objects.filter(pk=subsc_update_cancel.pk).exists() # update later should have not been changed assert subsc_update_later.plan == organization_plan assert subsc_update_later.update_on == today + timedelta(1) assert subsc_update.plan == organization_plan assert subsc_update.update_on == today + relativedelta(months=1) assert patched.call_args[0][0] == "organization" assert set(patched.call_args[0][1]) == set([ subsc_update.organization.uuid, subsc_update_cancel.organization.uuid ])
def test_list(self, api_client, user): """List subscriptions""" size = 10 organization = OrganizationFactory(admins=[user]) api_client.force_authenticate(user=user) SubscriptionFactory.create_batch(size, organization=organization) response = api_client.get( f"/pp-api/organizations/{organization.uuid}/subscriptions/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size assert "id" in response_json["results"][0]
def test_destroy(self, api_client, user, mocker): """Test cancelling a subscription""" mocker.patch("stripe.Plan.create") mocked_stripe_subscription = mocker.patch( "squarelet.organizations.models.Subscription.stripe_subscription") organization = OrganizationFactory(admins=[user]) subscription = SubscriptionFactory(organization=organization) api_client.force_authenticate(user=user) response = api_client.delete( f"/pp-api/organizations/{organization.uuid}/subscriptions/" f"{subscription.pk}/") assert response.status_code == status.HTTP_204_NO_CONTENT subscription.refresh_from_db() assert subscription.cancelled assert mocked_stripe_subscription.cancel_at_period_end is True mocked_stripe_subscription.save.assert_called_once()
def test_retrieve(self, api_client, user, mocker): """Test retrieving a subscription""" mocker.patch("stripe.Plan.create") organization = OrganizationFactory(admins=[user]) subscription = SubscriptionFactory(organization=organization) api_client.force_authenticate(user=user) response = api_client.get( f"/pp-api/organizations/{organization.uuid}/subscriptions/" f"{subscription.pk}/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert "id" in response_json
def test_update(self, api_client, user, mocker): """Test updating an organization""" mocked_modify = mocker.patch("stripe.Subscription.modify") mocker.patch( "squarelet.organizations.models.Subscription.stripe_subscription") organization = OrganizationFactory(admins=[user]) SubscriptionFactory(organization=organization) api_client.force_authenticate(user=user) response = api_client.patch( f"/pp-api/organizations/{organization.uuid}/", {"max_users": 42}) assert response.status_code == status.HTTP_200_OK organization.refresh_from_db() assert organization.max_users == 42 mocked_modify.assert_called_once()
def test_subscribed(self, api_client, user): """List entitlements for a subscribed user""" size = 10 api_client.force_authenticate(user=user) org = OrganizationFactory(admins=[user]) plan = PlanFactory(for_individuals=False, for_groups=True, public=False) plan.private_organizations.add(org) # set entitlements for the plan, as well as entitlements not in the plan subscribed_entitlements = EntitlementFactory.create_batch(size) plan.entitlements.set(subscribed_entitlements) SubscriptionFactory(plan=plan, organization=org) # these do not get attached to the plan EntitlementFactory.create_batch(size) response = api_client.get(f"/pp-api/entitlements/?subscribed=true") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size