Exemple #1
0
class TestRmapNodeViews(OsfTestCase):
    def setUp(self):
        super(TestRmapNodeViews, self).setUp()
        self.user = AuthUserFactory()
        self.project = ProjectFactory(creator=self.user, is_public=True)
        self.url = self.project.api_url_for('node_rmap_post')

        self._old_rmap_url = settings.RMAP_BASE_URL
        self._old_rmap_pass = settings.RMAP_PASS

        settings.RMAP_BASE_URL = 'rmaptest.test/'
        settings.RMAP_PASS = '******'

        self._set_up_mock_response_for_node(self.project)

    def _set_up_mock_response_for_node(self, node):
        rmap_url = _rmap_url_for_node(node)
        httpretty.register_uri(httpretty.POST,
                               rmap_url,
                               body='abc123',
                               status=200)

    def tearDown(self):
        super(TestRmapNodeViews, self).tearDown()
        settings.RMAP_BASE_URL = self._old_rmap_url
        settings.RMAP_PASS = self._old_rmap_pass

    def test_rmap_get_valid_no_disco_id(self):
        res = self.app.get(self.url, auth=self.user.auth)
        assert_equal(res.status_code, 200)
        assert_equal(res.json['disco_id'], None)

    def test_rmap_get_valid_with_disco_id(self):
        disco_id = 'pan1cath3d1sc0'
        self.project.set_identifier_value('disco', disco_id)
        res = self.app.get(self.url, auth=self.user.auth)
        assert_equal(res.status_code, 200)
        assert_equal(res.json['disco_id'], disco_id)

    def test_rmap_post_valid(self):
        res = self.app.post_json(self.url, {}, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        self.project.reload()

        rmap_id = self.project.get_identifier('disco')
        assert_true(bool(rmap_id))
        assert_equal(rmap_id.value, 'abc123')

    def test_rmap_post_non_contributor_should_error(self):
        noncontrib = AuthUserFactory()
        res = self.app.post_json(self.url, {},
                                 auth=noncontrib.auth,
                                 expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_rmap_post_non_admin_contributor_should_error(self):
        non_admin = AuthUserFactory()
        self.project.add_contributor(non_admin,
                                     permissions=['read', 'write'],
                                     auth=Auth(self.user))
        self.project.save()
        res = self.app.post_json(self.url, {},
                                 auth=non_admin.auth,
                                 expect_errors=True)
        assert_equal(res.status_code, 403)

    def test_rmap_post_non_public_project_should_error(self):
        private_project = ProjectFactory(creator=self.user, is_public=False)
        url = private_project.api_url_for('node_rmap_post')
        res = self.app.post_json(url, {},
                                 auth=self.user.auth,
                                 expect_errors=True)
        assert_equal(res.status_code, 400)