def create_fake_project(creator, n_users, privacy, n_components, name, n_tags, presentation_name, is_registration): auth = Auth(user=creator) project_title = name if name else fake.science_sentence() if not is_registration: project = ProjectFactory(title=project_title, description=fake.science_paragraph(), creator=creator) else: project = RegistrationFactory(title=project_title, description=fake.science_paragraph(), creator=creator) project.set_privacy(privacy) for _ in range(n_users): contrib = create_fake_user() project.add_contributor(contrib, auth=auth) if isinstance(n_components, int): for _ in range(n_components): NodeFactory(project=project, title=fake.science_sentence(), description=fake.science_paragraph(), creator=creator) elif isinstance(n_components, list): render_generations_from_node_structure_list(project, creator, n_components) for _ in range(n_tags): project.add_tag(fake.science_word(), auth=auth) if presentation_name is not None: project.add_tag(presentation_name, auth=auth) project.add_tag('poster', auth=auth) project.save() logger.info('Created project: {0}'.format(project.title)) return project
def test_read_permission_contributor_can_comment(self): project = ProjectFactory() user = UserFactory() project.set_privacy('private') project.add_contributor(user, permissions=[permissions.READ]) project.save() assert_true(project.can_comment(Auth(user=user)))
def test_read_permission_contributor_can_comment(self): project = ProjectFactory() user = UserFactory() project.set_privacy('private') project.add_contributor(user, permissions=[permissions.READ]) project.save() assert_true(project.can_comment(Auth(user=user)))
class TestPiwik(OsfTestCase): def setUp(self): super(TestPiwik, self).setUp() self.users = [ AuthUserFactory() for _ in range(3) ] self.consolidate_auth = Auth(user=self.users[0]) self.project = ProjectFactory(creator=self.users[0], is_public=True) self.project.add_contributor(contributor=self.users[1]) self.project.save() def test_contains_iframe_and_src(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[0].auth ).maybe_follow() assert_in('iframe', res) assert_in('src', res) assert_in(settings.PIWIK_HOST, res) def test_anonymous_no_token(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[2].auth ).maybe_follow() assert_in('token_auth=anonymous', res) def test_contributor_token(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[1].auth ).maybe_follow() assert_in(self.users[1].piwik_token, res) def test_no_user_token(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key) ).maybe_follow() assert_in('token_auth=anonymous', res) def test_private_alert(self): self.project.set_privacy('private', auth=self.consolidate_auth) self.project.save() res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[0].auth ).maybe_follow().normal_body assert_in( 'Usage statistics are collected only for public resources.', res )
class TestPiwik(OsfTestCase): def setUp(self): super(TestPiwik, self).setUp() self.users = [ AuthUserFactory() for _ in range(3) ] self.consolidate_auth = Auth(user=self.users[0]) self.project = ProjectFactory(creator=self.users[0], is_public=True) self.project.add_contributor(contributor=self.users[1]) self.project.save() def test_contains_iframe_and_src(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[0].auth ).maybe_follow() assert_in('iframe', res) assert_in('src', res) assert_in(settings.PIWIK_HOST, res) def test_anonymous_no_token(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[2].auth ).maybe_follow() assert_in('token_auth=anonymous', res) def test_contributor_token(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[1].auth ).maybe_follow() assert_in(self.users[1].piwik_token, res) def test_no_user_token(self): res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key) ).maybe_follow() assert_in('token_auth=anonymous', res) def test_private_alert(self): self.project.set_privacy('private', auth=self.consolidate_auth) self.project.save() res = self.app.get( '/{0}/statistics/'.format(self.project._primary_key), auth=self.users[0].auth ).maybe_follow().normal_body assert_in( 'Usage statistics are collected only for public resources.', res )
class TestProject(SearchTestCase): def setUp(self): super(TestProject, self).setUp() search.delete_index(elastic_search.INDEX) search.create_index(elastic_search.INDEX) self.user = UserFactory(fullname='John Deacon') self.project = ProjectFactory(title='Red Special', creator=self.user) def test_new_project_private(self): # Verify that a private project is not present in Elastic Search. docs = query(self.project.title)['results'] assert_equal(len(docs), 0) def test_make_public(self): # Make project public, and verify that it is present in Elastic # Search. self.project.set_privacy('public') docs = query(self.project.title)['results'] assert_equal(len(docs), 1)
class TestProject(SearchTestCase): def setUp(self): super(TestProject, self).setUp() search.delete_index(elastic_search.INDEX) search.create_index(elastic_search.INDEX) self.user = UserFactory(fullname='John Deacon') self.project = ProjectFactory(title='Red Special', creator=self.user) def test_new_project_private(self): # Verify that a private project is not present in Elastic Search. docs = query(self.project.title)['results'] assert_equal(len(docs), 0) def test_make_public(self): # Make project public, and verify that it is present in Elastic # Search. self.project.set_privacy('public') docs = query(self.project.title)['results'] assert_equal(len(docs), 1)
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags, presentation_name): auth = Auth(user=creator) project_title = name if name else fake.science_sentence() project = ProjectFactory(title=project_title, description=fake.science_paragraph(), creator=creator) project.set_privacy(privacy) for _ in range(n_users): contrib = create_fake_user() project.add_contributor(contrib, auth=auth) for _ in range(n_components): NodeFactory(project=project, title=fake.science_sentence(), description=fake.science_paragraph(), creator=creator) for _ in range(n_tags): project.add_tag(fake.science_word(), auth=auth) if presentation_name is not None: project.add_tag(presentation_name, auth=auth) project.add_tag('poster', auth=auth) project.save() logger.info('Created project: {0}'.format(project.title)) return project
class TestPublicNodes(SearchTestCase): def setUp(self): super(TestPublicNodes, self).setUp() self.user = UserFactory(usename='Doug Bogie') self.title = 'Red Special' self.consolidate_auth = Auth(user=self.user) self.project = ProjectFactory( title=self.title, creator=self.user, is_public=True, ) self.component = NodeFactory( parent=self.project, title=self.title, creator=self.user, is_public=True ) self.registration = ProjectFactory( title=self.title, creator=self.user, is_public=True, is_registration=True ) def test_make_private(self): """Make project public, then private, and verify that it is not present in search. """ self.project.set_privacy('private') docs = query('category:project AND ' + self.title)['results'] assert_equal(len(docs), 0) self.component.set_privacy('private') docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 0) self.registration.set_privacy('private') docs = query('category:registration AND ' + self.title)['results'] assert_equal(len(docs), 0) def test_public_parent_title(self): self.project.set_title('hello & world', self.consolidate_auth) self.project.save() docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 1) assert_equal(docs[0]['parent_title'], 'hello & world') assert_true(docs[0]['parent_url']) def test_make_parent_private(self): """Make parent of component, public, then private, and verify that the component still appears but doesn't link to the parent in search. """ self.project.set_privacy('private') docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 1) assert_equal(docs[0]['parent_title'], '-- private project --') assert_false(docs[0]['parent_url']) def test_delete_project(self): """ """ self.component.remove_node(self.consolidate_auth) docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 0) self.project.remove_node(self.consolidate_auth) docs = query('category:project AND ' + self.title)['results'] assert_equal(len(docs), 0) def test_change_title(self): """ """ title_original = self.project.title self.project.set_title( 'Blue Ordinary', self.consolidate_auth, save=True) docs = query('category:project AND ' + title_original)['results'] assert_equal(len(docs), 0) docs = query('category:project AND ' + self.project.title)['results'] assert_equal(len(docs), 1) def test_add_tags(self): tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family'] for tag in tags: docs = query('tags:"{}"'.format(tag))['results'] assert_equal(len(docs), 0) self.project.add_tag(tag, self.consolidate_auth, save=True) for tag in tags: docs = query('tags:"{}"'.format(tag))['results'] assert_equal(len(docs), 1) def test_remove_tag(self): tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family'] for tag in tags: self.project.add_tag(tag, self.consolidate_auth, save=True) self.project.remove_tag(tag, self.consolidate_auth, save=True) docs = query('tags:"{}"'.format(tag))['results'] assert_equal(len(docs), 0) def test_update_wiki(self): """Add text to a wiki page, then verify that project is found when searching for wiki text. """ wiki_content = { 'home': 'Hammer to fall', 'swag': '#YOLO' } for key, value in wiki_content.items(): docs = query(value)['results'] assert_equal(len(docs), 0) self.project.update_node_wiki( key, value, self.consolidate_auth, ) docs = query(value)['results'] assert_equal(len(docs), 1) def test_clear_wiki(self): """Add wiki text to page, then delete, then verify that project is not found when searching for wiki text. """ wiki_content = 'Hammer to fall' self.project.update_node_wiki( 'home', wiki_content, self.consolidate_auth, ) self.project.update_node_wiki('home', '', self.consolidate_auth) docs = query(wiki_content)['results'] assert_equal(len(docs), 0) def test_add_contributor(self): """Add a contributor, then verify that project is found when searching for contributor. """ user2 = UserFactory(fullname='Adam Lambert') docs = query('category:project AND "{}"'.format(user2.fullname))['results'] assert_equal(len(docs), 0) self.project.add_contributor(user2, save=True) docs = query('category:project AND "{}"'.format(user2.fullname))['results'] assert_equal(len(docs), 1) def test_remove_contributor(self): """Add and remove a contributor, then verify that project is not found when searching for contributor. """ user2 = UserFactory(fullname='Brian May') self.project.add_contributor(user2, save=True) self.project.remove_contributor(user2, self.consolidate_auth) docs = query('category:project AND "{}"'.format(user2.fullname))['results'] assert_equal(len(docs), 0) def test_hide_contributor(self): user2 = UserFactory(fullname='Brian May') self.project.add_contributor(user2) self.project.set_visible(user2, False, save=True) docs = query('category:project AND "{}"'.format(user2.fullname))['results'] assert_equal(len(docs), 0) self.project.set_visible(user2, True, save=True) docs = query('category:project AND "{}"'.format(user2.fullname))['results'] assert_equal(len(docs), 1) def test_wrong_order_search(self): title_parts = self.title.split(' ') title_parts.reverse() title_search = ' '.join(title_parts) docs = query(title_search)['results'] assert_equal(len(docs), 3) def test_tag_aggregation(self): tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family'] for tag in tags: self.project.add_tag(tag, self.consolidate_auth, save=True) docs = query(self.title)['tags'] assert len(docs) == 3 for doc in docs: assert doc['key'] in tags
class TestPublicNodes(SearchTestCase): def setUp(self): super(TestPublicNodes, self).setUp() self.user = UserFactory(usename='Doug Bogie') self.title = 'Red Special' self.consolidate_auth = Auth(user=self.user) self.project = ProjectFactory( title=self.title, creator=self.user, is_public=True, ) self.component = NodeFactory(parent=self.project, title=self.title, creator=self.user, is_public=True) self.registration = ProjectFactory(title=self.title, creator=self.user, is_public=True, is_registration=True) def test_make_private(self): # Make project public, then private, and verify that it is not present # in search. self.project.set_privacy('private') docs = query('category:project AND ' + self.title)['results'] assert_equal(len(docs), 0) self.component.set_privacy('private') docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 0) def test_public_parent_title(self): self.project.set_title('hello & world', self.consolidate_auth) self.project.save() docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 1) assert_equal(docs[0]['parent_title'], 'hello & world') assert_true(docs[0]['parent_url']) def test_make_parent_private(self): # Make parent of component, public, then private, and verify that the # component still appears but doesn't link to the parent in search. self.project.set_privacy('private') docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 1) assert_equal(docs[0]['parent_title'], '-- private project --') assert_false(docs[0]['parent_url']) def test_delete_project(self): self.component.remove_node(self.consolidate_auth) docs = query('category:component AND ' + self.title)['results'] assert_equal(len(docs), 0) self.project.remove_node(self.consolidate_auth) docs = query('category:project AND ' + self.title)['results'] assert_equal(len(docs), 0) def test_change_title(self): title_original = self.project.title self.project.set_title('Blue Ordinary', self.consolidate_auth, save=True) docs = query('category:project AND ' + title_original)['results'] assert_equal(len(docs), 0) docs = query('category:project AND ' + self.project.title)['results'] assert_equal(len(docs), 1) def test_add_tags(self): tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family'] for tag in tags: docs = query('tags:"{}"'.format(tag))['results'] assert_equal(len(docs), 0) self.project.add_tag(tag, self.consolidate_auth, save=True) for tag in tags: docs = query('tags:"{}"'.format(tag))['results'] assert_equal(len(docs), 1) def test_remove_tag(self): tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family'] for tag in tags: self.project.add_tag(tag, self.consolidate_auth, save=True) self.project.remove_tag(tag, self.consolidate_auth, save=True) docs = query('tags:"{}"'.format(tag))['results'] assert_equal(len(docs), 0) def test_update_wiki(self): """Add text to a wiki page, then verify that project is found when searching for wiki text. """ wiki_content = {'home': 'Hammer to fall', 'swag': '#YOLO'} for key, value in wiki_content.items(): docs = query(value)['results'] assert_equal(len(docs), 0) self.project.update_node_wiki( key, value, self.consolidate_auth, ) docs = query(value)['results'] assert_equal(len(docs), 1) def test_clear_wiki(self): # Add wiki text to page, then delete, then verify that project is not # found when searching for wiki text. wiki_content = 'Hammer to fall' self.project.update_node_wiki( 'home', wiki_content, self.consolidate_auth, ) self.project.update_node_wiki('home', '', self.consolidate_auth) docs = query(wiki_content)['results'] assert_equal(len(docs), 0) def test_add_contributor(self): # Add a contributor, then verify that project is found when searching # for contributor. user2 = UserFactory(fullname='Adam Lambert') docs = query('category:project AND "{}"'.format( user2.fullname))['results'] assert_equal(len(docs), 0) self.project.add_contributor(user2, save=True) docs = query('category:project AND "{}"'.format( user2.fullname))['results'] assert_equal(len(docs), 1) def test_remove_contributor(self): # Add and remove a contributor, then verify that project is not found # when searching for contributor. user2 = UserFactory(fullname='Brian May') self.project.add_contributor(user2, save=True) self.project.remove_contributor(user2, self.consolidate_auth) docs = query('category:project AND "{}"'.format( user2.fullname))['results'] assert_equal(len(docs), 0) def test_hide_contributor(self): user2 = UserFactory(fullname='Brian May') self.project.add_contributor(user2) self.project.set_visible(user2, False, save=True) docs = query('category:project AND "{}"'.format( user2.fullname))['results'] assert_equal(len(docs), 0) self.project.set_visible(user2, True, save=True) docs = query('category:project AND "{}"'.format( user2.fullname))['results'] assert_equal(len(docs), 1) def test_wrong_order_search(self): title_parts = self.title.split(' ') title_parts.reverse() title_search = ' '.join(title_parts) docs = query(title_search)['results'] assert_equal(len(docs), 3) def test_tag_aggregation(self): tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family'] for tag in tags: self.project.add_tag(tag, self.consolidate_auth, save=True) docs = query(self.title)['tags'] assert len(docs) == 3 for doc in docs: assert doc['key'] in tags
class TestPublicNodes(SearchTestCase): def setUp(self): super(TestPublicNodes, self).setUp() self.user = UserFactory(usename="Doug Bogie") self.title = "Red Special" self.consolidate_auth = Auth(user=self.user) self.project = ProjectFactory(title=self.title, creator=self.user, is_public=True) self.component = NodeFactory(parent=self.project, title=self.title, creator=self.user, is_public=True) self.registration = ProjectFactory(title=self.title, creator=self.user, is_public=True, is_registration=True) def test_make_private(self): """Make project public, then private, and verify that it is not present in search. """ self.project.set_privacy("private") docs = query("category:project AND " + self.title)["results"] assert_equal(len(docs), 0) self.component.set_privacy("private") docs = query("category:component AND " + self.title)["results"] assert_equal(len(docs), 0) self.registration.set_privacy("private") docs = query("category:registration AND " + self.title)["results"] assert_equal(len(docs), 0) def test_public_parent_title(self): self.project.set_title("hello & world", self.consolidate_auth) self.project.save() docs = query("category:component AND " + self.title)["results"] assert_equal(len(docs), 1) assert_equal(docs[0]["parent_title"], "hello & world") assert_true(docs[0]["parent_url"]) def test_make_parent_private(self): """Make parent of component, public, then private, and verify that the component still appears but doesn't link to the parent in search. """ self.project.set_privacy("private") docs = query("category:component AND " + self.title)["results"] assert_equal(len(docs), 1) assert_equal(docs[0]["parent_title"], "-- private project --") assert_false(docs[0]["parent_url"]) def test_delete_project(self): """ """ self.component.remove_node(self.consolidate_auth) docs = query("category:component AND " + self.title)["results"] assert_equal(len(docs), 0) self.project.remove_node(self.consolidate_auth) docs = query("category:project AND " + self.title)["results"] assert_equal(len(docs), 0) def test_change_title(self): """ """ title_original = self.project.title self.project.set_title("Blue Ordinary", self.consolidate_auth, save=True) docs = query("category:project AND " + title_original)["results"] assert_equal(len(docs), 0) docs = query("category:project AND " + self.project.title)["results"] assert_equal(len(docs), 1) def test_add_tags(self): tags = ["stonecoldcrazy", "just a poor boy", "from-a-poor-family"] for tag in tags: docs = query('tags:"{}"'.format(tag))["results"] assert_equal(len(docs), 0) self.project.add_tag(tag, self.consolidate_auth, save=True) for tag in tags: docs = query('tags:"{}"'.format(tag))["results"] assert_equal(len(docs), 1) def test_remove_tag(self): tags = ["stonecoldcrazy", "just a poor boy", "from-a-poor-family"] for tag in tags: self.project.add_tag(tag, self.consolidate_auth, save=True) self.project.remove_tag(tag, self.consolidate_auth, save=True) docs = query('tags:"{}"'.format(tag))["results"] assert_equal(len(docs), 0) def test_update_wiki(self): """Add text to a wiki page, then verify that project is found when searching for wiki text. """ wiki_content = {"home": "Hammer to fall", "swag": "#YOLO"} for key, value in wiki_content.items(): docs = query(value)["results"] assert_equal(len(docs), 0) self.project.update_node_wiki(key, value, self.consolidate_auth) docs = query(value)["results"] assert_equal(len(docs), 1) def test_clear_wiki(self): """Add wiki text to page, then delete, then verify that project is not found when searching for wiki text. """ wiki_content = "Hammer to fall" self.project.update_node_wiki("home", wiki_content, self.consolidate_auth) self.project.update_node_wiki("home", "", self.consolidate_auth) docs = query(wiki_content)["results"] assert_equal(len(docs), 0) def test_add_contributor(self): """Add a contributor, then verify that project is found when searching for contributor. """ user2 = UserFactory(fullname="Adam Lambert") docs = query('category:project AND "{}"'.format(user2.fullname))["results"] assert_equal(len(docs), 0) self.project.add_contributor(user2, save=True) docs = query('category:project AND "{}"'.format(user2.fullname))["results"] assert_equal(len(docs), 1) def test_remove_contributor(self): """Add and remove a contributor, then verify that project is not found when searching for contributor. """ user2 = UserFactory(fullname="Brian May") self.project.add_contributor(user2, save=True) self.project.remove_contributor(user2, self.consolidate_auth) docs = query('category:project AND "{}"'.format(user2.fullname))["results"] assert_equal(len(docs), 0) def test_hide_contributor(self): user2 = UserFactory(fullname="Brian May") self.project.add_contributor(user2) self.project.set_visible(user2, False, save=True) docs = query('category:project AND "{}"'.format(user2.fullname))["results"] assert_equal(len(docs), 0) self.project.set_visible(user2, True, save=True) docs = query('category:project AND "{}"'.format(user2.fullname))["results"] assert_equal(len(docs), 1) def test_wrong_order_search(self): title_parts = self.title.split(" ") title_parts.reverse() title_search = " ".join(title_parts) docs = query(title_search)["results"] assert_equal(len(docs), 3) def test_tag_aggregation(self): tags = ["stonecoldcrazy", "just a poor boy", "from-a-poor-family"] for tag in tags: self.project.add_tag(tag, self.consolidate_auth, save=True) docs = query(self.title)["tags"] assert len(docs) == 3 for doc in docs: assert doc["key"] in tags