def test_create_approvalworkflow(self):
        user_john = factories.User(first_name='John', last_name='Lennon')
        user_paul = factories.User(first_name='Paul', last_name='McCartney')
        assigned_user = factories.TolaUser(user=user_john,
                                           organization=self.organization)
        requested_user = factories.TolaUser(user=user_paul,
                                            organization=self.organization)
        approval_type = factories.ApprovalType()
        assigned_user_url = reverse('tolauser-detail',
                                    kwargs={'pk': assigned_user.id},
                                    request=self.request)
        requested_user_url = reverse('tolauser-detail',
                                     kwargs={'pk': requested_user.id},
                                     request=self.request)
        approval_type_url = reverse('approvaltype-detail',
                                    kwargs={'pk': approval_type.id},
                                    request=self.request)
        user_url = reverse('user-detail', kwargs={'pk': self.user.id},
                           request=self.request)

        data = {'assigned_to': assigned_user_url,
                'requested_from': requested_user_url,
                'approval_type': approval_type_url}
        self.request = APIRequestFactory().post('/api/approvalworkflow/', data)
        self.request.user = self.user
        view = ApprovalWorkflowViewSet.as_view({'post': 'create'})
        response = view(self.request)

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['assigned_to'], assigned_user_url)
        self.assertEqual(response.data['requested_from'], requested_user_url)
        self.assertEqual(response.data['created_by'], user_url)
예제 #2
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.tola_user = factories.TolaUser()

        self.tola_user_ringo = factories.TolaUser(
            user=factories.User(first_name='Ringo', last_name='Starr'),
            organization=self.tola_user.organization)
        self.tola_user_george = factories.TolaUser(
            user=factories.User(first_name='George', last_name='Harrison'),
            organization=factories.Organization(name='Other Org'))
예제 #3
0
    def test_demo_env_no_wflvl1_matching(self):
        os.environ['APP_BRANCH'] = DEMO_BRANCH
        factories.WorkflowLevel1(name=DEFAULT_WORKFLOWLEVEL1_NAME)
        factories.TolaUser()  # triggers the signal
        self.assertEqual(WorkflowTeam.objects.all().count(), 0)

        factories.WorkflowLevel1(id=DEFAULT_WORKFLOWLEVEL1_ID, name='Any')
        factories.TolaUser(
            user=factories.User(first_name='Ringo', last_name='Starr')
        )  # triggers the signal
        self.assertEqual(WorkflowTeam.objects.all().count(), 0)
        os.environ['APP_BRANCH'] = ''
    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))
예제 #5
0
    def test_clear_database_clears_stuff(self):
        organization = Organization.objects.create(**DEFAULT_ORG)
        country = Country.objects.create(code=DEFAULT_COUNTRY_CODES[0])
        Country.objects.create(code=DEFAULT_COUNTRY_CODES[1])

        factories.TolaUser(
            organization=Organization.objects.create(name='Org To Delete'),
            country=Country.objects.create(code='RM'),
            name='Kurt Cobain')

        command = Command()
        command._clear_database()

        tolauser = TolaUser.objects.get(name='Kurt Cobain')
        self.assertEqual(tolauser.organization, organization)
        self.assertEqual(tolauser.country, country)
        organizations = Organization.objects.all()
        self.assertEqual(organizations[0], organization)
        self.assertEqual(len(organizations), 1)
        self.assertTrue(
            Country.objects.filter(code__in=DEFAULT_COUNTRY_CODES).exists())
        self.assertEqual(Country.objects.all().count(), 2)
        self.assertEqual(Group.objects.all().count(), 0)
        self.assertEqual(Sector.objects.all().count(), 0)
        self.assertEqual(Stakeholder.objects.all().count(), 0)
        self.assertEqual(Milestone.objects.all().count(), 0)
        self.assertEqual(WorkflowLevel1.objects.all().count(), 0)
        self.assertEqual(WorkflowLevel2.objects.all().count(), 0)
        self.assertEqual(Level.objects.all().count(), 0)
        self.assertEqual(Frequency.objects.all().count(), 0)
        self.assertEqual(Indicator.objects.all().count(), 0)
        self.assertEqual(PeriodicTarget.objects.all().count(), 0)
        self.assertEqual(CollectedData.objects.all().count(), 0)
        self.assertEqual(WorkflowLevel1Sector.objects.all().count(), 0)
        self.assertEqual(WorkflowTeam.objects.all().count(), 0)
예제 #6
0
    def test_load_demo_data_and_restore_does_not_crash(self):
        args = []
        opts = {}
        call_command('loadinitialdata', *args, **opts)
        # Just to double check sequences later
        factories.WorkflowLevel1.create_batch(20)
        # To check assignments to this user
        tola_user = factories.TolaUser(
            organization=Organization.objects.create(name='Org To Delete'),
            country=Country.objects.create(code='RM'),
            name='Kurt Cobain')

        args = ['--restore']
        opts = {}
        call_command('loadinitialdata', *args, **opts)

        # We check that we don't have repeated data and that sequences are
        # reset.
        WorkflowLevel1.objects.get(id=DEFAULT_WORKFLOW_LEVEL_1S[0][0],
                                   name=DEFAULT_WORKFLOW_LEVEL_1S[0][1])
        WorkflowLevel1.objects.get(id=DEFAULT_WORKFLOW_LEVEL_1S[1][0],
                                   name=DEFAULT_WORKFLOW_LEVEL_1S[1][1])
        expected_next = WorkflowLevel1.objects.all().count() + 1
        wflvl1_next = factories.WorkflowLevel1()
        self.assertEqual(wflvl1_next.id, expected_next)

        wflvl1s = [
            id for id in WorkflowTeam.objects.\
                values_list('workflowlevel1_id', flat=True).\
                filter(workflow_user=tola_user)
        ]
        self.assertEqual(sorted(wflvl1s), [3, 6])
예제 #7
0
 def test_list_groups_normaluser(self):
     tola_user = factories.TolaUser()
     self.request_get.user = tola_user.user
     view = GroupViewSet.as_view({'get': 'list'})
     response = view(self.request_get)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 1)
 def test_list_organization_normaluser_one_result(self):
     tola_user = factories.TolaUser()
     self.request.user = tola_user.user
     view = OrganizationViewSet.as_view({'get': 'list'})
     response = view(self.request)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 1)
예제 #9
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)
예제 #10
0
    def test_demo_workflowteam_assignment_not_reassigned_on_update(
            self, mock_tsync):
        os.environ['APP_BRANCH'] = DEMO_BRANCH
        mock_tsync.create_instance.return_value = Mock()
        role = factories.Group(name=ROLE_VIEW_ONLY)
        wflvl1_0 = factories.WorkflowLevel1(
            id=DEFAULT_WORKFLOW_LEVEL_1S[0][0],
            name=DEFAULT_WORKFLOW_LEVEL_1S[0][1])
        wflvl1_1 = factories.WorkflowLevel1(
            id=DEFAULT_WORKFLOW_LEVEL_1S[1][0],
            name=DEFAULT_WORKFLOW_LEVEL_1S[1][1])

        tola_user = factories.TolaUser(user=factories.User(
            first_name='Ringo', last_name='Starr'))  # triggers the signal
        tola_user.name = 'Laura Pausini'
        tola_user.save()

        num_results = WorkflowTeam.objects.filter(
            workflow_user=tola_user, role=role,
            workflowlevel1=wflvl1_0).count()
        self.assertEqual(num_results, 1)
        num_results = WorkflowTeam.objects.filter(
            workflow_user=tola_user, role=role,
            workflowlevel1=wflvl1_1).count()
        self.assertEqual(num_results, 1)
예제 #11
0
    def test_filter_checklist_owner_org_admin(self):
        group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
        self.tola_user.user.groups.add(group_org_admin)

        another_user = factories.User(first_name='Johnn', last_name='Lennon')
        another_tola = factories.TolaUser(user=another_user)
        wkflvl1_1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wkflvl1_2 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        wkflvl2_1 = factories.WorkflowLevel2(name='WorkflowLevel2 A',
                                             workflowlevel1=wkflvl1_1)
        wkflvl2_2 = factories.WorkflowLevel2(name='WorkflowLevel2 B',
                                             workflowlevel1=wkflvl1_2)
        checklist1 = factories.Checklist(name='Checklist A',
                                         owner=self.tola_user,
                                         workflowlevel2=wkflvl2_1)
        factories.Checklist(name='Checklist B',
                            owner=another_tola,
                            workflowlevel2=wkflvl2_2)

        request = self.factory.get('/api/checklist'
                                   '/?owner=%s' % self.tola_user.pk)
        request.user = self.tola_user.user
        view = ChecklistViewSet.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'], checklist1.name)
예제 #12
0
 def setUp(self):
     os.environ['APP_BRANCH'] = ''
     logging.disable(logging.ERROR)
     self.group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
     self.group_program_admin = factories.Group(name=ROLE_PROGRAM_ADMIN)
     self.org = factories.Organization(chargebee_subscription_id='12345')
     self.tola_user = factories.TolaUser(organization=self.org)
예제 #13
0
 def setUp(self):
     self.client = Client()
     self.user = factories.User()
     self.user.set_password('tola123')
     self.user.save()
     factories.TolaUser(user=self.user)
     self.client.login(username=self.user.username, password='******')
예제 #14
0
 def test_list_disaggregationtype_normaluser(self):
     tola_user = factories.TolaUser()
     self.request_get.user = tola_user.user
     view = DisaggregationTypeViewSet.as_view({'get': 'list'})
     response = view(self.request_get)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 0)
예제 #15
0
파일: test.py 프로젝트: Yemeni/TolaTables
    def test_silo_detail_import_done(self):
        # Create Silo, Read and CeleryTask
        read_type = factories.ReadType(read_type="CSV")
        read = factories.Read(owner=self.user,
                              type=read_type,
                              read_name="TEST SILO DONE",
                              description="unittest")
        silo = factories.Silo(owner=self.user, public=False)
        silo.reads.add(read)
        factories.CeleryTask(content_object=read,
                             task_status=CeleryTask.TASK_FINISHED)
        factories.TolaUser(user=self.user)
        # Check view
        request = self.factory.get(self.silo_detail_url)
        request.user = self.user

        response = silo_detail(request, silo.pk)
        self.assertContains(
            response, '<a href="/show_read/{}" target="_blank">{}</a>'.format(
                read.id, read.read_name))
        self.assertNotContains(
            response, '<span class="btn-sm btn-danger">Import Failed</span>')
        self.assertNotContains(
            response, '<span class="btn-sm btn-warning">Import running</span>')
        self.assertNotContains(
            response, '<h4 style="color:#ff3019">Import process failed</h4>')
        self.assertNotContains(response, '<h4>Import process running</h4>')
예제 #16
0
    def test_check_token_no_public_attribute(self):
        tola_user = factories.TolaUser()

        with self.assertRaises(AttributeError) as context:
            self.token_generator.check_token(tola_user, 'TokenTest')
            self.assertTrue('\'dict\' object has no attribute \'test\''
                            in context.exception)
예제 #17
0
    def test_create_workflowteam_org_admin(self):
        WorkflowTeam.objects.create(
            workflow_user=self.tola_user,
            workflowlevel1=self.wflvl1,
            role=factories.Group(name=ROLE_ORGANIZATION_ADMIN))

        wflvl1_url = reverse('workflowlevel1-detail',
                             kwargs={'pk': self.wflvl1.id})
        user_george = factories.User(first_name='George', last_name='Harrison')
        tola_user_george = factories.TolaUser(
            user=user_george, organization=factories.Organization())
        tolauser_url = reverse('tolauser-detail',
                               kwargs={'pk': tola_user_george.id})
        role = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
        role_url = reverse('group-detail', kwargs={'pk': role.id})
        data = {
            'role': role_url,
            'workflow_user': tolauser_url,
            'workflowlevel1': wflvl1_url,
        }

        request = self.factory.post(None, data)
        request.user = self.tola_user.user
        view = WorkflowTeamViewSet.as_view({'post': 'create'})
        response = view(request)
        self.assertEqual(response.status_code, 201)

        WorkflowTeam.objects.get(
            workflowlevel1=self.wflvl1,
            workflow_user=tola_user_george,
            role=role,
        )
예제 #18
0
 def setUp(self):
     logging.disable(logging.ERROR)
     settings.ELASTICSEARCH_ENABLED = True
     self.org = factories.Organization()
     self.indexer = ElasticsearchIndexer()
     self.factory = APIRequestFactory()
     self.tola_user = factories.TolaUser(organization=self.org)
예제 #19
0
    def test_save_data_customform_default_columns(self):
        user = factories.User(first_name='Homer', last_name='Simpson')
        sender_hikaya_user = factories.TolaUser(user=user)

        data = {
            'silo_id': self.silo.id,
            'data': {
                'name': 'John Lennon',
                'age': 40,
                'city': 'Liverpool'
            },
            'submitted_by': sender_hikaya_user.hikaya_user_uuid.__str__(),
        }

        request = self.factory.post('api/customform/save_data',
                                    data=json.dumps(data),
                                    content_type='application/json')
        request.user = self.hikaya_user.user
        view = CustomFormViewSet.as_view({'post': 'save_data'})
        response = view(request)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['detail'], 'It was successfully saved.')
        self.assertEqual(self.silo.data_count, 1)

        # check if the default columns were created
        customform_silo = LabelValueStore.objects.get(silo_id=self.silo.id)
        table_column_names = customform_silo._dynamic_fields.keys()
        for default_col in CustomFormViewSet._default_columns:
            self.assertIn(default_col['name'], table_column_names)

        self.assertEqual(len(table_column_names), 6)
예제 #20
0
    def test_response_201_create(self, mock_requests):
        external_response = {
            'url': 'http://testserver/api/tolauser/2',
            'tola_user_uuid': 1234567890,
            'name': 'John Lennon',
        }
        mock_requests.post.return_value = Mock(
            status_code=201, content=json.dumps(external_response))

        self.tola_user.user.is_staff = True
        self.tola_user.user.is_superuser = True
        self.tola_user.user.save()

        user = factories.User(first_name='John', last_name='Lennon')
        tolauser = factories.TolaUser(user=user, tola_user_uuid=1234567890)
        data = {
            'first_name': user.first_name,
            'last_name': user.last_name,
            'email': user.email,
            'username': user.username,
            'tola_user_uuid': tolauser.tola_user_uuid
        }

        response = register_user(data, tolauser)
        result = json.loads(response.content)

        self.assertEqual(result['tola_user_uuid'], 1234567890)
        mock_requests.post.assert_called_once_with(
            'https://tolatrack.com/accounts/register/',
            data={'username': '******',
                  'first_name': 'John',
                  'last_name': 'Lennon',
                  'tola_user_uuid': tolauser.tola_user_uuid,
                  'email': '*****@*****.**'},
            headers={'Authorization': 'Token TheToken'})
예제 #21
0
 def test_list_codedfield_normaluser(self):
     tola_user = factories.TolaUser()
     self.request.user = tola_user.user
     view = CodedFieldViewSet.as_view({'get': 'list'})
     response = view(self.request)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 0)
예제 #22
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)
예제 #23
0
 def setUp(self):
     self.client = Client()
     self.user = factories.User()
     self.user.set_password('hikaya123')
     self.user.save()
     factories.TolaUser(user=self.user)
     self.silo = factories.Silo(owner=self.user)
     self.client.login(username=self.user.username, password='******')
예제 #24
0
    def test_form_validate_fail_owner_from_diff_org(self,
                                                    mock_get_workflowteams):
        mock_get_workflowteams.return_value = []
        user = factories.User(first_name='Homer', last_name='Simpson')
        factories.TolaUser(user=user, organization=self.tola_user.organization)
        another_user = factories.User(username='******')
        factories.TolaUser(user=another_user)
        silo = factories.Silo(owner=self.user)

        data = {
            'name': silo.name,
            'owner': another_user.id,
            'shared': [user.id],
        }
        form = forms.SiloForm(user=self.user, data=data, instance=silo)

        self.assertFalse(form.is_valid())
예제 #25
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.hikaya_user = factories.TolaUser()

        self.read = factories.Read(read_name="test_data",
                                   owner=self.hikaya_user.user)

        self.read_type = factories.ReadType.create(read_type='CSV')
예제 #26
0
 def setUp(self):
     factories.Organization(id=1)
     self.user = factories.User(first_name='Homer', last_name='Simpson')
     self.hikaya_user = factories.TolaUser(user=self.user)
     self.read = factories.Read(read_name="test_data",
                                owner=self.hikaya_user.user)
     self.silo = factories.Silo(owner=self.hikaya_user.user,
                                reads=[self.read])
     self.factory = APIRequestFactory()
예제 #27
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.tola_user = factories.TolaUser()

        self.read = factories.Read(read_name="test_data",
                                   owner=self.tola_user.user)
        self.silo = factories.Silo(owner=self.tola_user.user,
                                   reads=[self.read])
        self._import_json(self.silo, self.read)
예제 #28
0
    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])
예제 #29
0
    def setUp(self):
        self.country = factories.Country()
        SiteProfile.objects.bulk_create([
            SiteProfile(name='SiteProfile1', country=self.country),
            SiteProfile(name='SiteProfile2', country=self.country),
        ])
        factories.Indicator.create_batch(2)

        self.factory = APIRequestFactory()
        self.tola_user = factories.TolaUser()
예제 #30
0
 def test_activated_save_two_times(self):
     """
     When the TolaUser is saved, a WorkflowTeam object for that user and
     the default program is created only once.
     """
     role_program_admin = factories.Group(name=ROLE_PROGRAM_ADMIN)
     tolauser = factories.TolaUser()  # triggers the signal
     tolauser.save()  # triggers the signal again
     wft = WorkflowTeam.objects.get(workflow_user=tolauser)
     self.assertEqual(wft.role, role_program_admin)