def test_filter_documentation_country_superuser(self):
        self.tola_user.user.is_staff = True
        self.tola_user.user.is_superuser = True
        self.tola_user.user.save()

        wflvl1_1 = factories.WorkflowLevel1(
            name='WorkflowLevel1_1', organization=self.tola_user.organization)
        WorkflowTeam.objects.create(workflow_user=self.tola_user,
                                    workflowlevel1=wflvl1_1)
        documentation1 = factories.Documentation(name='Document 1',
                                                 workflowlevel1=wflvl1_1)

        another_org = factories.Organization(name='Another Org')
        wflvl1_2 = factories.WorkflowLevel1(name='WorkflowLevel1_2',
                                            organization=another_org)
        WorkflowTeam.objects.create(workflow_user=self.tola_user,
                                    workflowlevel1=wflvl1_2)
        factories.Documentation(name='Document 2', workflowlevel1=wflvl1_2)

        request = self.factory.get('/api/documentation'
                                   '/?workflowlevel1__organization__id=%s' %
                                   self.tola_user.organization.pk)
        request.user = self.tola_user.user
        view = DocumentationViewSet.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'], documentation1.name)
    def test_filter_documentation_org_org_admin(self):
        group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
        self.tola_user.user.groups.add(group_org_admin)

        country1 = factories.Country()
        wflvl1_1 = factories.WorkflowLevel1(
            name='WorkflowLevel1_1',
            country=[country1],
            organization=self.tola_user.organization)
        WorkflowTeam.objects.create(workflow_user=self.tola_user,
                                    workflowlevel1=wflvl1_1)
        documentation1 = factories.Documentation(name='Document 1',
                                                 workflowlevel1=wflvl1_1)

        country2 = factories.Country(country='Brazil', code='BR')
        wflvl1_2 = factories.WorkflowLevel1(
            name='WorkflowLevel1_2',
            country=[country2],
            organization=self.tola_user.organization)
        WorkflowTeam.objects.create(workflow_user=self.tola_user,
                                    workflowlevel1=wflvl1_2)
        factories.Documentation(name='Document 2', workflowlevel1=wflvl1_2)

        request = self.factory.get('/api/documentation'
                                   '/?workflowlevel1__country__country=%s' %
                                   country1.country)
        request.user = self.tola_user.user
        view = DocumentationViewSet.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'], documentation1.name)
    def test_delete_documentation_normal_user(self):
        wflvl1 = factories.WorkflowLevel1()
        documentation = factories.Documentation(workflowlevel1=wflvl1)

        request = self.factory.delete('/api/documentation/')
        request.user = self.tola_user.user
        view = DocumentationViewSet.as_view({'delete': 'destroy'})
        response = view(request, pk=documentation.pk)
        self.assertEquals(response.status_code, 403)
        Documentation.objects.get(pk=documentation.pk)
    def test_list_documentation_normaluser_one_result(self):
        request = self.factory.get('/api/documentation/')
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        WorkflowTeam.objects.create(workflow_user=self.tola_user,
                                    workflowlevel1=wflvl1)
        factories.Documentation(workflowlevel1=wflvl1)

        request.user = self.tola_user.user
        view = DocumentationViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
    def test_delete_documentation_diff_org_program_admin(self):
        another_org = factories.Organization(name='Another Org')
        wflvl1 = factories.WorkflowLevel1(organization=another_org)
        documentation = factories.Documentation(workflowlevel1=wflvl1)
        group_org_admin = factories.Group(name=ROLE_PROGRAM_ADMIN)
        self.tola_user.user.groups.add(group_org_admin)

        request = self.factory.delete('/api/documentation/')
        request.user = self.tola_user.user
        view = DocumentationViewSet.as_view({'delete': 'destroy'})
        response = view(request, pk=documentation.pk)
        self.assertEquals(response.status_code, 403)
        Documentation.objects.get(pk=documentation.pk)
    def test_delete_documentation_org_admin(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        documentation = factories.Documentation(workflowlevel1=wflvl1)
        group_org_admin = factories.Group(name=ROLE_ORGANIZATION_ADMIN)
        self.tola_user.user.groups.add(group_org_admin)

        request = self.factory.delete('/api/documentation/')
        request.user = self.tola_user.user
        view = DocumentationViewSet.as_view({'delete': 'destroy'})
        response = view(request, pk=documentation.pk)
        self.assertEquals(response.status_code, 204)
        self.assertRaises(Documentation.DoesNotExist,
                          Documentation.objects.get,
                          pk=documentation.pk)
 def test_delete_documentation_superuser(self):
     self.tola_user.user.is_superuser = True
     self.tola_user.user.is_staff = True
     self.tola_user.user.save()
     wflvl1 = factories.WorkflowLevel1(
         organization=self.tola_user.organization)
     documentation = factories.Documentation(workflowlevel1=wflvl1)
     request = self.factory.delete('/api/documentation/')
     request.user = self.tola_user.user
     view = DocumentationViewSet.as_view({'delete': 'destroy'})
     response = view(request, pk=documentation.pk)
     self.assertEquals(response.status_code, 204)
     self.assertRaises(Documentation.DoesNotExist,
                       Documentation.objects.get,
                       pk=documentation.pk)
    def test_delete_documentation_program_admin(self):
        wflvl1 = factories.WorkflowLevel1(
            organization=self.tola_user.organization)
        documentation = factories.Documentation(workflowlevel1=wflvl1)
        WorkflowTeam.objects.create(
            workflow_user=self.tola_user,
            workflowlevel1=wflvl1,
            role=factories.Group(name=ROLE_PROGRAM_ADMIN))

        request = self.factory.delete('/api/documentation/')
        request.user = self.tola_user.user
        view = DocumentationViewSet.as_view({'delete': 'destroy'})
        response = view(request, pk=documentation.pk)
        self.assertEquals(response.status_code, 204)
        self.assertRaises(Documentation.DoesNotExist,
                          Documentation.objects.get,
                          pk=documentation.pk)