summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/config.py5
-rw-r--r--gitlab/tests/test_config.py111
-rw-r--r--test-requirements.txt1
3 files changed, 113 insertions, 4 deletions
diff --git a/gitlab/config.py b/gitlab/config.py
index c9dc5aa..4d0abb8 100644
--- a/gitlab/config.py
+++ b/gitlab/config.py
@@ -15,12 +15,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-try:
- import ConfigParser as configparser
-except ImportError:
- import configparser
import os
+from six.moves import configparser
_DEFAULT_FILES = [
'/etc/python-gitlab.cfg',
diff --git a/gitlab/tests/test_config.py b/gitlab/tests/test_config.py
new file mode 100644
index 0000000..2b9cce4
--- /dev/null
+++ b/gitlab/tests/test_config.py
@@ -0,0 +1,111 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2016 Gauvain Pocentek <gauvain@pocentek.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+try:
+ import unittest
+except ImportError:
+ import unittest2 as unittest
+
+import mock
+import six
+
+from gitlab import config
+
+
+valid_config = u"""[global]
+default = one
+ssl_verify = true
+timeout = 2
+
+[one]
+url = http://one.url
+private_token = ABCDEF
+
+[two]
+url = https://two.url
+private_token = GHIJKL
+ssl_verify = false
+timeout = 10
+"""
+
+no_default_config = u"""[global]
+[there]
+url = http://there.url
+private_token = ABCDEF
+"""
+
+missing_attr_config = u"""[global]
+[one]
+url = http://one.url
+
+[two]
+private_token = ABCDEF
+
+[three]
+meh = hem
+"""
+
+
+class TestConfigParser(unittest.TestCase):
+ @mock.patch('six.moves.builtins.open')
+ def test_invalid_id(self, m_open):
+ fd = six.StringIO(no_default_config)
+ fd.close = mock.Mock(return_value=None)
+ m_open.return_value = fd
+ self.assertRaises(config.GitlabIDError, config.GitlabConfigParser)
+
+ fd = six.StringIO(valid_config)
+ fd.close = mock.Mock(return_value=None)
+ m_open.return_value = fd
+ self.assertRaises(config.GitlabDataError,
+ config.GitlabConfigParser,
+ gitlab_id='not_there')
+
+ @mock.patch('six.moves.builtins.open')
+ def test_invalid_data(self, m_open):
+ fd = six.StringIO(missing_attr_config)
+ fd.close = mock.Mock(return_value=None)
+ m_open.return_value = fd
+ self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
+ gitlab_id='one')
+ self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
+ gitlab_id='two')
+ self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
+ gitlab_id='three')
+
+ @mock.patch('six.moves.builtins.open')
+ def test_valid_data(self, m_open):
+ fd = six.StringIO(valid_config)
+ fd.close = mock.Mock(return_value=None)
+ m_open.return_value = fd
+
+ cp = config.GitlabConfigParser()
+ self.assertEqual("one", cp.gitlab_id)
+ self.assertEqual("http://one.url", cp.url)
+ self.assertEqual("ABCDEF", cp.token)
+ self.assertEqual(2, cp.timeout)
+ self.assertEqual(True, cp.ssl_verify)
+
+ fd = six.StringIO(valid_config)
+ fd.close = mock.Mock(return_value=None)
+ m_open.return_value = fd
+ cp = config.GitlabConfigParser(gitlab_id="two")
+ self.assertEqual("two", cp.gitlab_id)
+ self.assertEqual("https://two.url", cp.url)
+ self.assertEqual("GHIJKL", cp.token)
+ self.assertEqual(10, cp.timeout)
+ self.assertEqual(False, cp.ssl_verify)
diff --git a/test-requirements.txt b/test-requirements.txt
index c16328a..87b1721 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -2,5 +2,6 @@ discover
testrepository
hacking>=0.9.2,<0.10
httmock
+mock
sphinx>=1.1.2,!=1.2.0,<1.3
sphinxcontrib-napoleon