Ejemplo n.º 1
0
 def test_authorized_user_accessible_platforms_titles(
         self, authenticated_client, organizations, platforms, valid_identity, titles,
         report_type_nd):
     identity = Identity.objects.select_related('user').get(identity=valid_identity)
     organization = organizations[0]
     platform = platforms[0]
     UserOrganization.objects.create(user=identity.user, organization=organization)
     OrganizationPlatform.objects.create(organization=organization, platform=platform)
     # we need to connect some titles with the platform which is done indirectly through
     # AccessLog instances
     # we create 2 access logs but both for the same title so that we can check that
     # - title is present in the output only once - distinct is used properly
     # - second title is not present - the filtering works OK
     rt = report_type_nd(0)
     metric = Metric.objects.create(short_name='m1', name='Metric1')
     import_batch = ImportBatch.objects.create(platform=platform, organization=organization,
                                               report_type=rt)
     al1 = AccessLog.objects.create(
         platform=platform, target=titles[0], value=1, date='2019-01-01', report_type=rt,
         metric=metric, organization=organization, import_batch=import_batch
     )
     al2 = AccessLog.objects.create(
         platform=platform, target=titles[0], value=1, date='2019-02-01', report_type=rt,
         metric=metric, organization=organization, import_batch=import_batch
     )
     create_platformtitle_links_from_accesslogs([al1, al2])
     resp = authenticated_client.get(reverse('platform-title-list',
                                             args=[organization.pk, platform.pk]))
     assert resp.status_code == 200
     print(resp.json())
     assert len(resp.json()) == 1
     assert resp.json()[0]['isbn'] == titles[0].isbn
     assert resp.json()[0]['name'] == titles[0].name
Ejemplo n.º 2
0
 def test_authorized_user_accessible_platforms_titles_count_organization_filter(
         self, authenticated_client, organizations, platforms, valid_identity, titles,
         report_type_nd, interest_rt):
     """
     Test that when using the API to get number of accesses to a title on a platform,
     that data for a different organization are not counted in
     """
     identity = Identity.objects.select_related('user').get(identity=valid_identity)
     organization = organizations[0]
     platform = platforms[0]
     other_organization = organizations[1]
     UserOrganization.objects.create(user=identity.user, organization=organization)
     OrganizationPlatform.objects.create(organization=organization, platform=platform)
     # we need to connect some titles with the platform which is done indirectly through
     # AccessLog instances
     # we create 2 access logs but both for the same title so that we can check that
     # - title is present in the output only once - distinct is used properly
     # - second title is not present - the filtering works OK
     rt = report_type_nd(0)
     ig = InterestGroup.objects.create(short_name='interest1', position=1)
     metric = Metric.objects.create(short_name='m1', name='Metric1')
     ReportInterestMetric.objects.create(report_type=rt, metric=metric, interest_group=ig)
     PlatformInterestReport.objects.create(report_type=rt, platform=platform)
     import_batch1 = ImportBatch.objects.create(platform=platform, organization=organization,
                                                report_type=rt)
     import_batch2 = ImportBatch.objects.create(platform=platform, report_type=rt,
                                                organization=other_organization)
     al1 = AccessLog.objects.create(
         platform=platform, target=titles[0], value=3, date='2019-01-01', report_type=rt,
         metric=metric, organization=organization, import_batch=import_batch1
     )
     al2 = AccessLog.objects.create(
         platform=platform, target=titles[0], value=2, date='2019-01-01', report_type=rt,
         metric=metric, organization=other_organization, import_batch=import_batch2
     )
     create_platformtitle_links_from_accesslogs([al1, al2])
     sync_interest_by_import_batches()
     resp = authenticated_client.get(reverse('platform-title-interest-list',
                                             args=[organization.pk, platform.pk]))
     assert resp.status_code == 200
     assert 'results' in resp.json()
     data = resp.json()['results']
     assert len(data) == 1
     assert data[0]['isbn'] == titles[0].isbn
     assert data[0]['name'] == titles[0].name
     assert data[0]['interests']['interest1'] == 3
Ejemplo n.º 3
0
    def test_extra_params_is_not_polluted_by_extra_data(
            self, organizations, report_type_nd):
        assert SushiCredentials.objects.count() == 0
        data = [
            {
                'platform': 'XXX',
                'organization': organizations[1].internal_id,
                'customer_id': 'BBB',
                'requestor_id': 'RRRX',
                'URL': 'http://this.is/test/2',
                'version': 5,
                'extra_attrs': 'auth=un,pass;api_key=kekekeyyy;foo=bar'
            },
        ]
        Platform.objects.create(short_name='XXX', name='XXXX', ext_id=10)
        stats = import_sushi_credentials(data)
        assert stats['added'] == 1
        assert SushiCredentials.objects.count() == 1
        credentials = SushiCredentials.objects.all()
        # let's create
        cr1 = credentials[0]
        cr1.create_sushi_client()
        report = CounterReportType.objects.create(
            code='tr',
            name='tr',
            counter_version=5,
            report_type=report_type_nd(0))
        orig_params = deepcopy(Sushi5Client.EXTRA_PARAMS)

        def mock_get_report_data(*args, **kwargs):
            return Counter5ReportBase()

        Sushi5Client.get_report_data = mock_get_report_data
        cr1.fetch_report(report,
                         start_date='2020-01-01',
                         end_date='2020-01-31')
        assert orig_params == Sushi5Client.EXTRA_PARAMS
Ejemplo n.º 4
0
 def test_authorized_user_accessible_platforms_with_titles(
         self, authenticated_client, organizations, platforms, valid_identity, titles,
         report_type_nd, interest_rt):
     identity = Identity.objects.select_related('user').get(identity=valid_identity)
     organization = organizations[0]
     platform = platforms[0]
     UserOrganization.objects.create(user=identity.user, organization=organization)
     OrganizationPlatform.objects.create(organization=organization, platform=platform)
     # we need to create access logs to connect the platform and title
     rt = report_type_nd(0)
     ig = InterestGroup.objects.create(short_name='interest1', position=1)
     metric = Metric.objects.create(short_name='m1', name='Metric1')
     ReportInterestMetric.objects.create(report_type=rt, metric=metric, interest_group=ig)
     PlatformInterestReport.objects.create(report_type=rt, platform=platform)
     import_batch1 = ImportBatch.objects.create(platform=platform, organization=organization,
                                                report_type=rt)
     import_batch2 = ImportBatch.objects.create(platform=platform,
                                                organization=organizations[1], report_type=rt)
     AccessLog.objects.create(platform=platform, target=titles[0], value=5, date='2019-01-01',
                              report_type=rt, metric=metric, organization=organization,
                              import_batch=import_batch1)
     AccessLog.objects.create(platform=platform, target=titles[0], value=7, date='2019-01-01',
                              report_type=rt, metric=metric, organization=organizations[1],
                              import_batch=import_batch2)
     sync_interest_by_import_batches()
     resp = authenticated_client.get(reverse('detailed-platform-list', args=[organization.pk]))
     assert resp.status_code == 200
     print(resp.content)
     assert len(resp.json()) == 1
     assert resp.json()[0]['pk'] == platform.pk
     assert resp.json()[0]['title_count'] == 1
     assert resp.json()[0]['interests']['interest1']['value'] == 5
     # try with date range outside
     resp = authenticated_client.get(reverse('detailed-platform-list',
                                             args=[organization.pk]) + '?start=2019-02')
     assert resp.status_code == 200
     assert len(resp.json()) == 0
Ejemplo n.º 5
0
 def fn(name):
     rt = report_type_nd(0, short_name=name + 'rt')
     return CounterReportType.objects.create(code=name,
                                             counter_version=5,
                                             name=name + ' title',
                                             report_type=rt)
Ejemplo n.º 6
0
def counter_report_type(report_type_nd):
    report_type = report_type_nd(0)
    yield CounterReportType.objects.create(code='TR',
                                           counter_version=5,
                                           name='Title report',
                                           report_type=report_type)