def test_preprint_contributor_changes_updates_preprints_share( self, mock_on_preprint_updated, user, file, auth): preprint = PreprintFactory(is_published=True, creator=user) assert mock_on_preprint_updated.call_count == 2 user2 = AuthUserFactory() preprint.primary_file = file preprint.add_contributor(contributor=user2, auth=auth, save=True) assert mock_on_preprint_updated.call_count == 5 preprint.move_contributor(contributor=user, index=0, auth=auth, save=True) assert mock_on_preprint_updated.call_count == 7 data = [{ 'id': user._id, 'permissions': ADMIN, 'visible': True }, { 'id': user2._id, 'permissions': WRITE, 'visible': False }] preprint.manage_contributors(data, auth=auth, save=True) assert mock_on_preprint_updated.call_count == 9 preprint.update_contributor(user2, READ, True, auth=auth, save=True) assert mock_on_preprint_updated.call_count == 11 preprint.remove_contributor(contributor=user2, auth=auth) assert mock_on_preprint_updated.call_count == 13
def test_datacite_format_creators_for_preprint(self): preprint = PreprintFactory(project=self.node, is_published=True) verified_user = AuthUserFactory(external_identity={'ORCID': {'1234-1234-1234-1234': 'VERIFIED'}}) linked_user = AuthUserFactory(external_identity={'ORCID': {'1234-nope-1234-nope': 'LINK'}}) preprint.add_contributor(verified_user, visible=True) preprint.add_contributor(linked_user, visible=True) preprint.save() formatted_creators = metadata.format_creators(preprint) contributors_with_orcids = 0 guid_identifiers = [] for creator_xml in formatted_creators: assert creator_xml.find('creatorName').text != u'{}, {}'.format(self.invisible_contrib.family_name, self.invisible_contrib.given_name) name_identifiers = creator_xml.findall('nameIdentifier') for name_identifier in name_identifiers: if name_identifier.attrib['nameIdentifierScheme'] == 'ORCID': assert name_identifier.attrib['schemeURI'] == 'http://orcid.org/' contributors_with_orcids += 1 else: guid_identifiers.append(name_identifier.text) assert name_identifier.attrib['nameIdentifierScheme'] == 'OSF' assert name_identifier.attrib['schemeURI'] == settings.DOMAIN assert contributors_with_orcids >= 1 assert len(formatted_creators) == len(self.node.visible_contributors) assert sorted(guid_identifiers) == sorted([contrib.absolute_url for contrib in preprint.visible_contributors])
def preprint(self, user, contrib): preprint = PreprintFactory(creator=user) preprint.add_contributor(contrib, permissions=permissions.WRITE, visible=True, save=True) return preprint
def preprint(self, node_admin, provider): preprint = PreprintFactory( provider=provider, is_published=False) preprint.add_contributor( node_admin, permissions=osf_permissions.ADMIN) return preprint
def preprint(self, user, user_write_contrib): preprint = PreprintFactory(creator=user) preprint.add_contributor( user_write_contrib, permissions=permissions.WRITE, visible=True, save=True) return preprint
def preprint_with_contributor(self, mergee): preprint = PreprintFactory() preprint.add_contributor(mergee, permissions=WRITE, visible=False, save=True) preprint.save() return preprint
def preprint(self, user, contribs): preprint = PreprintFactory(creator=user) for contrib in contribs: if contrib._id != user._id: preprint.add_contributor(contrib, permissions=permissions.WRITE, visible=True, save=True) return preprint
def preprint_unpublished(self, user_admin_contrib, user_write_contrib, provider_one, project_public, subject): preprint = PreprintFactory(creator=user_admin_contrib, filename='mgla.pdf', provider=provider_one, subjects=[[subject._id]], project=project_public, is_published=False) preprint.add_contributor(user_write_contrib, 'write', save=True) return preprint
def preprint(self, user_admin_contrib, user_write_contrib, project, provider, subject): preprint = PreprintFactory(creator=user_admin_contrib, filename='saor.pdf', provider=provider, subjects=[[subject._id]], project=project, is_published=True) preprint.add_contributor(user_write_contrib, WRITE, save=True) return preprint
def post_mod_preprint(self, admin, write_contrib, post_mod_provider): post = PreprintFactory( creator=admin, provider=post_mod_provider, ) post.save() post.add_contributor(contributor=write_contrib, permissions='write', save=True) return post
def none_mod_preprint(self, admin, write_contrib, none_mod_provider): preprint = PreprintFactory( creator=admin, provider=none_mod_provider, ) preprint.save() preprint.add_contributor(contributor=write_contrib, permissions=permissions.WRITE, save=True) return preprint
def preprint(self, user_admin_contrib, user_write_contrib, project, provider, subject): preprint = PreprintFactory( creator=user_admin_contrib, filename='saor.pdf', provider=provider, subjects=[[subject._id]], project=project, is_published=True) preprint.add_contributor(user_write_contrib, 'write', save=True) return preprint
def auto_withdrawable_pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory(creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending') pre.save() pre.add_contributor(contributor=write_contrib, permissions=permissions.WRITE, save=True) return pre
def preprint(self, user, contribs): preprint = PreprintFactory(creator=user) for contrib in contribs: if contrib._id != user._id: preprint.add_contributor( contrib, permissions=permissions.WRITE, visible=True, save=True ) return preprint
def none_mod_preprint(self, admin, write_contrib, none_mod_provider): preprint = PreprintFactory( creator=admin, provider=none_mod_provider, ) preprint.save() preprint.add_contributor( contributor=write_contrib, permissions='write', save=True ) return preprint
def preprint_unpublished(self, user_admin_contrib, provider_one, project_public, user_write_contrib, subject): preprint = PreprintFactory(creator=user_admin_contrib, filename='mgla.pdf', provider=provider_one, subjects=[[subject._id]], project=project_public, is_published=False, machine_state=DefaultStates.INITIAL.value) preprint.add_contributor(user_write_contrib, permissions='write', save=True) return preprint
def pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory(creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending') pre.ever_public = True pre.save() pre.add_contributor(contributor=write_contrib, permissions='write', save=True) pre.is_public = True pre.save() return pre
def auto_withdrawable_pre_mod_preprint(self, admin, write_contrib, pre_mod_provider): pre = PreprintFactory( creator=admin, provider=pre_mod_provider, is_published=False, machine_state='pending' ) pre.save() pre.add_contributor( contributor=write_contrib, permissions='write', save=True ) return pre
def test_filter_withdrawn_preprint(self, app, url, user, project_one, provider_one, provider_two): preprint_one = PreprintFactory(is_published=False, creator=user, project=project_one, provider=provider_one) preprint_one.date_withdrawn = timezone.now() preprint_one.is_public = True preprint_one.is_published = True preprint_one.date_published = timezone.now() preprint_one.machine_state = 'accepted' assert preprint_one.ever_public is False # Putting this preprint in a weird state, is verified_publishable, but has been # withdrawn and ever_public is False. This is to isolate withdrawal portion of query preprint_one.save() preprint_two = PreprintFactory(creator=user, project=project_one, provider=provider_two) preprint_two.date_withdrawn = timezone.now() preprint_two.ever_public = True preprint_two.save() # Unauthenticated can only see withdrawn preprints that have been public expected = [preprint_two._id] res = app.get(url) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # Noncontribs can only see withdrawn preprints that have been public user2 = AuthUserFactory() expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # contribs can only see withdrawn preprints that have been public user2 = AuthUserFactory() preprint_one.add_contributor(user2, 'read') preprint_two.add_contributor(user2, 'read') expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) expected = [preprint_two._id] # Admins can only see withdrawn preprints that have been public res = app.get(url, auth=user.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual)
def preprint_unpublished( self, user_admin_contrib, user_write_contrib, provider_one, project_public, subject): preprint = PreprintFactory(creator=user_admin_contrib, filename='mgla.pdf', provider=provider_one, subjects=[[subject._id]], project=project_public, is_published=False) preprint.add_contributor(user_write_contrib, 'write', save=True) return preprint
def preprint_unpublished( self, user_admin_contrib, provider_one, project_public, user_write_contrib, subject): preprint = PreprintFactory(creator=user_admin_contrib, filename='mgla.pdf', provider=provider_one, subjects=[[subject._id]], project=project_public, is_published=False, machine_state=DefaultStates.INITIAL.value) preprint.add_contributor(user_write_contrib, permissions='write', save=True) return preprint
def preprint_unpublished( self, user_admin_contrib, user_write_contrib, provider_one, project_public, subject): preprint = PreprintFactory(creator=user_admin_contrib, filename='mgla.pdf', provider=provider_one, subjects=[[subject._id]], project=project_public, machine_state='pending', is_published=False) preprint.add_contributor(user_write_contrib, permissions=permissions.WRITE, save=True) return preprint
def test_filter_withdrawn_preprint(self, app, url, user): preprint_one = PreprintFactory(is_published=False, creator=user) preprint_one.date_withdrawn = timezone.now() preprint_one.is_public = True preprint_one.is_published = True preprint_one.date_published = timezone.now() preprint_one.machine_state = 'accepted' assert preprint_one.ever_public is False # Putting this preprint in a weird state, is verified_publishable, but has been # withdrawn and ever_public is False. This is to isolate withdrawal portion of query preprint_one.save() preprint_two = PreprintFactory(creator=user) preprint_two.date_withdrawn = timezone.now() preprint_two.ever_public = True preprint_two.save() # Unauthenticated users cannot see users/me/preprints url = '/{}users/me/preprints/?version=2.2&'.format(API_BASE) expected = [preprint_two._id] res = app.get(url, expect_errors=True) assert res.status_code == 401 # Noncontribs cannot see withdrawn preprints user2 = AuthUserFactory() url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # Read contribs - contrib=False on UserPreprints filter so read contribs can only see # withdrawn preprints that were once public user2 = AuthUserFactory() preprint_one.add_contributor(user2, permissions.READ, save=True) preprint_two.add_contributor(user2, permissions.READ, save=True) url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) expected = [preprint_two._id] # Admin contribs can only see withdrawn preprints that were once public res = app.get(url, auth=user.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual)
def test_filter_withdrawn_preprint(self, app, url, user): preprint_one = PreprintFactory(is_published=False, creator=user) preprint_one.date_withdrawn = timezone.now() preprint_one.is_public = True preprint_one.is_published = True preprint_one.date_published = timezone.now() preprint_one.machine_state = 'accepted' assert preprint_one.ever_public is False # Putting this preprint in a weird state, is verified_publishable, but has been # withdrawn and ever_public is False. This is to isolate withdrawal portion of query preprint_one.save() preprint_two = PreprintFactory(creator=user) preprint_two.date_withdrawn = timezone.now() preprint_two.ever_public = True preprint_two.save() # Unauthenticated users cannot see users/me/preprints url = '/{}users/me/preprints/?version=2.2&'.format(API_BASE) expected = [preprint_two._id] res = app.get(url, expect_errors=True) assert res.status_code == 401 # Noncontribs cannot see withdrawn preprints user2 = AuthUserFactory() url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # Read contribs - contrib=False on UserPreprints filter so read contribs can only see # withdrawn preprints that were once public user2 = AuthUserFactory() preprint_one.add_contributor(user2, 'read', save=True) preprint_two.add_contributor(user2, 'read', save=True) url = '/{}users/{}/preprints/?version=2.2&'.format(API_BASE, user2._id) expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) expected = [preprint_two._id] # Admin contribs can only see withdrawn preprints that were once public res = app.get(url, auth=user.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual)
def test_filter_withdrawn_preprint(self, app, url, user, project_one, provider_one, provider_two): preprint_one = PreprintFactory(is_published=False, creator=user, project=project_one, provider=provider_one) preprint_one.date_withdrawn = timezone.now() preprint_one.is_public = True preprint_one.is_published = True preprint_one.date_published = timezone.now() preprint_one.machine_state = 'accepted' assert preprint_one.ever_public is False # Putting this preprint in a weird state, is verified_publishable, but has been # withdrawn and ever_public is False. This is to isolate withdrawal portion of query preprint_one.save() preprint_two = PreprintFactory(creator=user, project=project_one, provider=provider_two) preprint_two.date_withdrawn = timezone.now() preprint_two.ever_public = True preprint_two.save() # Unauthenticated can only see withdrawn preprints that have been public expected = [preprint_two._id] res = app.get(url) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # Noncontribs can only see withdrawn preprints that have been public user2 = AuthUserFactory() expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) # contribs can only see withdrawn preprints that have been public user2 = AuthUserFactory() preprint_one.add_contributor(user2, 'read') preprint_two.add_contributor(user2, 'read') expected = [preprint_two._id] res = app.get(url, auth=user2.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual) expected = [preprint_two._id] # Admins can only see withdrawn preprints that have been public res = app.get(url, auth=user.auth) actual = [preprint['id'] for preprint in res.json['data']] assert set(expected) == set(actual)
class TestPreprintCitationContentChicago(ApiTestCase): def setUp(self): super(TestPreprintCitationContentChicago, self).setUp() self.admin_contributor = AuthUserFactory() self.published_preprint = PreprintFactory( title='A Study of Coffee', creator=self.admin_contributor) self.published_preprint.save() self.admin_contributor.given_name = 'Grapes' self.admin_contributor.middle_names = ' Coffee Beans ' self.admin_contributor.family_name = 'McGee' self.admin_contributor.save() self.published_preprint_url = '/{}preprints/{}/citation/chicago-author-date/'.format( API_BASE, self.published_preprint._id) self.second_contrib = AuthUserFactory() self.second_contrib.given_name = 'Darla' self.second_contrib.middle_names = 'Texas Toast' self.second_contrib.family_name = 'Jenkins' self.second_contrib.suffix = 'Junior' self.second_contrib.save() self.third_contrib = AuthUserFactory() self.third_contrib.given_name = 'Lilith' self.third_contrib.middle_names = 'Radar' self.third_contrib.family_name = 'Schematics' self.third_contrib.save() def test_not_published(self): self.published_preprint.date_published = None self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, u'McGee, Grapes C. B. {} “{}.” {}. {}.'.format( 'n.d.', self.published_preprint.title, self.published_preprint.provider.name, 'doi:' + self.published_preprint.article_doi, ) ) def test_one_author(self): res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) # test_suffix self.admin_contributor.suffix = 'Junior' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., Junior. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) # test_no_middle_names self.admin_contributor.suffix = '' self.admin_contributor.middle_names = '' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) def test_two_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., and Darla T. T. Jenkins, Junior. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) def test_three_authors_and_title_with_period(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) self.published_preprint.title = 'This Preprint ends in a Period.' self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., Darla T. T. Jenkins, Junior, and Lilith R. Schematics. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), 'This Preprint Ends in a Period', self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi) ) def test_eleven_contributors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) for i in range(1, 9): new_user = AuthUserFactory() new_user.given_name = 'James' new_user.family_name = 'Taylor{}'.format(i) new_user.save() self.published_preprint.add_contributor(new_user) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., Darla T. T. Jenkins, Junior, Lilith R. Schematics, James Taylor1, James Taylor2, James Taylor3, James Taylor4, et al. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) )
class TestPreprintCitationContentAPA(ApiTestCase): def setUp(self): super(TestPreprintCitationContentAPA, self).setUp() self.admin_contributor = AuthUserFactory() self.published_preprint = PreprintFactory( title='A Study of Coffee', creator=self.admin_contributor ) self.published_preprint.save() self.admin_contributor.given_name = 'Grapes' self.admin_contributor.middle_names = ' Coffee Beans ' self.admin_contributor.family_name = 'McGee' self.admin_contributor.save() self.second_contrib = AuthUserFactory() self.second_contrib.given_name = 'Darla' self.second_contrib.middle_names = 'Texas Toast' self.second_contrib.family_name = 'Jenkins' self.second_contrib.suffix = 'Junior' self.second_contrib.save() self.third_contrib = AuthUserFactory() self.third_contrib.given_name = 'Lilith' self.third_contrib.middle_names = 'Radar' self.third_contrib.family_name = 'Schematics' self.third_contrib.save() self.published_preprint_url = '/{}preprints/{}/citation/apa/'.format( API_BASE, self.published_preprint._id) def test_not_published(self): self.published_preprint.date_published = None self.published_preprint.save() self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, u'McGee, G. C. B., & Jenkins, D. T. T., Junior. ({}). {}. {}'.format( 'n.d.', self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_one_author(self): res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) # test_suffix self.admin_contributor.suffix = 'Junior' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Junior. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) # test_no_middle_names self.admin_contributor.suffix = '' self.admin_contributor.middle_names = '' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_two_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., & Jenkins, D. T. T., Junior. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_three_authors_and_title_with_period(self): self.published_preprint.title = 'This Title Ends in a Period.' self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Jenkins, D. T. T., Junior, & Schematics, L. R. ({}). {}. {}'.format( date, 'This Title Ends in a Period', 'https://doi.org/' + self.published_preprint.article_doi) ) def test_seven_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) for i in range(1, 5): new_user = AuthUserFactory() new_user.given_name = 'James' new_user.family_name = 'Taylor{}'.format(i) new_user.save() self.published_preprint.add_contributor(new_user) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Jenkins, D. T. T., Junior, Schematics, L. R., Taylor1, J., Taylor2, J., Taylor3, J., & Taylor4, J. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_eight_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) for i in range(1, 6): new_user = AuthUserFactory() new_user.given_name = 'James' new_user.family_name = 'Taylor{}'.format(i) new_user.save() self.published_preprint.add_contributor(new_user) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Jenkins, D. T. T., Junior, Schematics, L. R., Taylor1, J., Taylor2, J., Taylor3, J., … Taylor5, J. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) )
class TestPreprintCitationContentMLA(ApiTestCase): def setUp(self): super(TestPreprintCitationContentMLA, self).setUp() self.admin_contributor = AuthUserFactory() self.published_preprint = PreprintFactory( creator=self.admin_contributor) self.published_preprint.title = 'My Preprint' self.published_preprint.save() self.admin_contributor.given_name = 'Grapes' self.admin_contributor.middle_names = ' Coffee Beans ' self.admin_contributor.family_name = 'McGee' self.admin_contributor.save() self.published_preprint_url = '/{}preprints/{}/citation/modern-language-association/'.format( API_BASE, self.published_preprint._id) self.second_contrib = AuthUserFactory() self.second_contrib.given_name = 'Darla' self.second_contrib.middle_names = 'Texas Toast' self.second_contrib.family_name = 'Jenkins' self.second_contrib.suffix = 'Junior' self.second_contrib.save() self.third_contrib = AuthUserFactory() self.third_contrib.given_name = 'Lilith' self.third_contrib.middle_names = 'Radar' self.third_contrib.family_name = 'Schematics' self.third_contrib.save() def test_not_published(self): self.published_preprint.date_published = None self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, u'McGee, Grapes C. B. “{}.” {}, {} Web.'.format( self.published_preprint.title, self.published_preprint.provider.name, 'n.d.') ) def test_one_author(self): res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) # test_suffix self.admin_contributor.suffix = 'Junior' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) # test_no_middle_names self.admin_contributor.suffix = '' self.admin_contributor.middle_names = '' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_citation_no_repeated_periods(self): self.published_preprint.title = 'A Study of Coffee.' self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_citation_osf_provider(self): self.published_preprint.title = 'A Study of Coffee.' self.published_preprint.save() self.published_preprint.provider.name = 'Open Science Framework' self.published_preprint.provider.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_two_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_three_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) # first name suffix self.admin_contributor.suffix = 'Jr.' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association'))
class TestPreprintBannerView(OsfTestCase): def setUp(self): super(TestPreprintBannerView, self).setUp() self.admin = AuthUserFactory() self.write_contrib = AuthUserFactory() self.read_contrib = AuthUserFactory() self.non_contrib = AuthUserFactory() self.provider_one = PreprintProviderFactory() self.project_one = ProjectFactory(creator=self.admin, is_public=True) self.project_one.add_contributor(self.write_contrib, ['write', 'read']) self.project_one.add_contributor(self.read_contrib, ['read']) self.subject_one = SubjectFactory() self.preprint = PreprintFactory(creator=self.admin, filename='mgla.pdf', provider=self.provider_one, subjects=[[self.subject_one._id]], project=self.project_one, is_published=True) self.preprint.add_contributor(self.write_contrib, 'write') self.preprint.add_contributor(self.read_contrib, 'read') def test_public_project_published_preprint(self): url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_in('Has supplemental materials for', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_in('Has supplemental materials for', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_in('Has supplemental materials for', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_in('Has supplemental materials for', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_in('Has supplemental materials for', res.body) def test_public_project_abandoned_preprint(self): self.preprint.machine_state = 'initial' self.preprint.save() url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_not_in('Has supplemental materials for', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_not_in('Has supplemental materials for', res.body) def test_public_project_deleted_preprint(self): self.preprint.deleted = timezone.now() self.preprint.save() url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_not_in('Has supplemental materials for', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_not_in('Has supplemental materials for', res.body) def test_public_project_private_preprint(self): self.preprint.is_public = False self.preprint.save() url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_in('Has supplemental materials for', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_in('Has supplemental materials for', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_in('Has supplemental materials for', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_not_in('Has supplemental materials for', res.body) def test_public_project_orphaned_preprint(self): self.preprint.primary_file = None self.preprint.save() url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_in('Has supplemental materials for', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_in('Has supplemental materials for', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_in('Has supplemental materials for', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_not_in('Has supplemental materials for', res.body) def test_public_project_unpublished_preprint(self): self.preprint.is_published = False self.preprint.save() url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_in('Has supplemental materials for', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_in('Has supplemental materials for', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_in('Has supplemental materials for', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_not_in('Has supplemental materials for', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_not_in('Has supplemental materials for', res.body) def test_public_project_pending_preprint_post_moderation(self): self.preprint.machine_state = 'pending' provider = PreprintProviderFactory(reviews_workflow='post-moderation') self.preprint.provider = provider self.preprint.save() url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is publicly available and searchable but is subject to removal by a moderator.', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is publicly available and searchable but is subject to removal by a moderator.', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is publicly available and searchable but is subject to removal by a moderator.', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_in('on {}'.format(self.preprint.provider.name), res.body) assert_not_in('Pending\n', res.body) assert_not_in('This preprint is publicly available and searchable but is subject to removal by a moderator.', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_in('on {}'.format(self.preprint.provider.name), res.body) assert_not_in('Pending\n', res.body) assert_not_in('This preprint is publicly available and searchable but is subject to removal by a moderator.', res.body) def test_implicit_admins_can_see_project_status(self): project = ProjectFactory(creator=self.admin) component = NodeFactory(creator=self.admin, parent=project) project.add_contributor(self.write_contrib, ['read', 'write', 'admin']) project.save() preprint = PreprintFactory(creator=self.admin, filename='mgla.pdf', provider=self.provider_one, subjects=[[self.subject_one._id]], project=component, is_published=True) preprint.machine_state = 'pending' provider = PreprintProviderFactory(reviews_workflow='post-moderation') preprint.provider = provider preprint.save() url = component.web_url_for('view_project') res = self.app.get(url, auth=self.write_contrib.auth) assert_in('{}'.format(preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is publicly available and searchable but is subject to removal by a moderator.', res.body) def test_public_project_pending_preprint_pre_moderation(self): self.preprint.machine_state = 'pending' provider = PreprintProviderFactory(reviews_workflow='pre-moderation') self.preprint.provider = provider self.preprint.save() url = self.project_one.web_url_for('view_project') # Admin - preprint res = self.app.get(url, auth=self.admin.auth) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is not publicly available or searchable until approved by a moderator.', res.body) # Write - preprint res = self.app.get(url, auth=self.write_contrib.auth) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is not publicly available or searchable until approved by a moderator.', res.body) # Read - preprint res = self.app.get(url, auth=self.read_contrib.auth) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_in('Pending\n', res.body) assert_in('This preprint is not publicly available or searchable until approved by a moderator.', res.body) # Noncontrib - preprint res = self.app.get(url, auth=self.non_contrib.auth) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_not_in('Pending\n', res.body) assert_not_in('This preprint is not publicly available or searchable until approved by a moderator.', res.body) # Unauthenticated - preprint res = self.app.get(url) assert_in('{}'.format(self.preprint.provider.name), res.body) assert_not_in('Pending\n', res.body) assert_not_in('This preprint is not publicly available or searchable until approved by a moderator.', res.body)
def preprint_with_contributor(self, mergee): preprint = PreprintFactory() preprint.add_contributor(mergee, permissions='write', visible=False, save=True) preprint.save() return preprint
class TestPreprintProvidersList(ApiTestCase): def setUp(self): super(TestPreprintProvidersList, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/files/'.format(API_BASE, self.preprint._id) self.user_two = AuthUserFactory() def test_published_preprint_files(self): # Unauthenticated res = self.app.get(self.url) assert res.status_code == 200 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_unpublished_preprint_files(self): self.preprint.is_published = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_private_preprint_files(self): self.preprint.is_public = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_abandoned_preprint_files(self): self.preprint.machine_state = DefaultStates.INITIAL.value self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_orphaned_preprint_files(self): self.preprint.primary_file = None self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_deleted_preprint_files(self): self.preprint.deleted = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 404 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404 def test_withdrawn_preprint_files(self): self.preprint.date_withdrawn = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 403 def test_return_published_files_logged_out(self): res = self.app.get(self.url) assert_equal(res.status_code, 200) assert_equal(len(res.json['data']), 1) assert_equal( res.json['data'][0]['attributes']['provider'], 'osfstorage' ) def test_does_not_return_storage_addons_link(self): res = self.app.get(self.url, auth=self.user.auth) assert_not_in('storage_addons', res.json['data'][0]['links']) def test_does_not_return_new_folder_link(self): res = self.app.get(self.url, auth=self.user.auth) assert_not_in('new_folder', res.json['data'][0]['links']) def test_returns_provider_data(self): res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) assert_true(isinstance(res.json['data'], list)) assert_equal(res.content_type, 'application/vnd.api+json') data = res.json['data'][0] assert_equal(data['attributes']['kind'], 'folder') assert_equal(data['attributes']['name'], 'osfstorage') assert_equal(data['attributes']['provider'], 'osfstorage') assert_equal(data['attributes']['preprint'], self.preprint._id) assert_equal(data['attributes']['path'], '/') assert_equal(data['attributes']['node'], None) def test_osfstorage_file_data_not_found(self): res = self.app.get( '{}osfstorage/{}'.format(self.url, self.preprint.primary_file._id), auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404) def test_returns_osfstorage_folder_version_two(self): res = self.app.get( '{}osfstorage/'.format(self.url), auth=self.user.auth) assert_equal(res.status_code, 200) def test_returns_osf_storage_folder_version_two_point_two(self): res = self.app.get( '{}osfstorage/?version=2.2'.format(self.url), auth=self.user.auth) assert_equal(res.status_code, 200) def test_osfstorage_folder_data_not_found(self): fobj = self.preprint.root_folder.append_folder('NewFolder') fobj.save() res = self.app.get( '{}osfstorage/{}'.format(self.url, fobj._id), auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404)
class TestRemoveContributor(AdminTestCase): def setUp(self): super(TestRemoveContributor, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.user_2 = AuthUserFactory() self.preprint.add_contributor(self.user_2) self.preprint.save() self.view = views.PreprintRemoveContributorView self.request = RequestFactory().post('/fake_path') self.url = reverse('preprints:remove_user', kwargs={'guid': self.preprint._id, 'user_id': self.user._id}) def test_get_object(self): view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user._id) preprint, user = view.get_object() assert isinstance(preprint, Preprint) assert isinstance(user, OSFUser) @mock.patch('admin.preprints.views.Preprint.remove_contributor') def test_remove_contributor(self, mock_remove_contributor): user_id = self.user_2._id preprint_id = self.preprint._id view = setup_log_view(self.view(), self.request, guid=preprint_id, user_id=user_id) view.delete(self.request) mock_remove_contributor.assert_called_with(self.user_2, None, log=False) def test_integration_remove_contributor(self): assert self.user_2 in self.preprint.contributors view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2._id) count = AdminLogEntry.objects.count() view.delete(self.request) assert self.user_2 not in self.preprint.contributors assert AdminLogEntry.objects.count() == count + 1 def test_do_not_remove_last_admin(self): assert len(list(self.preprint.get_admin_contributors(self.preprint.contributors))) == 1 view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user._id) count = AdminLogEntry.objects.count() view.delete(self.request) self.preprint.reload() # Reloads instance to show that nothing was removed assert len(list(self.preprint.contributors)) == 2 assert len(list(self.preprint.get_admin_contributors(self.preprint.contributors))) == 1 assert AdminLogEntry.objects.count() == count def test_no_log(self): view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2._id) view.delete(self.request) assert self.preprint.logs.latest().action != PreprintLog.CONTRIB_REMOVED def test_no_user_permissions_raises_error(self): request = RequestFactory().get(self.url) request.user = self.user with pytest.raises(PermissionDenied): self.view.as_view()(request, guid=self.preprint._id, user_id=self.user) def test_correct_view_permissions(self): change_permission = Permission.objects.get(codename='change_preprint') view_permission = Permission.objects.get(codename='view_preprint') self.user.user_permissions.add(change_permission) self.user.user_permissions.add(view_permission) self.user.save() request = RequestFactory().get(self.url) request.user = self.user response = self.view.as_view()(request, guid=self.preprint._id, user_id=self.user._id) assert response.status_code == 200
class TestPreprintCitationContentMLA(ApiTestCase): def setUp(self): super(TestPreprintCitationContentMLA, self).setUp() self.admin_contributor = AuthUserFactory() self.published_preprint = PreprintFactory( creator=self.admin_contributor) self.published_preprint.title = 'My Preprint' self.published_preprint.save() self.admin_contributor.given_name = 'Grapes' self.admin_contributor.middle_names = ' Coffee Beans ' self.admin_contributor.family_name = 'McGee' self.admin_contributor.save() self.published_preprint_url = '/{}preprints/{}/citation/modern-language-association/'.format( API_BASE, self.published_preprint._id) self.second_contrib = AuthUserFactory() self.second_contrib.given_name = 'Darla' self.second_contrib.middle_names = 'Texas Toast' self.second_contrib.family_name = 'Jenkins' self.second_contrib.suffix = 'Junior' self.second_contrib.save() self.third_contrib = AuthUserFactory() self.third_contrib.given_name = 'Lilith' self.third_contrib.middle_names = 'Radar' self.third_contrib.family_name = 'Schematics' self.third_contrib.save() def test_not_published(self): self.published_preprint.date_published = None self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, u'McGee, Grapes C. B. “{}.” {}, {} Web.'.format( self.published_preprint.title, self.published_preprint.provider.name, 'n.d.') ) def test_one_author(self): res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) # test_suffix self.admin_contributor.suffix = 'Junior' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) # test_no_middle_names self.admin_contributor.suffix = '' self.admin_contributor.middle_names = '' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_citation_no_repeated_periods(self): self.published_preprint.title = 'A Study of Coffee.' self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_citation_osf_provider(self): self.published_preprint.title = 'A Study of Coffee.' self.published_preprint.save() self.published_preprint.provider.name = 'Open Science Framework' self.published_preprint.provider.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_two_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) def test_three_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association')) # first name suffix self.admin_contributor.suffix = 'Jr.' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, render_citation(self.published_preprint, 'modern-language-association'))
class TestPreprintCitationContentAPA(ApiTestCase): def setUp(self): super(TestPreprintCitationContentAPA, self).setUp() self.admin_contributor = AuthUserFactory() self.published_preprint = PreprintFactory( title='A Study of Coffee', creator=self.admin_contributor ) self.published_preprint.save() self.admin_contributor.given_name = 'Grapes' self.admin_contributor.middle_names = ' Coffee Beans ' self.admin_contributor.family_name = 'McGee' self.admin_contributor.save() self.second_contrib = AuthUserFactory() self.second_contrib.given_name = 'Darla' self.second_contrib.middle_names = 'Texas Toast' self.second_contrib.family_name = 'Jenkins' self.second_contrib.suffix = 'Junior' self.second_contrib.save() self.third_contrib = AuthUserFactory() self.third_contrib.given_name = 'Lilith' self.third_contrib.middle_names = 'Radar' self.third_contrib.family_name = 'Schematics' self.third_contrib.save() self.published_preprint_url = '/{}preprints/{}/citation/apa/'.format( API_BASE, self.published_preprint._id) def test_not_published(self): self.published_preprint.date_published = None self.published_preprint.save() self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, u'McGee, G. C. B., & Jenkins, D. T. T., Junior. ({}). {}. {}'.format( 'n.d.', self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_one_author(self): res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) # test_suffix self.admin_contributor.suffix = 'Junior' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Junior. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) # test_no_middle_names self.admin_contributor.suffix = '' self.admin_contributor.middle_names = '' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_two_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., & Jenkins, D. T. T., Junior. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_three_authors_and_title_with_period(self): self.published_preprint.title = 'This Title Ends in a Period.' self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Jenkins, D. T. T., Junior, & Schematics, L. R. ({}). {}. {}'.format( date, 'This Title Ends in a Period', 'https://doi.org/' + self.published_preprint.article_doi) ) def test_seven_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) for i in range(1, 5): new_user = AuthUserFactory() new_user.given_name = 'James' new_user.family_name = 'Taylor{}'.format(i) new_user.save() self.published_preprint.add_contributor(new_user) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Jenkins, D. T. T., Junior, Schematics, L. R., Taylor1, J., Taylor2, J., Taylor3, J., & Taylor4, J. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) ) def test_eight_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) for i in range(1, 6): new_user = AuthUserFactory() new_user.given_name = 'James' new_user.family_name = 'Taylor{}'.format(i) new_user.save() self.published_preprint.add_contributor(new_user) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published.strftime('%Y, %B %-d') assert_equal(citation, u'McGee, G. C. B., Jenkins, D. T. T., Junior, Schematics, L. R., Taylor1, J., Taylor2, J., Taylor3, J., … Taylor5, J. ({}). {}. {}'.format( date, self.published_preprint.title, 'https://doi.org/' + self.published_preprint.article_doi ) )
class TestPreprintCitationContentChicago(ApiTestCase): def setUp(self): super(TestPreprintCitationContentChicago, self).setUp() self.admin_contributor = AuthUserFactory() self.published_preprint = PreprintFactory( title='A Study of Coffee', creator=self.admin_contributor) self.published_preprint.save() self.admin_contributor.given_name = 'Grapes' self.admin_contributor.middle_names = ' Coffee Beans ' self.admin_contributor.family_name = 'McGee' self.admin_contributor.save() self.published_preprint_url = '/{}preprints/{}/citation/chicago-author-date/'.format( API_BASE, self.published_preprint._id) self.second_contrib = AuthUserFactory() self.second_contrib.given_name = 'Darla' self.second_contrib.middle_names = 'Texas Toast' self.second_contrib.family_name = 'Jenkins' self.second_contrib.suffix = 'Junior' self.second_contrib.save() self.third_contrib = AuthUserFactory() self.third_contrib.given_name = 'Lilith' self.third_contrib.middle_names = 'Radar' self.third_contrib.family_name = 'Schematics' self.third_contrib.save() def test_not_published(self): self.published_preprint.date_published = None self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] assert_equal(citation, u'McGee, Grapes C. B. {} “{}.” {}. {}.'.format( 'n.d.', self.published_preprint.title, self.published_preprint.provider.name, 'doi:' + self.published_preprint.article_doi, ) ) def test_one_author(self): res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) # test_suffix self.admin_contributor.suffix = 'Junior' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., Junior. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) # test_no_middle_names self.admin_contributor.suffix = '' self.admin_contributor.middle_names = '' self.admin_contributor.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) def test_two_authors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., and Darla T. T. Jenkins, Junior. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) ) def test_three_authors_and_title_with_period(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) self.published_preprint.title = 'This Preprint ends in a Period.' self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., Darla T. T. Jenkins, Junior, and Lilith R. Schematics. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), 'This Preprint Ends in a Period', self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi) ) def test_eleven_contributors(self): self.published_preprint.add_contributor(self.second_contrib) self.published_preprint.add_contributor(self.third_contrib) for i in range(1, 9): new_user = AuthUserFactory() new_user.given_name = 'James' new_user.family_name = 'Taylor{}'.format(i) new_user.save() self.published_preprint.add_contributor(new_user) self.published_preprint.save() res = self.app.get(self.published_preprint_url) assert_equal(res.status_code, 200) citation = res.json['data']['attributes']['citation'] date = self.published_preprint.date_published assert_equal(citation, u'McGee, Grapes C. B., Darla T. T. Jenkins, Junior, Lilith R. Schematics, James Taylor1, James Taylor2, James Taylor3, James Taylor4, et al. {}. “{}.” {}. {}. {}.'.format( date.strftime('%Y'), self.published_preprint.title, self.published_preprint.provider.name, date.strftime('%B %-d'), 'doi:' + self.published_preprint.article_doi ) )
def preprint_with_merger_and_mergee(self, mergee, merger): preprint = PreprintFactory() preprint.add_contributor(mergee) preprint.add_contributor(merger) preprint.save() return preprint
class TestRemoveContributor(AdminTestCase): def setUp(self): super(TestRemoveContributor, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.user_2 = AuthUserFactory() self.preprint.add_contributor(self.user_2) self.preprint.save() self.view = views.PreprintRemoveContributorView self.request = RequestFactory().post('/fake_path') self.url = reverse('preprints:remove_user', kwargs={'guid': self.preprint._id, 'user_id': self.user._id}) def test_get_object(self): view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user._id) preprint, user = view.get_object() assert isinstance(preprint, Preprint) assert isinstance(user, OSFUser) @mock.patch('admin.preprints.views.Preprint.remove_contributor') def test_remove_contributor(self, mock_remove_contributor): user_id = self.user_2._id preprint_id = self.preprint._id view = setup_log_view(self.view(), self.request, guid=preprint_id, user_id=user_id) view.delete(self.request) mock_remove_contributor.assert_called_with(self.user_2, None, log=False) def test_integration_remove_contributor(self): assert self.user_2 in self.preprint.contributors view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2._id) count = AdminLogEntry.objects.count() view.delete(self.request) assert self.user_2 not in self.preprint.contributors assert AdminLogEntry.objects.count() == count + 1 def test_do_not_remove_last_admin(self): assert len(list(self.preprint.get_admin_contributors(self.preprint.contributors))) == 1 view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user._id) count = AdminLogEntry.objects.count() view.delete(self.request) self.preprint.reload() # Reloads instance to show that nothing was removed assert len(list(self.preprint.contributors)) == 2 assert len(list(self.preprint.get_admin_contributors(self.preprint.contributors))) == 1 assert AdminLogEntry.objects.count() == count def test_no_log(self): view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2._id) view.delete(self.request) assert self.preprint.logs.latest().action != PreprintLog.CONTRIB_REMOVED def test_no_user_permissions_raises_error(self): request = RequestFactory().get(self.url) request.user = self.user with pytest.raises(PermissionDenied): self.view.as_view()(request, guid=self.preprint._id, user_id=self.user) def test_correct_view_permissions(self): change_permission = Permission.objects.get(codename='change_preprint') view_permission = Permission.objects.get(codename='view_preprint') self.user.user_permissions.add(change_permission) self.user.user_permissions.add(view_permission) self.user.save() request = RequestFactory().get(self.url) request.user = self.user response = self.view.as_view()(request, guid=self.preprint._id, user_id=self.user._id) assert response.status_code == 200
class TestRemoveContributor(AdminTestCase): def setUp(self): super(TestRemoveContributor, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.user_2 = AuthUserFactory() self.preprint.add_contributor(self.user_2) self.preprint.save() self.view = views.PreprintRemoveContributorView self.request = RequestFactory().post('/fake_path') self.url = reverse('preprints:remove-user', kwargs={ 'guid': self.preprint._id, 'user_id': self.user._id }) def test_remove_contributor(self): user_id = self.user_2.id preprint_id = self.preprint._id view = setup_log_view(self.view(), self.request, guid=preprint_id, user_id=user_id) view.post(self.request) assert not self.preprint.contributors.filter(id=user_id) def test_integration_remove_contributor(self): assert self.user_2 in self.preprint.contributors view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2.id) count = AdminLogEntry.objects.count() view.post(self.request) assert self.user_2 not in self.preprint.contributors assert AdminLogEntry.objects.count() == count + 1 def test_do_not_remove_last_admin(self): assert len( list( self.preprint.get_admin_contributors( self.preprint.contributors))) == 1 view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user.id) count = AdminLogEntry.objects.count() patch_messages(self.request) view.post(self.request) self.preprint.reload( ) # Reloads instance to show that nothing was removed assert len(list(self.preprint.contributors)) == 2 assert len( list( self.preprint.get_admin_contributors( self.preprint.contributors))) == 1 assert AdminLogEntry.objects.count() == count def test_no_log(self): view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2.id) view.post(self.request) assert self.preprint.logs.latest( ).action != PreprintLog.CONTRIB_REMOVED
class TestPreprintProvidersList(ApiTestCase): def setUp(self): super(TestPreprintProvidersList, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/files/'.format(API_BASE, self.preprint._id) self.user_two = AuthUserFactory() def test_published_preprint_files(self): # Unauthenticated res = self.app.get(self.url) assert res.status_code == 200 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_unpublished_preprint_files(self): self.preprint.is_published = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_private_preprint_files(self): self.preprint.is_public = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_abandoned_preprint_files(self): self.preprint.machine_state = DefaultStates.INITIAL.value self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_orphaned_preprint_files(self): self.preprint.primary_file = None self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_deleted_preprint_files(self): self.preprint.deleted = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 404 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404 def test_withdrawn_preprint_files(self): self.preprint.date_withdrawn = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 403 def test_return_published_files_logged_out(self): res = self.app.get(self.url) assert_equal(res.status_code, 200) assert_equal(len(res.json['data']), 1) assert_equal(res.json['data'][0]['attributes']['provider'], 'osfstorage') def test_does_not_return_storage_addons_link(self): res = self.app.get(self.url, auth=self.user.auth) assert_not_in('storage_addons', res.json['data'][0]['links']) def test_does_not_return_new_folder_link(self): res = self.app.get(self.url, auth=self.user.auth) assert_not_in('new_folder', res.json['data'][0]['links']) def test_returns_provider_data(self): res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) assert_true(isinstance(res.json['data'], list)) assert_equal(res.content_type, 'application/vnd.api+json') data = res.json['data'][0] assert_equal(data['attributes']['kind'], 'folder') assert_equal(data['attributes']['name'], 'osfstorage') assert_equal(data['attributes']['provider'], 'osfstorage') assert_equal(data['attributes']['preprint'], self.preprint._id) assert_equal(data['attributes']['path'], '/') assert_equal(data['attributes']['node'], None) def test_osfstorage_file_data_not_found(self): res = self.app.get('{}osfstorage/{}'.format( self.url, self.preprint.primary_file._id), auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404) def test_returns_osfstorage_folder_version_two(self): res = self.app.get('{}osfstorage/'.format(self.url), auth=self.user.auth) assert_equal(res.status_code, 200) def test_returns_osf_storage_folder_version_two_point_two(self): res = self.app.get('{}osfstorage/?version=2.2'.format(self.url), auth=self.user.auth) assert_equal(res.status_code, 200) def test_osfstorage_folder_data_not_found(self): fobj = self.preprint.root_folder.append_folder('NewFolder') fobj.save() res = self.app.get('{}osfstorage/{}'.format(self.url, fobj._id), auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404)
def preprint(self, submitter, preprint_contributor, moderator): pp = PreprintFactory(creator=submitter) pp.add_contributor(preprint_contributor, save=True) pp.provider.get_group('moderator').user_set.add(moderator) return pp
class TestPreprintFilesList(ApiTestCase): def setUp(self): super(TestPreprintFilesList, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/files/osfstorage/'.format( API_BASE, self.preprint._id) self.user_two = AuthUserFactory() def test_published_preprint_files(self): # Unauthenticated res = self.app.get(self.url) assert res.status_code == 200 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_unpublished_preprint_files(self): self.preprint.is_published = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_private_preprint_files(self): self.preprint.is_public = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_abandoned_preprint_files(self): self.preprint.machine_state = DefaultStates.INITIAL.value self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_orphaned_preprint_files(self): self.preprint.primary_file = None self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_deleted_preprint_files(self): self.preprint.deleted = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 404 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404 def test_withdrawn_preprint_files(self): self.preprint.date_withdrawn = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, WRITE, save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 403 def test_not_just_primary_file_returned(self): filename = 'my second file' second_file = OsfStorageFile.create( target_object_id=self.preprint.id, target_content_type=ContentType.objects.get_for_model( self.preprint), path='/{}'.format(filename), name=filename, materialized_path='/{}'.format(filename)) second_file.save() from addons.osfstorage import settings as osfstorage_settings second_file.create_version( self.user, { 'object': '06d80e', 'service': 'cloud', osfstorage_settings.WATERBUTLER_RESOURCE: 'osf', }, { 'size': 1337, 'contentType': 'img/png' }).save() second_file.parent = self.preprint.root_folder second_file.save() assert len(self.preprint.files.all()) == 2 res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 data = res.json['data'] assert len(data) == 2 assert data[0]['id'] == self.preprint.primary_file._id def test_nested_file_as_primary_file_is_returned(self): # Primary file can be any file nested somewhere under the preprint's root folder. subfolder = self.preprint.root_folder.append_folder('subfolder') subfolder.save() primary_file = self.preprint.primary_file primary_file.move_under(subfolder) primary_file.save() assert_equal(subfolder.children[0], primary_file) assert_equal(primary_file.parent, subfolder) res = self.app.get(self.url, auth=self.user.auth) assert len(res.json['data']) == 1 data = res.json['data'][0] assert data['id'] == subfolder._id assert data['attributes']['kind'] == 'folder' assert data['attributes']['path'] == '/{}/'.format(subfolder._id) assert data['attributes']['materialized_path'] == '/{}/'.format( subfolder.name) def test_cannot_access_other_addons(self): url = '/{}preprints/{}/files/github/'.format(API_BASE, self.preprint._id) res = self.app.get(url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404
class TestPreprintFilesList(ApiTestCase): def setUp(self): super(TestPreprintFilesList, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.url = '/{}preprints/{}/files/osfstorage/'.format(API_BASE, self.preprint._id) self.user_two = AuthUserFactory() def test_published_preprint_files(self): # Unauthenticated res = self.app.get(self.url) assert res.status_code == 200 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_unpublished_preprint_files(self): self.preprint.is_published = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_private_preprint_files(self): self.preprint.is_public = False self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_abandoned_preprint_files(self): self.preprint.machine_state = DefaultStates.INITIAL.value self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_orphaned_preprint_files(self): self.preprint.primary_file = None self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth) assert res.status_code == 200 # Admin contrib res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 def test_deleted_preprint_files(self): self.preprint.deleted = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 404 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 404 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404 def test_withdrawn_preprint_files(self): self.preprint.date_withdrawn = timezone.now() self.preprint.save() # Unauthenticated res = self.app.get(self.url, expect_errors=True) assert res.status_code == 401 # Noncontrib res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Write contributor self.preprint.add_contributor(self.user_two, 'write', save=True) res = self.app.get(self.url, auth=self.user_two.auth, expect_errors=True) assert res.status_code == 403 # Admin contrib res = self.app.get(self.url, auth=self.user.auth, expect_errors=True) assert res.status_code == 403 def test_not_just_primary_file_returned(self): filename = 'my second file' second_file = OsfStorageFile.create( target_object_id=self.preprint.id, target_content_type=ContentType.objects.get_for_model(self.preprint), path='/{}'.format(filename), name=filename, materialized_path='/{}'.format(filename)) second_file.save() from addons.osfstorage import settings as osfstorage_settings second_file.create_version(self.user, { 'object': '06d80e', 'service': 'cloud', osfstorage_settings.WATERBUTLER_RESOURCE: 'osf', }, { 'size': 1337, 'contentType': 'img/png' }).save() second_file.parent = self.preprint.root_folder second_file.save() assert len(self.preprint.files.all()) == 2 res = self.app.get(self.url, auth=self.user.auth) assert res.status_code == 200 data = res.json['data'] assert len(data) == 2 assert data[0]['id'] == self.preprint.primary_file._id def test_nested_file_as_primary_file_is_returned(self): # Primary file can be any file nested somewhere under the preprint's root folder. subfolder = self.preprint.root_folder.append_folder('subfolder') subfolder.save() primary_file = self.preprint.primary_file primary_file.move_under(subfolder) primary_file.save() assert_equal(subfolder.children[0], primary_file) assert_equal(primary_file.parent, subfolder) res = self.app.get(self.url, auth=self.user.auth) assert len(res.json['data']) == 1 data = res.json['data'][0] assert data['id'] == subfolder._id assert data['attributes']['kind'] == 'folder' assert data['attributes']['path'] == '/{}/'.format(subfolder._id) assert data['attributes']['materialized_path'] == '/{}/'.format(subfolder.name) def test_cannot_access_other_addons(self): url = '/{}preprints/{}/files/github/'.format(API_BASE, self.preprint._id) res = self.app.get(url, auth=self.user.auth, expect_errors=True) assert res.status_code == 404
class TestCiteprocpyMLA(OsfTestCase): MLA_DATE_FORMAT = '%-d {month} %Y' # MLA month abreviations here # http://www.pomfret.ctschool.net/computer_classes/documents/mla-abbreviationsofmonths.pdf MLA_MONTH_MAP = { 1: 'Jan.', 2: 'Feb.', 3: 'Mar.', 4: 'Apr.', 5: 'May', 6: 'June', 7: 'July', 8: 'Aug.', 9: 'Sept.', 10: 'Oct.', 11: 'Nov.', 12: 'Dec.', } def setUp(self): super(TestCiteprocpyMLA, self).setUp() self.user = UserFactory(fullname='John Tordoff') self.second_contrib = UserFactory(fullname='Carson Wentz') self.third_contrib = UserFactory(fullname='Nick Foles') self.preprint = PreprintFactory(creator=self.user, title='My Preprint') date = timezone.now().date() self.formated_date = date.strftime( self.MLA_DATE_FORMAT).format(month=self.MLA_MONTH_MAP[date.month]) def test_render_citations_mla_one_author(self): citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John. “{}.” {}, {}. Web.'.format( self.preprint.title, self.preprint.provider.name, self.formated_date)) # test_suffix self.user.suffix = 'Junior' self.user.save() citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John, Junior. “{}.” {}, {}. Web.'.format( self.preprint.title, self.preprint.provider.name, self.formated_date)) # test_no_middle_names self.user.suffix = '' self.user.middle_names = '' self.user.save() citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John. “{}.” {}, {}. Web.'.format( self.preprint.title, self.preprint.provider.name, self.formated_date)) def test_citation_no_repeated_periods(self): self.preprint.title = 'A Study of Coffee.' self.preprint.save() citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John. “{}” {}, {}. Web.'.format( self.preprint.title, self.preprint.provider.name, self.formated_date)) def test_citation_osf_provider(self): self.preprint.title = 'A Study of Coffee.' self.preprint.save() self.preprint.provider.name = 'Open Science Framework' self.preprint.provider.save() citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John. “{}” {}, {}. Web.'.format(self.preprint.title, 'OSF Preprints', self.formated_date)) def test_two_authors(self): self.preprint.add_contributor(self.second_contrib) self.preprint.save() citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John, and Carson Wentz. “{}.” {}, {}. Web.'.format( self.preprint.title, self.preprint.provider.name, self.formated_date)) def test_three_authors(self): self.preprint.add_contributor(self.second_contrib) self.preprint.add_contributor(self.third_contrib) self.preprint.save() citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John, et al. “{}.” {}, {}. Web.'.format( self.preprint.title, self.preprint.provider.name, self.formated_date)) # first name suffix self.user.suffix = 'Jr.' self.user.save() citation = render_citation(self.preprint, 'modern-language-association') assert_equal( citation, u'Tordoff, John, Jr., et al. “{}.” {}, {}. Web.'.format( self.preprint.title, self.preprint.provider.name, self.formated_date))