Exemple #1
0
    def test_serialize_private_node(self):
        user = UserFactory()
        auth = Auth(user=user)
        public = ProjectFactory.build(is_public=True)
        # Add contributor with write permissions to avoid admin permission cascade
        public.add_contributor(user, permissions=['read', 'write'])
        public.save()
        private = ProjectFactory(parent=public, is_public=False)
        NodeFactory(parent=private)
        collector = rubeus.NodeFileCollector(node=public, auth=auth)

        private_dummy = collector._serialize_node(private)
        assert_false(private_dummy['permissions']['edit'])
        assert_false(private_dummy['permissions']['view'])
        assert_equal(private_dummy['name'], 'Private Component')
        assert_equal(len(private_dummy['children']), 0)
Exemple #2
0
 def test_delete_project_with_component_returns_error(self):
     project = ProjectFactory(creator=self.user)
     component = NodeFactory(parent=project, creator=self.user)
     # Return a 400 because component must be deleted before deleting the parent
     res = self.app.delete_json_api(
         '/{}nodes/{}/'.format(API_BASE, project._id),
         auth=self.user.auth,
         expect_errors=True
     )
     assert_equal(res.status_code, 400)
     errors = res.json['errors']
     assert_equal(len(errors), 1)
     assert_equal(
         errors[0]['detail'],
         'Any child components must be deleted prior to deleting this project.'
     )
Exemple #3
0
 def test_must_be_contributor_parent_write_private_project(self):
     user = UserFactory()
     node = NodeFactory(parent=self.private_project, creator=user)
     contrib = UserFactory()
     self.private_project.add_contributor(contrib,
                                          auth=Auth(
                                              self.private_project.creator),
                                          permissions=['read', 'write'])
     self.private_project.save()
     with assert_raises(HTTPError) as exc_info:
         view_that_needs_contributor_or_public(
             pid=self.private_project._id,
             nid=node._id,
             user=contrib,
         )
     assert_equal(exc_info.exception.code, 403)
Exemple #4
0
    def setUp(self):
        super(FileCommentMoveRenameTestMixin, self).setUp()
        self.user = UserFactory()
        self.project = ProjectFactory(creator=self.user)
        self.project.add_addon(self.provider, auth=Auth(self.user))
        self.project.save()
        self.project_settings = self.project.get_addon(self.provider)
        self.project_settings.folder = '/Folder1'
        self.project_settings.save()

        self.component = NodeFactory(parent=self.project, creator=self.user)
        self.component.add_addon(self.provider, auth=Auth(self.user))
        self.component.save()
        self.component_settings = self.component.get_addon(self.provider)
        self.component_settings.folder = '/Folder2'
        self.component_settings.save()
Exemple #5
0
    def setUp(self):
        super(TestNodeDetail, self).setUp()
        self.user = AuthUserFactory()

        self.user_two = AuthUserFactory()

        self.public_project = ProjectFactory(title="Project One", is_public=True, creator=self.user)
        self.private_project = ProjectFactory(title="Project Two", is_public=False, creator=self.user)
        self.public_url = '/{}nodes/{}/'.format(API_BASE, self.public_project._id)
        self.private_url = '/{}nodes/{}/'.format(API_BASE, self.private_project._id)

        self.public_component = NodeFactory(parent=self.public_project, creator=self.user, is_public=True)
        self.public_component_url = '/{}nodes/{}/'.format(API_BASE, self.public_component._id)
        self.read_permissions = ['read']
        self.write_permissions = ['read', 'write']
        self.admin_permissions = ['read', 'admin', 'write']
    def test_delete_user_is_admin_but_not_affiliated_with_inst(self):
        user = AuthUserFactory()
        node = NodeFactory(creator=user)
        node.affiliated_institutions.append(self.institution)
        node.save()
        assert_in(self.institution, node.affiliated_institutions)

        res = self.app.delete_json_api(
            self.institution_nodes_url,
            self.create_payload(node._id),
            auth=user.auth
        )

        assert_equal(res.status_code, 204)
        node.reload()
        assert_not_in(self.institution, node.affiliated_institutions)
Exemple #7
0
    def test_user_is_read_only(self):
        user = AuthUserFactory()
        user.affiliated_institutions.append(self.institution)
        node = NodeFactory()
        node.add_contributor(user, permissions=[permissions.READ])
        node.save()

        res = self.app.post_json_api(
            self.institution_nodes_url,
            self.create_payload(node._id),
            auth=user.auth,
            expect_errors=True
        )

        assert_equal(res.status_code, 403)
        node.reload()
        assert_not_in(self.institution, node.affiliated_institutions)
Exemple #8
0
    def test_get_wiki_url_pointer_component(self):
        """Regression test for issues
        https://github.com/CenterForOpenScience/osf/issues/363 and
        https://github.com/CenterForOpenScience/openscienceframework.org/issues/574

        """
        user = UserFactory()
        pointed_node = NodeFactory(creator=user)
        project = ProjectFactory(creator=user)
        auth = Auth(user=user)
        project.add_pointer(pointed_node, auth=auth, save=True)

        serialized = _serialize_wiki_toc(project, auth)
        assert_equal(
            serialized[0]['url'],
            pointed_node.web_url_for('project_wiki_view', wname='home', _guid=True)
        )
Exemple #9
0
 def test_was_invited(self):
     referrer = UserFactory()
     node = NodeFactory(creator=referrer)
     name = fake.name()
     email = fake.email()
     user = node.add_unregistered_contributor(
         fullname=name,
         email=email,
         auth=Auth(user=referrer),
     )
     user.register(email, 'secret')
     assert_true(is_invited(user))
     user.is_invited = None
     user.save()
     main(dry_run=False)
     user.reload()
     assert_true(user.is_invited)
Exemple #10
0
 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_delete_user_is_admin_but_not_affiliated_with_inst(self):
        user = AuthUserFactory()
        node = NodeFactory(creator=user)
        node.affiliated_institutions.append(self.institution1)
        node.save()
        assert_in(self.institution1, node.affiliated_institutions)

        res = self.app.delete_json_api(
            '/{0}nodes/{1}/relationships/institutions/'.format(
                API_BASE, node._id),
            self.create_payload(self.institution1._id),
            auth=user.auth,
        )

        assert_equal(res.status_code, 204)
        node.reload()
        assert_not_in(self.institution1, node.affiliated_institutions)
    def test_serialize_log(self):
        node = NodeFactory(category='hypothesis')
        log = NodeLogFactory(params={'node': node._primary_key})
        node.logs.append(log)
        node.save()
        d = serialize_log(log)
        assert_equal(d['action'], log.action)
        assert_equal(d['node']['node_type'], 'component')
        assert_equal(d['node']['category'], 'Hypothesis')

        assert_equal(d['node']['url'], log.node.url)
        assert_equal(d['date'], framework_utils.iso8601format(log.date))
        assert_in('contributors', d)
        assert_equal(d['user']['fullname'], log.user.fullname)
        assert_equal(d['user']['url'], log.user.url)
        assert_equal(d['params'], log.params)
        assert_equal(d['node']['title'], log.node.title)
    def test_remove_institution_not_admin_but_affiliated(self):
        node = NodeFactory(creator=self.user)
        user = AuthUserFactory()
        user.affiliated_institutions.append(self.institution)
        user.save()
        node.primary_institution = self.institution
        node.add_contributor(user, auth=Auth(self.user))
        node.save()

        res = self.app.put_json_api(
            '/{0}nodes/{1}/relationships/institution/'.format(
                API_BASE, node._id), {'data': None},
            auth=user.auth,
            expect_errors=True)

        assert_equal(res.status_code, 403)
        assert_equal(node.primary_institution, self.institution)
Exemple #14
0
 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)
Exemple #15
0
    def test_node_children_list_does_not_include_deleted(self):
        child_project = NodeFactory(parent=self.public_project, creator=self.user)
        child_project.save()

        res = self.app.get(self.public_project_url, auth=self.user.auth)
        assert_equal(res.status_code, 200)
        ids = [node['id'] for node in res.json['data']]
        assert_in(child_project._id, ids)
        assert_equal(2, len(ids))

        child_project.is_deleted = True
        child_project.save()

        res = self.app.get(self.public_project_url, auth=self.user.auth)
        assert_equal(res.status_code, 200)
        ids = [node['id'] for node in res.json['data']]
        assert_not_in(child_project._id, ids)
        assert_equal(1, len(ids))
Exemple #16
0
    def test_do_migration(self):
        project = ProjectFactory()
        user = UserFactory()
        node = NodeFactory(parent=project)
        node.add_contributor(contributor=user)
        node.save()
        for log in node.logs:
            if log.action == 'contributor_added':
                project.logs.append(log)
        project.save()

        node_list = get_targets()
        do_migration(node_list)

        logs = [
            each for each in project.logs if each.action == 'contributor_added'
        ]
        assert len(logs) is 1
        assert logs[0].should_hide is True
    def test_user_with_institution_and_permissions_through_patch(self):
        self.user.affiliated_institutions.append(self.institution)
        self.user.save()
        node = NodeFactory(creator=self.user)
        res = self.app.patch_json_api(
            '/{0}nodes/{1}/relationships/institution/'.format(
                API_BASE, node._id),
            {'data': {
                'type': 'institutions',
                'id': self.institution._id
            }},
            auth=self.user.auth)

        assert_equal(res.status_code, 200)
        data = res.json['data']
        assert_equal(data['type'], 'institutions')
        assert_equal(data['id'], self.institution._id)
        node.reload()
        assert_equal(node.primary_institution, self.institution)
 def setUp(self):
     super(TestShortUrls, self).setUp()
     self.user = UserFactory()
     # Add an API key for quicker authentication
     api_key = ApiKeyFactory()
     self.user.api_keys.append(api_key)
     self.user.save()
     self.auth = ('test', api_key._primary_key)
     self.consolidate_auth = Auth(user=self.user, api_key=api_key)
     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)
Exemple #19
0
    def test_get_node_name(self):
        user = UserFactory()
        auth = Auth(user=user)
        another_user = UserFactory()
        another_auth = Auth(user=another_user)

        # Public (Can View)
        public_project = ProjectFactory(is_public=True)
        collector = rubeus.NodeFileCollector(node=public_project, auth=another_auth)
        node_name =  sanitize.unescape_entities(public_project.title)
        assert_equal(collector._get_node_name(public_project), node_name)

        # Private  (Can't View)
        registration_private = RegistrationFactory(creator=user)
        registration_private.is_public = False
        registration_private.save()
        collector = rubeus.NodeFileCollector(node=registration_private, auth=another_auth)
        assert_equal(collector._get_node_name(registration_private), u'Private Registration')

        content = ProjectFactory(creator=user)
        node = ProjectFactory(creator=user)

        forked_private = node.fork_node(auth=auth)
        forked_private.is_public = False
        forked_private.save()
        collector = rubeus.NodeFileCollector(node=forked_private, auth=another_auth)
        assert_equal(collector._get_node_name(forked_private), u'Private Fork')

        pointer_private = node.add_pointer(content, auth=auth)
        pointer_private.is_public = False
        pointer_private.save()
        collector = rubeus.NodeFileCollector(node=pointer_private, auth=another_auth)
        assert_equal(collector._get_node_name(pointer_private), u'Private Link')

        private_project = ProjectFactory(is_public=False)
        collector = rubeus.NodeFileCollector(node=private_project, auth=another_auth)
        assert_equal(collector._get_node_name(private_project), u'Private Component')

        private_node = NodeFactory(is_public=False)
        collector = rubeus.NodeFileCollector(node=private_node, auth=another_auth)
        assert_equal(collector._get_node_name(private_node), u'Private Component')
Exemple #20
0
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags):
    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)

    project.save()
    logger.info('Created project: {0}'.format(project.title))
    return project
Exemple #21
0
    def setUp(self):
        super(TestIdentifierDetail, self).setUp()
        self.user = AuthUserFactory()

        self.registration = RegistrationFactory(creator=self.user,
                                                is_public=True)
        self.registration_identifier = IdentifierFactory(
            referent=self.registration)
        self.registration_url = '/{}identifiers/{}/'.format(
            API_BASE, self.registration_identifier._id)

        self.node = NodeFactory(creator=self.user, is_public=True)
        self.node_identifier = IdentifierFactory(referent=self.node)
        self.node_url = '/{}identifiers/{}/'.format(API_BASE,
                                                    self.node_identifier._id)

        self.registration_res = self.app.get(self.registration_url)
        self.registration_data = self.registration_res.json['data']

        self.node_res = self.app.get(self.node_url)
        self.node_data = self.node_res.json['data']
Exemple #22
0
    def setUp(self):
        super(TestSearchSerializer, self).setUp()

        self.user = AuthUserFactory()
        self.project = ProjectFactory(creator=self.user, is_public=True)
        self.component = NodeFactory(parent=self.project,
                                     creator=self.user,
                                     is_public=True)
        self.file = utils.create_test_file(self.component, self.user)

        ensure_schemas()
        self.schema = MetaSchema.find_one(
            Q('name', 'eq',
              'Replication Recipe (Brandt et al., 2013): Post-Completion')
            & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION))

        with mock_archive(self.project,
                          autocomplete=True,
                          autoapprove=True,
                          schema=self.schema) as registration:
            self.registration = registration
 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 all contributors, client-side handles marking
     # contributors that are already added to the child.
     assert_equal(
         len(res.json['contributors']),
         2,
     )
 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,
     )
Exemple #25
0
    def test_linked_nodes_only_return_viewable_nodes(self):
        user = AuthUserFactory()
        new_linking_node = NodeFactory(creator=user)
        self.linked_node.add_contributor(user, auth=self.auth, save=True)
        self.linked_node2.add_contributor(user, auth=self.auth, save=True)
        self.public_node.add_contributor(user, auth=self.auth, save=True)
        new_linking_node.add_pointer(self.linked_node, auth=Auth(user))
        new_linking_node.add_pointer(self.linked_node2, auth=Auth(user))
        new_linking_node.add_pointer(self.public_node, auth=Auth(user))
        new_linking_node.save()
        new_linking_registration = RegistrationFactory(
            project=new_linking_node, creator=self.user)

        res = self.app.get('/{}registrations/{}/linked_nodes/'.format(
            API_BASE, new_linking_registration._id),
                           auth=user.auth)

        assert_equal(res.status_code, 200)
        nodes_returned = [
            linked_node['id'] for linked_node in res.json['data']
        ]
        assert_equal(len(nodes_returned), len(self.node_ids))

        for node_id in self.node_ids:
            assert_in(node_id, nodes_returned)

        self.linked_node2.remove_contributor(user, auth=self.auth)
        self.public_node.remove_contributor(user, auth=self.auth)

        res = self.app.get('/{}registrations/{}/linked_nodes/'.format(
            API_BASE, new_linking_registration._id),
                           auth=user.auth)
        nodes_returned = [
            linked_node['id'] for linked_node in res.json['data']
        ]
        assert_equal(len(nodes_returned), len(self.node_ids) - 1)

        assert_in(self.linked_node._id, nodes_returned)
        assert_in(self.public_node._id, nodes_returned)
        assert_not_in(self.linked_node2._id, nodes_returned)
Exemple #26
0
def create_fake_project(creator, n_users, privacy, n_components, name, n_tags, presentation_name, is_registration, is_preprint, preprint_providers):
    auth = Auth(user=creator)
    project_title = name if name else fake.science_sentence()
    if is_preprint:
        providers_to_add = []
        if preprint_providers:
            providers = preprint_providers.split(',')
            for provider in providers:
                try:
                    preprint_provider = models.PreprintProvider.find_one(Q('_id', 'eq', provider))
                except NoResultsFound:
                    preprint_provider = PreprintProviderFactory(name=provider)
                providers_to_add.append(preprint_provider)
        privacy = 'public'
        project = PreprintFactory(title=project_title, description=fake.science_paragraph(), creator=creator, providers=providers_to_add)
    elif is_registration:
        project = RegistrationFactory(title=project_title, description=fake.science_paragraph(), creator=creator)
    else:
        project = ProjectFactory(title=project_title, description=fake.science_paragraph(), creator=creator)
    project.set_privacy(privacy)
    for _ in range(n_users):
        contrib = create_fake_user()
        project.add_contributor(contrib, auth=auth)
    if isinstance(n_components, int):
        for _ in range(n_components):
            NodeFactory(project=project, title=fake.science_sentence(), description=fake.science_paragraph(),
                        creator=creator)
    elif isinstance(n_components, list):
        render_generations_from_node_structure_list(project, creator, n_components)
    for _ in range(n_tags):
        project.add_tag(fake.science_word(), auth=auth)
    if presentation_name is not None:
        project.add_tag(presentation_name, auth=auth)
        project.add_tag('poster', auth=auth)

    project.save()
    logger.info('Created project: {0}'.format(project.title))
    return project
Exemple #27
0
    def test_node_serialization(self):
        parent = ProjectFactory(creator=self.user)
        node = NodeFactory(creator=self.user, parent=parent)
        req = make_drf_request_with_version(version='2.0')
        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['current_user_can_comment'], False)
        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)
Exemple #28
0
 def setUp(self):
     super(TestNodeRelationshipNodeLinks, self).setUp()
     self.user = AuthUserFactory()
     self.user2 = AuthUserFactory()
     self.auth = Auth(self.user)
     self.linking_node = NodeFactory(creator=self.user)
     self.admin_node = NodeFactory(creator=self.user)
     self.contributor_node = NodeFactory(creator=self.user2)
     self.contributor_node.add_contributor(self.user, auth=Auth(self.user2))
     self.contributor_node.save()
     self.other_node = NodeFactory()
     self.private_node = NodeFactory(creator=self.user)
     self.public_node = NodeFactory(is_public=True)
     self.linking_node.add_pointer(self.private_node, auth=self.auth)
     self.public_linking_node = NodeFactory(is_public=True,
                                            creator=self.user2)
     self.public_linking_node.add_pointer(self.private_node,
                                          auth=Auth(self.user2))
     self.public_linking_node.add_pointer(self.public_node,
                                          auth=Auth(self.user2))
     self.url = '/{}nodes/{}/relationships/linked_nodes/'.format(
         API_BASE, self.linking_node._id)
     self.public_url = '/{}nodes/{}/relationships/linked_nodes/'.format(
         API_BASE, self.public_linking_node._id)
Exemple #29
0
 def test_wiki_url_for_component_returns_200(self):
     component = NodeFactory(project=self.project, is_public=True)
     url = component.web_url_for('project_wiki_page', wname='home')
     res = self.app.get(url)
     assert_equal(res.status_code, 200)
Exemple #30
0
 def setUp(self):
     super(TestFindGuidsWithoutReferents, self).setUp()
     self.node = NodeFactory()
     self.nontarget_guid= Guid(referent=self.node)
     self.nontarget_guid.save()