def test_list(self, api_client): """List organizations""" size = 10 OrganizationFactory.create_batch(size) response = api_client.get(f"/pp-api/organizations/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size
def test_destroy(self, api_client, user): member = UserFactory() organization = OrganizationFactory(admins=[user], users=[member]) api_client.force_authenticate(user=user) response = api_client.delete( f"/pp-api/organizations/{organization.uuid}/memberships/" f"{member.individual_organization_id}/") assert response.status_code == status.HTTP_204_NO_CONTENT assert not organization.has_member(member)
def test_update(self, api_client, user): """Test updating a membership""" member = UserFactory() organization = OrganizationFactory(admins=[user], users=[member]) api_client.force_authenticate(user=user) response = api_client.patch( f"/pp-api/organizations/{organization.uuid}/memberships/" f"{member.individual_organization_id}/", {"admin": True}, ) assert response.status_code == status.HTTP_200_OK assert organization.has_admin(member)
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_retrieve(self, api_client, user): """Test retrieving a membership""" organization = OrganizationFactory(users=[user]) response = api_client.get( f"/pp-api/organizations/{organization.uuid}/memberships/" f"{user.individual_organization_id}/") assert response.status_code == status.HTTP_200_OK
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_create(self, api_client, user, mocker): """Create a subscription""" mocker.patch("stripe.Plan.create") stripe_id = "stripe_subscription_id" mocked_customer = mocker.patch( "squarelet.organizations.models.Customer.stripe_customer", email=None, **{"subscriptions.create.return_value": Mock(id=stripe_id)}, ) plan = OrganizationPlanFactory() organization = OrganizationFactory(admins=[user]) api_client.force_authenticate(user=user) data = {"plan": plan.pk, "token": "stripe_token"} response = api_client.post( f"/pp-api/organizations/{organization.uuid}/subscriptions/", data) assert response.status_code == status.HTTP_201_CREATED mocked_customer.subscriptions.create.assert_called_with( items=[{ "plan": plan.stripe_id, "quantity": organization.max_users }], billing="charge_automatically", days_until_due=None, ) assert mocked_customer.email == organization.email assert mocked_customer.source == "stripe_token" assert mocked_customer.save.call_count == 2 assert organization.subscriptions.first().subscription_id == stripe_id
def test_get_viewable(self): admin, member, user = UserFactory.create_batch(3) private_org = OrganizationFactory(admins=[admin], private=True) MembershipFactory(organization=private_org, user=member) assert Membership.objects.get_viewable(member).count() == 3 assert Membership.objects.get_viewable(user).count() == 1 another_user = UserFactory() assert member.memberships.get_viewable(another_user).count() == 0
def test_list(self, api_client, user): """List organizations""" size = 10 organization = OrganizationFactory(admins=[user]) MembershipFactory.create_batch(size, organization=organization) response = api_client.get( f"/pp-api/organizations/{organization.uuid}/memberships/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size + 1
def test_list(self, api_client, user): """List invitations for an organization""" organization = OrganizationFactory(admins=[user]) api_client.force_authenticate(user=user) size = 10 InvitationFactory.create_batch(size, organization=organization) response = api_client.get( f"/pp-api/organizations/{organization.uuid}/invitations/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size
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_list_expand_plans(self, api_client, user): """List subscriptions expanding plans""" 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/?expand=plan" ) assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size assert "name" in response_json["results"][0]["plan"]
def test_list(self, api_client, user): """List user memberships""" api_client.force_authenticate(user=user) organization = OrganizationFactory(admins=[user], private=True) new_user = UserFactory() MembershipFactory(organization=organization, user=new_user, admin=False) response = api_client.get(f"/pp-api/users/me/memberships/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) # will return individual organization membership # and membership from organization created above assert len(response_json["results"]) == 2
def test_create_user_request(self, api_client, user, mailoutbox): """User requests to join an organization""" admin = UserFactory() organization = OrganizationFactory(admins=[admin]) api_client.force_authenticate(user=user) response = api_client.post( f"/pp-api/organizations/{organization.uuid}/invitations/") assert response.status_code == status.HTTP_201_CREATED invitation = organization.invitations.first() assert invitation.request assert invitation.user == user assert len(mailoutbox) == 1 mail = mailoutbox[0] assert mail.subject == f"{user} has requested to join {organization}" assert mail.to == [admin.email]
def test_create_admin_invite(self, api_client, user, mailoutbox): """Admin invites a user to join""" organization = OrganizationFactory(admins=[user]) api_client.force_authenticate(user=user) data = {"email": "*****@*****.**"} response = api_client.post( f"/pp-api/organizations/{organization.uuid}/invitations/", data) assert response.status_code == status.HTTP_201_CREATED response_json = json.loads(response.content) assert not response_json["request"] assert response_json["user"] is None assert len(mailoutbox) == 1 mail = mailoutbox[0] assert mail.subject == f"Invitation to join {organization.name}" assert mail.to == [data["email"]]
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_list_expand_data(self, api_client, user): """List invitations for an organization expanding user and org data""" organization = OrganizationFactory(admins=[user]) api_client.force_authenticate(user=user) size = 10 InvitationFactory.create_batch(size, user=user, organization=organization) response = api_client.get( f"/pp-api/organizations/{organization.uuid}/invitations/" f"?expand=organization,user") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size assert "name" in response_json["results"][0]["organization"] assert "username" in response_json["results"][0]["user"]
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