diff options
author | Gauvain Pocentek <gauvain@pocentek.net> | 2017-11-10 09:09:18 +0100 |
---|---|---|
committer | Gauvain Pocentek <gauvain@pocentek.net> | 2017-11-10 09:09:18 +0100 |
commit | e9b158363e5b0ea451638b1c3a660f138a24521d (patch) | |
tree | 06700ef327e9ddec0b453938da593329a44e2da5 /gitlab/tests/test_gitlab.py | |
parent | ba6e09ec804bf5cea39282590bb4cb829a836873 (diff) | |
download | gitlab-e9b158363e5b0ea451638b1c3a660f138a24521d.tar.gz |
Rework authentication args handling
* Raise exceptions when conflicting arguments are used
* Build the auth headers when instanciating Gitlab, not on each request
* Enable anonymous Gitlab objects (#364)
Add docs and unit tests
Diffstat (limited to 'gitlab/tests/test_gitlab.py')
-rw-r--r-- | gitlab/tests/test_gitlab.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index 027de0c..d9853d0 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -27,6 +27,7 @@ except ImportError: from httmock import HTTMock # noqa from httmock import response # noqa from httmock import urlmatch # noqa +import requests import six import gitlab @@ -884,6 +885,54 @@ class TestGitlabMethods(unittest.TestCase): self.assertRaises(GitlabUpdateError, self.gl.update, obj) +class TestGitlabAuth(unittest.TestCase): + def test_invalid_auth_args(self): + self.assertRaises(ValueError, + Gitlab, + "http://localhost", api_version='4', + private_token='private_token', oauth_token='bearer') + self.assertRaises(ValueError, + Gitlab, + "http://localhost", api_version='4', + oauth_token='bearer', http_username='foo', + http_password='bar') + self.assertRaises(ValueError, + Gitlab, + "http://localhost", api_version='4', + private_token='private_token', http_password='bar') + self.assertRaises(ValueError, + Gitlab, + "http://localhost", api_version='4', + private_token='private_token', http_username='foo') + + def test_private_token_auth(self): + gl = Gitlab('http://localhost', private_token='private_token', + api_version='4') + self.assertEqual(gl.private_token, 'private_token') + self.assertEqual(gl.oauth_token, None) + self.assertEqual(gl._http_auth, None) + self.assertEqual(gl.headers['PRIVATE-TOKEN'], 'private_token') + self.assertNotIn('Authorization', gl.headers) + + def test_oauth_token_auth(self): + gl = Gitlab('http://localhost', oauth_token='oauth_token', + api_version='4') + self.assertEqual(gl.private_token, None) + self.assertEqual(gl.oauth_token, 'oauth_token') + self.assertEqual(gl._http_auth, None) + self.assertEqual(gl.headers['Authorization'], 'Bearer oauth_token') + self.assertNotIn('PRIVATE-TOKEN', gl.headers) + + def test_http_auth(self): + gl = Gitlab('http://localhost', private_token='private_token', + http_username='foo', http_password='bar', api_version='4') + self.assertEqual(gl.private_token, 'private_token') + self.assertEqual(gl.oauth_token, None) + self.assertIsInstance(gl._http_auth, requests.auth.HTTPBasicAuth) + self.assertEqual(gl.headers['PRIVATE-TOKEN'], 'private_token') + self.assertNotIn('Authorization', gl.headers) + + class TestGitlab(unittest.TestCase): def setUp(self): |