|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2016 Gauvain Pocentek <gauvain@pocentek.net> |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU Lesser General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +try: |
| 19 | + import unittest |
| 20 | +except ImportError: |
| 21 | + import unittest2 as unittest |
| 22 | + |
| 23 | +import mock |
| 24 | +import six |
| 25 | + |
| 26 | +from gitlab import config |
| 27 | + |
| 28 | + |
| 29 | +valid_config = u"""[global] |
| 30 | +default = one |
| 31 | +ssl_verify = true |
| 32 | +timeout = 2 |
| 33 | +
|
| 34 | +[one] |
| 35 | +url = http://one.url |
| 36 | +private_token = ABCDEF |
| 37 | +
|
| 38 | +[two] |
| 39 | +url = https://two.url |
| 40 | +private_token = GHIJKL |
| 41 | +ssl_verify = false |
| 42 | +timeout = 10 |
| 43 | +""" |
| 44 | + |
| 45 | +no_default_config = u"""[global] |
| 46 | +[there] |
| 47 | +url = http://there.url |
| 48 | +private_token = ABCDEF |
| 49 | +""" |
| 50 | + |
| 51 | +missing_attr_config = u"""[global] |
| 52 | +[one] |
| 53 | +url = http://one.url |
| 54 | +
|
| 55 | +[two] |
| 56 | +private_token = ABCDEF |
| 57 | +
|
| 58 | +[three] |
| 59 | +meh = hem |
| 60 | +""" |
| 61 | + |
| 62 | + |
| 63 | +class TestConfigParser(unittest.TestCase): |
| 64 | + @mock.patch('six.moves.builtins.open') |
| 65 | + def test_invalid_id(self, m_open): |
| 66 | + fd = six.StringIO(no_default_config) |
| 67 | + fd.close = mock.Mock(return_value=None) |
| 68 | + m_open.return_value = fd |
| 69 | + self.assertRaises(config.GitlabIDError, config.GitlabConfigParser) |
| 70 | + |
| 71 | + fd = six.StringIO(valid_config) |
| 72 | + fd.close = mock.Mock(return_value=None) |
| 73 | + m_open.return_value = fd |
| 74 | + self.assertRaises(config.GitlabDataError, |
| 75 | + config.GitlabConfigParser, |
| 76 | + gitlab_id='not_there') |
| 77 | + |
| 78 | + @mock.patch('six.moves.builtins.open') |
| 79 | + def test_invalid_data(self, m_open): |
| 80 | + fd = six.StringIO(missing_attr_config) |
| 81 | + fd.close = mock.Mock(return_value=None) |
| 82 | + m_open.return_value = fd |
| 83 | + self.assertRaises(config.GitlabDataError, config.GitlabConfigParser, |
| 84 | + gitlab_id='one') |
| 85 | + self.assertRaises(config.GitlabDataError, config.GitlabConfigParser, |
| 86 | + gitlab_id='two') |
| 87 | + self.assertRaises(config.GitlabDataError, config.GitlabConfigParser, |
| 88 | + gitlab_id='three') |
| 89 | + |
| 90 | + @mock.patch('six.moves.builtins.open') |
| 91 | + def test_valid_data(self, m_open): |
| 92 | + fd = six.StringIO(valid_config) |
| 93 | + fd.close = mock.Mock(return_value=None) |
| 94 | + m_open.return_value = fd |
| 95 | + |
| 96 | + cp = config.GitlabConfigParser() |
| 97 | + self.assertEqual("one", cp.gitlab_id) |
| 98 | + self.assertEqual("http://one.url", cp.url) |
| 99 | + self.assertEqual("ABCDEF", cp.token) |
| 100 | + self.assertEqual(2, cp.timeout) |
| 101 | + self.assertEqual(True, cp.ssl_verify) |
| 102 | + |
| 103 | + fd = six.StringIO(valid_config) |
| 104 | + fd.close = mock.Mock(return_value=None) |
| 105 | + m_open.return_value = fd |
| 106 | + cp = config.GitlabConfigParser(gitlab_id="two") |
| 107 | + self.assertEqual("two", cp.gitlab_id) |
| 108 | + self.assertEqual("https://two.url", cp.url) |
| 109 | + self.assertEqual("GHIJKL", cp.token) |
| 110 | + self.assertEqual(10, cp.timeout) |
| 111 | + self.assertEqual(False, cp.ssl_verify) |
0 commit comments