def test_has_member(self): """Test has_member method""" org = OrganizationFactory() users = UserFactory.create_batch(2) MembershipFactory(user=users[0], organization=org) assert_true(org.has_member(users[0])) assert_false(org.has_member(users[1]))
def test_has_admin(self): """Test has_admin method""" org = OrganizationFactory() users = UserFactory.create_batch(2) MembershipFactory(user=users[0], organization=org, admin=True) MembershipFactory(user=users[1], organization=org, admin=False) assert_true(org.has_admin(users[0])) assert_false(org.has_admin(users[1]))
def _test_calc_return_requests(self): """Test calculating the return requests""" composer = FOIAComposerFactory( status='submitted', agencies=AgencyFactory.create_batch(6), num_org_requests=1, num_monthly_requests=2, num_reg_requests=3, user__profile__num_requests=5, user__profile__monthly_requests=10, user__profile__organization=OrganizationFactory(num_requests=100), ) values = [ (7, 4, 2, 1), (6, 3, 2, 1), (5, 3, 2, 0), (4, 3, 1, 0), (3, 3, 0, 0), (2, 2, 0, 0), (1, 1, 0, 0), ] for total, reg, monthly, org in values: eq_( composer._calc_return_requests(total), { 'regular': reg, 'monthly': monthly, 'org': org, }, )
def _test_return_requests(self): """Test return requests""" organization = OrganizationFactory(num_requests=100) composer = FOIAComposerFactory( status='submitted', num_org_requests=1, num_monthly_requests=2, num_reg_requests=3, user__profile__num_requests=5, user__profile__monthly_requests=10, user__profile__organization=organization, ) composer._return_requests({ 'regular': 2, 'monthly': 0, 'org': 1, }) composer.user.profile.refresh_from_db() composer.user.profile.organization.refresh_from_db() eq_(composer.num_reg_requests, 1) eq_(composer.num_monthly_requests, 2) eq_(composer.num_org_requests, 0) eq_(composer.user.profile.num_requests, 7) eq_(composer.user.profile.monthly_requests, 10) eq_(composer.user.profile.organization.num_requests, 101)
def test_decrease_max_users(self): """Decrease max users""" ent = OrganizationEntitlementFactory() organization = OrganizationFactory( entitlement=ent, date_update=date(2019, 2, 21), requests_per_month=75, monthly_requests=33, ) organization.update_data({ "name": organization.name, "slug": organization.slug, "individual": False, "private": False, "entitlements": [ent_json(ent, date(2019, 2, 21))], "max_users": 7, "card": "", }) organization.refresh_from_db() eq_(organization.requests_per_month, 60) eq_(organization.monthly_requests, 33)
def test_monthly_restore(self): """Monthly restore""" ent = OrganizationEntitlementFactory() organization = OrganizationFactory( entitlement=ent, date_update=date(2019, 2, 21), requests_per_month=50, monthly_requests=33, ) organization.update_data({ "name": organization.name, "slug": organization.slug, "individual": False, "private": False, "entitlements": [ent_json(ent, date(2019, 3, 21))], "max_users": 5, "card": "", }) organization.refresh_from_db() eq_(organization.requests_per_month, 50) eq_(organization.monthly_requests, 50)
def test_downgrade_subscription(self): """Downgrade a subscription""" # Downgrades only happen at monthly restore OrganizationPlanFactory() plus = PlanFactory( name='Plus', minimum_users=5, base_requests=100, ) organization = OrganizationFactory( plan=plus, date_update=date(2019, 2, 21), max_users=5, requests_per_month=100, monthly_requests=83, ) organization.update_data({ 'name': organization.name, 'slug': organization.slug, 'individual': False, 'private': False, 'plan': 'organization', 'date_update': date(2019, 3, 21), 'max_users': 5, 'card': '', }) organization.refresh_from_db() eq_(organization.requests_per_month, 50) eq_(organization.monthly_requests, 50)
def test_upgrade_subscription(self): """Upgrade a subscription""" PlanFactory( name='Plus', minimum_users=5, base_requests=100, ) organization = OrganizationFactory( plan=OrganizationPlanFactory(), date_update=date(2019, 2, 21), max_users=5, requests_per_month=50, monthly_requests=33, ) organization.update_data({ 'name': organization.name, 'slug': organization.slug, 'individual': False, 'private': False, 'plan': 'plus', 'date_update': date(2019, 2, 21), 'max_users': 5, 'card': '', }) organization.refresh_from_db() eq_(organization.requests_per_month, 100) eq_(organization.monthly_requests, 83)
def test_downgrade_subscription(self): """Downgrade a subscription""" # Downgrades only happen at monthly restore ent = OrganizationEntitlementFactory() plus = EntitlementFactory(name="Plus", resources=dict(minimum_users=5, base_requests=100)) organization = OrganizationFactory( entitlement=plus, date_update=date(2019, 2, 21), requests_per_month=100, monthly_requests=83, ) organization.update_data({ "name": organization.name, "slug": organization.slug, "individual": False, "private": False, "entitlements": [ent_json(ent, date(2019, 3, 21))], "max_users": 5, "card": "", }) organization.refresh_from_db() eq_(organization.requests_per_month, 50) eq_(organization.monthly_requests, 50)
def test_org_share(self): """Test sharing with your organization""" org = OrganizationFactory() user = UserFactory() MembershipFactory(user=user, organization=org, active=False) self.foia.embargo = True self.foia.composer.organization = org # fellow org member cannot view it before sharing is turned on assert_false(self.foia.has_perm(user, 'view')) self.creator.profile.org_share = True # now org member can view it assert_true(self.foia.has_perm(user, 'view')) # non-org member still cannot view it assert_false(self.foia.has_perm(self.editor, 'view'))
class OrganizationViewsTests(TestCase): """Test the views for the organization app""" def setUp(self): """Set up models for the organization""" self.org = OrganizationFactory() self.client = Client() def test_index(self): """The index should redirect""" response = self.client.get(reverse('org-index')) eq_(response.status_code, 200) def test_detail(self): """Detail page should redirect""" response = self.client.get(self.org.get_absolute_url()) eq_(response.status_code, 200)
def test_get_viewable_org_not_shared(self): """Test get viewable for an org not shared composer""" org = OrganizationFactory() org_user1 = UserFactory(profile__org_share=False) org_user2 = UserFactory() MembershipFactory(user=org_user1, organization=org, active=False) MembershipFactory(user=org_user2, organization=org, active=False) FOIARequestFactory( composer__status='filed', embargo=True, composer__user=org_user1, composer__organization=org, ) assert_true(FOIAComposer.objects.get_viewable(self.staff).exists()) assert_false(FOIAComposer.objects.get_viewable(self.user).exists()) assert_false(FOIAComposer.objects.get_viewable(org_user2).exists()) assert_false(FOIAComposer.objects.get_viewable(self.anon).exists())
def test_create_subscription(self): """Create a new subscription""" OrganizationPlanFactory() organization = OrganizationFactory() organization.update_data({ 'name': organization.name, 'slug': organization.slug, 'individual': False, 'private': False, 'plan': 'organization', 'date_update': date(2019, 2, 21), 'max_users': 5, 'card': '', }) organization.refresh_from_db() eq_(organization.requests_per_month, 50) eq_(organization.monthly_requests, 50)
def test_cancel_subscription(self): """Cancel a subscription""" ent = FreeEntitlementFactory() organization = OrganizationFactory( entitlement=OrganizationEntitlementFactory(), date_update=date(2019, 2, 21), requests_per_month=50, monthly_requests=33, ) organization.update_data({ "name": organization.name, "slug": organization.slug, "individual": False, "private": False, "entitlements": [ent_json(ent, None)], "max_users": 5, "card": "", }) organization.refresh_from_db() eq_(organization.requests_per_month, 0) eq_(organization.monthly_requests, 0)
def test_create_subscription(self): """Create a new subscription""" ent = OrganizationEntitlementFactory() organization = OrganizationFactory() organization.update_data({ "name": organization.name, "slug": organization.slug, "individual": False, "private": False, "entitlements": [ent_json(ent, date(2019, 2, 21))], "max_users": 5, "card": "", }) organization.refresh_from_db() eq_(organization.requests_per_month, 50) eq_(organization.monthly_requests, 50)
def test_decrease_max_users(self): """Decrease max users""" organization = OrganizationFactory( plan=OrganizationPlanFactory(), date_update=date(2019, 2, 21), max_users=10, requests_per_month=75, monthly_requests=33, ) organization.update_data({ 'name': organization.name, 'slug': organization.slug, 'individual': False, 'private': False, 'plan': 'organization', 'date_update': date(2019, 2, 21), 'max_users': 7, 'card': '', }) organization.refresh_from_db() eq_(organization.requests_per_month, 60) eq_(organization.monthly_requests, 33)
def test_monthly_restore(self): """Monthly restore""" organization = OrganizationFactory( plan=OrganizationPlanFactory(), date_update=date(2019, 2, 21), max_users=5, requests_per_month=50, monthly_requests=33, ) organization.update_data({ 'name': organization.name, 'slug': organization.slug, 'individual': False, 'private': False, 'plan': 'organization', 'date_update': date(2019, 3, 21), 'max_users': 5, 'card': '', }) organization.refresh_from_db() eq_(organization.requests_per_month, 50) eq_(organization.monthly_requests, 50)
def test_cancel_subscription(self): """Cancel a subscription""" FreePlanFactory() organization = OrganizationFactory( plan=OrganizationPlanFactory(), date_update=date(2019, 2, 21), max_users=5, requests_per_month=50, monthly_requests=33, ) organization.update_data({ 'name': organization.name, 'slug': organization.slug, 'individual': False, 'private': False, 'plan': 'free', 'date_update': None, 'max_users': 5, 'card': '', }) organization.refresh_from_db() eq_(organization.requests_per_month, 0) eq_(organization.monthly_requests, 0)
def test_make_requests(self): """Test Org make_requests method""" org = OrganizationFactory(monthly_requests=10, number_requests=10) request_count = org.make_requests(5) org.refresh_from_db() eq_(request_count, {"monthly": 5, "regular": 0}) eq_(org.monthly_requests, 5) eq_(org.number_requests, 10) request_count = org.make_requests(10) org.refresh_from_db() eq_(request_count, {"monthly": 5, "regular": 5}) eq_(org.monthly_requests, 0) eq_(org.number_requests, 5) request_count = org.make_requests(4) org.refresh_from_db() eq_(request_count, {"monthly": 0, "regular": 4}) eq_(org.monthly_requests, 0) eq_(org.number_requests, 1) with assert_raises(InsufficientRequestsError): request_count = org.make_requests(2) org.refresh_from_db() eq_(org.monthly_requests, 0) eq_(org.number_requests, 1)
def setUp(self): """Set up models for the organization""" self.org = OrganizationFactory() self.client = Client()