Beispiel #1
0
class TestGitHubOAuthMixin(TestController, TestCase):
    def setUp(self):
        super(TestGitHubOAuthMixin, self).setUp()
        c.user = Mock()
        self.mix = GitHubOAuthMixin()

    def test_oauth_has_access_no_scope(self):
        self.assertFalse(self.mix.oauth_has_access(None))
        self.assertFalse(self.mix.oauth_has_access(''))

    def test_oauth_has_access_no_token(self):
        c.user.get_tool_data.return_value = None
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_no_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.assert_called_once_with(
            'https://api.github.com/?access_token=token', timeout=10)

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_with_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        req.head.return_value.headers = {'X-OAuth-Scopes': ''}
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {
            'X-OAuth-Scopes': 'some, other:scopes'
        }
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {
            'X-OAuth-Scopes': 'write:repo_hook, user'
        }
        self.assertTrue(self.mix.oauth_has_access('write:repo_hook'))

    @patch.dict(
        config, {
            'github_importer.client_id': '123456',
            'github_importer.client_secret': 'deadbeef'
        })
    @patch('forgeimporters.github.OAuth2Session', MagicMock())
    @patch('forgeimporters.github.session', MagicMock())
    @patch('forgeimporters.github.request', MagicMock())
    def test_oauth_callback_complete(self):
        with patch.object(
                self.mix,
                'oauth_callback_complete') as _mock, raises(HTTPFound):
            self.mix.oauth_callback()
        self.assertEqual(_mock.call_count, 1)
Beispiel #2
0
class TestGitHubOAuthMixin(TestController, TestCase):

    def setUp(self):
        super(TestGitHubOAuthMixin, self).setUp()
        c.user = Mock()
        self.mix = GitHubOAuthMixin()

    def test_oauth_has_access_no_scope(self):
        self.assertFalse(self.mix.oauth_has_access(None))
        self.assertFalse(self.mix.oauth_has_access(''))

    def test_oauth_has_access_no_token(self):
        c.user.get_tool_data.return_value = None
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_no_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.assert_called_once_with('https://api.github.com/?access_token=token', timeout=10)

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_with_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        req.head.return_value.headers = {'X-OAuth-Scopes': ''}
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {'X-OAuth-Scopes': 'some, other:scopes'}
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {'X-OAuth-Scopes': 'write:repo_hook, user'}
        self.assertTrue(self.mix.oauth_has_access('write:repo_hook'))

    @patch.dict(config, {'github_importer.client_id': '123456',
                         'github_importer.client_secret': 'deadbeef'})
    @patch('forgeimporters.github.OAuth2Session', MagicMock())
    @patch('forgeimporters.github.session', MagicMock())
    @patch('forgeimporters.github.request', MagicMock())
    def test_oauth_callback_complete(self):
        with patch.object(self.mix, 'oauth_callback_complete') as _mock, \
                patch('forgeimporters.github.redirect') as tg_redir:
            self.mix.oauth_callback()
        self.assertEqual(_mock.call_count, 1)
        self.assertEqual(tg_redir.call_count, 1)
Beispiel #3
0
class TestGitHubOAuthMixin(TestController, TestCase):
    def setUp(self):
        super(TestGitHubOAuthMixin, self).setUp()
        c.user = Mock()
        self.mix = GitHubOAuthMixin()

    def test_oauth_has_access_no_scope(self):
        self.assertFalse(self.mix.oauth_has_access(None))
        self.assertFalse(self.mix.oauth_has_access(''))

    def test_oauth_has_access_no_token(self):
        c.user.get_tool_data.return_value = None
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_no_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.assert_called_once_with(
            'https://api.github.com/?access_token=token')

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_with_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        req.head.return_value.headers = {'X-OAuth-Scopes': ''}
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {
            'X-OAuth-Scopes': 'some, other:scopes'
        }
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {
            'X-OAuth-Scopes': 'write:repo_hook, user'
        }
        self.assertTrue(self.mix.oauth_has_access('write:repo_hook'))

    @patch('forgeimporters.github.OAuth2Session')
    def test_oauth_callback_complete(self, oauth):
        with patch.object(self.mix, 'oauth_callback_complete') as _mock:
            self.mix.oauth_callback()
            _mock.assert_called_once()
Beispiel #4
0
class TestGitHubOAuthMixin(TestController, TestCase):

    def setUp(self):
        super(TestGitHubOAuthMixin, self).setUp()
        c.user = Mock()
        self.mix = GitHubOAuthMixin()

    def test_oauth_has_access_no_scope(self):
        self.assertFalse(self.mix.oauth_has_access(None))
        self.assertFalse(self.mix.oauth_has_access(''))

    def test_oauth_has_access_no_token(self):
        c.user.get_tool_data.return_value = None
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_no_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.assert_called_once_with('https://api.github.com/?access_token=token')

    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_with_headers(self, req):
        c.user.get_tool_data.return_value = 'token'
        req.head.return_value.headers = {'X-OAuth-Scopes': ''}
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {'X-OAuth-Scopes': 'some, other:scopes'}
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        req.head.return_value.headers = {'X-OAuth-Scopes': 'write:repo_hook, user'}
        self.assertTrue(self.mix.oauth_has_access('write:repo_hook'))

    @patch('forgeimporters.github.OAuth2Session')
    def test_oauth_callback_complete(self, oauth):
        with patch.object(self.mix, 'oauth_callback_complete') as _mock:
            self.mix.oauth_callback()
            _mock.assert_called_once()
Beispiel #5
0
class TestGitHubOAuthMixin(TestController, TestCase):
    def setUp(self):
        super(TestGitHubOAuthMixin, self).setUp()
        c.user = Mock()
        self.mix = GitHubOAuthMixin()

    def test_oauth_has_access_no_scope(self):
        self.assertFalse(self.mix.oauth_has_access(None))
        self.assertFalse(self.mix.oauth_has_access(''))

    def test_oauth_has_access_no_token(self):
        c.user.get_tool_data.return_value = None
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))

    @patch.dict(
        config, {
            'github_importer.client_id': '123456',
            'github_importer.client_secret': 'deadbeef'
        })
    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_no(self, req):
        c.user.get_tool_data.return_value = 'some-token'
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))
        call_args = req.post.call_args[0]
        self.assertEqual(
            call_args, ('https://api.github.com/applications/123456/token', ))
        call_kwargs = req.post.call_args[1]
        assert call_kwargs['auth']
        self.assertEqual(call_kwargs['json'], {'access_token': 'some-token'})

    @patch.dict(
        config, {
            'github_importer.client_id': '123456',
            'github_importer.client_secret': 'deadbeef'
        })
    @patch('forgeimporters.github.requests')
    def test_oauth_has_access_yes(self, req):
        c.user.get_tool_data.return_value = 'some-token'

        req.post.return_value.json.return_value = {'scopes': []}
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))

        req.post.return_value.json.return_value = {
            'scopes': ['some', 'other:scopes']
        }
        self.assertFalse(self.mix.oauth_has_access('write:repo_hook'))

        req.post.return_value.json.return_value = {
            'scopes': ['write:repo_hook', 'user']
        }
        self.assertTrue(self.mix.oauth_has_access('write:repo_hook'))

    @patch.dict(
        config, {
            'github_importer.client_id': '123456',
            'github_importer.client_secret': 'deadbeef'
        })
    @patch('forgeimporters.github.OAuth2Session', MagicMock())
    @patch('forgeimporters.github.session', MagicMock())
    @patch('forgeimporters.github.request', MagicMock())
    def test_oauth_callback_complete(self):
        with patch.object(self.mix, 'oauth_callback_complete') as _mock, \
                patch('forgeimporters.github.redirect') as tg_redir:
            self.mix.oauth_callback()
        self.assertEqual(_mock.call_count, 1)
        self.assertEqual(tg_redir.call_count, 1)