def preprint_two(self, user, project_two, provider_two, subject_two): preprint_two = PreprintFactory(creator=user, project=project_two, filename='howto_reason.txt', provider=provider_two, subjects=[[subject_two._id]]) preprint_two.created = '2013-12-11 10:09:08.070605+00:00' preprint_two.date_published = '2013-12-11 10:09:08.070605+00:00' preprint_two.original_publication_date = '2013-12-11 10:09:08.070605+00:00' preprint_two.save() return preprint_two
def test_datacite_format_creators_for_preprint(self): preprint = PreprintFactory(project=self.node, is_published=True) verified_user = AuthUserFactory(external_identity={'ORCID': {'1234-1234-1234-1234': 'VERIFIED'}}) linked_user = AuthUserFactory(external_identity={'ORCID': {'1234-nope-1234-nope': 'LINK'}}) preprint.add_contributor(verified_user, visible=True) preprint.add_contributor(linked_user, visible=True) preprint.save() formatted_creators = metadata.format_creators(preprint) contributors_with_orcids = 0 guid_identifiers = [] for creator_xml in formatted_creators: assert creator_xml.find('creatorName').text != u'{}, {}'.format(self.invisible_contrib.family_name, self.invisible_contrib.given_name) name_identifiers = creator_xml.findall('nameIdentifier') for name_identifier in name_identifiers: if name_identifier.attrib['nameIdentifierScheme'] == 'ORCID': assert name_identifier.attrib['schemeURI'] == 'http://orcid.org/' contributors_with_orcids += 1 else: guid_identifiers.append(name_identifier.text) assert name_identifier.attrib['nameIdentifierScheme'] == 'OSF' assert name_identifier.attrib['schemeURI'] == settings.DOMAIN assert contributors_with_orcids >= 1 assert len(formatted_creators) == len(self.node.visible_contributors) assert sorted(guid_identifiers) == sorted([contrib.absolute_url for contrib in preprint.visible_contributors])
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags, presentation_name, is_registration, is_preprint, preprint_provider): auth = Auth(user=creator) project_title = name if name else fake.science_sentence() if is_preprint: provider = None if preprint_provider: try: provider = models.PreprintProvider.objects.get(_id=provider) except models.PreprintProvider.DoesNotExist: pass if not provider: provider = PreprintProviderFactory(name=fake.science_word()) privacy = 'public' mock_change_identifier = mock.patch( 'website.identifiers.client.EzidClient.update_identifier') mock_change_identifier.start() mock_change_identifier_preprints = mock.patch( 'website.identifiers.client.CrossRefClient.update_identifier') mock_change_identifier_preprints.start() project = PreprintFactory(title=project_title, description=fake.science_paragraph(), creator=creator, provider=provider) node = project.node elif is_registration: project = RegistrationFactory(title=project_title, description=fake.science_paragraph(), creator=creator) node = project else: project = ProjectFactory(title=project_title, description=fake.science_paragraph(), creator=creator) node = project node.set_privacy(privacy) for _ in range(n_users): contrib = create_fake_user() node.add_contributor(contrib, auth=auth) if isinstance(n_components, int): for _ in range(n_components): NodeFactory(parent=node, title=fake.science_sentence(), description=fake.science_paragraph(), creator=creator) elif isinstance(n_components, list): render_generations_from_node_structure_list(node, creator, n_components) for _ in range(n_tags): node.add_tag(fake.science_word(), auth=auth) if presentation_name is not None: node.add_tag(presentation_name, auth=auth) node.add_tag('poster', auth=auth) node.save() project.save() logger.info('Created project: {0}'.format(node.title)) return project
def preprint_three(self, user, project_three, provider_three, subject_one, subject_two): preprint_three = PreprintFactory(creator=user, project=project_three, filename='darn_reason.txt', provider=provider_three, subjects=[[subject_one._id], [subject_two._id]]) preprint_three.date_created = '2013-12-11 10:09:08.070605+00:00' preprint_three.date_published = '2013-12-11 10:09:08.070605+00:00' preprint_three.is_published = False preprint_three.save() return preprint_three
def test_resolve_guid_download_errors(self): testfile = TestFile.get_or_create(self.node, 'folder/path') testfile.name = 'asdf' testfile.materialized_path = '/folder/path' guid = testfile.get_guid(create=True) testfile.save() testfile.delete() res = self.app.get('/{}/download'.format(guid), expect_errors=True) assert res.status_code == 404 pp = PreprintFactory(is_published=False) res = self.app.get(pp.url + 'download', expect_errors=True) assert res.status_code == 404 pp.is_published = True pp.save() pp.node.is_public = False pp.node.save() non_contrib = AuthUserFactory() res = self.app.get(pp.url + 'download', auth=non_contrib.auth, expect_errors=True) assert res.status_code == 403 pp.node.is_deleted = True pp.node.save() res = self.app.get(pp.url + 'download', auth=non_contrib.auth, expect_errors=True) assert res.status_code == 410
def test_resolve_guid_download_errors(self): testfile = TestFile.get_or_create(self.node, 'folder/path') testfile.name = 'asdf' testfile.materialized_path = '/folder/path' guid = testfile.get_guid(create=True) testfile.save() testfile.delete() res = self.app.get('/{}/download'.format(guid), expect_errors=True) assert res.status_code == 404 pp = PreprintFactory(is_published=False) res = self.app.get(pp.url + 'download', expect_errors=True) assert res.status_code == 404 pp.is_published = True pp.save() pp.node.is_public = False pp.node.save() non_contrib = AuthUserFactory() res = self.app.get(pp.url + 'download', auth=non_contrib.auth, expect_errors=True) assert res.status_code == 403 pp.node.is_deleted = True pp.node.save() res = self.app.get(pp.url + 'download', auth=non_contrib.auth, expect_errors=True) assert res.status_code == 410
class TestPreprintSaveShareHook(OsfTestCase): def setUp(self): super(TestPreprintSaveShareHook, self).setUp() self.admin = AuthUserFactory() self.auth = Auth(user=self.admin) self.provider = PreprintProviderFactory( name='Lars Larson Snowmobiling Experience') self.project = ProjectFactory(creator=self.admin, is_public=True) self.subject = SubjectFactory() self.subject_two = SubjectFactory() self.file = api_test_utils.create_test_file(self.project, self.admin, 'second_place.pdf') self.preprint = PreprintFactory(creator=self.admin, filename='second_place.pdf', provider=self.provider, subjects=[[self.subject._id]], project=self.project, is_published=False) @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_unpublished_not_called(self, mock_on_preprint_updated): self.preprint.save() assert not mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_published_called(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) assert mock_on_preprint_updated.called # This covers an edge case where a preprint is forced back to unpublished # that it sends the information back to share @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_unpublished_called_forced(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) self.preprint.published = False self.preprint.save(**{'force_update': True}) assert_equal(mock_on_preprint_updated.call_count, 2) @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_published_called(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) assert mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_published_subject_change_called(self, mock_on_preprint_updated): self.preprint.is_published = True self.preprint.set_subjects([[self.subject_two._id]], auth=self.auth, save=True) assert mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_unpublished_subject_change_not_called( self, mock_on_preprint_updated): self.preprint.set_subjects([[self.subject_two._id]], auth=self.auth, save=True) assert not mock_on_preprint_updated.called
def preprint_one(self, user, project_one, provider_one, subject_one): preprint_one = PreprintFactory(creator=user, project=project_one, provider=provider_one, subjects=[[subject_one._id]]) preprint_one.original_publication_date = '2013-12-25 10:09:08.070605+00:00' preprint_one.save() return preprint_one
def test_delete_preprint_primary_file(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() file = preprint.files.all()[0] with assert_raises(FileNodeIsPrimaryFile): file.delete()
def test_delete_preprint_primary_file(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() file = preprint.files.all()[0] with assert_raises(FileNodeIsPrimaryFile): file.delete()
def preprint_with_contributor(self, mergee): preprint = PreprintFactory() preprint.add_contributor(mergee, permissions=WRITE, visible=False, save=True) preprint.save() return preprint
def preprint_one(self, user, project_one, provider_one, subject_one): preprint_one = PreprintFactory( creator=user, project=project_one, provider=provider_one, subjects=[[subject_one._id]]) preprint_one.original_publication_date = '2013-12-25 10:09:08.070605+00:00' preprint_one.save() return preprint_one
def post_mod_preprint(self, admin, write_contrib, post_mod_provider): post = PreprintFactory( creator=admin, provider=post_mod_provider, ) post.save() post.add_contributor(contributor=write_contrib, permissions='write', save=True) return post
def test_move_preprint_primary_file_to_node(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() to_move = preprint.files.all()[0] assert_true(to_move.is_preprint_primary) move_to = self.node_settings.get_root().append_folder('Cloud') with assert_raises(FileNodeIsPrimaryFile): moved = to_move.move_under(move_to, name='Tuna')
def test_move_preprint_primary_file_to_node(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() to_move = preprint.files.all()[0] assert_true(to_move.is_preprint_primary) move_to = self.node_settings.get_root().append_folder('Cloud') with assert_raises(FileNodeIsPrimaryFile): moved = to_move.move_under(move_to, name='Tuna')
def none_mod_preprint(self, admin, write_contrib, none_mod_provider): preprint = PreprintFactory( creator=admin, provider=none_mod_provider, ) preprint.save() preprint.add_contributor(contributor=write_contrib, permissions=permissions.WRITE, save=True) return preprint
class TestPreprintSaveShareHook(OsfTestCase): def setUp(self): super(TestPreprintSaveShareHook, self).setUp() self.admin = AuthUserFactory() self.auth = Auth(user=self.admin) self.provider = PreprintProviderFactory(name='Lars Larson Snowmobiling Experience') self.project = ProjectFactory(creator=self.admin, is_public=True) self.subject = SubjectFactory() self.subject_two = SubjectFactory() self.file = api_test_utils.create_test_file(self.project, self.admin, 'second_place.pdf') self.preprint = PreprintFactory(creator=self.admin, filename='second_place.pdf', provider=self.provider, subjects=[[self.subject._id]], project=self.project, is_published=False) @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_unpublished_not_called(self, mock_on_preprint_updated): self.preprint.save() assert not mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_published_called(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) assert mock_on_preprint_updated.called # This covers an edge case where a preprint is forced back to unpublished # that it sends the information back to share @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_unpublished_called_forced(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) self.preprint.published = False self.preprint.save(**{'force_update': True}) assert_equal(mock_on_preprint_updated.call_count, 2) @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_published_called(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) assert mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_published_subject_change_called(self, mock_on_preprint_updated): self.preprint.is_published = True self.preprint.set_subjects([[self.subject_two._id]], auth=self.auth, save=True) assert mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.s') def test_save_unpublished_subject_change_not_called(self, mock_on_preprint_updated): self.preprint.set_subjects([[self.subject_two._id]], auth=self.auth, save=True) assert not mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.requests') @mock.patch('website.project.tasks.settings.SHARE_URL', 'ima_real_website') def test_send_to_share_is_true(self, mock_requests): self.preprint.provider.access_token = 'Snowmobiling' self.preprint.provider.save() on_preprint_updated(self.preprint._id) assert mock_requests.post.called
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags, presentation_name, is_registration, is_preprint, preprint_provider): auth = Auth(user=creator) project_title = name if name else fake.science_sentence() if is_preprint: provider = None if preprint_provider: try: provider = models.PreprintProvider.find_one( Q('_id', 'eq', provider)) except NoResultsFound: pass if not provider: provider = PreprintProviderFactory(name=fake.science_word()) privacy = 'public' project = PreprintFactory(title=project_title, description=fake.science_paragraph(), creator=creator, provider=provider) node = project.node elif is_registration: project = RegistrationFactory(title=project_title, description=fake.science_paragraph(), creator=creator) node = project else: project = ProjectFactory(title=project_title, description=fake.science_paragraph(), creator=creator) node = project node.set_privacy(privacy) for _ in range(n_users): contrib = create_fake_user() node.add_contributor(contrib, auth=auth) if isinstance(n_components, int): for _ in range(n_components): NodeFactory(project=node, title=fake.science_sentence(), description=fake.science_paragraph(), creator=creator) elif isinstance(n_components, list): render_generations_from_node_structure_list(node, creator, n_components) for _ in range(n_tags): node.add_tag(fake.science_word(), auth=auth) if presentation_name is not None: node.add_tag(presentation_name, auth=auth) node.add_tag('poster', auth=auth) node.save() project.save() logger.info('Created project: {0}'.format(node.title)) return project
def none_mod_preprint(self, admin, write_contrib, none_mod_provider): preprint = PreprintFactory( creator=admin, provider=none_mod_provider, ) preprint.save() preprint.node.add_contributor( contributor=write_contrib, permissions=permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS, save=True) return preprint
def auto_withdrawable_pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory(creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending') pre.save() pre.add_contributor(contributor=write_contrib, permissions=permissions.WRITE, save=True) return pre
def test_copy_node_file_to_preprint(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() to_copy = self.node_settings.get_root().append_file('Carp') copy_to = preprint.root_folder copied = to_copy.copy_under(copy_to) assert_equal(copied.parent, copy_to) assert_equal(copied.target, preprint)
def test_copy_node_file_to_preprint(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() to_copy = self.node_settings.get_root().append_file('Carp') copy_to = preprint.root_folder copied = to_copy.copy_under(copy_to) assert_equal(copied.parent, copy_to) assert_equal(copied.target, preprint)
def preprint_two(self, user, project_two, provider_two, subject_two): preprint_two = PreprintFactory( creator=user, project=project_two, filename='howto_reason.txt', provider=provider_two, subjects=[[subject_two._id]]) preprint_two.created = '2013-12-11 10:09:08.070605+00:00' preprint_two.date_published = '2013-12-11 10:09:08.070605+00:00' preprint_two.original_publication_date = '2013-12-11 10:09:08.070605+00:00' preprint_two.save() return preprint_two
def test_gets( self, app, user_one, user_two, preprint, project_public, project_private): # test_authorized_in_gets_200 url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_one.auth) assert res.status_code == 200 assert res.content_type == 'application/vnd.api+json' # test_anonymous_gets_200 url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url) assert res.status_code == 200 assert res.content_type == 'application/vnd.api+json' # test_get_preprints_logged_in url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_one.auth) node_json = res.json['data'] ids = [each['id'] for each in node_json] assert preprint._id in ids assert project_public._id not in ids assert project_private._id not in ids # test_get_projects_not_logged_in url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url) node_json = res.json['data'] ids = [each['id'] for each in node_json] assert preprint._id in ids assert project_public._id not in ids assert project_private._id not in ids # test_get_projects_logged_in_as_different_user url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_two.auth) node_json = res.json['data'] ids = [each['id'] for each in node_json] assert preprint._id in ids assert project_public._id not in ids assert project_private._id not in ids abandoned_preprint = PreprintFactory(creator=user_one, finish=False) abandoned_preprint.machine_state = 'initial' abandoned_preprint.save() url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_one.auth) actual = [result['id'] for result in res.json['data']] assert abandoned_preprint._id not in actual
def none_mod_preprint(self, admin, write_contrib, none_mod_provider): preprint = PreprintFactory( creator=admin, provider=none_mod_provider, ) preprint.save() preprint.node.add_contributor( contributor=write_contrib, permissions=permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS, save=True ) return preprint
def none_mod_preprint(self, admin, write_contrib, none_mod_provider): preprint = PreprintFactory( creator=admin, provider=none_mod_provider, ) preprint.save() preprint.add_contributor( contributor=write_contrib, permissions='write', save=True ) return preprint
def test_gets(self, app, user_one, user_two, preprint, project_public, project_private): # test_authorized_in_gets_200 url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_one.auth) assert res.status_code == 200 assert res.content_type == 'application/vnd.api+json' # test_anonymous_gets_200 url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url) assert res.status_code == 200 assert res.content_type == 'application/vnd.api+json' # test_get_preprints_logged_in url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_one.auth) node_json = res.json['data'] ids = [each['id'] for each in node_json] assert preprint._id in ids assert project_public._id not in ids assert project_private._id not in ids # test_get_projects_not_logged_in url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url) node_json = res.json['data'] ids = [each['id'] for each in node_json] assert preprint._id in ids assert project_public._id not in ids assert project_private._id not in ids # test_get_projects_logged_in_as_different_user url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_two.auth) node_json = res.json['data'] ids = [each['id'] for each in node_json] assert preprint._id in ids assert project_public._id not in ids assert project_private._id not in ids abandoned_preprint = PreprintFactory(creator=user_one, finish=False) abandoned_preprint.machine_state = 'initial' abandoned_preprint.save() url = '/{}users/{}/preprints/'.format(API_BASE, user_one._id) res = app.get(url, auth=user_one.auth) actual = [result['id'] for result in res.json['data']] assert abandoned_preprint._id not in actual
def pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory(creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending') pre.ever_public = True pre.save() pre.add_contributor(contributor=write_contrib, permissions='write', save=True) pre.is_public = True pre.save() return pre
def pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory(creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending') pre.ever_public = True pre.save() pre.node.add_contributor( contributor=write_contrib, permissions=permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS, save=True) pre.node.is_public = True pre.node.save() return pre
def test_move_preprint_primary_file_within_preprint(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() folder = OsfStorageFolder(name='foofolder', target=preprint) folder.save() to_move = preprint.files.all()[0] assert_true(to_move.is_preprint_primary) moved = to_move.move_under(folder, name='Tuna') assert preprint.primary_file == to_move assert to_move.parent == folder assert folder.target == preprint
def test_move_preprint_primary_file_within_preprint(self): user = UserFactory() preprint = PreprintFactory(creator=user) preprint.save() folder = OsfStorageFolder(name='foofolder', target=preprint) folder.save() to_move = preprint.files.all()[0] assert_true(to_move.is_preprint_primary) moved = to_move.move_under(folder, name='Tuna') assert preprint.primary_file == to_move assert to_move.parent == folder assert folder.target == preprint
class TestPreprintFactory(OsfTestCase): def setUp(self): super(TestPreprintFactory, self).setUp() self.user = AuthUserFactory() self.auth = Auth(user=self.user) self.preprint = PreprintFactory(creator=self.user) self.preprint.save() def test_is_preprint(self): assert_true(self.preprint.node.is_preprint) def test_preprint_is_public(self): assert_true(self.preprint.node.is_public)
def auto_withdrawable_pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory( creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending' ) pre.save() pre.add_contributor( contributor=write_contrib, permissions='write', save=True ) return pre
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 stuck_preprint(self): preprint = PreprintFactory(set_doi=False) preprint.date_published = preprint.date_published - timedelta( days=settings.DAYS_CROSSREF_DOIS_MUST_BE_STUCK_BEFORE_EMAIL + 1) # match guid to the fixture crossref_works_response.json guid = preprint.guids.first() provider = preprint.provider provider.doi_prefix = '10.31236' provider.save() guid._id = 'guid0' guid.save() preprint.save() return preprint
class TestPreprintFactory(OsfTestCase): def setUp(self): super(TestPreprintFactory, self).setUp() self.user = AuthUserFactory() self.auth = Auth(user=self.user) self.preprint = PreprintFactory(creator=self.user) self.preprint.save() def test_is_preprint(self): assert_true(self.preprint.node.is_preprint) def test_preprint_is_public(self): assert_true(self.preprint.node.is_public)
def preprint_three( self, user, project_three, provider_three, subject_one, subject_two): preprint_three = PreprintFactory( creator=user, project=project_three, filename='darn_reason.txt', provider=provider_three, subjects=[[subject_one._id], [subject_two._id]]) preprint_three.created = '2013-12-11 10:09:08.070605+00:00' preprint_three.date_published = '2013-12-11 10:09:08.070605+00:00' preprint_three.original_publication_date = '2013-12-11 10:09:08.070605+00:00' preprint_three.is_published = False preprint_three.save() return preprint_three
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_implicit_admins_can_see_project_status(self): project = ProjectFactory(creator=self.admin) component = NodeFactory(creator=self.admin, parent=project) project.add_contributor(self.write_contrib, ['read', 'write', 'admin']) project.save() preprint = PreprintFactory(creator=self.admin, filename='mgla.pdf', provider=self.provider_one, subjects=[[self.subject_one._id]], project=component, is_published=True) preprint.machine_state = 'pending' provider = PreprintProviderFactory(reviews_workflow='post-moderation') preprint.provider = provider preprint.save() url = component.web_url_for('view_project') res = self.app.get(url, auth=self.write_contrib.auth) assert_in('{}'.format(preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is publicly available and searchable but is subject to removal by a moderator.', res.body)
def pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory( creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending' ) pre.ever_public = True pre.save() pre.node.add_contributor( contributor=write_contrib, permissions=permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS, save=True ) pre.node.is_public = True pre.node.save() return pre
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags, presentation_name, is_registration, is_preprint, preprint_provider): auth = Auth(user=creator) project_title = name if name else fake.science_sentence() if is_preprint: provider = None if preprint_provider: try: provider = models.PreprintProvider.objects.get(_id=provider) except models.PreprintProvider.DoesNotExist: pass if not provider: provider = PreprintProviderFactory(name=fake.science_word()) privacy = 'public' mock_change_identifier = mock.patch('website.identifiers.client.EzidClient.update_identifier') mock_change_identifier.start() mock_change_identifier_preprints = mock.patch('website.identifiers.client.CrossRefClient.update_identifier') mock_change_identifier_preprints.start() project = PreprintFactory(title=project_title, description=fake.science_paragraph(), creator=creator, provider=provider) node = project.node elif is_registration: project = RegistrationFactory(title=project_title, description=fake.science_paragraph(), creator=creator) node = project else: project = ProjectFactory(title=project_title, description=fake.science_paragraph(), creator=creator) node = project node.set_privacy(privacy) for _ in range(n_users): contrib = create_fake_user() node.add_contributor(contrib, auth=auth) if isinstance(n_components, int): for _ in range(n_components): NodeFactory(parent=node, title=fake.science_sentence(), description=fake.science_paragraph(), creator=creator) elif isinstance(n_components, list): render_generations_from_node_structure_list(node, creator, n_components) for _ in range(n_tags): node.add_tag(fake.science_word(), auth=auth) if presentation_name is not None: node.add_tag(presentation_name, auth=auth) node.add_tag('poster', auth=auth) node.save() project.save() logger.info('Created project: {0}'.format(node.title)) return project
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_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_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
class TestPreprintProviders(OsfTestCase): def setUp(self): super(TestPreprintProviders, self).setUp() self.preprint = PreprintFactory(provider=None, is_published=False) self.provider = PreprintProviderFactory(name='WWEArxiv') def test_add_provider(self): assert_not_equal(self.preprint.provider, self.provider) self.preprint.provider = self.provider self.preprint.save() self.preprint.reload() assert_equal(self.preprint.provider, self.provider) def test_remove_provider(self): self.preprint.provider = None self.preprint.save() self.preprint.reload() assert_equal(self.preprint.provider, None) def test_find_provider(self): self.preprint.provider = self.provider self.preprint.save() self.preprint.reload() assert ('branded', 'WWEArxiv') == find_preprint_provider(self.preprint.node)
class TestPreprintSaveShareHook(OsfTestCase): def setUp(self): super(TestPreprintSaveShareHook, self).setUp() self.admin = AuthUserFactory() self.auth = Auth(user=self.admin) self.provider = PreprintProviderFactory(name='Lars Larson Snowmobiling Experience') self.project = ProjectFactory(creator=self.admin, is_public=True) self.subject = SubjectFactory() self.subject_two = SubjectFactory() self.file = api_test_utils.create_test_file(self.project, self.admin, 'second_place.pdf') self.preprint = PreprintFactory(creator=self.admin, filename='second_place.pdf', provider=self.provider, subjects=[[self.subject._id]], project=self.project, is_published=False) @mock.patch('website.preprints.tasks.on_preprint_updated.si') def test_save_unpublished_not_called(self, mock_on_preprint_updated): self.preprint.save() assert not mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.si') def test_save_published_called(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) assert mock_on_preprint_updated.called # This covers an edge case where a preprint is forced back to unpublished # that it sends the information back to share @mock.patch('website.preprints.tasks.on_preprint_updated.si') def test_save_unpublished_called_forced(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) self.preprint.published = False self.preprint.save(**{'force_update': True}) assert_equal(mock_on_preprint_updated.call_count, 2) @mock.patch('website.preprints.tasks.on_preprint_updated.si') def test_save_published_called(self, mock_on_preprint_updated): self.preprint.set_published(True, auth=self.auth, save=True) assert mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.on_preprint_updated.si') def test_save_published_subject_change_called(self, mock_on_preprint_updated): self.preprint.is_published = True self.preprint.set_subjects([[self.subject_two._id]], auth=self.auth) assert mock_on_preprint_updated.called call_args, call_kwargs = mock_on_preprint_updated.call_args assert call_kwargs.get('old_subjects') == [self.subject.id] @mock.patch('website.preprints.tasks.on_preprint_updated.si') def test_save_unpublished_subject_change_not_called(self, mock_on_preprint_updated): self.preprint.set_subjects([[self.subject_two._id]], auth=self.auth) assert not mock_on_preprint_updated.called @mock.patch('website.preprints.tasks.requests') @mock.patch('website.preprints.tasks.settings.SHARE_URL', 'ima_real_website') def test_send_to_share_is_true(self, mock_requests): self.preprint.provider.access_token = 'Snowmobiling' self.preprint.provider.save() on_preprint_updated(self.preprint._id) assert mock_requests.post.called @mock.patch('osf.models.preprint_service.update_or_enqueue_on_preprint_updated') def test_node_contributor_changes_updates_preprints_share(self, mock_on_preprint_updated): # A user is added as a contributor self.preprint.is_published = True self.preprint.save() assert mock_on_preprint_updated.call_count == 1 user = AuthUserFactory() node = self.preprint.node node.preprint_file = self.file node.add_contributor(contributor=user, auth=self.auth) assert mock_on_preprint_updated.call_count == 2 node.move_contributor(contributor=user, index=0, auth=self.auth) assert mock_on_preprint_updated.call_count == 3 data = [{'id': self.admin._id, 'permission': 'admin', 'visible': True}, {'id': user._id, 'permission': 'write', 'visible': False}] node.manage_contributors(data, auth=self.auth, save=True) assert mock_on_preprint_updated.call_count == 4 node.update_contributor(user, 'read', True, auth=self.auth, save=True) assert mock_on_preprint_updated.call_count == 5 node.remove_contributor(contributor=user, auth=self.auth) assert mock_on_preprint_updated.call_count == 6 @mock.patch('website.preprints.tasks.settings.SHARE_URL', 'a_real_url') @mock.patch('website.preprints.tasks._async_update_preprint_share.delay') @mock.patch('website.preprints.tasks.requests') def test_call_async_update_on_500_failure(self, requests, mock_async): self.preprint.provider.access_token = 'Snowmobiling' requests.post.return_value = MockShareResponse(501) update_preprint_share(self.preprint) assert mock_async.called @mock.patch('website.preprints.tasks.settings.SHARE_URL', 'a_real_url') @mock.patch('website.preprints.tasks.send_desk_share_preprint_error') @mock.patch('website.preprints.tasks._async_update_preprint_share.delay') @mock.patch('website.preprints.tasks.requests') def test_no_call_async_update_on_400_failure(self, requests, mock_async, mock_mail): self.preprint.provider.access_token = 'Snowmobiling' requests.post.return_value = MockShareResponse(400) update_preprint_share(self.preprint) assert not mock_async.called assert mock_mail.called
def private_preprint(user): preprint = PreprintFactory(creator=user) preprint.is_public = False preprint.save() return preprint
def withdrawn_preprint(user): preprint = PreprintFactory(is_published=False, creator=user) preprint.date_withdrawn = timezone.now() preprint.save() return preprint
def deleted_preprint(user): preprint = PreprintFactory(creator=user) preprint.deleted = timezone.now() preprint.save() return preprint
def preprint_with_merger_and_mergee(self, mergee, merger): preprint = PreprintFactory() preprint.add_contributor(mergee) preprint.add_contributor(merger) preprint.save() return preprint
def ham_preprint(self): ham_preprint = PreprintFactory() ham_preprint.spam_status = SpamStatus.HAM ham_preprint.save() return ham_preprint
def orphaned_preprint(self, orphaned_preprint_node): orphaned_preprint = PreprintFactory(project=orphaned_preprint_node) orphaned_preprint.primary_file = None orphaned_preprint.save() return orphaned_preprint
def spam_preprint(self): spam_preprint = PreprintFactory() spam_preprint.spam_status = SpamStatus.SPAM spam_preprint.save() return spam_preprint
def flagged_preprint(self): flagged_preprint = PreprintFactory() flagged_preprint.spam_status = SpamStatus.FLAGGED flagged_preprint.save() return flagged_preprint
class TestPreprintFilesList(ApiTestCase): def setUp(self): super(TestPreprintFilesList, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/files/osfstorage/'.format(API_BASE, self.preprint._id) self.user_two = AuthUserFactory() def test_published_preprint_files(self): # Unauthenticated res = self.app.get(self.url) assert res.status_code == 200 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_unpublished_preprint_files(self): self.preprint.is_published = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_private_preprint_files(self): self.preprint.is_public = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_abandoned_preprint_files(self): self.preprint.machine_state = DefaultStates.INITIAL.value self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_orphaned_preprint_files(self): self.preprint.primary_file = None self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_deleted_preprint_files(self): self.preprint.deleted = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 404 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404 def test_withdrawn_preprint_files(self): self.preprint.date_withdrawn = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 403 def test_not_just_primary_file_returned(self): filename = 'my second file' second_file = OsfStorageFile.create( target_object_id=self.preprint.id, target_content_type=ContentType.objects.get_for_model(self.preprint), path='/{}'.format(filename), name=filename, materialized_path='/{}'.format(filename)) second_file.save() from addons.osfstorage import settings as osfstorage_settings second_file.create_version(self.user, { 'object': '06d80e', 'service': 'cloud', osfstorage_settings.WATERBUTLER_RESOURCE: 'osf', }, { 'size': 1337, 'contentType': 'img/png' }).save() second_file.parent = self.preprint.root_folder second_file.save() assert len(self.preprint.files.all()) == 2 res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 data = res.json['data'] assert len(data) == 2 assert data[0]['id'] == self.preprint.primary_file._id def test_nested_file_as_primary_file_is_returned(self): # Primary file can be any file nested somewhere under the preprint's root folder. subfolder = self.preprint.root_folder.append_folder('subfolder') subfolder.save() primary_file = self.preprint.primary_file primary_file.move_under(subfolder) primary_file.save() assert_equal(subfolder.children[0], primary_file) assert_equal(primary_file.parent, subfolder) res = self.app.get(self.url, auth=self.user.auth) assert len(res.json['data']) == 1 data = res.json['data'][0] assert data['id'] == subfolder._id assert data['attributes']['kind'] == 'folder' assert data['attributes']['path'] == '/{}/'.format(subfolder._id) assert data['attributes']['materialized_path'] == '/{}/'.format(subfolder.name) def test_cannot_access_other_addons(self): url = '/{}preprints/{}/files/github/'.format(API_BASE, self.preprint._id) res = self.app.get(url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404
class TestPreprintProvidersList(ApiTestCase): def setUp(self): super(TestPreprintProvidersList, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/files/'.format(API_BASE, self.preprint._id) self.user_two = AuthUserFactory() def test_published_preprint_files(self): # Unauthenticated res = self.app.get(self.url) assert res.status_code == 200 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_unpublished_preprint_files(self): self.preprint.is_published = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_private_preprint_files(self): self.preprint.is_public = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_abandoned_preprint_files(self): self.preprint.machine_state = DefaultStates.INITIAL.value self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_orphaned_preprint_files(self): self.preprint.primary_file = None self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_deleted_preprint_files(self): self.preprint.deleted = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 404 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404 def test_withdrawn_preprint_files(self): self.preprint.date_withdrawn = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 403 def test_return_published_files_logged_out(self): res = self.app.get(self.url) assert_equal(res.status_code, 200) assert_equal(len(res.json['data']), 1) assert_equal( res.json['data'][0]['attributes']['provider'], 'osfstorage' ) def test_does_not_return_storage_addons_link(self): res = self.app.get(self.url, auth=self.user.auth) assert_not_in('storage_addons', res.json['data'][0]['links']) def test_does_not_return_new_folder_link(self): res = self.app.get(self.url, auth=self.user.auth) assert_not_in('new_folder', res.json['data'][0]['links']) def test_returns_provider_data(self): res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) assert_true(isinstance(res.json['data'], list)) assert_equal(res.content_type, 'application/vnd.api+json') data = res.json['data'][0] assert_equal(data['attributes']['kind'], 'folder') assert_equal(data['attributes']['name'], 'osfstorage') assert_equal(data['attributes']['provider'], 'osfstorage') assert_equal(data['attributes']['preprint'], self.preprint._id) assert_equal(data['attributes']['path'], '/') assert_equal(data['attributes']['node'], None) def test_osfstorage_file_data_not_found(self): res = self.app.get( '{}osfstorage/{}'.format(self.url, self.preprint.primary_file._id), auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404) def test_returns_osfstorage_folder_version_two(self): res = self.app.get( '{}osfstorage/'.format(self.url), auth=self.user.auth) assert_equal(res.status_code, 200) def test_returns_osf_storage_folder_version_two_point_two(self): res = self.app.get( '{}osfstorage/?version=2.2'.format(self.url), auth=self.user.auth) assert_equal(res.status_code, 200) def test_osfstorage_folder_data_not_found(self): fobj = self.preprint.root_folder.append_folder('NewFolder') fobj.save() res = self.app.get( '{}osfstorage/{}'.format(self.url, fobj._id), auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404)
class TestPreprintProvider(OsfTestCase): def setUp(self): super(TestPreprintProvider, self).setUp() self.preprint = PreprintFactory(provider=None, is_published=False) self.provider = PreprintProviderFactory(name='WWEArxiv') def test_add_provider(self): assert_not_equal(self.preprint.provider, self.provider) self.preprint.provider = self.provider self.preprint.save() self.preprint.reload() assert_equal(self.preprint.provider, self.provider) def test_remove_provider(self): self.preprint.provider = None self.preprint.save() self.preprint.reload() assert_equal(self.preprint.provider, None) def test_find_provider(self): self.preprint.provider = self.provider self.preprint.save() self.preprint.reload() assert ('branded', self.provider) == find_preprint_provider(self.preprint.node) def test_top_level_subjects(self): subj_a = SubjectFactory(provider=self.provider, text='A') subj_b = SubjectFactory(provider=self.provider, text='B') subj_aa = SubjectFactory(provider=self.provider, text='AA', parent=subj_a) subj_ab = SubjectFactory(provider=self.provider, text='AB', parent=subj_a) subj_ba = SubjectFactory(provider=self.provider, text='BA', parent=subj_b) subj_bb = SubjectFactory(provider=self.provider, text='BB', parent=subj_b) subj_aaa = SubjectFactory(provider=self.provider, text='AAA', parent=subj_aa) some_other_provider = PreprintProviderFactory(name='asdfArxiv') subj_asdf = SubjectFactory(provider=some_other_provider) assert set(self.provider.top_level_subjects) == set([subj_a, subj_b]) def test_all_subjects(self): subj_a = SubjectFactory(provider=self.provider, text='A') subj_b = SubjectFactory(provider=self.provider, text='B') subj_aa = SubjectFactory(provider=self.provider, text='AA', parent=subj_a) subj_ab = SubjectFactory(provider=self.provider, text='AB', parent=subj_a) subj_ba = SubjectFactory(provider=self.provider, text='BA', parent=subj_b) subj_bb = SubjectFactory(provider=self.provider, text='BB', parent=subj_b) subj_aaa = SubjectFactory(provider=self.provider, text='AAA', parent=subj_aa) some_other_provider = PreprintProviderFactory(name='asdfArxiv') subj_asdf = SubjectFactory(provider=some_other_provider) assert set(self.provider.all_subjects) == set([subj_a, subj_b, subj_aa, subj_ab, subj_ba, subj_bb, subj_aaa]) def test_highlighted_subjects(self): subj_a = SubjectFactory(provider=self.provider, text='A') subj_b = SubjectFactory(provider=self.provider, text='B') subj_aa = SubjectFactory(provider=self.provider, text='AA', parent=subj_a) subj_ab = SubjectFactory(provider=self.provider, text='AB', parent=subj_a) subj_ba = SubjectFactory(provider=self.provider, text='BA', parent=subj_b) subj_bb = SubjectFactory(provider=self.provider, text='BB', parent=subj_b) subj_aaa = SubjectFactory(provider=self.provider, text='AAA', parent=subj_aa) assert set(self.provider.highlighted_subjects) == set([subj_a, subj_b]) subj_aaa.highlighted = True subj_aaa.save() assert set(self.provider.highlighted_subjects) == set([subj_aaa])