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 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 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
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 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_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 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_preprint_providers_permission_denied(self): noncontrib = AuthUserFactory() 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=noncontrib.auth, expect_errors=True ) assert_equal(res.status_code, 403) 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)