def test_list_filter_by_org(self, api_client, user): """List plans for a given organization""" api_client.force_authenticate(user=user) org = OrganizationFactory(admins=[user]) ind_plan = PlanFactory(for_individuals=True, for_groups=False, public=True) grp_plan = PlanFactory(for_individuals=False, for_groups=True, public=True) priv_plan = PlanFactory(for_individuals=False, for_groups=True, public=False) my_plan = PlanFactory(for_individuals=False, for_groups=True, public=False) my_plan.private_organizations.add(org) response = api_client.get(f"/pp-api/plans/", {"organization": org.uuid}) assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) # you should not be able to see the individual plan or the private plan # which is not shared with you plan_ids = [j["id"] for j in response_json["results"]] assert ind_plan.pk not in plan_ids assert grp_plan.pk in plan_ids assert priv_plan.pk not in plan_ids assert my_plan.pk in plan_ids
def test_list(self, api_client): """List plans""" size = 10 PlanFactory.create_batch(size) response = api_client.get(f"/pp-api/plans/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len( response_json["results"]) == Plan.objects.get_public().count() assert "id" in response_json["results"][0]
def test_public(self): public_plan = PlanFactory() private_plan = PlanFactory(public=False) entitlement = EntitlementFactory() assert not entitlement.public entitlement.plans.set([private_plan]) assert not entitlement.public entitlement.plans.set([public_plan]) assert entitlement.public entitlement.plans.set([private_plan, public_plan]) assert entitlement.public
def test_retrieve(self, api_client, entitlement, mocker): """Test retrieving an entitlement""" mocker.patch("stripe.Plan.create") # make entitlement public by adding it to a public plan plan = PlanFactory(public=True) plan.entitlements.add(entitlement) response = api_client.get(f"/pp-api/entitlements/{entitlement.pk}/") assert response.status_code == status.HTTP_200_OK
def test_retrieve(self, api_client, mocker): """Test retrieving a plan""" mocker.patch("stripe.Plan.create") plan = PlanFactory() response = api_client.get(f"/pp-api/plans/{plan.id}/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert "id" in response_json
def test_list_filter_by_account(self, api_client): """List plans by stripe account""" size = 2 PlanFactory.create_batch(size, stripe_account=StripeAccounts.muckrock) PlanFactory.create_batch(size, stripe_account=StripeAccounts.presspass) for account in [StripeAccounts.muckrock, StripeAccounts.presspass]: response = api_client.get(f"/pp-api/plans/", {"account": account}) assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert (len( response_json["results"]) == Plan.objects.get_public().filter( stripe_account=account).count()) response = api_client.get(f"/pp-api/plans/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len( response_json["results"]) == Plan.objects.get_public().count()
def test_retrieve_expand_client(self, api_client, entitlement, mocker): """Test retrieving an entitlement""" mocker.patch("stripe.Plan.create") # make entitlement public by adding it to a public plan plan = PlanFactory(public=True) plan.entitlements.add(entitlement) response = api_client.get( f"/pp-api/entitlements/{entitlement.pk}/?expand=client") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert "name" in response_json["client"]
def test_list(self, api_client, mocker): """List entitlements""" size = 10 entitlements = EntitlementFactory.create_batch(size) mocker.patch("stripe.Plan.create") plan = PlanFactory(public=True) plan.entitlements.set(entitlements) response = api_client.get(f"/pp-api/entitlements/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == Entitlement.objects.get_public( ).count()
def test_list_expand_clients(self, api_client, mocker): """List entitlements""" size = 10 entitlements = EntitlementFactory.create_batch(size) mocker.patch("stripe.Plan.create") # make entitlements public by adding it to a public plan plan = PlanFactory(public=True) plan.entitlements.set(entitlements) response = api_client.get(f"/pp-api/entitlements/?expand=client") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == Entitlement.objects.get_public( ).count() for entitlement in response_json["results"]: assert "name" in entitlement["client"]
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