예제 #1
0
    def test_partner_user_list(self):
        """Test the API response and queryset count.
        Also, the ordering by UserFilter will be tested: last_login, first_name, last_name, partner, and ordering.
        """
        # Create some test users for partner
        NUM_TEST_USERS = 2
        for idx in range(NUM_TEST_USERS):
            user = factories.PartnerUserFactory(partner=self.partner)
            factories.IPPRPRoleFactory(user=user,
                                       workspace=self.workspace,
                                       role=PRP_ROLE_TYPES.ip_editor)

            if idx == 0:
                factories.IPPRPRoleFactory(user=user,
                                           workspace=self.workspace,
                                           role=PRP_ROLE_TYPES.ip_admin)

        response = self.client.get(reverse('users') + '?portal=IP')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(NUM_TEST_USERS, response.data['count'])

        role_count_annotated_queryset = User.objects.exclude(
            id=self.user.id).annotate(
                role_count=Count('prp_roles')).order_by('-id')

        # API Ordering test
        response = self.client.get(
            reverse('users') + '?portal=IP&ordering=-status')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data['results'][0]['id'],
            role_count_annotated_queryset.order_by('-last_login',
                                                   '-role_count').first().id)

        response = self.client.get(
            reverse('users') + '?portal=IP&ordering=status')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data['results'][0]['id'],
            role_count_annotated_queryset.order_by('last_login',
                                                   'role_count').first().id)

        # API Filtering test
        filter_user = User.objects.get(
            prp_roles__role=PRP_ROLE_TYPES.ip_admin, )
        filter_args = "?portal=IP&name_email={}&status=active&partners={}&roles={}&workspaces={}&clusters={}".format(
            filter_user.first_name,
            filter_user.partner_id,
            PRP_ROLE_TYPES.ip_admin,
            self.workspace.id,
            '',
        )

        response = self.client.get(reverse('users') + filter_args)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['results'][0]['id'], filter_user.id)
    def setUp(self):

        self.workspace = factories.WorkspaceFactory()
        self.response_plan = factories.ResponsePlanFactory(
            workspace=self.workspace,
            start=beginning_of_this_year,
            end=end_of_this_year,
        )
        self.cluster = factories.ClusterFactory(type='cccm', response_plan=self.response_plan)

        self.carto_table = factories.CartoDBTableFactory()
        self.loc1 = factories.LocationFactory()
        self.loc2 = factories.LocationFactory()
        self.loc1.workspaces.add(self.workspace)
        self.loc2.workspaces.add(self.workspace)
        self.unicef_officer = factories.PersonFactory()
        self.unicef_focal_point = factories.PersonFactory()
        self.partner_focal_point = factories.PersonFactory()
        self.objective = factories.ClusterObjectiveFactory(
            cluster=self.cluster,
            locations=[
                self.loc1,
                self.loc2,
            ]
        )
        self.activity = factories.ClusterActivityFactory(
            cluster_objective=self.objective,
            locations=[
                self.loc1, self.loc2
            ]
        )
        self.partner = factories.PartnerFactory(country_code=faker.country_code(), clusters=[self.cluster, ])
        self.user = factories.NonPartnerUserFactory()
        self.partner_user = factories.PartnerUserFactory(partner=self.partner)
        factories.ClusterPRPRoleFactory(user=self.user, workspace=self.workspace, cluster=self.cluster, role=PRP_ROLE_TYPES.cluster_imo)
        factories.IPPRPRoleFactory(user=self.partner_user, workspace=self.workspace, role=PRP_ROLE_TYPES.ip_authorized_officer)
        factories.IPPRPRoleFactory(user=self.partner_user, workspace=self.workspace, cluster=None, role=PRP_ROLE_TYPES.cluster_member)
        self.project = factories.PartnerProjectFactory(
            partner=self.partner,
            clusters=[self.cluster],
            locations=[self.loc1, self.loc2],
            start_date=datetime.date(today.year, 3, 1),
            end_date=datetime.date(today.year, 10, 25),
        )
        super().setUp()
예제 #3
0
    def setUp(self):
        self.workspace = factories.WorkspaceFactory()
        self.partner = factories.PartnerFactory(
            country_code=faker.country_code())
        self.user = factories.PartnerUserFactory(partner=self.partner)
        self.ao_user_role = factories.IPPRPRoleFactory(
            user=self.user,
            workspace=self.workspace,
            role=PRP_ROLE_TYPES.ip_authorized_officer)

        super().setUp()
예제 #4
0
    def test_user_type(self):
        user = factories.PartnerUserFactory()
        self.assertFalse(user.prp_roles.exists())
        self.assertIsNone(user.user_type)

        # partner role
        factories.IPPRPRoleFactory(user=user, workspace=self.workspace)
        self.assertEqual(user.user_type, USER_TYPES.partner)

        # cluster im role
        factories.ClusterPRPRoleFactory(
            user=user,
            workspace=self.workspace,
            role=PRP_ROLE_TYPES.cluster_imo,
        )
        self.assertEqual(user.user_type, USER_TYPES.imo)

        # cluster admin
        factories.ClusterPRPRoleFactory(
            user=user,
            workspace=self.workspace,
            role=PRP_ROLE_TYPES.cluster_system_admin,
        )
        self.assertEqual(user.user_type, USER_TYPES.cluster_admin)
    def setUp(self):

        self.workspace = factories.WorkspaceFactory()
        self.response_plan = factories.ResponsePlanFactory(
            workspace=self.workspace,
            start=beginning_of_this_year,
            end=end_of_this_year,
        )
        self.cluster = factories.ClusterFactory(type='cccm', response_plan=self.response_plan)

        self.carto_table = factories.CartoDBTableFactory()
        self.loc1 = factories.LocationFactory()
        self.loc2 = factories.LocationFactory()
        self.loc1.workspaces.add(self.workspace)
        self.loc2.workspaces.add(self.workspace)
        self.unicef_officer = factories.PersonFactory()
        self.unicef_focal_point = factories.PersonFactory()
        self.partner_focal_point = factories.PersonFactory()
        self.objective = factories.ClusterObjectiveFactory(
            cluster=self.cluster,
            locations=[
                self.loc1,
                self.loc2,
            ]
        )
        self.activity = factories.ClusterActivityFactory(
            cluster_objective=self.objective,
            locations=[
                self.loc1, self.loc2
            ]
        )
        self.partner = factories.PartnerFactory(country_code=faker.country_code(), clusters=[self.cluster, ])
        self.user = factories.NonPartnerUserFactory()
        self.partner_user = factories.PartnerUserFactory(partner=self.partner)
        factories.ClusterPRPRoleFactory(user=self.user, workspace=self.workspace, cluster=self.cluster, role=PRP_ROLE_TYPES.cluster_imo)
        factories.IPPRPRoleFactory(user=self.partner_user, workspace=self.workspace, role=PRP_ROLE_TYPES.ip_authorized_officer)
        factories.IPPRPRoleFactory(user=self.partner_user, workspace=self.workspace, cluster=None, role=PRP_ROLE_TYPES.cluster_member)
        self.project = factories.PartnerProjectFactory(
            partner=self.partner,
            clusters=[self.cluster],
            locations=[self.loc1, self.loc2],
        )
        self.p_activity = factories.ClusterActivityPartnerActivityFactory(
            partner=self.partner,
            cluster_activity=self.activity,
        )
        self.project_context = factories.PartnerActivityProjectContextFactory(
            project=self.project,
            activity=self.p_activity,
            start_date=datetime.date(today.year, 3, 1),
            end_date=datetime.date(today.year, 10, 25),
        )
        self.sample_disaggregation_value_map = {
            "height": ["tall", "medium", "short", "extrashort"],
            "age": ["1-2m", "3-4m", "5-6m", '7-10m', '11-13m', '14-16m'],
            "gender": ["male", "female", "other"],
        }

        blueprint = factories.QuantityTypeIndicatorBlueprintFactory(
            unit=IndicatorBlueprint.NUMBER,
            calculation_formula_across_locations=IndicatorBlueprint.SUM,
            calculation_formula_across_periods=IndicatorBlueprint.SUM,
        )
        self.partneractivity_reportable = factories.QuantityReportableToPartnerActivityProjectContextFactory(
            content_object=self.project_context, blueprint=blueprint
        )

        factories.LocationWithReportableLocationGoalFactory(
            location=self.loc1,
            reportable=self.partneractivity_reportable,
        )

        factories.LocationWithReportableLocationGoalFactory(
            location=self.loc2,
            reportable=self.partneractivity_reportable,
        )

        self.pd = factories.ProgrammeDocumentFactory(
            workspace=self.workspace,
            partner=self.partner,
            sections=[factories.SectionFactory(), ],
            unicef_officers=[self.unicef_officer, ],
            unicef_focal_point=[self.unicef_focal_point, ],
            partner_focal_point=[self.partner_focal_point, ]
        )

        for idx in range(2):
            qpr_period = factories.QPRReportingPeriodDatesFactory(programme_document=self.pd)
            factories.ProgressReportFactory(
                start_date=qpr_period.start_date,
                end_date=qpr_period.end_date,
                due_date=qpr_period.due_date,
                report_number=idx + 1,
                report_type=qpr_period.report_type,
                is_final=False,
                programme_document=self.pd,
                submitted_by=self.user,
                submitting_user=self.user,
            )

        for idx in range(6):
            hr_period = factories.HRReportingPeriodDatesFactory(programme_document=self.pd)
            factories.ProgressReportFactory(
                start_date=hr_period.start_date,
                end_date=hr_period.end_date,
                due_date=hr_period.due_date,
                report_number=idx + 1,
                report_type=hr_period.report_type,
                is_final=False,
                programme_document=self.pd,
                submitted_by=self.user,
                submitting_user=self.user,
            )

        self.cp_output = factories.PDResultLinkFactory(
            programme_document=self.pd,
        )
        self.llo = factories.LowerLevelOutputFactory(
            cp_output=self.cp_output,
        )
        self.llo_reportable = factories.QuantityReportableToLowerLevelOutputFactory(
            content_object=self.llo,
            blueprint=factories.QuantityTypeIndicatorBlueprintFactory(
                unit=IndicatorBlueprint.NUMBER,
                calculation_formula_across_locations=IndicatorBlueprint.SUM,
            )
        )

        self.llo_reportable.disaggregations.clear()
        self.partneractivity_reportable.disaggregations.clear()

        # Create the disaggregations and values in the db for all response plans
        # including one for no response plan as well
        for disagg_name, values in self.sample_disaggregation_value_map.items():
            disagg = factories.IPDisaggregationFactory(name=disagg_name)
            cluster_disagg = factories.DisaggregationFactory(name=disagg_name, response_plan=self.response_plan)

            self.llo_reportable.disaggregations.add(disagg)
            self.partneractivity_reportable.disaggregations.add(cluster_disagg)

            for value in values:
                factories.DisaggregationValueFactory(
                    disaggregation=cluster_disagg,
                    value=value
                )
                factories.DisaggregationValueFactory(
                    disaggregation=disagg,
                    value=value
                )

        factories.LocationWithReportableLocationGoalFactory(
            location=self.loc1,
            reportable=self.llo_reportable,
        )

        factories.LocationWithReportableLocationGoalFactory(
            location=self.loc2,
            reportable=self.llo_reportable,
        )

        for _ in range(2):
            factories.ClusterIndicatorReportFactory(
                reportable=self.partneractivity_reportable,
                report_status=INDICATOR_REPORT_STATUS.submitted,
            )

        # Creating Level-3 disaggregation location data for all locations
        generate_3_num_disagg_data(self.partneractivity_reportable, indicator_type="quantity")

        for loc_data in IndicatorLocationData.objects.filter(indicator_report__reportable=self.partneractivity_reportable):
            QuantityIndicatorDisaggregator.post_process(loc_data)

        for pr in self.pd.progress_reports.all():
            factories.ProgressReportIndicatorReportFactory(
                progress_report=pr,
                reportable=self.llo_reportable,
                report_status=INDICATOR_REPORT_STATUS.submitted,
                overall_status=OVERALL_STATUS.met,
            )

        # Creating Level-3 disaggregation location data for all locations
        generate_3_num_disagg_data(self.llo_reportable, indicator_type="quantity")

        for loc_data in IndicatorLocationData.objects.filter(indicator_report__reportable=self.llo_reportable):
            QuantityIndicatorDisaggregator.post_process(loc_data)

        super().setUp()
예제 #6
0
    def test_cluster_user_list(self):
        """Test the API response for cluster users.
        """
        # Create some test users for partner
        self.imo_user = factories.NonPartnerUserFactory()
        response_plan = factories.ResponsePlanFactory(workspace=self.workspace)
        cluster = factories.ClusterFactory(response_plan=response_plan)
        factories.ClusterPRPRoleFactory(user=self.imo_user,
                                        workspace=self.workspace,
                                        cluster=cluster,
                                        role=PRP_ROLE_TYPES.cluster_imo)

        # Test API as IMO first
        self.client.force_authenticate(self.imo_user)

        # Create 1 cluster viewer and 1 cluster member with Partner
        NUM_TEST_USERS = 2
        for idx in range(NUM_TEST_USERS):
            if idx == 0:
                user = factories.NonPartnerUserFactory()
                factories.ClusterPRPRoleFactory(
                    user=user,
                    workspace=self.workspace,
                    cluster=cluster,
                    role=PRP_ROLE_TYPES.cluster_viewer)
            else:
                user = factories.PartnerUserFactory(partner=self.partner)
                factories.IPPRPRoleFactory(
                    user=user,
                    workspace=self.workspace,
                    role=PRP_ROLE_TYPES.ip_editor,
                )
                factories.ClusterPRPRoleFactory(
                    user=user,
                    workspace=self.workspace,
                    cluster=cluster,
                    role=PRP_ROLE_TYPES.cluster_member)

        # Test users + IMO user
        response = self.client.get(reverse('users') + '?portal=CLUSTER')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(NUM_TEST_USERS + 1, response.data['count'])

        # Test API as Cluster member
        member_user = User.objects.filter(
            prp_roles__role=PRP_ROLE_TYPES.cluster_member).first()
        self.client.force_authenticate(member_user)

        response = self.client.get(reverse('users') + '?portal=CLUSTER')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            User.objects.filter(
                prp_roles__role=PRP_ROLE_TYPES.cluster_member).count(),
            response.data['count'])

        # Test API as Cluster viewer
        viewer_user = User.objects.filter(
            prp_roles__role=PRP_ROLE_TYPES.cluster_viewer).first()
        self.client.force_authenticate(viewer_user)

        response = self.client.get(reverse('users') + '?portal=CLUSTER')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)