def setUp(self): super(TestPreprintDelete, self).setUp() self.user = AuthUserFactory() self.unpublished_preprint = PreprintFactory(creator=self.user, is_published=False) self.published_preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{{}}/'.format(API_BASE)
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 setUp(self): super(PreprintCitationsMixin, self).setUp() self.admin_contributor = AuthUserFactory() self.published_preprint = PreprintFactory( creator=self.admin_contributor) self.unpublished_preprint = PreprintFactory( creator=self.admin_contributor, is_published=False)
def setUp(self): super(TestSetPreprintFile, self).setUp() self.user = AuthUserFactory() self.auth = Auth(user=self.user) self.read_write_user = AuthUserFactory() self.read_write_user_auth = Auth(user=self.read_write_user) self.project = ProjectFactory(creator=self.user) self.file = OsfStorageFile.create( is_file=True, node=self.project, path='/panda.txt', name='panda.txt', materialized_path='/panda.txt') self.file.save() self.file_two = OsfStorageFile.create( is_file=True, node=self.project, path='/pandapanda.txt', name='pandapanda.txt', materialized_path='/pandapanda.txt') self.file_two.save() self.project.add_contributor(self.read_write_user, permissions=[permissions.WRITE]) self.project.save() self.preprint = PreprintFactory(project=self.project, finish=False)
def setUp(self): super(TestPreprintUpdateLicense, self).setUp() ensure_licenses() self.admin_contributor = AuthUserFactory() self.rw_contributor = AuthUserFactory() self.read_contributor = AuthUserFactory() self.non_contributor = AuthUserFactory() self.preprint_provider = PreprintProviderFactory() self.preprint = PreprintFactory(creator=self.admin_contributor, provider=self.preprint_provider) self.preprint.node.add_contributor(self.rw_contributor, auth=Auth(self.admin_contributor)) self.preprint.node.add_contributor(self.read_contributor, auth=Auth(self.admin_contributor), permissions=['read']) self.preprint.node.save() self.cc0_license = NodeLicense.find_one(Q('name', 'eq', 'CC0 1.0 Universal')) self.mit_license = NodeLicense.find_one(Q('name', 'eq', 'MIT License')) self.no_license = NodeLicense.find_one(Q('name', 'eq', 'No license')) self.preprint_provider.licenses_acceptable = [self.cc0_license, self.no_license] self.preprint_provider.save() self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id)
def setUp(self): super(TestPreprintUpdate, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id) self.subject = SubjectFactory()
def setUp(self): super(TestPreprintServicePermissions, self).setUp() self.user = AuthUserFactory() self.write_contrib = AuthUserFactory() self.project = ProjectFactory(creator=self.user) self.project.add_contributor(self.write_contrib, permissions=[permissions.WRITE]) self.preprint = PreprintFactory(project=self.project, is_published=False)
def setUp(self): super(TestNodePreprintList, self).setUp() self.user = AuthUserFactory() self.auth = Auth(self.user) self.preprint = PreprintFactory(creator=self.user, is_published=False) self.url = '/{}nodes/{}/preprints/'.format(API_BASE, self.preprint.node._id)
def test_read_write_user_already_a_preprint(self): assert_in(self.other_user, self.public_project.contributors) preprint = PreprintFactory(creator=self.user) preprint.add_contributor(self.other_user, permissions=[permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS], save=True) file_one_preprint = test_utils.create_test_file(preprint, self.user, 'openupthatwindow.pdf') already_preprint_payload = build_preprint_create_payload(preprint._id, self.subject._id, file_one_preprint._id) res = self.app.post_json_api(self.url, already_preprint_payload, auth=self.other_user.auth, expect_errors=True) assert_equal(res.status_code, 403)
def setUp(self): super(TestPreprintRelationshipPreprintProvider, self).setUp() self.user = AuthUserFactory() self.read_write_user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user, providers=None) self.preprint.add_contributor(self.read_write_user) self.preprint.save() self.preprint_provider_one = PreprintProviderFactory() self.preprint_provider_two = PreprintProviderFactory() self.preprint_preprint_providers_url = self.create_url(self.preprint._id)
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 setUp(self): super(TestUserPreprints, self).setUp() self.user_one = AuthUserFactory() self.user_two = AuthUserFactory() self.preprint = PreprintFactory(title="Preprint User One", creator=self.user_one) self.public_project = ProjectFactory(title="Public Project User One", is_public=True, creator=self.user_one) self.private_project = ProjectFactory(title="Private Project User One", is_public=False, creator=self.user_one)
def setUp(self): super(TestUserNodesPreprintsFiltering, self).setUp() self.user = AuthUserFactory() self.no_preprints_node = ProjectFactory(creator=self.user) self.valid_preprint_node = ProjectFactory(creator=self.user) self.orphaned_preprint_node = ProjectFactory(creator=self.user) self.abandoned_preprint_node = ProjectFactory(creator=self.user) self.valid_preprint = PreprintFactory(project=self.valid_preprint_node) self.abandoned_preprint = PreprintFactory(project=self.abandoned_preprint_node, is_published=False) self.orphaned_preprint = PreprintFactory(project=self.orphaned_preprint_node) self.orphaned_preprint.node._is_preprint_orphan = True self.orphaned_preprint.node.save() self.url_base = '/{}users/me/nodes/?filter[preprint]='.format(API_BASE)
def setUp(self): super(TestPreprintList, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/'.format(API_BASE) self.project = ProjectFactory(creator=self.user)
def setUp(self): super(TestPreprintDetail, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id) self.res = self.app.get(self.url) self.data = self.res.json['data']
class TestNodePreprintList(ApiTestCase): def setUp(self): super(TestNodePreprintList, self).setUp() self.user = AuthUserFactory() self.auth = Auth(self.user) self.preprint = PreprintFactory(creator=self.user, is_published=False) self.url = '/{}nodes/{}/preprints/'.format(API_BASE, self.preprint.node._id) def test_user_can_see_own_unpublished_preprint(self): res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 200) assert_equal(res.json['data'][0]['id'], self.preprint._id) def test_other_user_can_see_unpublished_preprint_on_public_node(self): noncontrib = AuthUserFactory() self.preprint.node.set_privacy('public') res = self.app.get(self.url, auth=noncontrib.auth) assert_equal(res.status_code, 200) assert_equal(res.json['data'][0]['id'], self.preprint._id) def test_other_user_cannot_see_unpublished_preprint_on_private_node(self): noncontrib = AuthUserFactory() res = self.app.get(self.url, auth=noncontrib.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_user_can_see_own_published_preprint(self): self.preprint.set_published(True, auth=self.auth) res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) assert_equal(res.json['data'][0]['id'], self.preprint._id) def test_other_user_can_see_published_preprint_on_public_node(self): self.preprint.set_published(True, auth=self.auth) noncontrib = AuthUserFactory() res = self.app.get(self.url, auth=noncontrib.auth) assert_equal(res.status_code, 200) assert_equal(res.json['data'][0]['id'], self.preprint._id)
def test_already_a_preprint(self): preprint = PreprintFactory(creator=self.user) file_one_preprint = test_utils.create_test_file(preprint, self.user, 'openupthatwindow.pdf') already_preprint_payload = build_preprint_create_payload(preprint._id, self.subject._id, file_one_preprint._id) res = self.app.post_json_api(self.url, already_preprint_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 409) assert_equal(res.json['errors'][0]['detail'], 'This node already stored as a preprint, use the update method instead.')
def setUp(self): super(TestOnPreprintUpdatedTask, self).setUp() self.user = AuthUserFactory() self.auth = Auth(user=self.user) self.preprint = PreprintFactory() self.preprint.node.add_tag('preprint', self.auth, save=False) self.preprint.node.add_tag('spoderman', self.auth, save=False) self.preprint.node.add_unregistered_contributor( 'BoJack Horseman', '*****@*****.**', Auth(self.preprint.node.creator)) self.preprint.node.add_contributor(self.user, visible=False) self.preprint.node.save() self.preprint.node.creator.given_name = 'ZZYZ' self.preprint.node.creator.save() self.preprint.set_subjects([[SubjectFactory()._id]], auth=Auth(self.preprint.node.creator), save=False)
class TestOnPreprintUpdatedTask(OsfTestCase): def setUp(self): super(TestOnPreprintUpdatedTask, self).setUp() self.preprint = PreprintFactory() def test_format_preprint(self): res = format_preprint(self.preprint) types = [gn['@type'] for gn in res] subject_names = [x['text'] for hier in self.preprint.get_subjects() for x in hier] contribs = {} for i, user in enumerate(self.preprint.node.contributors): contribs.update({i: user}) for type_ in ['throughlinks', 'throughsubjects', 'throughidentifiers', 'preprint', 'subject', 'person', 'link', 'contributor', 'identifier', 'person']: assert type_ in types for graph_node in res: if graph_node['@type'] == 'throughlinks': assert_equal(set(graph_node.keys()), set(['@id', '@type', 'creative_work', 'link'])) if graph_node['@type'] == 'throughsubjects': assert_equal(set(graph_node.keys()), set(['@id', '@type', 'creative_work', 'subject'])) if graph_node['@type'] == 'throughidentifiers': assert_equal(set(graph_node.keys()), set(['@id', '@type', 'identifier', 'person'])) if graph_node['@type'] == 'preprint': assert_equal(set(graph_node.keys()), set([ '@id', '@type', 'contributors', 'title', 'tags', 'date_published', 'date_updated', 'description', 'institutions', 'is_deleted', 'links', 'subjects', ])) assert_equal(graph_node['date_published'], self.preprint.date_published.isoformat()) assert_equal(graph_node['date_updated'], self.preprint.date_modified.isoformat()) assert_equal(graph_node['description'], self.preprint.node.description) assert_equal(graph_node['is_deleted'], self.preprint.node.is_deleted) assert_equal(graph_node['title'], self.preprint.node.title) if graph_node['@type'] == 'subject': assert_equal(set(graph_node.keys()), set(['@id', '@type', 'name'])) assert_in(graph_node['name'], subject_names) if graph_node['@type'] == 'person': assert_equal(set(graph_node.keys()), set([ '@id', '@type', 'additional_name', 'affiliations', 'family_name', 'given_name', 'identifiers', 'suffix' ])) if graph_node['@type'] == 'link': assert_equal(set(graph_node.keys()), set(['@id', '@type', 'type', 'url'])) if graph_node['@type'] == 'contributor': assert_equal(set(graph_node.keys()), set([ '@id', '@type', 'bibliographic', 'cited_name', 'creative_work', 'order_cited', 'person' ])) user = contribs[graph_node['order_cited']] assert_equal(graph_node['cited_name'], user.fullname) assert_equal(graph_node['bibliographic'], bool(user._id in self.preprint.node.visible_contributor_ids)) if graph_node['@type'] == 'identifier': assert_equal(set(graph_node.keys()), set(['@id', '@type', 'base_url', 'url']))
class TestPreprintProviders(OsfTestCase): def setUp(self): super(TestPreprintProviders, self).setUp() self.preprint = PreprintFactory(providers=[]) self.provider = PreprintProviderFactory(name='WWEArxiv') def test_add_provider(self): assert_equal(self.preprint.preprint_providers, []) self.preprint.add_preprint_provider(self.provider, user=self.preprint.creator, save=True) assert_items_equal(self.preprint.preprint_providers, [self.provider]) def test_remove_provider(self): self.preprint.add_preprint_provider(self.provider, user=self.preprint.creator, save=True) assert_items_equal(self.preprint.preprint_providers, [self.provider]) self.preprint.remove_preprint_provider(self.provider, user=self.preprint.creator, save=True) assert_equal(self.preprint.preprint_providers, [])
def setUp(self): super(TestPreprintFiltering, self).setUp() self.user = AuthUserFactory() self.provider = PreprintProviderFactory(name='wwe') self.provider_two = PreprintProviderFactory(name='wcw') self.subject = SubjectFactory() self.subject_two = SubjectFactory() self.preprint = PreprintFactory(creator=self.user, provider=self.provider, subjects=[[self.subject._id]]) self.preprint_two = PreprintFactory(creator=self.user, filename='woo.txt', provider=self.provider_two, subjects=[[self.subject_two._id]]) self.preprint_three = PreprintFactory(creator=self.user, filename='stonecold.txt', provider=self.provider, subjects=[[self.subject._id], [self.subject_two._id] ])
def test_already_a_preprint_with_conflicting_provider(self): preprint = PreprintFactory(creator=self.user) file_one_preprint = test_utils.create_test_file( preprint.node, self.user, 'openupthatwindow.pdf') already_preprint_payload = build_preprint_create_payload( preprint.node._id, preprint.provider._id, file_one_preprint._id) res = self.app.post_json_api(self.url, already_preprint_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 409) assert_in( 'Only one preprint per provider can be submitted for a node.', res.json['errors'][0]['detail'])
def setUp(self): super(TestOnPreprintUpdatedTask, self).setUp() self.user = AuthUserFactory() self.auth = Auth(user=self.user) self.preprint = PreprintFactory() self.preprint.node.add_tag('preprint', self.auth, save=False) self.preprint.node.add_tag('spoderman', self.auth, save=False) self.preprint.node.add_unregistered_contributor('BoJack Horseman', '*****@*****.**', Auth(self.preprint.node.creator)) self.preprint.node.add_contributor(self.user, visible=False) self.preprint.node.save() self.preprint.node.creator.given_name = 'ZZYZ' self.preprint.node.creator.save() self.preprint.set_subjects([[SubjectFactory()._id]], auth=Auth(self.preprint.node.creator), save=False)
class TestPreprintProviders(OsfTestCase): def setUp(self): super(TestPreprintProviders, self).setUp() self.preprint = PreprintFactory(providers=[]) 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 setUp(self): super(TestPreprintFiltering, self).setUp() self.user = AuthUserFactory() self.provider = PreprintProviderFactory(name='wwe') self.preprint = PreprintFactory(creator=self.user, providers=[self.provider]) self.preprint.add_tag('nature boy', Auth(self.user), save=False) self.preprint.add_tag('ric flair', Auth(self.user), save=False) self.preprint.save() self.provider_two = PreprintProviderFactory(name='wcw') self.preprint_two = PreprintFactory(creator=self.user, filename='woo.txt', providers=[self.provider_two]) self.preprint_two.add_tag('nature boy', Auth(self.user), save=False) self.preprint_two.add_tag('woo', Auth(self.user), save=False) self.preprint_two.save() self.preprint_three = PreprintFactory(creator=self.user, filename='stonecold.txt', providers=[self.provider]) self.preprint_three.add_tag('stone', Auth(self.user), save=False) self.preprint_two.add_tag('cold', Auth(self.user), save=False) self.preprint_three.save()
class TestPreprintUpdate(ApiTestCase): def setUp(self): super(TestPreprintUpdate, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id) self.subject = SubjectFactory() def test_update_preprint_title(self): update_title_payload = build_preprint_update_payload(self.preprint._id, attributes={'title': 'A new title'}) res = self.app.patch_json_api(self.url, update_title_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.title, 'A new title') def test_update_preprint_tags(self): update_tags_payload = build_preprint_update_payload(self.preprint._id, attributes={'tags': ['newtag', 'bluetag']}) res = self.app.patch_json_api(self.url, update_tags_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_in('newtag', self.preprint.tags) assert_in('bluetag', self.preprint.tags) assert_in('tag_added', [l.action for l in self.preprint.logs]) def test_update_preprint_tags_not_list(self): update_tags_payload = build_preprint_update_payload(self.preprint._id, attributes={'tags': 'newtag'}) res = self.app.patch_json_api(self.url, update_tags_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) def test_update_preprint_none_tags(self): update_tags_payload = build_preprint_update_payload(self.preprint._id, attributes={'tags': [None]}) res = self.app.patch_json_api(self.url, update_tags_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) def test_update_preprint_subjects(self): assert_not_equal(self.preprint.preprint_subjects[0], self.subject._id) update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"subjects": [self.subject._id]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.preprint_subjects[0], self.subject) def test_update_invalid_subjects(self): preprint_subjects = self.preprint.preprint_subjects update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"subjects": ['wwe']}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) self.preprint.reload() assert_equal(self.preprint.preprint_subjects, preprint_subjects) def test_update_primary_file(self): new_file = test_utils.create_test_file(self.preprint, 'openupthatwindow.pdf') relationships = { "primary_file": { "data": { "type": "file", "id": new_file._id } } } assert_not_equal(self.preprint.preprint_file, new_file) update_file_payload = build_preprint_update_payload(self.preprint._id, relationships=relationships) res = self.app.patch_json_api(self.url, update_file_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.preprint_file, new_file) def test_new_primary_not_in_node(self): project = ProjectFactory() file_for_project = test_utils.create_test_file(project, 'letoutthatantidote.pdf') relationships = { "primary_file": { "data": { "type": "file", "id": file_for_project._id } } } update_file_payload = build_preprint_update_payload(self.preprint._id, relationships=relationships) res = self.app.patch_json_api(self.url, update_file_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) self.preprint.reload() assert_not_equal(self.preprint.preprint_file, file_for_project) def test_update_doi(self): new_doi = '10.123/456/789' assert_not_equal(self.preprint.preprint_doi, new_doi) update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"doi": new_doi}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.preprint_doi, new_doi) preprint_detail = self.app.get(self.url, auth=self.user.auth).json['data'] assert_equal(preprint_detail['links']['doi'], 'https://dx.doi.org/{}'.format(new_doi))
class TestOnPreprintUpdatedTask(OsfTestCase): def setUp(self): super(TestOnPreprintUpdatedTask, self).setUp() self.user = AuthUserFactory() self.auth = Auth(user=self.user) self.preprint = PreprintFactory() self.preprint.node.add_tag('preprint', self.auth, save=False) self.preprint.node.add_tag('spoderman', self.auth, save=False) self.preprint.node.add_unregistered_contributor('BoJack Horseman', '*****@*****.**', Auth(self.preprint.node.creator)) self.preprint.node.add_contributor(self.user, visible=False) self.preprint.node.save() self.preprint.node.creator.given_name = 'ZZYZ' self.preprint.node.creator.save() self.preprint.set_subjects([[SubjectFactory()._id]], auth=Auth(self.preprint.node.creator), save=False) def tearDown(self): handlers.celery_before_request() super(TestOnPreprintUpdatedTask, self).tearDown() def test_format_preprint(self): res = format_preprint(self.preprint) assert set(gn['@type'] for gn in res) == {'creator', 'contributor', 'throughsubjects', 'subject', 'throughtags', 'tag', 'workidentifier', 'agentidentifier', 'person', 'preprint'} nodes = dict(enumerate(res)) preprint = nodes.pop(next(k for k, v in nodes.items() if v['@type'] == 'preprint')) assert preprint['title'] == self.preprint.node.title assert preprint['description'] == self.preprint.node.description assert preprint['is_deleted'] == (not self.preprint.is_published or not self.preprint.node.is_public or self.preprint.node.is_preprint_orphan) assert preprint['date_updated'] == self.preprint.date_modified.isoformat() assert preprint['date_published'] == self.preprint.date_published.isoformat() tags = [nodes.pop(k) for k, v in nodes.items() if v['@type'] == 'tag'] through_tags = [nodes.pop(k) for k, v in nodes.items() if v['@type'] == 'throughtags'] assert sorted(tag['@id'] for tag in tags) == sorted(tt['tag']['@id'] for tt in through_tags) assert sorted(tag['name'] for tag in tags) == ['preprint', 'spoderman'] subjects = [nodes.pop(k) for k, v in nodes.items() if v['@type'] == 'subject'] through_subjects = [nodes.pop(k) for k, v in nodes.items() if v['@type'] == 'throughsubjects'] assert sorted(subject['@id'] for subject in subjects) == sorted(tt['subject']['@id'] for tt in through_subjects) assert sorted(subject['name'] for subject in subjects) == ['Example Subject #1'] people = sorted([nodes.pop(k) for k, v in nodes.items() if v['@type'] == 'person'], key=lambda x: x['given_name']) assert people == [{ '@id': people[0]['@id'], '@type': 'person', 'given_name': u'BoJack', 'family_name': u'Horseman', }, { '@id': people[1]['@id'], '@type': 'person', 'given_name': self.user.given_name, 'family_name': self.user.family_name, }, { '@id': people[2]['@id'], '@type': 'person', 'given_name': self.preprint.node.creator.given_name, 'family_name': self.preprint.node.creator.family_name, }] creators = sorted([nodes.pop(k) for k, v in nodes.items() if v['@type'] == 'creator'], key=lambda x: x['order_cited']) assert creators == [{ '@id': creators[0]['@id'], '@type': 'creator', 'order_cited': 0, 'cited_as': self.preprint.node.creator.fullname, 'agent': {'@id': people[2]['@id'], '@type': 'person'}, 'creative_work': {'@id': preprint['@id'], '@type': preprint['@type']}, }, { '@id': creators[1]['@id'], '@type': 'creator', 'order_cited': 1, 'cited_as': 'BoJack Horseman', 'agent': {'@id': people[0]['@id'], '@type': 'person'}, 'creative_work': {'@id': preprint['@id'], '@type': preprint['@type']}, }] contributors = [nodes.pop(k) for k, v in nodes.items() if v['@type'] == 'contributor'] assert contributors == [{ '@id': contributors[0]['@id'], '@type': 'contributor', 'cited_as': self.user.fullname, 'agent': {'@id': people[1]['@id'], '@type': 'person'}, 'creative_work': {'@id': preprint['@id'], '@type': preprint['@type']}, }] agentidentifiers = {nodes.pop(k)['uri'] for k, v in nodes.items() if v['@type'] == 'agentidentifier'} assert agentidentifiers == set([ 'mailto:' + self.user.username, 'mailto:' + self.preprint.node.creator.username, self.user.profile_image_url(), self.preprint.node.creator.profile_image_url(), ]) | set(urlparse.urljoin(settings.DOMAIN, user.profile_url) for user in self.preprint.node.contributors if user.is_registered) workidentifiers = {nodes.pop(k)['uri'] for k, v in nodes.items() if v['@type'] == 'workidentifier'} assert workidentifiers == set([ 'http://dx.doi.org/{}'.format(self.preprint.article_doi), urlparse.urljoin(settings.DOMAIN, self.preprint.url) ]) assert nodes == {}
class TestPreprintUpdate(ApiTestCase): def setUp(self): super(TestPreprintUpdate, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id) self.subject = SubjectFactory() def test_update_preprint_title(self): update_title_payload = build_preprint_update_payload( self.preprint._id, attributes={'title': 'A new title'}) res = self.app.patch_json_api(self.url, update_title_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.title, 'A new title') def test_update_preprint_permission_denied(self): update_title_payload = build_preprint_update_payload( self.preprint._id, attributes={'title': 'A new title'}) noncontrib = AuthUserFactory() res = self.app.patch_json_api(self.url, update_title_payload, auth=noncontrib.auth, expect_errors=True) assert_equal(res.status_code, 403) res = self.app.patch_json_api(self.url, update_title_payload, expect_errors=True) assert_equal(res.status_code, 401) def test_update_preprint_tags(self): update_tags_payload = build_preprint_update_payload( self.preprint._id, attributes={'tags': ['newtag', 'bluetag']}) res = self.app.patch_json_api(self.url, update_tags_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_in('newtag', self.preprint.tags) assert_in('bluetag', self.preprint.tags) assert_in('tag_added', [l.action for l in self.preprint.logs]) def test_update_preprint_tags_not_list(self): update_tags_payload = build_preprint_update_payload( self.preprint._id, attributes={'tags': 'newtag'}) res = self.app.patch_json_api(self.url, update_tags_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) def test_update_preprint_none_tags(self): update_tags_payload = build_preprint_update_payload( self.preprint._id, attributes={'tags': [None]}) res = self.app.patch_json_api(self.url, update_tags_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) def test_update_preprint_subjects(self): assert_not_equal(self.preprint.preprint_subjects[0], self.subject._id) update_subjects_payload = build_preprint_update_payload( self.preprint._id, attributes={"subjects": [self.subject._id]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.preprint_subjects[0], self.subject) def test_update_invalid_subjects(self): preprint_subjects = self.preprint.preprint_subjects update_subjects_payload = build_preprint_update_payload( self.preprint._id, attributes={"subjects": ['wwe']}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) self.preprint.reload() assert_equal(self.preprint.preprint_subjects, preprint_subjects) def test_update_primary_file(self): new_file = test_utils.create_test_file(self.preprint, 'openupthatwindow.pdf') relationships = { "primary_file": { "data": { "type": "file", "id": new_file._id } } } assert_not_equal(self.preprint.preprint_file, new_file) update_file_payload = build_preprint_update_payload( self.preprint._id, relationships=relationships) res = self.app.patch_json_api(self.url, update_file_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.preprint_file, new_file) def test_new_primary_not_in_node(self): project = ProjectFactory() file_for_project = test_utils.create_test_file( project, 'letoutthatantidote.pdf') relationships = { "primary_file": { "data": { "type": "file", "id": file_for_project._id } } } update_file_payload = build_preprint_update_payload( self.preprint._id, relationships=relationships) res = self.app.patch_json_api(self.url, update_file_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) self.preprint.reload() assert_not_equal(self.preprint.preprint_file, file_for_project) def test_update_doi(self): new_doi = '10.123/456/789' assert_not_equal(self.preprint.preprint_doi, new_doi) update_subjects_payload = build_preprint_update_payload( self.preprint._id, attributes={"doi": new_doi}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.preprint_doi, new_doi) preprint_detail = self.app.get(self.url, auth=self.user.auth).json['data'] assert_equal(preprint_detail['links']['doi'], 'https://dx.doi.org/{}'.format(new_doi)) def test_write_contrib_cannot_set_preprint_file(self): user_two = AuthUserFactory() self.preprint.add_contributor(user_two, permissions=['read', 'write'], auth=Auth(self.user), save=True) new_file = test_utils.create_test_file(self.preprint, 'openupthatwindow.pdf') data = { 'data': { 'type': 'primary_file', 'id': self.preprint._id, 'attributes': {}, 'relationships': { 'primary_file': { 'data': { 'type': 'file', 'id': new_file._id } } } } } res = self.app.patch_json_api(self.url, data, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_noncontrib_cannot_set_preprint_file(self): user_two = AuthUserFactory() new_file = test_utils.create_test_file(self.preprint, 'openupthatwindow.pdf') data = { 'data': { 'type': 'primary_file', 'id': self.preprint._id, 'attributes': {}, 'relationships': { 'primary_file': { 'data': { 'type': 'file', 'id': new_file._id } } } } } res = self.app.patch_json_api(self.url, data, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_write_contrib_cannot_set_subjects(self): user_two = AuthUserFactory() self.preprint.add_contributor(user_two, permissions=['read', 'write'], auth=Auth(self.user), save=True) assert_not_equal(self.preprint.preprint_subjects[0], self.subject._id) update_subjects_payload = build_preprint_update_payload( self.preprint._id, attributes={"subjects": [self.subject._id]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_not_equal(self.preprint.preprint_subjects[0], self.subject._id) def test_noncontrib_cannot_set_subjects(self): user_two = AuthUserFactory() self.preprint.add_contributor(user_two, permissions=['read', 'write'], auth=Auth(self.user), save=True) assert_not_equal(self.preprint.preprint_subjects[0], self.subject._id) update_subjects_payload = build_preprint_update_payload( self.preprint._id, attributes={"subjects": [self.subject._id]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_not_equal(self.preprint.preprint_subjects[0], self.subject._id)
class TestSetPreprintFile(OsfTestCase): def setUp(self): super(TestSetPreprintFile, self).setUp() self.user = AuthUserFactory() self.auth = Auth(user=self.user) self.read_write_user = AuthUserFactory() self.read_write_user_auth = Auth(user=self.read_write_user) self.project = ProjectFactory(creator=self.user) self.file = OsfStorageFile.create( is_file=True, node=self.project, path='/panda.txt', name='panda.txt', materialized_path='/panda.txt') self.file.save() self.file_two = OsfStorageFile.create( is_file=True, node=self.project, path='/pandapanda.txt', name='pandapanda.txt', materialized_path='/pandapanda.txt') self.file_two.save() self.project.add_contributor(self.read_write_user, permissions=[permissions.WRITE]) self.project.save() self.preprint = PreprintFactory(project=self.project, finish=False) @assert_logs(NodeLog.MADE_PUBLIC, 'project') @assert_logs(NodeLog.PREPRINT_INITIATED, 'project', -2) def test_is_preprint_property_new_file_to_published(self): assert_false(self.project.is_preprint) self.preprint.set_primary_file(self.file, auth=self.auth, save=True) self.project.reload() assert_false(self.project.is_preprint) with assert_raises(ValueError): self.preprint.set_published(True, auth=self.auth, save=True) self.preprint.provider = PreprintProviderFactory() self.preprint.set_subjects([[SubjectFactory()._id]], auth=self.auth, save=True) self.project.reload() assert_false(self.project.is_preprint) self.preprint.set_published(True, auth=self.auth, save=True) self.project.reload() assert_true(self.project.is_preprint) def test_project_made_public(self): assert_false(self.project.is_public) self.preprint.set_primary_file(self.file, auth=self.auth, save=True) assert_false(self.project.is_public) with assert_raises(ValueError): self.preprint.set_published(True, auth=self.auth, save=True) self.preprint.provider = PreprintProviderFactory() self.preprint.set_subjects([[SubjectFactory()._id]], auth=self.auth, save=True) self.project.reload() assert_false(self.project.is_public) self.preprint.set_published(True, auth=self.auth, save=True) self.project.reload() assert_true(self.project.is_public) def test_add_primary_file(self): self.preprint.set_primary_file(self.file, auth=self.auth, save=True) assert_equal(self.project.preprint_file, self.file) assert_equal(type(self.project.preprint_file), type(self.file.stored_object)) @assert_logs(NodeLog.PREPRINT_FILE_UPDATED, 'project') def test_change_primary_file(self): self.preprint.set_primary_file(self.file, auth=self.auth, save=True) assert_equal(self.project.preprint_file, self.file) self.preprint.set_primary_file(self.file_two, auth=self.auth, save=True) assert_equal(self.project.preprint_file._id, self.file_two._id) def test_add_invalid_file(self): with assert_raises(AttributeError): self.preprint.set_primary_file('inatlanta', auth=self.auth, save=True) def test_preprint_created_date(self): self.preprint.set_primary_file(self.file, auth=self.auth, save=True) assert_equal(self.project.preprint_file._id, self.file._id) assert(self.preprint.date_created) assert_not_equal(self.project.date_created, self.preprint.date_created) def test_non_admin_update_file(self): self.preprint.set_primary_file(self.file, auth=self.auth, save=True) assert_equal(self.project.preprint_file._id, self.file._id) with assert_raises(PermissionsError): self.preprint.set_primary_file(self.file_two, auth=self.read_write_user_auth, save=True) assert_equal(self.project.preprint_file._id, self.file._id)
class TestPreprintRelationshipPreprintProvider(ApiTestCase): def setUp(self): super(TestPreprintRelationshipPreprintProvider, self).setUp() self.user = AuthUserFactory() self.read_write_user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user, providers=None) self.preprint.add_contributor(self.read_write_user) self.preprint.save() self.preprint_provider_one = PreprintProviderFactory() self.preprint_provider_two = PreprintProviderFactory() self.preprint_preprint_providers_url = self.create_url(self.preprint._id) def create_url(self, preprint_id): return '/{0}preprints/{1}/relationships/preprint_providers/'.format(API_BASE, preprint_id) def create_payload(self, *preprint_provider_ids): data = [] for provider_id in preprint_provider_ids: data.append({'type': 'preprint_providers', 'id': provider_id}) return {'data': data} def test_add_preprint_providers(self): assert_equal(self.preprint.preprint_providers, None) res = self.app.post_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_one._id, self.preprint_provider_two._id), auth=self.user.auth ) assert_equal(res.status_code, 201) # check the relationship self.preprint.reload() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) assert_in(self.preprint_provider_two, self.preprint.preprint_providers) def test_add_through_patch_one_provider_while_removing_other(self): self.preprint.preprint_providers = [self.preprint_provider_one] self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) assert_not_in(self.preprint_provider_two, self.preprint.preprint_providers) res = self.app.patch_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_two._id), auth=self.user.auth ) assert_equal(res.status_code, 200) self.preprint.reload() assert_not_in(self.preprint_provider_one, self.preprint.preprint_providers) assert_in(self.preprint_provider_two, self.preprint.preprint_providers) def test_add_through_post_to_preprint_with_provider(self): self.preprint.preprint_providers = [self.preprint_provider_one] self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) assert_not_in(self.preprint_provider_two, self.preprint.preprint_providers) res = self.app.post_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_two._id), auth=self.user.auth ) assert_equal(res.status_code, 201) self.preprint.reload() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) assert_in(self.preprint_provider_two, self.preprint.preprint_providers) def test_add_provider_with_no_permissions(self): new_user = AuthUserFactory() new_user.save() res = self.app.post_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_one._id), auth=new_user.auth, expect_errors=True, ) assert_equal(res.status_code, 403) def test_delete_nothing(self): res = self.app.delete_json_api( self.preprint_preprint_providers_url, self.create_payload(), auth=self.user.auth ) assert_equal(res.status_code, 204) def test_remove_providers(self): self.preprint.preprint_providers = [self.preprint_provider_one] self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) res = self.app.put_json_api( self.preprint_preprint_providers_url, self.create_payload(), auth=self.user.auth ) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.preprint_providers, []) def test_remove_providers_with_no_auth(self): res = self.app.put_json_api( self.preprint_preprint_providers_url, self.create_payload(), expect_errors=True ) assert_equal(res.status_code, 401) def test_using_post_making_no_changes_returns_204(self): self.preprint.preprint_providers = [self.preprint_provider_one] self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) res = self.app.post_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_one._id), auth=self.user.auth ) assert_equal(res.status_code, 204) self.preprint.reload() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) def test_delete_user_is_admin(self): self.preprint.preprint_providers = [self.preprint_provider_one] self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) res = self.app.delete_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_one._id), auth=self.user.auth ) assert_equal(res.status_code, 204) self.preprint.reload() assert_not_in(self.preprint_provider_one, self.preprint.preprint_providers) def test_delete_provider_user_is_read_write(self): self.preprint.preprint_providers = [self.preprint_provider_one] self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) res = self.app.delete_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_one._id), auth=self.read_write_user.auth, expect_errors=True ) assert_equal(res.status_code, 403) self.preprint.reload() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) def test_add_provider_user_is_read_write(self): self.preprint.preprint_providers = [] self.preprint.preprint_providers.append(self.preprint_provider_one) self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) res = self.app.post_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_two._id), auth=self.read_write_user.auth, expect_errors=True ) assert_equal(res.status_code, 403) self.preprint.reload() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) def test_change_provider_user_is_read_write(self): self.preprint.preprint_providers = [] self.preprint.preprint_providers.append(self.preprint_provider_one) self.preprint.save() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) res = self.app.put_json_api( self.preprint_preprint_providers_url, self.create_payload(self.preprint_provider_two._id), auth=self.read_write_user.auth, expect_errors=True ) assert_equal(res.status_code, 403) self.preprint.reload() assert_in(self.preprint_provider_one, self.preprint.preprint_providers) def test_get_relationship_information(self): res = self.app.get(self.preprint_preprint_providers_url,auth=self.user.auth) assert_equal(res.status_code, 200) def test_invalid_relationship_type(self): invalid_type_payload = self.create_payload(self.preprint_provider_one._id) invalid_type_payload['data'][0]['type'] = 'socks' res = self.app.put_json_api( self.preprint_preprint_providers_url, invalid_type_payload, auth=self.user.auth, expect_errors=True ) assert_equal(res.status_code, 409) def test_provider_does_not_exist(self): res = self.app.post_json_api( self.preprint_preprint_providers_url, self.create_payload('nope nope nope'), auth=self.user.auth, expect_errors=True ) assert_equal(res.status_code, 404)
class TestPreprintUpdate(ApiTestCase): def setUp(self): super(TestPreprintUpdate, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id) self.subject = SubjectFactory() def test_update_preprint_permission_denied(self): update_doi_payload = build_preprint_update_payload(self.preprint._id, attributes={'article_doi': '10.123/456/789'}) noncontrib = AuthUserFactory() res = self.app.patch_json_api(self.url, update_doi_payload, auth=noncontrib.auth, expect_errors=True) assert_equal(res.status_code, 403) res = self.app.patch_json_api(self.url, update_doi_payload, expect_errors=True) assert_equal(res.status_code, 401) def test_update_subjects(self): assert_not_equal(self.preprint.subjects[0], [self.subject._id]) update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"subjects": [[self.subject._id]]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.subjects[0], [self.subject._id]) def test_update_invalid_subjects(self): subjects = self.preprint.subjects update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"subjects": [['wwe']]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) self.preprint.reload() assert_equal(self.preprint.subjects, subjects) def test_update_primary_file(self): new_file = test_utils.create_test_file(self.preprint.node, 'openupthatwindow.pdf') relationships = { "primary_file": { "data": { "type": "file", "id": new_file._id } } } assert_not_equal(self.preprint.primary_file, new_file) update_file_payload = build_preprint_update_payload(self.preprint._id, relationships=relationships) res = self.app.patch_json_api(self.url, update_file_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.node.reload() assert_equal(self.preprint.primary_file, new_file) def test_new_primary_not_in_node(self): project = ProjectFactory() file_for_project = test_utils.create_test_file(project, 'letoutthatantidote.pdf') relationships = { "primary_file": { "data": { "type": "file", "id": file_for_project._id } } } update_file_payload = build_preprint_update_payload(self.preprint._id, relationships=relationships) res = self.app.patch_json_api(self.url, update_file_payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) self.preprint.reload() assert_not_equal(self.preprint.primary_file, file_for_project) def test_update_doi(self): new_doi = '10.1234/ASDFASDF' assert_not_equal(self.preprint.article_doi, new_doi) update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"doi": new_doi}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=self.user.auth) assert_equal(res.status_code, 200) self.preprint.node.reload() assert_equal(self.preprint.article_doi, new_doi) preprint_detail = self.app.get(self.url, auth=self.user.auth).json['data'] assert_equal(preprint_detail['links']['doi'], 'https://dx.doi.org/{}'.format(new_doi)) def test_write_contrib_cannot_set_primary_file(self): user_two = AuthUserFactory() self.preprint.node.add_contributor(user_two, permissions=['read', 'write'], auth=Auth(self.user), save=True) new_file = test_utils.create_test_file(self.preprint.node, 'openupthatwindow.pdf') data = { 'data':{ 'type': 'primary_file', 'id': self.preprint._id, 'attributes': {}, 'relationships': { 'primary_file': { 'data': { 'type': 'file', 'id': new_file._id } } } } } res = self.app.patch_json_api(self.url, data, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_noncontrib_cannot_set_primary_file(self): user_two = AuthUserFactory() new_file = test_utils.create_test_file(self.preprint.node, 'openupthatwindow.pdf') data = { 'data':{ 'type': 'primary_file', 'id': self.preprint._id, 'attributes': {}, 'relationships': { 'primary_file': { 'data': { 'type': 'file', 'id': new_file._id } } } } } res = self.app.patch_json_api(self.url, data, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_write_contrib_cannot_set_subjects(self): user_two = AuthUserFactory() self.preprint.node.add_contributor(user_two, permissions=['read', 'write'], auth=Auth(self.user), save=True) assert_not_equal(self.preprint.subjects[0], self.subject._id) update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"subjects": [[self.subject._id]]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_not_equal(self.preprint.subjects[0], self.subject._id) def test_noncontrib_cannot_set_subjects(self): user_two = AuthUserFactory() self.preprint.node.add_contributor(user_two, permissions=['read', 'write'], auth=Auth(self.user), save=True) assert_not_equal(self.preprint.subjects[0], self.subject._id) update_subjects_payload = build_preprint_update_payload(self.preprint._id, attributes={"subjects": [[self.subject._id]]}) res = self.app.patch_json_api(self.url, update_subjects_payload, auth=user_two.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_not_equal(self.preprint.subjects[0], self.subject._id)
class TestPreprintFiltering(ApiTestCase): def setUp(self): super(TestPreprintFiltering, self).setUp() self.user = AuthUserFactory() self.provider = PreprintProviderFactory(name='wwe') self.preprint = PreprintFactory(creator=self.user, providers=[self.provider]) self.preprint.add_tag('nature boy', Auth(self.user), save=False) self.preprint.add_tag('ric flair', Auth(self.user), save=False) self.preprint.save() self.provider_two = PreprintProviderFactory(name='wcw') self.preprint_two = PreprintFactory(creator=self.user, filename='woo.txt', providers=[self.provider_two]) self.preprint_two.add_tag('nature boy', Auth(self.user), save=False) self.preprint_two.add_tag('woo', Auth(self.user), save=False) self.preprint_two.save() self.preprint_three = PreprintFactory(creator=self.user, filename='stonecold.txt', providers=[self.provider]) self.preprint_three.add_tag('stone', Auth(self.user), save=False) self.preprint_two.add_tag('cold', Auth(self.user), save=False) self.preprint_three.save() def tearDown(self): super(TestPreprintFiltering, self).tearDown() Node.remove() def test_filtering_tags(self): # both preprint and preprint_two have nature boy url = '/{}preprints/?filter[tags]={}'.format(API_BASE, 'nature boy') res = self.app.get(url, auth=self.user.auth) reg_json = res.json['data'] ids = [each['id'] for each in reg_json] assert_in(self.preprint._id, ids) assert_in(self.preprint_two._id, ids) assert_not_in(self.preprint_three._id, ids) # filtering two tags # preprint has both tags; preprint_two only has one url = '/{}preprints/?filter[tags]={}&filter[tags]={}'.format(API_BASE, 'nature boy', 'ric flair') res = self.app.get(url, auth=self.user.auth) reg_json = res.json['data'] ids = [each['id'] for each in reg_json] assert_in(self.preprint._id, ids) assert_not_in(self.preprint_two._id, ids) assert_not_in(self.preprint_three._id, ids) def test_filter_by_doi(self): url = '/{}preprints/?filter[doi]={}'.format(API_BASE, self.preprint.preprint_doi) res = self.app.get(url, auth=self.user.auth) data = res.json['data'] assert_equal(len(data), 1) for result in data: assert_equal(self.preprint._id, result['id'])
def setUp(self): super(TestOnPreprintUpdatedTask, self).setUp() self.preprint = PreprintFactory()
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags, presentation_name, is_registration, is_preprint, preprint_providers): auth = Auth(user=creator) project_title = name if name else fake.science_sentence() if is_preprint: providers_to_add = [] if preprint_providers: providers = preprint_providers.split(',') for provider in providers: try: preprint_provider = models.PreprintProvider.find_one(Q('_id', 'eq', provider)) except NoResultsFound: preprint_provider = PreprintProviderFactory(name=provider) providers_to_add.append(preprint_provider) privacy = 'public' project = PreprintFactory(title=project_title, description=fake.science_paragraph(), creator=creator, providers=providers_to_add) elif is_registration: project = RegistrationFactory(title=project_title, description=fake.science_paragraph(), creator=creator) else: project = ProjectFactory(title=project_title, description=fake.science_paragraph(), creator=creator) project.set_privacy(privacy) for _ in range(n_users): contrib = create_fake_user() project.add_contributor(contrib, auth=auth) if isinstance(n_components, int): for _ in range(n_components): NodeFactory(project=project, title=fake.science_sentence(), description=fake.science_paragraph(), creator=creator) elif isinstance(n_components, list): render_generations_from_node_structure_list(project, creator, n_components) for _ in range(n_tags): project.add_tag(fake.science_word(), auth=auth) if presentation_name is not None: project.add_tag(presentation_name, auth=auth) project.add_tag('poster', auth=auth) project.save() logger.info('Created project: {0}'.format(project.title)) return project
def setUp(self): super(TestPreprintProviders, self).setUp() self.preprint = PreprintFactory(providers=[]) self.provider = PreprintProviderFactory(name='WWEArxiv')
class TestPreprintUpdateLicense(ApiTestCase): def setUp(self): super(TestPreprintUpdateLicense, self).setUp() ensure_licenses() self.admin_contributor = AuthUserFactory() self.rw_contributor = AuthUserFactory() self.read_contributor = AuthUserFactory() self.non_contributor = AuthUserFactory() self.preprint_provider = PreprintProviderFactory() self.preprint = PreprintFactory(creator=self.admin_contributor, provider=self.preprint_provider) self.preprint.node.add_contributor(self.rw_contributor, auth=Auth(self.admin_contributor)) self.preprint.node.add_contributor(self.read_contributor, auth=Auth(self.admin_contributor), permissions=['read']) self.preprint.node.save() self.cc0_license = NodeLicense.find_one(Q('name', 'eq', 'CC0 1.0 Universal')) self.mit_license = NodeLicense.find_one(Q('name', 'eq', 'MIT License')) self.no_license = NodeLicense.find_one(Q('name', 'eq', 'No license')) self.preprint_provider.licenses_acceptable = [self.cc0_license, self.no_license] self.preprint_provider.save() self.url = '/{}preprints/{}/'.format(API_BASE, self.preprint._id) def make_payload(self, node_id, license_id=None, license_year=None, copyright_holders=None): attributes = {} if license_year and copyright_holders: attributes = { 'license_record': { 'year': license_year, 'copyright_holders': copyright_holders } } elif license_year: attributes = { 'license_record': { 'year': license_year } } elif copyright_holders: attributes = { 'license_record': { 'copyright_holders': copyright_holders } } return { 'data': { 'id': node_id, 'attributes': attributes, 'relationships': { 'license': { 'data': { 'type': 'licenses', 'id': license_id } } } } } if license_id else { 'data': { 'id': node_id, 'attributes': attributes } } def make_request(self, url, data, auth=None, expect_errors=False): return self.app.patch_json_api(url, data, auth=auth, expect_errors=expect_errors) def test_admin_can_update_license(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id ) assert_equal(self.preprint.license, None) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.license.node_license, self.cc0_license) assert_equal(self.preprint.license.year, None) assert_equal(self.preprint.license.copyright_holders, []) # check logs log = self.preprint.node.logs[-1] assert_equal(log.action, 'preprint_license_updated') assert_equal(log.params.get('preprint'), self.preprint._id) def test_admin_can_update_license_record(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.no_license._id, license_year='2015', copyright_holders=['Bojack Horseman, Princess Carolyn'] ) assert_equal(self.preprint.license, None) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.license.node_license, self.no_license) assert_equal(self.preprint.license.year, '2015') assert_equal(self.preprint.license.copyright_holders, ['Bojack Horseman, Princess Carolyn']) def test_rw_contributor_cannot_update_license(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id ) res = self.make_request(self.url, data, auth=self.rw_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'User must be an admin to update a preprint.') def test_read_contributor_cannot_update_license(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id ) res = self.make_request(self.url, data, auth=self.read_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'You do not have permission to perform this action.') def test_non_contributor_cannot_update_license(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id ) res = self.make_request(self.url, data, auth=self.non_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'You do not have permission to perform this action.') def test_unauthenticated_user_cannot_update_license(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id ) res = self.make_request(self.url, data, expect_errors=True) assert_equal(res.status_code, 401) assert_equal(res.json['errors'][0]['detail'], 'Authentication credentials were not provided.') def test_update_preprint_with_invalid_license_for_provider(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.mit_license._id ) assert_equal(self.preprint.license, None) res = self.make_request(self.url, data, auth=self.admin_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'Invalid license chosen for {}'.format(self.preprint_provider.name)) def test_update_preprint_with_existing_license_year_attribute_only(self): self.preprint.set_preprint_license( { 'id': self.no_license.id, 'year': '2014', 'copyrightHolders': ['Diane', 'Mr. Peanut Butter'] }, Auth(self.admin_contributor), ) self.preprint.save() assert_equal(self.preprint.license.node_license, self.no_license) assert_equal(self.preprint.license.year, '2014') assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter']) data = self.make_payload( node_id=self.preprint._id, license_year='2015' ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) assert_equal(res.status_code, 200) self.preprint.license.reload() assert_equal(self.preprint.license.node_license, self.no_license) assert_equal(self.preprint.license.year, '2015') assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter']) def test_update_preprint_with_existing_license_copyright_holders_attribute_only(self): self.preprint.set_preprint_license( { 'id': self.no_license.id, 'year': '2014', 'copyrightHolders': ['Diane', 'Mr. Peanut Butter'] }, Auth(self.admin_contributor), ) self.preprint.save() assert_equal(self.preprint.license.node_license, self.no_license) assert_equal(self.preprint.license.year, '2014') assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter']) data = self.make_payload( node_id=self.preprint._id, copyright_holders=['Bojack Horseman', 'Princess Carolyn'] ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) assert_equal(res.status_code, 200) self.preprint.license.reload() assert_equal(self.preprint.license.node_license, self.no_license) assert_equal(self.preprint.license.year, '2014') assert_equal(self.preprint.license.copyright_holders, ['Bojack Horseman', 'Princess Carolyn']) def test_update_preprint_with_existing_license_relationship_only(self): self.preprint.set_preprint_license( { 'id': self.no_license.id, 'year': '2014', 'copyrightHolders': ['Diane', 'Mr. Peanut Butter'] }, Auth(self.admin_contributor), ) self.preprint.save() assert_equal(self.preprint.license.node_license, self.no_license) assert_equal(self.preprint.license.year, '2014') assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter']) data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) assert_equal(res.status_code, 200) self.preprint.license.reload() assert_equal(self.preprint.license.node_license, self.cc0_license) assert_equal(self.preprint.license.year, '2014') assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter']) def test_update_preprint_with_existing_license_relationship_and_attributes(self): self.preprint.set_preprint_license( { 'id': self.no_license.id, 'year': '2014', 'copyrightHolders': ['Diane', 'Mr. Peanut Butter'] }, Auth(self.admin_contributor), save=True ) assert_equal(self.preprint.license.node_license, self.no_license) assert_equal(self.preprint.license.year, '2014') assert_equal(self.preprint.license.copyright_holders, ['Diane', 'Mr. Peanut Butter']) data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id, license_year='2015', copyright_holders=['Bojack Horseman', 'Princess Carolyn'] ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) assert_equal(res.status_code, 200) self.preprint.license.reload() assert_equal(self.preprint.license.node_license, self.cc0_license) assert_equal(self.preprint.license.year, '2015') assert_equal(self.preprint.license.copyright_holders, ['Bojack Horseman', 'Princess Carolyn']) def test_update_preprint_license_without_required_year_in_payload(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.no_license._id, copyright_holders=['Rick', 'Morty'] ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'year must be specified for this license') def test_update_preprint_license_without_required_copyright_holders_in_payload_(self): data = self.make_payload( node_id=self.preprint._id, license_id=self.no_license._id, license_year='1994' ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'copyrightHolders must be specified for this license') def test_update_preprint_license_does_not_change_project_license(self): self.preprint.node.set_node_license( { 'id': self.no_license.id, 'year': '2015', 'copyrightHolders': ['Simba', 'Mufasa'] }, auth=Auth(self.admin_contributor) ) self.preprint.node.save() assert_equal(self.preprint.node.node_license.node_license, self.no_license) data = self.make_payload( node_id=self.preprint._id, license_id=self.cc0_license._id ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) assert_equal(res.status_code, 200) self.preprint.reload() assert_equal(self.preprint.license.node_license, self.cc0_license) assert_equal(self.preprint.node.node_license.node_license, self.no_license) def test_update_preprint_license_without_change_does_not_add_log(self): self.preprint.set_preprint_license( { 'id': self.no_license.id, 'year': '2015', 'copyrightHolders': ['Kim', 'Kanye'] }, auth=Auth(self.admin_contributor), save=True ) before_num_logs = len(self.preprint.node.logs) before_update_log = self.preprint.node.logs[-1] data = self.make_payload( node_id=self.preprint._id, license_id=self.no_license._id, license_year='2015', copyright_holders=['Kanye', 'Kim'] ) res = self.make_request(self.url, data, auth=self.admin_contributor.auth) self.preprint.node.reload() after_num_logs = len(self.preprint.node.logs) after_update_log = self.preprint.node.logs[-1] assert_equal(res.status_code, 200) assert_equal(before_num_logs, after_num_logs) assert_equal(before_update_log._id, after_update_log._id)
class TestPreprintServicePermissions(OsfTestCase): def setUp(self): super(TestPreprintServicePermissions, self).setUp() self.user = AuthUserFactory() self.write_contrib = AuthUserFactory() self.project = ProjectFactory(creator=self.user) self.project.add_contributor(self.write_contrib, permissions=[permissions.WRITE]) self.preprint = PreprintFactory(project=self.project, is_published=False) def test_nonadmin_cannot_set_subjects(self): initial_subjects = self.preprint.subjects with assert_raises(PermissionsError): self.preprint.set_subjects([[SubjectFactory()._id]], auth=Auth(self.write_contrib), save=True) self.preprint.reload() assert_equal(initial_subjects, self.preprint.subjects) def test_nonadmin_cannot_set_file(self): initial_file = self.preprint.primary_file file = OsfStorageFile.create( is_file=True, node=self.project, path='/panda.txt', name='panda.txt', materialized_path='/panda.txt') file.save() with assert_raises(PermissionsError): self.preprint.set_primary_file(file, auth=Auth(self.write_contrib), save=True) self.preprint.reload() self.preprint.node.reload() assert_equal(initial_file._id, self.preprint.primary_file._id) def test_nonadmin_cannot_publish(self): assert_false(self.preprint.is_published) with assert_raises(PermissionsError): self.preprint.set_published(True, auth=Auth(self.write_contrib), save=True) assert_false(self.preprint.is_published) def test_admin_can_set_subjects(self): initial_subjects = self.preprint.subjects self.preprint.set_subjects([[SubjectFactory()._id]], auth=Auth(self.user), save=True) self.preprint.reload() assert_not_equal(initial_subjects, self.preprint.subjects) def test_admin_can_set_file(self): initial_file = self.preprint.primary_file file = OsfStorageFile.create( is_file=True, node=self.project, path='/panda.txt', name='panda.txt', materialized_path='/panda.txt') file.save() self.preprint.set_primary_file(file, auth=Auth(self.user), save=True) self.preprint.reload() self.preprint.node.reload() assert_not_equal(initial_file._id, self.preprint.primary_file._id) assert_equal(file._id, self.preprint.primary_file._id) def test_admin_can_publish(self): assert_false(self.preprint.is_published) self.preprint.set_published(True, auth=Auth(self.user), save=True) assert_true(self.preprint.is_published) def test_admin_cannot_unpublish(self): assert_false(self.preprint.is_published) self.preprint.set_published(True, auth=Auth(self.user), save=True) assert_true(self.preprint.is_published) with assert_raises(ValueError) as e: self.preprint.set_published(False, auth=Auth(self.user), save=True) assert_in('Cannot unpublish', e.exception.message)