def test_filter_workflowteam_superuser(self): self.tola_user.user.is_staff = True self.tola_user.user.is_superuser = True self.tola_user.user.save() another_org = factories.Organization(name='Another Org') wkflvl1_1 = factories.WorkflowLevel1( organization=self.tola_user.organization) wkflvl1_2 = factories.WorkflowLevel1(organization=another_org) workflowteam1 = factories.WorkflowTeam(workflow_user=self.tola_user, salary=1111, workflowlevel1=wkflvl1_1) factories.WorkflowTeam(workflow_user=self.tola_user, salary=2222, workflowlevel1=wkflvl1_2) request = self.factory.get( '/api/workflowteam/?workflowlevel1__organization__id=%s' % self.tola_user.organization.pk) request.user = self.tola_user.user view = WorkflowTeamViewSet.as_view({'get': 'list'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['salary'], str(workflowteam1.salary))
def test_check_seats_save_team_decrease(self): external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1_1') wflvl1_2 = factories.WorkflowLevel1(name='WorkflowLevel1_2') wfteam1_1 = factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_1, role=self.group_program_admin) wfteam1_2 = factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_2, role=self.group_program_admin) # It shouldn't increase the amount of used seats # the user already has a WorkflowTeam organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 1) # It shouldn't decrease the seats because the user still has # another WorkflowTeam wfteam1_1.role = self.group_view_only wfteam1_1.save() organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 1) # It should decrease the seats because the user isn't Org Admin # and doesn't have another WorkflowTeam wfteam1_2.role = self.group_view_only wfteam1_2.save() organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 0)
def test_check_seats_save_team_retrieve_subscription_fails(self): """ The number of seats will be increased in the system but it's not possible to check the quantity of the plan because the retrieve failed. """ self.tola_user.user.groups.add(self.group_org_admin) self.tola_user.user.save() self.org = Organization.objects.get(pk=self.org.id) user = factories.User(first_name='John', last_name='Lennon') tolauser = factories.TolaUser(user=user, organization=self.org) json_obj = { 'message': "Sorry, we couldn't find that resource", 'error_code': 'resource_not_found' } sub_response = APIError(404, json_obj) Subscription.retrieve = Mock(side_effect=sub_response) wflvl1 = factories.WorkflowLevel1(name='WorkflowLevel1') factories.WorkflowTeam(workflow_user=tolauser, workflowlevel1=wflvl1, role=self.group_program_admin) organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 2) self.assertEqual(len(mail.outbox), 0)
def test_exceeded_seats_not_notify_when_one_of_multiple_role_removed(self): """ If user is org admin and program admin at the same time and users orgadmin role removed then org admin should not get notification because user still has seat as program admin.""" external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) self.tola_user.user.groups.add(self.group_org_admin) self.tola_user.user.save() user = factories.User(first_name='John', last_name='Lennon') self.org = Organization.objects.get(pk=self.org.id) tolauser = factories.TolaUser(user=user, organization=self.org) tolauser.user.groups.add(self.group_org_admin) tolauser.user.save() wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1_1') factories.WorkflowTeam(workflow_user=tolauser, workflowlevel1=wflvl1_1, role=self.group_program_admin) # It should notify the OrgAdmin organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 2) self.assertEqual(len(mail.outbox), 2) self.assertIn('Edit user exceeding notification', mail.outbox[0].subject) tolauser.user.groups.remove(self.group_org_admin) self.assertEqual(organization.chargebee_used_seats, 2) self.assertEqual(len(mail.outbox), 2)
def test_check_seats_delete_team_not_decrease(self): external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1_1') wflvl1_2 = factories.WorkflowLevel1(name='WorkflowLevel1_2') wfteam1_1 = factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_1, role=self.group_program_admin) factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_2, role=self.group_program_admin) organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 1) wfteam1_1.delete() organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 1)
def test_check_seats_save_team_exceed_notify(self): os.environ['APP_BRANCH'] = PRODUCTION_BRANCH header = {'category': ['exceed_paid_plan', 'from_production_email']} headers = {'X-SMTPAPI': json.dumps(header)} self.tola_user.user.groups.add(self.group_org_admin) self.tola_user.user.save() self.org = Organization.objects.get(pk=self.org.id) user = factories.User(first_name='John', last_name='Lennon') tolauser = factories.TolaUser(user=user, organization=self.org) external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) wflvl1 = factories.WorkflowLevel1(name='WorkflowLevel1') factories.WorkflowTeam(workflow_user=tolauser, workflowlevel1=wflvl1, role=self.group_program_admin) # It should notify the OrgAdmin organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 2) self.assertEqual(len(mail.outbox), 1) self.assertIn('Edit user exceeding notification', mail.outbox[0].subject) self.assertEqual(mail.outbox[0].to, [self.tola_user.user.email]) self.assertEqual(mail.outbox[0].reply_to, ['*****@*****.**']) self.assertEqual(mail.outbox[0].bcc, ['*****@*****.**']) self.assertEqual(mail.outbox[0].extra_headers, headers) # Text body org_admin_name = 'Hi {},'.format(self.tola_user.name) self.assertIn(org_admin_name, mail.outbox[0].body) available_seats = 'Purchased user seats: 1' self.assertIn(available_seats, mail.outbox[0].body) used_seats = 'Current edit users in the system: 2' self.assertIn(used_seats, mail.outbox[0].body) payment_portal_url = 'example.com' self.assertIn(payment_portal_url, mail.outbox[0].body) # HTML body org_admin_name = '<br>Hi {},</span>'.format(self.tola_user.name) self.assertIn(org_admin_name, mail.outbox[0].alternatives[0][0]) available_seats = 'Purchased user seats: <b>1</b>' self.assertIn(available_seats, mail.outbox[0].alternatives[0][0]) used_seats = 'Current edit users in the system: <b>2</b>' self.assertIn(used_seats, mail.outbox[0].alternatives[0][0]) payment_portal_url = '<a href="example.com" target="_blank">Payment ' \ 'portal</a>' self.assertIn(payment_portal_url, mail.outbox[0].alternatives[0][0]) self.assertIn('Payment portal', mail.outbox[0].alternatives[0][0])
def test_check_seats_save_team_without_subscription(self): external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) self.tola_user.organization = factories.Organization() wflvl1 = factories.WorkflowLevel1(name='WorkflowLevel1') factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1, role=self.group_program_admin) organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 0)
def test_delete_workflowteam_role_without_benefit(self): factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=self.wflvl1, role=factories.Group(name=ROLE_PROGRAM_TEAM)) request = self.factory.delete(None) request.user = self.tola_user.user view = WorkflowTeamViewSet.as_view({'delete': 'destroy'}) response = view(request, pk=self.workflowteam.pk) self.assertEqual(response.status_code, 403) WorkflowTeam.objects.get(pk=self.workflowteam.pk)
def test_check_seats_save_team_demo(self, mock_tsync): os.environ['APP_BRANCH'] = DEMO_BRANCH mock_tsync.create_instance.return_value = Mock() self.tola_user.organization = factories.Organization() wflvl1 = factories.WorkflowLevel1(name='WorkflowLevel1') factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1, role=self.group_program_admin) organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 0)
def test_check_seats_save_team_increase(self): external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1_1') wflvl1_2 = factories.WorkflowLevel1(name='WorkflowLevel1_2') factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_1, role=self.group_program_admin) # It should increase the seats because the user doesn't # have any seat reserved for him organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 1) # It shouldn't increase the seats because the user already # has a seat reserved for him factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_2, role=self.group_program_admin) organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 1)
def test_create_portfolio_other_user(self): role_without_benefits = ROLE_PROGRAM_ADMIN factories.WorkflowTeam( workflow_user=self.tola_user, role=factories.Group(name=role_without_benefits)) data = {'name': 'New portfolio'} request = self.factory.post(None, data) request.user = self.tola_user.user view = PortfolioViewSet.as_view({'post': 'create'}) response = view(request) self.assertEqual(response.status_code, 403)
def test_filter_stakeholder(self): wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1') wflvl1_2 = factories.WorkflowLevel1() factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_1) factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1_2) stk1 = factories.Stakeholder(name='Stakeholder_1', organization=self.tola_user.organization, workflowlevel1=[wflvl1_1]) factories.Stakeholder(name='Stakeholder_2', organization=self.tola_user.organization, workflowlevel1=[wflvl1_2]) request = self.factory.get( '/api/stakeholder/?workflowlevel1__name=%s' % wflvl1_1.name) request.user = self.tola_user.user view = StakeholderViewSet.as_view({'get': 'list'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['name'], stk1.name)
def setUp(self): self.factory = APIRequestFactory() self.tola_user = factories.TolaUser() user_ringo = factories.User(first_name='Ringo', last_name='Starr') tola_user_ringo = factories.TolaUser( user=user_ringo, organization=factories.Organization()) self.wflvl1 = factories.WorkflowLevel1( organization=tola_user_ringo.organization) factories.WorkflowTeam(workflow_user=tola_user_ringo, workflowlevel1=self.wflvl1, partner_org=self.wflvl1.organization, role=factories.Group(name=ROLE_VIEW_ONLY))
def test_delete_workflowteam_program_admin(self): factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=self.wflvl1, role=factories.Group(name=ROLE_PROGRAM_ADMIN)) request = self.factory.delete(None) request.user = self.tola_user.user view = WorkflowTeamViewSet.as_view({'delete': 'destroy'}) response = view(request, pk=self.workflowteam.pk) self.assertEqual(response.status_code, 204) self.assertRaises(WorkflowTeam.DoesNotExist, WorkflowTeam.objects.get, pk=self.workflowteam.pk)
def test_update_workflowteam_program_admin(self): factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=self.wflvl1, role=factories.Group(name=ROLE_PROGRAM_ADMIN)) data = {'salary': '100'} request = self.factory.post(None, data) request.user = self.tola_user.user view = WorkflowTeamViewSet.as_view({'post': 'update'}) response = view(request, pk=self.workflowteam.pk) self.assertEqual(response.status_code, 200) salary_updated = WorkflowTeam.objects.\ values_list('salary', flat=True).get(pk=self.workflowteam.pk) self.assertEqual(salary_updated, '100')
def test_list_portfolio_program_team(self): factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=factories.WorkflowLevel1( organization=self.tola_user.organization), role=factories.Group(name=ROLE_PROGRAM_TEAM)) self.portfolio.organization = self.tola_user.organization self.portfolio.save() request = self.factory.get('') request.user = self.tola_user.user view = PortfolioViewSet.as_view({'get': 'list'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_delete_portfolio_org_admin_other_user(self): role_without_benefits = ROLE_PROGRAM_ADMIN factories.WorkflowTeam( workflow_user=self.tola_user, role=factories.Group(name=role_without_benefits)) self.portfolio.organization = self.tola_user.organization self.portfolio.save() request = self.factory.delete(None) request.user = self.tola_user.user view = PortfolioViewSet.as_view({'delete': 'destroy'}) response = view(request, pk=self.portfolio.pk) self.assertEquals(response.status_code, 403) Portfolio.objects.get(pk=self.portfolio.pk)
def test_update_portfolio_org_admin_other_user(self): role_without_benefits = ROLE_PROGRAM_ADMIN factories.WorkflowTeam( workflow_user=self.tola_user, role=factories.Group(name=role_without_benefits)) self.portfolio.organization = self.tola_user.organization self.portfolio.save() data = {'name': 'Some name'} request = self.factory.post(None, data) request.user = self.tola_user.user view = PortfolioViewSet.as_view({'post': 'update'}) response = view(request, pk=self.portfolio.pk) self.assertEqual(response.status_code, 403)
def test_check_seats_save_team_org_admin(self): # When a user is an org admin, the seat has to be updated with the # user groups signal, that's why it shouldn't be changed in this case. self.tola_user.user.groups.add(self.group_org_admin) self.tola_user.user.save() external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) wflvl1 = factories.WorkflowLevel1(name='WorkflowLevel1') factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1, role=self.group_program_admin) # It should have only one seat because of the Org Admin role organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 1)
def test_list_workflowteam_org_admin(self): group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) self.tola_user.user.groups.add(group_org_admin) wflvl1 = factories.WorkflowLevel1( organization=self.tola_user.organization) # Create a workflow team having a diff partner org factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1) request_get = self.factory.get('/api/workflowteam/') request_get.user = self.tola_user.user view = WorkflowTeamViewSet.as_view({'get': 'list'}) response = view(request_get) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_search_for_indicator(self): role_program_admin = factories.Group(name=ROLE_PROGRAM_ADMIN) wflvl1 = factories.WorkflowLevel1(organization=self.org) factories.WorkflowTeam(workflow_user=self.tola_user, workflowlevel1=wflvl1, role=role_program_admin) indicators = factories.Indicator.create_batch(3, workflowlevel1=[wflvl1]) # the test is too fast we need to wait for ES # so it can create the indexes time.sleep(1) # get data from all the indexes request = self.factory.get('') request.user = self.tola_user.user response = views.search(request, '_all', 'building') content = json.loads(response.content) # we have data only for indicator self.assertTrue(len(content['workflowlevel1']) == 0) self.assertTrue(len(content['workflowlevel2']) == 0) self.assertTrue(len(content['indicators']) > 0) self.assertTrue(len(content['collected_data']) == 0) indicator_1 = content['indicators'][0]['_source'] search_after = content['indicators'][0]['sort'] # get more data using the cursor request = self.factory.get('?search_after={},{}'.format( search_after[0], search_after[1])) request.user = self.tola_user.user response = views.search(request, '_indicators', 'building') content = json.loads(response.content) self.assertTrue(len(content['indicators']) > 0) indicator_2 = content['indicators'][0]['_source'] self.assertNotEqual(indicator_1['indicator_uuid'], indicator_2['indicator_uuid']) self.indexer.delete_workflowlevel1(wflvl1) for indicator in indicators: self.indexer.delete_indicator(indicator)
def test_list_workflowteam_org_admin_diff_user_same_org(self): group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN) self.tola_user.user.groups.add(group_org_admin) # Create a user belonging to other Project in other Org user_george = factories.User(first_name='George', last_name='Harrison') tola_user_george = factories.TolaUser( user=user_george, organization=self.tola_user.organization) wflvl1_other = factories.WorkflowLevel1( organization=self.tola_user.organization) factories.WorkflowTeam(workflow_user=tola_user_george, workflowlevel1=wflvl1_other) request_get = self.factory.get('/api/workflowteam/') request_get.user = self.tola_user.user view = WorkflowTeamViewSet.as_view({'get': 'list'}) response = view(request_get) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_exceeded_seats_not_notify_when_role_changed(self): """ If user is org admin and program admin and users orgadmin role removed then org admin should not get notification because user still has seat as program admin.""" os.environ['APP_BRANCH'] = STAGING_BRANCH header = {'category': ['exceed_paid_plan', 'from_staging_email']} headers = {'X-SMTPAPI': json.dumps(header)} external_response = self.ExternalResponse(None) Subscription.retrieve = Mock(return_value=external_response) self.tola_user.user.groups.add(self.group_org_admin) self.tola_user.user.save() self.org = Organization.objects.get(pk=self.org.id) user = factories.User(first_name='John', last_name='Lennon') tolauser = factories.TolaUser(user=user, organization=self.org) tolauser.user.save() wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1_1') wfl_team = factories.WorkflowTeam(workflow_user=tolauser, workflowlevel1=wflvl1_1, role=self.group_program_team) # It should notify the OrgAdmin organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 2) self.assertEqual(len(mail.outbox), 1) self.assertIn('Edit user exceeding notification', mail.outbox[0].subject) # after role changed it should not notify wfl_team.role = self.group_program_admin wfl_team.save() organization = Organization.objects.get(pk=self.org.id) self.assertEqual(organization.chargebee_used_seats, 2) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].extra_headers, headers)
def test_print_instance(self): wfteam = factories.WorkflowTeam() self.assertEqual(unicode(wfteam), (u'Thom Yorke - ProgramAdmin <Health and Survival ' u'for Syrians in Affected Regions>'))