def test_filter_withdrawn_preprint(self, app, url, user, project_one, provider_one, provider_two): preprint_one = PreprintFactory(is_published=False, creator=user, project=project_one, provider=provider_one) preprint_one.date_withdrawn = timezone.now() preprint_one.is_public = True preprint_one.is_published = True preprint_one.date_published = timezone.now() preprint_one.machine_state = 'accepted' assert preprint_one.ever_public is False # Putting this preprint in a weird state, is verified_publishable, but has been # withdrawn and ever_public is False. This is to isolate withdrawal portion of query preprint_one.save() preprint_two = PreprintFactory(creator=user, project=project_one, provider=provider_two) preprint_two.date_withdrawn = timezone.now() preprint_two.ever_public = True preprint_two.save() # Unauthenticated can only see withdrawn preprints that have been public expected = [preprint_two._id] res = app.get(url) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # Noncontribs can only see withdrawn preprints that have been public user2 = AuthUserFactory() expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # contribs can only see withdrawn preprints that have been public user2 = AuthUserFactory() preprint_one.add_contributor(user2, 'read') preprint_two.add_contributor(user2, 'read') expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) expected = [preprint_two._id] # Admins can only see withdrawn preprints that have been public res = app.get(url, auth=user.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual)
def test_filter_withdrawn_preprint(self, app, url, user): preprint_one = PreprintFactory(is_published=False, creator=user) preprint_one.date_withdrawn = timezone.now() preprint_one.is_public = True preprint_one.is_published = True preprint_one.date_published = timezone.now() preprint_one.machine_state = 'accepted' assert preprint_one.ever_public is False # Putting this preprint in a weird state, is verified_publishable, but has been # withdrawn and ever_public is False. This is to isolate withdrawal portion of query preprint_one.save() preprint_two = PreprintFactory(creator=user) preprint_two.date_withdrawn = timezone.now() preprint_two.ever_public = True preprint_two.save() # Unauthenticated users cannot see users/me/preprints url = '/{}users/me/preprints/?version=2.2&'.format(API_BASE) expected = [preprint_two._id] res = app.get(url, expect_errors=True) assert res.status_code == 401 # Noncontribs cannot see withdrawn preprints user2 = AuthUserFactory() url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # Read contribs - contrib=False on UserPreprints filter so read contribs can only see # withdrawn preprints that were once public user2 = AuthUserFactory() preprint_one.add_contributor(user2, 'read', save=True) preprint_two.add_contributor(user2, 'read', save=True) url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) expected = [preprint_two._id] # Admin contribs can only see withdrawn preprints that were once public res = app.get(url, auth=user.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual)
def test_filter_withdrawn_preprint(self, app, url, user): preprint_one = PreprintFactory(is_published=False, creator=user) preprint_one.date_withdrawn = timezone.now() preprint_one.is_public = True preprint_one.is_published = True preprint_one.date_published = timezone.now() preprint_one.machine_state = 'accepted' assert preprint_one.ever_public is False # Putting this preprint in a weird state, is verified_publishable, but has been # withdrawn and ever_public is False. This is to isolate withdrawal portion of query preprint_one.save() preprint_two = PreprintFactory(creator=user) preprint_two.date_withdrawn = timezone.now() preprint_two.ever_public = True preprint_two.save() # Unauthenticated users cannot see users/me/preprints url = '/{}users/me/preprints/?version=2.2&'.format(API_BASE) expected = [preprint_two._id] res = app.get(url, expect_errors=True) assert res.status_code == 401 # Noncontribs cannot see withdrawn preprints user2 = AuthUserFactory() url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # Read contribs - contrib=False on UserPreprints filter so read contribs can only see # withdrawn preprints that were once public user2 = AuthUserFactory() preprint_one.add_contributor(user2, permissions.READ, save=True) preprint_two.add_contributor(user2, permissions.READ, save=True) url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) expected = [preprint_two._id] # Admin contribs can only see withdrawn preprints that were once public res = app.get(url, auth=user.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual)
def test_withdrawn_preprints_list(self): pp = PreprintFactory(provider__reviews_workflow='pre-moderation', is_published=False, creator=self.user) pp.machine_state = 'pending' mod = AuthUserFactory() pp.provider.get_group('moderator').user_set.add(mod) pp.date_withdrawn = timezone.now() pp.save() assert not pp.ever_public # Sanity check unauth_res = self.app.get(self.url) user_res = self.app.get(self.url, auth=self.user.auth) mod_res = self.app.get(self.url, auth=mod.auth) unauth_res_ids = [each['id'] for each in unauth_res.json['data']] user_res_ids = [each['id'] for each in user_res.json['data']] mod_res_ids = [each['id'] for each in mod_res.json['data']] assert pp._id not in unauth_res_ids assert pp._id not in user_res_ids assert pp._id in mod_res_ids
def test_delete_from_share(self, mock_share): preprint = PreprintFactory() update_share(preprint) data = json.loads(mock_share.calls[-1].request.body.decode()) graph = data['data']['attributes']['data']['@graph'] share_preprint = next(n for n in graph if n['@type'] == 'preprint') assert not share_preprint['is_deleted'] preprint.date_withdrawn = datetime.now() update_share(preprint) data = json.loads(mock_share.calls[-1].request.body.decode()) graph = data['data']['attributes']['data']['@graph'] share_preprint = next(n for n in graph if n['@type'] == 'preprint') assert not share_preprint['is_deleted'] preprint.spam_status = SpamStatus.SPAM update_share(preprint) data = json.loads(mock_share.calls[-1].request.body.decode()) graph = data['data']['attributes']['data']['@graph'] share_preprint = next(n for n in graph if n['@type'] == 'preprint') assert share_preprint['is_deleted']
def withdrawn_preprint(user): preprint = PreprintFactory(is_published=False, creator=user) preprint.date_withdrawn = timezone.now() preprint.save() return preprint
def test_data_storage_usage_command(self): import logging logger = logging.getLogger(__name__) expected_summary_data = OrderedDict([ ('date', None), ('total', 0), ('deleted', 0), ('registrations', 0), ('nd_quick_files', 0), ('nd_public_nodes', 0), ('nd_private_nodes', 0), ('nd_preprints', 0), ('nd_supp_nodes', 0), ('canada_montreal', 0), ('australia_sydney', 0), ('germany_frankfurt', 0), ('united_states', 0), ]) user = UserFactory() user_addon = user.get_addon('osfstorage') user_addon.default_region_id = self.region_us region_ca = RegionFactory(_id='CA-1', name=u'Canada - Montréal') region_de = RegionFactory(_id='DE-1', name='Germany - Frankfurt') region_au = RegionFactory(_id='AU-1', name='Australia - Sydney') project_public_us = self.project(creator=user, is_public=True) small_size = next_file_size() file_size = next(small_size) project_public_us_test_file = create_test_file( target=project_public_us, user=user, size=file_size ) logger.debug(u'Public project, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_public_nodes'] += file_size expected_summary_data['united_states'] += file_size file_size = next(small_size) self.add_file_version( project_public_us_test_file, user=user, size=file_size, ) logger.debug(u'Public project file version, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_public_nodes'] += file_size expected_summary_data['united_states'] += file_size project_private_au = self.project(creator=user, is_public=False, region=region_au) file_size = next(small_size) create_test_file( target=project_private_au, user=user, size=file_size ) logger.debug(u'Private project, AU: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_private_nodes'] += file_size expected_summary_data['australia_sydney'] += file_size component_private_small_deleted_de = self.project( creator=user, is_public=False, region=region_de, parent=project_public_us ) file_size = next(small_size) deleted_file = create_test_file( target=component_private_small_deleted_de, user=user, size=file_size, ) logger.debug('Before deletion: {}'.format(deleted_file.target.title)) deleted_file.delete(user=user, save=True) logger.debug(u'Deleted project, DE: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['deleted'] += file_size expected_summary_data['germany_frankfurt'] += file_size logger.debug('After deletion: {}'.format(deleted_file.target.title)) file_size = next(small_size) PreprintFactory(creator=user, file_size=file_size) # preprint_us logger.debug(u'Preprint, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_preprints'] += file_size expected_summary_data['united_states'] += file_size user_addon.default_region_id = region_ca user_addon.save() file_size = next(small_size) preprint_with_supplement_ca = PreprintFactory(creator=user, file_size=file_size) logger.debug(u'Preprint, CA: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_preprints'] += file_size expected_summary_data['canada_montreal'] += file_size user_addon.default_region_id = self.region_us user_addon.save() supplementary_node_public_au = self.project(creator=user, is_public=True, region=region_au) preprint_with_supplement_ca.node = supplementary_node_public_au preprint_with_supplement_ca.save() file_size = next(small_size) create_test_file( target=supplementary_node_public_au, user=user, size=file_size ) logger.debug(u'Public supplemental project of Canadian preprint, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_supp_nodes'] += file_size expected_summary_data['nd_public_nodes'] += file_size expected_summary_data['australia_sydney'] += file_size file_size = next(small_size) withdrawn_preprint_us = PreprintFactory(creator=user, file_size=file_size) withdrawn_preprint_us.date_withdrawn = timezone.now() withdrawn_preprint_us.save() logger.debug(u'Withdrawn preprint, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_preprints'] += file_size expected_summary_data['united_states'] += file_size quickfiles_node_us = QuickFilesNode.objects.get(creator=user) file_size = next(small_size) create_test_file(target=quickfiles_node_us, user=user, size=file_size) logger.debug(u'Quickfile, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['nd_quick_files'] += file_size expected_summary_data['united_states'] += file_size file_size = next(small_size) quickfile_deleted = create_test_file( filename='deleted_test_file', target=quickfiles_node_us, user=user, size=file_size ) quickfile_deleted.delete(user=user, save=True) logger.debug(u'Deleted quickfile, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['deleted'] += file_size expected_summary_data['united_states'] += file_size project_to_register_us = self.project(creator=user, is_public=True, region=self.region_us) registration = self.registration(project=project_to_register_us, creator=user) file_size = next(small_size) create_test_file( target=registration, user=user, size=file_size ) assert registration.get_addon('osfstorage').region == self.region_us logger.debug(u'Registration, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['united_states'] += file_size expected_summary_data['registrations'] += file_size withdrawal = self.registration(project=project_to_register_us, creator=user, withdrawn=True) file_size = next(small_size) create_test_file( target=withdrawal, user=user, size=file_size ) logger.debug(u'Withdrawn registration, US: {}'.format(file_size)) expected_summary_data['total'] += file_size expected_summary_data['united_states'] += file_size expected_summary_data['registrations'] += file_size actual_summary_data = process_usages(dry_run=True, page_size=2) actual_keys = actual_summary_data.keys() for key in actual_summary_data: logger.info('Actual field: {}'.format(key)) expected_keys = expected_summary_data.keys() for key in expected_summary_data: logger.info('Expected field: {}'.format(key)) assert actual_keys == expected_keys assert len(actual_keys) != 0 for key in actual_keys: if key != 'date': assert (key, expected_summary_data[key]) == (key, actual_summary_data[key])