def test__initiate_embargo_adds_admins_on_child_nodes(self): project_admin = UserFactory() project_non_admin = UserFactory() child_admin = UserFactory() child_non_admin = UserFactory() grandchild_admin = UserFactory() project = ProjectFactory(creator=project_admin) project.add_contributor(project_non_admin, auth=Auth(project.creator), save=True) child = NodeFactory(creator=child_admin, parent=project) child.add_contributor(child_non_admin, auth=Auth(project.creator), save=True) grandchild = NodeFactory(creator=grandchild_admin, parent=child) # noqa embargo = project._initiate_embargo( project.creator, self.valid_embargo_end_date, for_existing_registration=True ) assert_in(project_admin._id, embargo.approval_state) assert_in(child_admin._id, embargo.approval_state) assert_in(grandchild_admin._id, embargo.approval_state) assert_not_in(project_non_admin._id, embargo.approval_state) assert_not_in(child_non_admin._id, embargo.approval_state)
class TestResolveGuid(OsfTestCase): def setUp(self): super(TestResolveGuid, self).setUp() self.node = NodeFactory() def test_resolve_guid(self): res_guid = self.app.get(self.node.web_url_for('node_setting', _guid=True), auth=self.node.creator.auth) res_full = self.app.get(self.node.web_url_for('node_setting'), auth=self.node.creator.auth) assert_equal(res_guid.text, res_full.text) def test_resolve_guid_no_referent(self): guid = models.Guid.load(self.node._id) guid.referent = None guid.save() res = self.app.get( self.node.web_url_for('node_setting', _guid=True), auth=self.node.creator.auth, expect_errors=True, ) assert_equal(res.status_code, 404) @mock.patch('website.project.model.Node.deep_url', None) def test_resolve_guid_no_url(self): res = self.app.get( self.node.web_url_for('node_setting', _guid=True), auth=self.node.creator.auth, expect_errors=True, ) assert_equal(res.status_code, 404)
class TestNodeInstitutionDetail(ApiTestCase): def setUp(self): super(TestNodeInstitutionDetail, self).setUp() self.institution = InstitutionFactory() self.node = NodeFactory(is_public=True) self.node.primary_institution = self.institution self.node.save() self.user = AuthUserFactory() self.node2 = NodeFactory(creator=self.user) def test_return_institution(self): url = '/{0}nodes/{1}/institution/'.format(API_BASE, self.node._id) res = self.app.get(url) assert_equal(res.status_code, 200) assert_equal(res.json['data']['attributes']['name'], self.institution.name) assert_equal(res.json['data']['id'], self.institution._id) def test_return_no_institution(self): url = '/{0}nodes/{1}/institution/'.format(API_BASE, self.node2._id) res = self.app.get( url, auth=self.user.auth, expect_errors=True ) assert_equal(res.status_code, 404)
def test_get_most_in_common_contributors(self): # project 1 (contrib 1, contrib 2, unreg_contrib 3) # - component 1 (contrib 1) # project 2 - add should show contrib 1 first (2 links), contrib 2 second (1 link) contributor_1 = AuthUserFactory() contributor_2 = AuthUserFactory() self.project.add_contributor(contributor_1, auth=self.auth) self.project.add_contributor(contributor_2, auth=self.auth) # has one unregistered contributor self.project.add_unregistered_contributor( fullname=fake.name(), email=fake.email(), auth=self.auth, ) self.project.save() component = NodeFactory(parent=self.project, creator=self.user) component.add_contributor(contributor_1, auth=self.auth) component.save() project_2 = ProjectFactory(creator=self.user) project_2.add_contributor(contributor_1, auth=self.auth) url = project_2.api_url_for('get_most_in_common_contributors') res = self.app.get(url, auth=self.user.auth) project_2.reload() res_contribs = res.json['contributors'] assert_equal(len(res.json['contributors']), 2) assert_equal(contributor_1._id, res_contribs[0]['id']) assert_equal(res_contribs[0]['n_projects_in_common'], 2) assert_equal(contributor_2._id, res_contribs[1]['id']) assert_equal(res_contribs[1]['n_projects_in_common'], 1)
def test_collect_components_deleted(self): node = NodeFactory(creator=self.project.creator, parent=self.project) node.is_deleted = True collector = rubeus.NodeFileCollector( self.project, Auth(user=UserFactory()) ) nodes = collector._collect_components(self.project, visited=[]) assert_equal(len(nodes), 0)
def test_migrate_wiki(self): node = NodeFactory() wiki_addon = node.get_addon('wiki') AddonWikiNodeSettings.remove_one(wiki_addon) assert_false(node.has_addon('wiki')) was_migrated = migrate_addons(node) assert_true(was_migrated) assert_true(node.has_addon('wiki'))
def test_migrate_osffiles(self): node = NodeFactory() osf_addon = node.get_addon('osffiles') AddonFilesNodeSettings.remove_one(osf_addon) assert_false(node.has_addon('osffiles')) was_migrated = migrate_addons(node) assert_true(was_migrated) assert_true(node.has_addon('osffiles'))
class TestValidProject(OsfTestCase): def setUp(self): super(TestValidProject, self).setUp() self.project = ProjectFactory() self.node = NodeFactory(project=self.project) self.retraction = RetractionFactory() self.auth = Auth(user=self.project.creator) def test_populates_kwargs_node(self): res = valid_project_helper(pid=self.project._id) assert_equal(res['node'], self.project) assert_is_none(res['parent']) def test_populates_kwargs_node_and_parent(self): res = valid_project_helper(pid=self.project._id, nid=self.node._id) assert_equal(res['parent'], self.project) assert_equal(res['node'], self.node) def test_project_not_found(self): with assert_raises(HTTPError) as exc_info: valid_project_helper(pid='fakepid') assert_equal(exc_info.exception.code, 404) def test_project_deleted(self): self.project.is_deleted = True self.project.save() with assert_raises(HTTPError) as exc_info: valid_project_helper(pid=self.project._id) assert_equal(exc_info.exception.code, 410) def test_node_not_found(self): with assert_raises(HTTPError) as exc_info: valid_project_helper(pid=self.project._id, nid='fakenid') assert_equal(exc_info.exception.code, 404) def test_node_deleted(self): self.node.is_deleted = True self.node.save() with assert_raises(HTTPError) as exc_info: valid_project_helper(pid=self.project._id, nid=self.node._id) assert_equal(exc_info.exception.code, 410) def test_valid_project_as_factory_allow_retractions_is_retracted(self): self.project.is_registration = True self.project.retraction = self.retraction self.retraction.state = Sanction.UNAPPROVED self.retraction.save() res = as_factory_allow_retractions(pid=self.project._id) assert_equal(res['node'], self.project) def test_collection_guid_not_found(self): collection = CollectionFactory() collection.add_pointer(self.project, self.auth) with assert_raises(HTTPError) as exc_info: valid_project_helper(pid=collection._id, nid=collection._id) assert_equal(exc_info.exception.code, 404)
def test_serialize_node_search_returns_only_visible_contributors(self): node = NodeFactory() non_visible_contributor = UserFactory() node.add_contributor(non_visible_contributor, visible=False) serialized_node = _serialize_node_search(node) assert_equal(serialized_node['firstAuthor'], node.visible_contributors[0].family_name) assert_equal(len(node.visible_contributors), 1) assert_false(serialized_node['etal'])
def test_user_is_admin(self): node = NodeFactory(creator=self.user) res = self.app.post_json_api( self.institution_nodes_url, self.create_payload(node._id), auth=self.user.auth ) assert_equal(res.status_code, 201) node.reload() assert_in(self.institution, node.affiliated_institutions)
def test_get_contributors_from_parent(self): self.project.add_contributor(AuthUserFactory(), auth=self.auth, visible=True) self.project.add_contributor(AuthUserFactory(), auth=self.auth, visible=False) self.project.save() component = NodeFactory(parent=self.project, creator=self.user) url = component.api_url_for("get_contributors_from_parent") res = self.app.get(url, auth=self.user.auth) # Should be one contributor to the parent who is both visible and # not a contributor on the component assert_equal(len(res.json["contributors"]), 1)
def test_fork_serialization(self): node = NodeFactory(creator=self.user) fork = node.fork_node(auth=Auth(user=node.creator)) result = NodeSerializer(fork, context={"request": make_drf_request()}).data data = result["data"] # Relationships relationships = data["relationships"] forked_from = relationships["forked_from"]["links"]["related"]["href"] assert_equal(urlparse(forked_from).path, "/{}nodes/{}/".format(API_BASE, node._id))
def test_a_public_component_from_home_page(self): component = NodeFactory(title='Foobar Component', is_public=True) # Searches a part of the name res = self.app.get('/').maybe_follow() component.reload() form = res.forms['searchBar'] form['q'] = 'Foobar' res = form.submit().maybe_follow() # A link to the component is shown as a result assert_in('Foobar Component', res)
def test_serialize_deleted(self): node = NodeFactory() info = serialize_node(node) assert_false(info['deleted']) node.is_deleted = True info = serialize_node(node) assert_true(info['deleted']) node.is_deleted = False info = serialize_node(node) assert_false(info['deleted'])
def test_a_public_component_from_home_page(self): component = NodeFactory(title="Foobar Component", is_public=True) # Searches a part of the name res = self.app.get("/").maybe_follow() component.reload() form = res.forms["searchBar"] form["q"] = "Foobar" res = form.submit().maybe_follow() # A link to the component is shown as a result assert_in("Foobar Component", res)
def test_was_not_invited(self): referrer = UserFactory() node = NodeFactory(creator=referrer) user = UserFactory() node.add_contributor(user, auth=Auth(referrer)) assert_false(is_invited(user)) user.is_invited = None user.save() main(dry_run=False) user.reload() assert_false(user.is_invited)
def test_collect_js_unique(self): self.project.get_addons.return_value[0].config.include_js = {'files': ['foo.js']} node = NodeFactory(project=self.project) mock_node_addon = mock.Mock() mock_node_addon.config.include_js = {'files': ['foo.js', 'baz.js']} node.get_addons = mock.Mock() node.get_addons.return_value = [mock_node_addon] assert_equal( rubeus.collect_addon_js(self.project), {'foo.js', 'baz.js'} )
def test_serialize_node_for_logs(self): node = NodeFactory() d = node.serialize() assert_equal(d['id'], node._primary_key) assert_equal(d['category'], node.category_display) assert_equal(d['node_type'], node.project_or_component) assert_equal(d['url'], node.url) assert_equal(d['title'], node.title) assert_equal(d['api_url'], node.api_url) assert_equal(d['is_public'], node.is_public) assert_equal(d['is_registration'], node.is_registration)
def setUp(self): super(TestMetricsGetOSFStatistics, self).setUp() Node.remove() time_now = get_previous_midnight() NodeFactory(category='project', date_created=time_now) NodeFactory(category='project', date_created=time_now - timedelta(days=1)) last_time = time_now - timedelta(days=2) NodeFactory(category='project', date_created=last_time) NodeFactory(category='project', date_created=last_time) get_days_statistics(last_time + timedelta(seconds=1)) self.time = time_now + timedelta(seconds=1)
def test_serialize_node_for_logs(self): node = NodeFactory() d = node.serialize() assert_equal(d["id"], node._primary_key) assert_equal(d["category"], node.category_display) assert_equal(d["node_type"], node.project_or_component) assert_equal(d["url"], node.url) assert_equal(d["title"], node.title) assert_equal(d["api_url"], node.api_url) assert_equal(d["is_public"], node.is_public) assert_equal(d["is_registration"], node.is_registration)
def test_affected_nodes(self): affected_node = NodeFactory() AddonWikiNodeSettings.remove_one(affected_node.get_addon('wiki')) assert_false(affected_node.has_addon('wiki')) unaffected_node = NodeFactory() assert_true(unaffected_node.has_addon('wiki')) affected_nodes = list(get_affected_nodes(self.db, AddonWikiNodeSettings)) assert_in(affected_node, affected_nodes) assert_not_in(unaffected_node, affected_nodes)
def test_collect_js_unique(self): self.project.get_addons.return_value[0].config.include_js = {'files': ['foo.js']} self.project.get_addons.return_value[0].config.short_name = 'dropbox' node = NodeFactory(parent=self.project) mock_node_addon = mock.Mock() mock_node_addon.config.include_js = {'files': ['foo.js', 'baz.js']} mock_node_addon.config.short_name = 'dropbox' node.get_addons = mock.Mock() node.get_addons.return_value = [mock_node_addon] result = rubeus.collect_addon_js(self.project) assert_in('foo.js', result) assert_in('baz.js', result)
def test_affiliated_component_without_affiliated_parent_returned(self): # version 2.2 self.node = ProjectFactory(is_public=True) self.component = NodeFactory(parent=self.node, is_public=True) self.component.affiliated_institutions.append(self.institution) self.component.save() url = '{}?version=2.2'.format(self.institution_node_url) res = self.app.get(url, auth=self.user1.auth) affiliated_node_ids = [node['id'] for node in res.json['data']] assert_equal(res.status_code, 200) assert_not_in(self.node._id, affiliated_node_ids) assert_in(self.component._id, affiliated_node_ids)
def test_collect_js_unique(self): self.project.get_addons.return_value[0].config.include_js = {"files": ["foo.js"]} self.project.get_addons.return_value[0].config.short_name = "dropbox" node = NodeFactory(parent=self.project) mock_node_addon = mock.Mock() mock_node_addon.config.include_js = {"files": ["foo.js", "baz.js"]} mock_node_addon.config.short_name = "dropbox" node.get_addons = mock.Mock() node.get_addons.return_value = [mock_node_addon] result = rubeus.collect_addon_js(self.project) assert_in("foo.js", result) assert_in("baz.js", result)
def test__on_complete_makes_project_and_components_public(self): project_admin = UserFactory() child_admin = UserFactory() grandchild_admin = UserFactory() project = ProjectFactory(creator=project_admin, is_public=False) child = NodeFactory(creator=child_admin, parent=project, is_public=False) grandchild = NodeFactory(creator=grandchild_admin, parent=child, is_public=False) # noqa registration = RegistrationFactory(project=project) with mock.patch.object(PreregCallbackMixin, '_notify_initiator'): registration.registration_approval._on_complete(self.user)
def test_user_does_not_have_node(self): node = NodeFactory() res = self.app.post_json_api( self.institution_nodes_url, self.create_payload(node._id), expect_errors=True, auth=self.user.auth ) assert_equal(res.status_code, 403) node.reload() assert_not_in(self.institution, node.affiliated_institutions)
def test_serialize_wiki_toc(self): project = ProjectFactory() auth = Auth(project.creator) NodeFactory(project=project, creator=project.creator) no_wiki = NodeFactory(project=project, creator=project.creator) project.save() serialized = _serialize_wiki_toc(project, auth=auth) assert_equal(len(serialized), 2) no_wiki.delete_addon('wiki', auth=auth) serialized = _serialize_wiki_toc(project, auth=auth) assert_equal(len(serialized), 1)
class TestComponents(OsfTestCase): def setUp(self): super(TestComponents, self).setUp() self.user = AuthUserFactory() self.consolidate_auth = Auth(user=self.user) self.project = ProjectFactory(creator=self.user) self.project.add_contributor(contributor=self.user, auth=self.consolidate_auth) # A non-project componenet self.component = NodeFactory(category="hypothesis", creator=self.user, parent=self.project) self.component.save() self.component.set_privacy("public", self.consolidate_auth) self.component.set_privacy("private", self.consolidate_auth) self.project.save() self.project_url = self.project.web_url_for("view_project") def test_can_create_component_from_a_project(self): res = self.app.get(self.project.url, auth=self.user.auth).maybe_follow() assert_in("Add Component", res) def test_can_create_component_from_a_component(self): res = self.app.get(self.component.url, auth=self.user.auth).maybe_follow() assert_in("Add Component", res) def test_sees_parent(self): res = self.app.get(self.component.url, auth=self.user.auth).maybe_follow() parent_title = res.html.find_all("h2", class_="node-parent-title") assert_equal(len(parent_title), 1) assert_in(self.project.title, parent_title[0].text) # Bs4 will handle unescaping HTML here def test_delete_project(self): res = self.app.get(self.component.url + "settings/", auth=self.user.auth).maybe_follow() assert_in("Delete {0}".format(self.component.project_or_component), res) def test_cant_delete_project_if_not_admin(self): non_admin = AuthUserFactory() self.component.add_contributor(non_admin, permissions=["read", "write"], auth=self.consolidate_auth, save=True) res = self.app.get(self.component.url + "settings/", auth=non_admin.auth).maybe_follow() assert_not_in("Delete {0}".format(self.component.project_or_component), res) def test_can_configure_comments_if_admin(self): res = self.app.get(self.component.url + "settings/", auth=self.user.auth).maybe_follow() assert_in("Configure Commenting", res) def test_cant_configure_comments_if_not_admin(self): non_admin = AuthUserFactory() self.component.add_contributor(non_admin, permissions=["read", "write"], auth=self.consolidate_auth, save=True) res = self.app.get(self.component.url + "settings/", auth=non_admin.auth).maybe_follow() assert_not_in("Configure commenting", res) def test_components_should_have_component_list(self): res = self.app.get(self.component.url, auth=self.user.auth) assert_in("Components", res)
def test_new_draft_registration_POST(self): target = NodeFactory(creator=self.user) payload = { 'schema_name': self.meta_schema.name, 'schema_version': self.meta_schema.schema_version } url = target.web_url_for('new_draft_registration') res = self.app.post(url, payload, auth=self.user.auth) assert_equal(res.status_code, http.FOUND) target.reload() draft = DraftRegistration.find_one(Q('branched_from', 'eq', target)) assert_equal(draft.registration_schema, self.meta_schema)
def test_user_is_admin_but_not_affiliated(self): user = AuthUserFactory() node = NodeFactory(creator=user) res = self.app.post_json_api( self.institution_nodes_url, self.create_payload(node._id), expect_errors=True, auth=user.auth ) assert_equal(res.status_code, 403) node.reload() assert_not_in(self.institution, node.affiliated_institutions)
def test_fork_serialization(self): node = NodeFactory(creator=self.user) fork = node.fork_node(auth=Auth(user=node.creator)) result = NodeSerializer(fork, context={'request': make_drf_request()}).data data = result['data'] # Relationships relationships = data['relationships'] forked_from = relationships['forked_from']['links']['related']['href'] assert_equal( urlparse(forked_from).path, '/{}nodes/{}/'.format(API_BASE, node._id) )
def test_collect_js_recursive(self): self.project.get_addons.return_value[0].config.include_js = {'files': ['foo.js']} self.project.get_addons.return_value[0].config.short_name = 'dropbox' node = NodeFactory(parent=self.project) mock_node_addon = mock.Mock() mock_node_addon.config.include_js = {'files': ['bar.js', 'baz.js']} mock_node_addon.config.short_name = 'dropbox' node.get_addons = mock.Mock() node.get_addons.return_value = [mock_node_addon] result = rubeus.collect_addon_js(self.project) assert_in('foo.js', result) assert_in('bar.js', result) assert_in('baz.js', result)
def setUp(self): super(TestViewOnlyLinksUpdate, self).setUp() self.url = '/{}nodes/{}/view_only_links/{}/'.format( API_BASE, self.public_project._id, self.view_only_link._id) self.user_two = AuthUserFactory() self.public_project.add_contributor(self.user_two, permissions=[permissions.ADMIN]) self.public_project_component = NodeFactory(is_public=True, creator=self.user, parent=self.public_project) self.public_project_component.save()
def test_retrieve_private_node_with_auth_with_inst(self): node = NodeFactory(is_public=False, creator=self.user) node.primary_institution = self.institution node.save() res = self.app.get( '/{0}nodes/{1}/relationships/institution/'.format(API_BASE, node._id), auth=self.user.auth ) assert_equal(res.status_code, 200) assert_equal(res.json['data']['type'], 'institutions') assert_equal(res.json['data']['id'], self.institution._id)
def test__on_complete_makes_project_and_components_public(self): project_admin = UserFactory() child_admin = UserFactory() grandchild_admin = UserFactory() project = ProjectFactory(creator=project_admin, is_public=False) child = NodeFactory(creator=child_admin, parent=project, is_public=False) grandchild = NodeFactory(creator=grandchild_admin, parent=child, is_public=False) # noqa registration = RegistrationFactory(project=project) registration._initiate_retraction(self.user) registration.retraction._on_complete(self.user) for each in registration.node_and_primary_descendants(): assert_true(each.is_public)
def test_only_add_existent_with_permissions(self): node = NodeFactory(creator=self.user) node.affiliated_institutions.append(self.institution) node.save() assert_in(self.institution, self.node1.affiliated_institutions) assert_in(self.institution, node.affiliated_institutions) res = self.app.post_json_api( self.institution_nodes_url, self.create_payload(node._id, self.node1._id), auth=self.user.auth ) assert_equal(res.status_code, 204)
def test_user_with_nodes_and_permissions(self): node = NodeFactory(creator=self.user) res = self.app.post_json_api( self.institution_nodes_url, self.create_payload(node._id), auth=self.user.auth ) assert_equal(res.status_code, 201) node_ids = [node_['id'] for node_ in res.json['data']] assert_in(node._id, node_ids) node.reload() assert_in(self.institution, node.affiliated_institutions)
def test_template_serialization(self): node = NodeFactory(creator=self.user) fork = node.use_as_template(auth=Auth(user=node.creator)) req = make_drf_request_with_version(version='2.0') result = NodeSerializer(fork, context={'request': req}).data data = result['data'] # Relationships relationships = data['relationships'] templated_from = relationships['template_node']['links']['related']['href'] assert_equal( urlparse(templated_from).path, '/{}nodes/{}/'.format(API_BASE, node._id) )
def test_register_template_make_public_makes_children_pending_registration(self, mock_enqueue): comp1 = NodeFactory(parent=self.node) NodeFactory(parent=comp1) url = self.node.api_url_for('register_draft_registration', draft_id=self.draft._id) res = self.app.post_json(url, self.immediate_payload, auth=self.user.auth) assert_equal(res.status_code, http.ACCEPTED) self.node.reload() # Most recent node is a registration reg = Node.load(self.node.node__registrations[-1]) for node in reg.get_descendants_recursive(): assert_true(node.is_registration) assert_true(node.is_pending_registration)
def setUp(self): super(TestApproveEmbargoTerminations, self).setUp() self.user = AuthUserFactory() self.node = NodeFactory(creator=self.user) NodeFactory(creator=self.user, parent=NodeFactory(creator=self.user, parent=self.node)) # requesting termination but less than 48 hours old with mock_archive(self.node, embargo=True, autoapprove=True) as registration: registration.request_embargo_termination(auth=Auth(self.user)) self.registration1 = registration # requesting termination and older than 48 hours with mock_archive(self.node, embargo=True, autoapprove=True) as registration: old_time = timezone.now() - timedelta(days=5) approval = registration.request_embargo_termination( auth=Auth(self.user)) EmbargoTerminationApproval._storage[0].store.update( {'_id': approval._id}, {'$set': { 'initiation_date': old_time }}, ) self.registration2 = registration # requesting termination and older than 48 hours, but approved with mock_archive(self.node, embargo=True, autoapprove=True) as registration: old_time = timezone.now() - timedelta(days=5) approval = registration.request_embargo_termination( auth=Auth(self.user)) EmbargoTerminationApproval._storage[0].store.update( {'_id': approval._id}, {'$set': { 'initiation_date': old_time }}, ) approval.state = Sanction.APPROVED approval.save() self.registration3 = registration # embargoed but not requesting termination with mock_archive(self.node, embargo=True, autoapprove=True) as registration: self.registration4 = registration EmbargoTerminationApproval._clear_caches()
class TestFigshareAPIWrapper(OsfTestCase): def setUp(self): OsfTestCase.setUp(self) self.node = NodeFactory() self.node.add_addon('figshare', auth=Auth(self.node.creator)) self.node.save() self.node_settings = self.node.get_addon('figshare') self.client = Figshare() def test_get_project_url(self): url = _get_project_url(self.node_settings, 123) expected = os.path.join(self.node_settings.api_url, 'projects', '123') assert_equal(url, expected)
class TestValidProject(OsfTestCase): def setUp(self): super(TestValidProject, self).setUp() self.project = ProjectFactory() self.node = NodeFactory(project=self.project) self.retraction = RetractionFactory() def test_populates_kwargs_node(self): res = valid_project_helper(pid=self.project._id) assert_equal(res['node'], self.project) assert_is_none(res['parent']) def test_populates_kwargs_node_and_parent(self): res = valid_project_helper(pid=self.project._id, nid=self.node._id) assert_equal(res['parent'], self.project) assert_equal(res['node'], self.node) def test_project_not_found(self): with assert_raises(HTTPError) as exc_info: valid_project_helper(pid='fakepid') assert_equal(exc_info.exception.code, 404) def test_project_deleted(self): self.project.is_deleted = True self.project.save() with assert_raises(HTTPError) as exc_info: valid_project_helper(pid=self.project._id) assert_equal(exc_info.exception.code, 410) def test_node_not_found(self): with assert_raises(HTTPError) as exc_info: valid_project_helper(pid=self.project._id, nid='fakenid') assert_equal(exc_info.exception.code, 404) def test_node_deleted(self): self.node.is_deleted = True self.node.save() with assert_raises(HTTPError) as exc_info: valid_project_helper(pid=self.project._id, nid=self.node._id) assert_equal(exc_info.exception.code, 410) def test_valid_project_as_factory_allow_retractions_is_retracted(self): self.project.is_registration = True self.project.retraction = self.retraction self.retraction.state = 'retracted' self.retraction.save() res = as_factory_allow_retractions(pid=self.project._id) assert_equal(res['node'], self.project)
def test_add_some_existant_others_not(self): assert_in(self.institution, self.node1.affiliated_institutions) node = NodeFactory(creator=self.user) res = self.app.post_json_api( self.institution_nodes_url, self.create_payload(node._id, self.node1._id), auth=self.user.auth ) assert_equal(res.status_code, 201) node.reload() self.node1.reload() assert_in(self.institution, self.node1.affiliated_institutions) assert_in(self.institution, node.affiliated_institutions)
def setUp(self): super(TestShortUrls, self).setUp() self.user = AuthUserFactory() self.auth = self.user.auth self.consolidate_auth = Auth(user=self.user) self.project = ProjectFactory(creator=self.user) # A non-project componenet self.component = NodeFactory(category='hypothesis', creator=self.user) self.project.nodes.append(self.component) self.component.save() # Hack: Add some logs to component; should be unnecessary pending # improvements to factories from @rliebz self.component.set_privacy('public', auth=self.consolidate_auth) self.component.set_privacy('private', auth=self.consolidate_auth) self.wiki = NodeWikiFactory(user=self.user, node=self.component)
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_retract_component_raises_NodeStateError(self): project = ProjectFactory(is_public=True, creator=self.user) component = NodeFactory(is_public=True, creator=self.user, parent=project) registration = RegistrationFactory(is_public=True, project=project) with assert_raises(NodeStateError): registration.nodes[0].retract_registration(self.user, self.valid_justification)
def test_node_serialization(self): parent = ProjectFactory(creator=self.user) node = NodeFactory(creator=self.user, parent=parent) req = make_drf_request() result = NodeSerializer(node, context={'request': req}).data data = result['data'] assert_equal(data['id'], node._id) assert_equal(data['type'], 'nodes') # Attributes attributes = data['attributes'] assert_equal(attributes['title'], node.title) assert_equal(attributes['description'], node.description) assert_equal(attributes['public'], node.is_public) assert_equal(attributes['tags'], [str(each) for each in node.tags]) assert_equal(attributes['category'], node.category) assert_equal(attributes['registration'], node.is_registration) assert_equal(attributes['fork'], node.is_fork) assert_equal(attributes['collection'], node.is_collection) # Relationships relationships = data['relationships'] assert_in('children', relationships) assert_in('contributors', relationships) assert_in('files', relationships) assert_in('parent', relationships) assert_in('affiliated_institutions', relationships) parent_link = relationships['parent']['links']['related']['href'] assert_equal( urlparse(parent_link).path, '/{}nodes/{}/'.format(API_BASE, parent._id)) assert_in('registrations', relationships) # Not a fork, so forked_from is removed entirely assert_not_in('forked_from', relationships)
def test_fork_private_components_no_access(self): url = self.public_registration_url + '?embed=children' private_component = NodeFactory(parent=self.public_registration, creator=self.user_two, is_public=False) res = self.app.post_json_api(url, self.fork_data, auth=self.user_three.auth) assert_equal(res.status_code, 201) # Private components that you do not have access to are not forked assert_equal(res.json['data']['embeds']['children']['links']['meta']['total'], 0)
def test_fork_components_you_can_access(self): url = self.private_registration_url + '?embed=children' new_component = NodeFactory(parent=self.private_registration, creator=self.user) res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth) assert_equal(res.status_code, 201) assert_equal(res.json['data']['embeds']['children']['links']['meta']['total'], 1) assert_equal(res.json['data']['embeds']['children']['data'][0]['id'], new_component.forks[0]._id)
def test_node_child_filtering(self): user = AuthUserFactory() project = ProjectFactory(creator=user) title1, title2 = fake.bs(), fake.bs() component = NodeFactory(title=title1, parent=project) component2 = NodeFactory(title=title2, parent=project) url = '/{}nodes/{}/children/?filter[title]={}'.format( API_BASE, project._id, title1) res = self.app.get(url, auth=user.auth) ids = [node['id'] for node in res.json['data']] assert_in(component._id, ids) assert_not_in(component2._id, ids)
def test_child_project_has_parent(self): public_component = NodeFactory(parent=self.public_project, creator=self.user, is_public=True) public_component_url = '/{}nodes/{}/'.format(API_BASE, public_component._id) res = self.app.get(public_component_url) assert_equal(res.status_code, 200) url = res.json['data']['relationships']['parent']['links']['related']['href'] assert_equal(urlparse(url).path, self.public_url)
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.build(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
def setUp(self): super(TestMetricsGetProjects, self).setUp() Node.remove() self.public_node = ProjectFactory(is_public=True) self.private_node = ProjectFactory(is_public=False) self.node_2 = NodeFactory() # creates parent project + node self.reg = RegistrationFactory(project=self.public_node)
def test_get_draft_registrations_only_gets_drafts_for_that_node(self): dummy = NodeFactory() # Drafts for dummy node for i in range(5): d = DraftRegistrationFactory(initiator=self.user, branched_from=dummy, meta_schema=self.meta_schema, schema_data={}) found = [self.draft] # Drafts for self.node for i in range(3): d = DraftRegistrationFactory(initiator=self.user, branched_from=self.node, meta_schema=self.meta_schema, schema_data={}) found.append(d) url = self.node.api_url_for('get_draft_registrations') res = self.app.get(url, auth=self.user.auth) assert_equal(res.status_code, http.OK) # 3 new, 1 from setUp assert_equal(len(res.json['drafts']), 4) for draft in res.json['drafts']: assert_in(draft['pk'], [f._id for f in found])