Beispiel #1
0
    def test_post_activity_fail_if_not_in_admin_group(self):
        """
        Test the user can only POST activities as a publisher of which he is
        in the admin Group
        """
        admin_group = OrganisationAdminGroupFactory.create()
        user = OrganisationUserFactory.create(user__username='******')

        # admin_group.organisationuser_set.add(user)

        self.c.force_authenticate(user.user)

        PublisherFactory.create()

        codelist_factory.VersionFactory.create(code="2.02")

        data = {
            "iati_identifier": "WOPA",
            "publisher_id": admin_group.publisher.id
        }

        res = self.c.post(
            "/api/publishers/{}/activities/?format=json".format(
                admin_group.publisher.id),
            data,
            format='json'
        )

        self.assertEqual(res.status_code, 403)
    def test_post_activity_fail_if_not_in_admin_group(self):
        """
        Test the user can only POST activities as a publisher of which he is
        in the admin Group
        """
        admin_group = OrganisationAdminGroupFactory.create()
        user = OrganisationUserFactory.create(user__username='******')

        # admin_group.organisationuser_set.add(user)

        self.c.force_authenticate(user.user)

        PublisherFactory.create()

        codelist_factory.VersionFactory.create(code="2.02")

        data = {
            "iati_identifier": "WOPA",
            "publisher_id": admin_group.publisher.id
        }

        res = self.c.post(
            "/api/publishers/{}/activities/?format=json".format(
                admin_group.publisher.id),
            data,
            format='json'
        )

        self.assertEqual(res.status_code, 403)
    def test_ReportingOrganisationSerializer(self):
        pub = PublisherFactory()

        org = pub.organisation
        serializer = serializers.ReportingOrganisationSerializer(
            org, context={'request': self.request_dummy})

        desired_url = reverse(
            'organisations:organisation-detail',
            kwargs={'pk': org.organisation_identifier},
        )

        self.assertEqual(
            serializer.data, {
                'id':
                org.id,
                'ref':
                org.organisation_identifier,
                'url':
                'http://testserver{0}'.format(desired_url),
                'type':
                OrderedDict([('code', org.type.code),
                             ('name', org.type.name)]),
                'narratives': []
            })
Beispiel #4
0
    def test_PublisherSerializer(self):
        publisher = PublisherFactory.build()
        serializer = PublisherSerializer(
            publisher, context={'request': self.request_dummy})

        assert serializer.data['org_id'] == publisher.org_id,\
            """
            'publisher.type' should be serialized to a field called 'type'
            """
        assert serializer.data['org_abbreviate'] == publisher.org_abbreviate,\
            """
            'publisher.source_url' should be serialized to a field called 'source_url'
            """
        assert serializer.data['org_name'] == publisher.org_name,\
            """
            'publisher.org_name' should be serialized to a field called 'org_name'
            """
        assert serializer.data['datasets'] == [],\
            """
            'publisher.datasets' should be serialized to a field called 'datasets'
            """
        assert serializer.data['activity_count'] == 0,\
            """
            'publisher.activity_count' should be serialized to a field called 'activity_count'
            """

        required_fields = ('url', 'org_id', 'org_abbreviate', 'org_name',
                           'datasets', 'activities', 'activity_count')

        assertion_msg = "the field '{0}' should be in the serialized dataset"
        for field in required_fields:
            assert field in serializer.data, assertion_msg.format(field)
Beispiel #5
0
    def test_activities_endpoint_by_reporting_organisation(self):
        '''Tests if Activities endpoint works properly when having
        organisation identifier as a query parameter
        '''
        publisher = PublisherFactory()
        organisation = publisher.organisation
        reporting_organisation = iati_factory.ActivityReportingOrganisationFactory(  # NOQA: E501
            organisation=organisation,
            # This is the query param:
            ref=organisation.organisation_identifier
        )

        url = reverse('activities:activity-list')
        endpoint_url = "%s?format=json&reporting_organisation_identifier=%s" % (  # NOQA: E501
            url, reporting_organisation.ref
        )

        response = self.c.get(endpoint_url)
        self.assertEqual(response.status_code, 200)

        resp_data = json.loads(response.content)
        self.assertEqual(resp_data['count'], 1)
        self.assertEqual(
            resp_data['results'][0]['iati_identifier'],
            reporting_organisation.activity.iati_identifier
        )
Beispiel #6
0
    def test_publisher_activities_link(self):
        '''Tests if PublisherSerializer returs proper link for its activities
        '''
        publisher = PublisherFactory()

        serializer = PublisherSerializer(
            publisher, context={'request': self.request_dummy})

        url = self.request_dummy.build_absolute_uri(
            reverse('activities:activity-list'))
        endpoint_url = (url + '?reporting_organisation_identifier=' +
                        publisher.publisher_iati_id)

        assert serializer.data['activities'] == endpoint_url
Beispiel #7
0
    def test_activities_endpoint_by_multiple_reporting_organisations(self):
        '''Tests if Activities endpoint works properly when having multiple
        organisation identifiers as a query parameter
        '''
        first_publisher = PublisherFactory()

        first_organisation = first_publisher.organisation
        second_organisation = iati_factory.OrganisationFactory(
            organisation_identifier='different_organisation_ID'
        )

        first_reporting_organisation = iati_factory.ActivityReportingOrganisationFactory(  # NOQA: E501
            organisation=first_organisation,
            # This is the query param:
            ref=first_organisation.organisation_identifier,
            activity=iati_factory.ActivityFactory(
                iati_identifier='A'
            )
        )
        second_reporting_organisation = iati_factory.ActivityReportingOrganisationFactory(  # NOQA: E501
            organisation=second_organisation,
            ref=second_organisation.organisation_identifier,
            activity=iati_factory.ActivityFactory(iati_identifier='B')
        )

        url = reverse('activities:activity-list')
        endpoint_url = "%s?format=json&reporting_organisation_identifier=%s,%s" % (  # NOQA: E501
            url,
            first_reporting_organisation.ref,
            second_reporting_organisation.ref
        )

        response = self.c.get(endpoint_url)
        self.assertEqual(response.status_code, 200)

        resp_data = json.loads(response.content)
        self.assertEqual(resp_data['count'], 2)

        self.assertEqual(
            resp_data['results'][0]['iati_identifier'],
            first_reporting_organisation.activity.iati_identifier
        )

        self.assertEqual(
            resp_data['results'][1]['iati_identifier'],
            second_reporting_organisation.activity.iati_identifier
        )