Ejemplo n.º 1
0
 def test_api_secondary_dim(self, counter_records, organizations, report_type_nd,
                            primary_dim, secondary_dim, count, authenticated_client):
     platform = Platform.objects.create(ext_id=1234, short_name='Platform1', name='Platform 1',
                                        provider='Provider 1')
     data = [
         ['Title1', '2018-01-01', '1v1', '2v1', '3v1', 1],
         ['Title1', '2018-01-01', '1v2', '2v1', '3v1', 2],
         ['Title2', '2018-01-01', '1v2', '2v2', '3v1', 4],
         ['Title1', '2018-02-01', '1v1', '2v1', '3v1', 8],
         ['Title2', '2018-02-01', '1v1', '2v2', '3v2', 16],
         ['Title1', '2018-03-01', '1v1', '2v3', '3v2', 32],
     ]
     crs = list(counter_records(data, metric='Hits', platform='Platform1'))
     organization = organizations[0]
     report_type = report_type_nd(3)
     import_batch = ImportBatch.objects.create(organization=organization, platform=platform,
                                               report_type=report_type)
     import_counter_records(report_type, organization, platform, crs, import_batch)
     assert AccessLog.objects.count() == 6
     metric = Metric.objects.get(short_name='Hits')
     if type(primary_dim) is int:
         primary_dim = report_type.dimensions_sorted[primary_dim-1].short_name
     params = {'organization': organization.pk,
               'metric': metric.pk,
               'platform': platform.pk,
               'prim_dim': primary_dim
               }
     if secondary_dim:
         params['sec_dim'] = report_type.dimensions_sorted[secondary_dim-1].short_name
     resp = authenticated_client.get(reverse('chart_data_raw', args=(report_type.pk,)),
                                     params)
     assert resp.status_code == 200
     data = json.loads(resp.content)
     assert 'data' in data
     assert len(data['data']) == count
Ejemplo n.º 2
0
 def get_data(params):
     resp = authenticated_client.get(reverse('chart_data_raw', args=(report_type.pk,)),
                                     params)
     assert resp.status_code == 200
     result = json.loads(resp.content)
     assert 'data' in result
     return result['data']
Ejemplo n.º 3
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.º 4
0
 def test_authorized_user_inaccessible_platforms(self, authenticated_client, organizations,
                                                 platforms, valid_identity):
     identity = Identity.objects.select_related('user').get(identity=valid_identity)
     UserOrganization.objects.create(user=identity.user, organization=organizations[0])
     resp = authenticated_client.get(reverse('platform-list', args=[organizations[0].pk]))
     assert resp.status_code == 200
     assert resp.json() == []
Ejemplo n.º 5
0
 def test_authorized_user_platforms_in_inaccessible_org(self, authenticated_client,
                                                        organizations, platforms):
     """
     There is an org and it has platforms, but the user cannot access the org
     """
     OrganizationPlatform.objects.create(organization=organizations[0], platform=platforms[0])
     resp = authenticated_client.get(reverse('platform-list', args=[organizations[0].pk]))
     assert resp.status_code == 404
Ejemplo n.º 6
0
 def test_authorized_user_accessible_platforms(self, authenticated_client, organizations,
                                               platforms, valid_identity, interest_rt):
     identity = Identity.objects.select_related('user').get(identity=valid_identity)
     UserOrganization.objects.create(user=identity.user, organization=organizations[0])
     OrganizationPlatform.objects.create(organization=organizations[0], platform=platforms[0])
     resp = authenticated_client.get(reverse('detailed-platform-list',
                                             args=[organizations[0].pk]))
     assert resp.status_code == 200
     assert len(resp.json()) == 0, 'no platforms without accesslogs'
Ejemplo n.º 7
0
 def test_authorized_user_no_authorization(self, authenticated_client,
                                           organizations):
     """
     User is authenticated but does not belong to any org - the list should be empty
     :param authenticated_client:
     :param organizations:
     :return:
     """
     resp = authenticated_client.get(reverse('organization-list'))
     assert resp.status_code == 200
     assert resp.json() == []
Ejemplo n.º 8
0
 def test_authorized_user_no_authorization_detail(self,
                                                  authenticated_client,
                                                  organizations):
     """
     User is authenticated but does not belong to any org - the list should be empty
     :param authenticated_client:
     :param organizations:
     :return:
     """
     resp = authenticated_client.get(
         reverse('organization-detail', args=[organizations[0].pk]))
     assert resp.status_code == 404
Ejemplo n.º 9
0
 def test_authorized_user_part_authorization(self, authenticated_client,
                                             organizations, valid_identity):
     """
     User is authenticated but does not belong to any org - the list should be empty
     """
     identity = Identity.objects.select_related('user').get(
         identity=valid_identity)
     UserOrganization.objects.create(user=identity.user,
                                     organization=organizations[1])
     resp = authenticated_client.get(reverse('organization-list'))
     assert resp.status_code == 200
     assert len(resp.json()) == 1
     assert resp.json()[0]['pk'] == organizations[1].pk
Ejemplo n.º 10
0
 def test_authorized_user_accessible_platforms_no_titles(
         self, authenticated_client, organizations, platforms, valid_identity, titles):
     """
     Titles are created by the 'titles' fixture, but should not appear in the result as they
     are not accessible for an associated platform
     """
     identity = Identity.objects.select_related('user').get(identity=valid_identity)
     UserOrganization.objects.create(user=identity.user, organization=organizations[0])
     OrganizationPlatform.objects.create(organization=organizations[0], platform=platforms[0])
     resp = authenticated_client.get(reverse('platform-title-list',
                                             args=[organizations[0].pk, platforms[0].pk]))
     assert resp.status_code == 200
     assert len(resp.json()) == 0
Ejemplo n.º 11
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.º 12
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.º 13
0
 def test_api_simple_data_0d(self, counter_records_0d, organizations, report_type_nd,
                             authenticated_client):
     platform = Platform.objects.create(ext_id=1234, short_name='Platform1', name='Platform 1',
                                        provider='Provider 1')
     organization = organizations[0]
     report_type = report_type_nd(0)  # type: ReportType
     import_batch = ImportBatch.objects.create(organization=organization, platform=platform,
                                               report_type=report_type)
     import_counter_records(report_type, organization, platform, counter_records_0d,
                            import_batch)
     assert AccessLog.objects.count() == 1
     metric = Metric.objects.get()
     resp = authenticated_client.get(
         reverse('chart_data_raw', args=(report_type.pk,)),
         {'organization': organization.pk,
          'metric': metric.pk,
          'platform': platform.pk,
          'prim_dim': 'date'})
     assert resp.status_code == 200
     data = json.loads(resp.content)
     assert 'data' in data
Ejemplo n.º 14
0
 def test_authorized_user_no_orgs(self, authenticated_client):
     resp = authenticated_client.get(reverse('organization-list'))
     assert resp.status_code == 200
     assert resp.json() == []
Ejemplo n.º 15
0
 def test_authorized_user_no_platforms_no_org(self, authenticated_client, organizations,
                                              interest_rt):
     resp = authenticated_client.get(reverse('detailed-platform-list',
                                             args=[organizations[0].pk]))
     assert resp.status_code == 404
Ejemplo n.º 16
0
 def test_authorized_user_no_org(self, authenticated_client, organizations,
                                 platforms):
     OrganizationPlatform.objects.create(organization=organizations[0], platform=platforms[0])
     resp = authenticated_client.get(reverse('platform-title-list',
                                             args=[organizations[0].pk, platforms[0].pk]))
     assert resp.status_code == 404