class RegistrationListFilteringMixin(object):
    def setUp(self):
        super(RegistrationListFilteringMixin, self).setUp()

        assert self.url, 'Subclasses of RegistrationListFilteringMixin must define self.url'

        self.user = AuthUserFactory()
        self.user_two = AuthUserFactory()

        self.A = ProjectFactory(creator=self.user)
        self.B1 = NodeFactory(parent=self.A, creator=self.user)
        self.B2 = NodeFactory(parent=self.A, creator=self.user)
        self.C1 = NodeFactory(parent=self.B1, creator=self.user)
        self.C2 = NodeFactory(parent=self.B2, creator=self.user)
        self.D2 = NodeFactory(parent=self.C2, creator=self.user)

        self.A.add_contributor(self.user_two, save=True)

        self.node_A = RegistrationFactory(project=self.A, creator=self.user)
        self.node_B2 = RegistrationFactory(project=self.B2, creator=self.user)

        self.parent_url = '{}filter[parent]='.format(self.url)
        self.root_url = '{}filter[root]='.format(self.url)
        self.tags_url = '{}filter[tags]='.format(self.url)
        self.contributors_url = '{}filter[contributors]='.format(self.url)

    def test_parent_filter_null(self):
        expected = [self.node_A._id, self.node_B2._id]
        res = self.app.get('{}null'.format(self.parent_url),
                           auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(set(expected), set(actual))

    def test_parent_filter_equals_returns_one(self):
        expected = [n._id for n in self.node_B2.get_nodes()]
        res = self.app.get('{}{}'.format(self.parent_url, self.node_B2._id),
                           auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(len(actual), 1)
        assert_equal(expected, actual)

    def test_parent_filter_equals_returns_multiple(self):
        expected = [n._id for n in self.node_A.get_nodes()]
        res = self.app.get('{}{}'.format(self.parent_url, self.node_A._id),
                           auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(len(actual), 2)
        assert_equal(set(expected), set(actual))

    def test_root_filter_null(self):
        res = self.app.get('{}null'.format(self.root_url),
                           auth=self.user.auth,
                           expect_errors=True)
        assert_equal(res.status_code, 400)
        assert_equal(res.json['errors'][0]['source']['parameter'], 'filter')

    def test_root_filter_equals_returns_branch(self):
        expected = [n._id for n in Node.objects.get_children(self.node_B2)]
        expected.append(self.node_B2._id)
        res = self.app.get('{}{}'.format(self.root_url, self.node_B2._id),
                           auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(set(expected), set(actual))

    def test_root_filter_equals_returns_tree(self):
        expected = [n._id for n in Node.objects.get_children(self.node_A)]
        expected.append(self.node_A._id)
        res = self.app.get('{}{}'.format(self.root_url, self.node_A._id),
                           auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(len(actual), 6)
        assert_equal(set(expected), set(actual))

    def test_tag_filter(self):
        self.node_A.add_tag('nerd', auth=Auth(self.node_A.creator), save=True)
        expected = [self.node_A._id]
        res = self.app.get('{}nerd'.format(self.tags_url), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(expected, actual)

        res = self.app.get('{}bird'.format(self.tags_url), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal([], actual)

    def test_contributor_filter(self):
        expected = [self.node_A._id]
        res = self.app.get('{}{}'.format(self.contributors_url,
                                         self.user_two._id),
                           auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(expected, actual)
class TestDataCiteViews(OsfTestCase):
    """ This tests the v1 views for Project/Registration DOI creation."""

    def setUp(self):
        super(TestDataCiteViews, self).setUp()
        self.user = AuthUserFactory()
        self.node = RegistrationFactory(creator=self.user, is_public=True)
        self.client = DataCiteClient(base_url = 'https://mds.fake.datacite.org', prefix=settings.DATACITE_PREFIX)

    @responses.activate
    def test_datacite_create_identifiers_not_exists(self):
        responses.add(
            responses.Response(
                responses.POST,
                self.client.base_url + '/metadata',
                body='OK (10.5072/FK2osf.io/cq695)',
                status=201,
            )
        )
        responses.add(
            responses.Response(
                responses.POST,
                self.client.base_url + '/doi',
                body='OK (10.5072/FK2osf.io/cq695)',
                status=201,
            )
        )
        with mock.patch('osf.models.Registration.get_doi_client') as mock_get_doi:
            mock_get_doi.return_value = self.client
            res = self.app.post(
                self.node.api_url_for('node_identifiers_post'),
                auth=self.user.auth,
            )
        self.node.reload()
        assert res.json['doi'] == self.node.get_identifier_value('doi')
        assert res.status_code == 201

    @responses.activate
    def test_datacite_get_by_identifier(self):
        self.node.set_identifier_value('doi', 'FK424601')
        self.node.set_identifier_value('ark', 'fk224601')

        with mock.patch('osf.models.Registration.get_doi_client') as mock_get_doi:
            mock_get_doi.return_value = self.client

            res_doi = self.app.get(
                self.node.web_url_for(
                    'get_referent_by_identifier',
                    category='doi',
                    value=self.node.get_identifier_value('doi'),
                ),
            )

        assert res_doi.status_code == 302
        assert_urls_equal(res_doi.headers['Location'], self.node.absolute_url)

    @responses.activate
    def test_datacite_get_by_identifier_not_found(self):
        self.node.set_identifier_value('doi', 'FK424601')
        with mock.patch('osf.models.Registration.get_doi_client') as mock_get_doi:
            mock_get_doi.return_value = self.client
            res = self.app.get(
                self.node.web_url_for(
                    'get_referent_by_identifier',
                    category='doi',
                    value='fakedoi',
                ),
                expect_errors=True,
            )
        assert res.status_code == 404

    def test_qatest_doesnt_make_dois(self):
        self.node.add_tag('qatest', auth=Auth(self.user))
        assert not request_identifiers(self.node)
class TestDataCiteViews(OsfTestCase):
    """ This tests the v1 views for Project/Registration DOI creation."""
    def setUp(self):
        super(TestDataCiteViews, self).setUp()
        self.user = AuthUserFactory()
        self.node = RegistrationFactory(creator=self.user, is_public=True)
        self.client = DataCiteClient(base_url='https://mds.fake.datacite.org',
                                     prefix=settings.DATACITE_PREFIX)

    @responses.activate
    def test_datacite_create_identifiers_not_exists(self):
        responses.add(
            responses.Response(
                responses.POST,
                self.client.base_url + '/metadata',
                body='OK (10.70102/FK2osf.io/cq695)',
                status=201,
            ))
        responses.add(
            responses.Response(
                responses.POST,
                self.client.base_url + '/doi',
                body='OK (10.70102/FK2osf.io/cq695)',
                status=201,
            ))
        with mock.patch(
                'osf.models.Registration.get_doi_client') as mock_get_doi:
            mock_get_doi.return_value = self.client
            res = self.app.post(
                self.node.api_url_for('node_identifiers_post'),
                auth=self.user.auth,
            )
        self.node.reload()
        assert res.json['doi'] == self.node.get_identifier_value('doi')
        assert res.status_code == 201

    @responses.activate
    def test_datacite_get_by_identifier(self):
        self.node.set_identifier_value('doi', 'FK424601')
        self.node.set_identifier_value('ark', 'fk224601')

        with mock.patch(
                'osf.models.Registration.get_doi_client') as mock_get_doi:
            mock_get_doi.return_value = self.client

            res_doi = self.app.get(
                self.node.web_url_for(
                    'get_referent_by_identifier',
                    category='doi',
                    value=self.node.get_identifier_value('doi'),
                ), )

        assert res_doi.status_code == 302
        assert_urls_equal(res_doi.headers['Location'], self.node.absolute_url)

    @responses.activate
    def test_datacite_get_by_identifier_not_found(self):
        self.node.set_identifier_value('doi', 'FK424601')
        with mock.patch(
                'osf.models.Registration.get_doi_client') as mock_get_doi:
            mock_get_doi.return_value = self.client
            res = self.app.get(
                self.node.web_url_for(
                    'get_referent_by_identifier',
                    category='doi',
                    value='fakedoi',
                ),
                expect_errors=True,
            )
        assert res.status_code == 404

    def test_qatest_doesnt_make_dois(self):
        self.node.add_tag('qatest', auth=Auth(self.user))
        assert not request_identifiers(self.node)
Exemple #4
0
class RegistrationListFilteringMixin(object):

    def setUp(self):
        super(RegistrationListFilteringMixin, self).setUp()

        assert self.url, 'Subclasses of RegistrationListFilteringMixin must define self.url'

        self.user = AuthUserFactory()
        self.user_two = AuthUserFactory()

        self.A = ProjectFactory(creator=self.user)
        self.B1 = NodeFactory(parent=self.A, creator=self.user)
        self.B2 = NodeFactory(parent=self.A, creator=self.user)
        self.C1 = NodeFactory(parent=self.B1, creator=self.user)
        self.C2 = NodeFactory(parent=self.B2, creator=self.user)
        self.D2 = NodeFactory(parent=self.C2, creator=self.user)

        self.A.add_contributor(self.user_two, save=True)

        self.node_A = RegistrationFactory(project=self.A, creator=self.user)
        self.node_B2 = RegistrationFactory(project=self.B2, creator=self.user)

        self.parent_url = '{}filter[parent]='.format(self.url)
        self.root_url ='{}filter[root]='.format(self.url)
        self.tags_url ='{}filter[tags]='.format(self.url)
        self.contributors_url ='{}filter[contributors]='.format(self.url)

    def test_parent_filter_null(self):
        expected = [self.node_A._id, self.node_B2._id]
        res = self.app.get('{}null'.format(self.parent_url), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(set(expected), set(actual))

    def test_parent_filter_equals_returns_one(self):
        expected = [n._id for n in self.node_B2.get_nodes()]
        res = self.app.get('{}{}'.format(self.parent_url, self.node_B2._id), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(len(actual), 1)
        assert_equal(expected, actual)

    def test_parent_filter_equals_returns_multiple(self):
        expected = [n._id for n in self.node_A.get_nodes()]
        res = self.app.get('{}{}'.format(self.parent_url, self.node_A._id), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(len(actual), 2)
        assert_equal(set(expected), set(actual))

    def test_root_filter_null(self):
        res = self.app.get('{}null'.format(self.root_url), auth=self.user.auth, expect_errors=True)
        assert_equal(res.status_code, 400)
        assert_equal(res.json['errors'][0]['source']['parameter'], 'filter')

    def test_root_filter_equals_returns_branch(self):
        expected = [n._id for n in Node.objects.get_children(self.node_B2)]
        expected.append(self.node_B2._id)
        res = self.app.get('{}{}'.format(self.root_url, self.node_B2._id), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(set(expected), set(actual))

    def test_root_filter_equals_returns_tree(self):
        expected = [n._id for n in Node.objects.get_children(self.node_A)]
        expected.append(self.node_A._id)
        res = self.app.get('{}{}'.format(self.root_url, self.node_A._id), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(len(actual), 6)
        assert_equal(set(expected), set(actual))

    def test_tag_filter(self):
        self.node_A.add_tag('nerd', auth=Auth(self.node_A.creator), save=True)
        expected = [self.node_A._id]
        res = self.app.get('{}nerd'.format(self.tags_url), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(expected, actual)

        res = self.app.get('{}bird'.format(self.tags_url), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal([], actual)

    def test_contributor_filter(self):
        expected = [self.node_A._id]
        res = self.app.get('{}{}'.format(self.contributors_url, self.user_two._id), auth=self.user.auth)
        actual = [node['id'] for node in res.json['data']]
        assert_equal(expected, actual)