Esempio n. 1
0
    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)

        fd = six.StringIO(valid_config)
        fd.close = mock.Mock(return_value=None)
        m_open.return_value = fd
        cp = config.GitlabConfigParser(gitlab_id="three")
        self.assertEqual("three", cp.gitlab_id)
        self.assertEqual("https://three.url", cp.url)
        self.assertEqual("MNOPQR", cp.token)
        self.assertEqual(2, cp.timeout)
        self.assertEqual("/path/to/CA/bundle.crt", cp.ssl_verify)
Esempio n. 2
0
def test_valid_data(m_open, monkeypatch):
    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd

    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        cp = config.GitlabConfigParser()
    assert "one" == cp.gitlab_id
    assert "http://one.url" == cp.url
    assert "ABCDEF" == cp.private_token
    assert cp.oauth_token is None
    assert 2 == cp.timeout
    assert cp.ssl_verify is True
    assert cp.per_page is None

    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        cp = config.GitlabConfigParser(gitlab_id="two")
    assert "two" == cp.gitlab_id
    assert "https://two.url" == cp.url
    assert "GHIJKL" == cp.private_token
    assert cp.oauth_token is None
    assert 10 == cp.timeout
    assert cp.ssl_verify is False

    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        cp = config.GitlabConfigParser(gitlab_id="three")
    assert "three" == cp.gitlab_id
    assert "https://three.url" == cp.url
    assert "MNOPQR" == cp.private_token
    assert cp.oauth_token is None
    assert 2 == cp.timeout
    assert "/path/to/CA/bundle.crt" == cp.ssl_verify
    assert 50 == cp.per_page

    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        cp = config.GitlabConfigParser(gitlab_id="four")
    assert "four" == cp.gitlab_id
    assert "https://four.url" == cp.url
    assert cp.private_token is None
    assert "STUV" == cp.oauth_token
    assert 2 == cp.timeout
    assert cp.ssl_verify is True
Esempio n. 3
0
def test_invalid_id(m_open, mock_clean_env, monkeypatch):
    fd = io.StringIO(no_default_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        config.GitlabConfigParser("there")
        with pytest.raises(config.GitlabIDError):
            config.GitlabConfigParser()
        fd = io.StringIO(valid_config)
        fd.close = mock.Mock(return_value=None)
        m_open.return_value = fd
        with pytest.raises(config.GitlabDataError):
            config.GitlabConfigParser(gitlab_id="not_there")
Esempio n. 4
0
def test_invalid_id(m_open, path_exists):
    fd = io.StringIO(no_default_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    path_exists.return_value = True
    config.GitlabConfigParser("there")
    with pytest.raises(config.GitlabIDError):
        config.GitlabConfigParser()

    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    with pytest.raises(config.GitlabDataError):
        config.GitlabConfigParser(gitlab_id="not_there")
Esempio n. 5
0
 def test_invalid_data(self, m_open):
     fd = six.StringIO(missing_attr_config)
     fd.close = mock.Mock(return_value=None,
                          side_effect=lambda: fd.seek(0))
     m_open.return_value = fd
     config.GitlabConfigParser('one')
     config.GitlabConfigParser('one')
     self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
                       gitlab_id='two')
     self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
                       gitlab_id='three')
     with self.assertRaises(config.GitlabDataError) as emgr:
         config.GitlabConfigParser('four')
     self.assertEqual('Unsupported per_page number: 200',
                      emgr.exception.args[0])
Esempio n. 6
0
def test_invalid_data(m_open, path_exists):
    fd = io.StringIO(missing_attr_config)
    fd.close = mock.Mock(return_value=None, side_effect=lambda: fd.seek(0))
    m_open.return_value = fd
    path_exists.return_value = True

    config.GitlabConfigParser("one")
    config.GitlabConfigParser("one")
    with pytest.raises(config.GitlabDataError):
        config.GitlabConfigParser(gitlab_id="two")
    with pytest.raises(config.GitlabDataError):
        config.GitlabConfigParser(gitlab_id="three")
    with pytest.raises(config.GitlabDataError) as emgr:
        config.GitlabConfigParser("four")
    assert "Unsupported per_page number: 200" == emgr.value.args[0]
Esempio n. 7
0
def test_valid_data(m_open, path_exists):
    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    path_exists.return_value = True

    cp = config.GitlabConfigParser()
    assert "one" == cp.gitlab_id
    assert "http://one.url" == cp.url
    assert "ABCDEF" == cp.private_token
    assert None == cp.oauth_token
    assert 2 == cp.timeout
    assert True == cp.ssl_verify
    assert cp.per_page is None

    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    cp = config.GitlabConfigParser(gitlab_id="two")
    assert "two" == cp.gitlab_id
    assert "https://two.url" == cp.url
    assert "GHIJKL" == cp.private_token
    assert None == cp.oauth_token
    assert 10 == cp.timeout
    assert False == cp.ssl_verify

    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    cp = config.GitlabConfigParser(gitlab_id="three")
    assert "three" == cp.gitlab_id
    assert "https://three.url" == cp.url
    assert "MNOPQR" == cp.private_token
    assert None == cp.oauth_token
    assert 2 == cp.timeout
    assert "/path/to/CA/bundle.crt" == cp.ssl_verify
    assert 50 == cp.per_page

    fd = io.StringIO(valid_config)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    cp = config.GitlabConfigParser(gitlab_id="four")
    assert "four" == cp.gitlab_id
    assert "https://four.url" == cp.url
    assert None == cp.private_token
    assert "STUV" == cp.oauth_token
    assert 2 == cp.timeout
    assert True == cp.ssl_verify
Esempio n. 8
0
    def test_valid_data(self, m_open, path_exists):
        fd = six.StringIO(valid_config)
        fd.close = mock.Mock(return_value=None)
        m_open.return_value = fd
        path_exists.return_value = True

        cp = config.GitlabConfigParser()
        self.assertEqual("one", cp.gitlab_id)
        self.assertEqual("http://one.url", cp.url)
        self.assertEqual("ABCDEF", cp.private_token)
        self.assertEqual(None, cp.oauth_token)
        self.assertEqual(2, cp.timeout)
        self.assertEqual(True, cp.ssl_verify)
        self.assertIsNone(cp.per_page)

        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.private_token)
        self.assertEqual(None, cp.oauth_token)
        self.assertEqual(10, cp.timeout)
        self.assertEqual(False, 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="three")
        self.assertEqual("three", cp.gitlab_id)
        self.assertEqual("https://three.url", cp.url)
        self.assertEqual("MNOPQR", cp.private_token)
        self.assertEqual(None, cp.oauth_token)
        self.assertEqual(2, cp.timeout)
        self.assertEqual("/path/to/CA/bundle.crt", cp.ssl_verify)
        self.assertEqual(50, cp.per_page)

        fd = six.StringIO(valid_config)
        fd.close = mock.Mock(return_value=None)
        m_open.return_value = fd
        cp = config.GitlabConfigParser(gitlab_id="four")
        self.assertEqual("four", cp.gitlab_id)
        self.assertEqual("https://four.url", cp.url)
        self.assertEqual(None, cp.private_token)
        self.assertEqual("STUV", cp.oauth_token)
        self.assertEqual(2, cp.timeout)
        self.assertEqual(True, cp.ssl_verify)
Esempio n. 9
0
def test_data_from_helper(m_open, monkeypatch, tmp_path):
    helper = tmp_path / "helper.sh"
    helper.write_text(
        dedent("""\
            #!/bin/sh
            echo "secret"
            """))
    helper.chmod(0o755)

    fd = io.StringIO(
        dedent(f"""\
            [global]
            default = helper

            [helper]
            url = https://helper.url
            oauth_token = helper: {helper}
            """))

    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        cp = config.GitlabConfigParser(gitlab_id="helper")
    assert "helper" == cp.gitlab_id
    assert "https://helper.url" == cp.url
    assert cp.private_token is None
    assert "secret" == cp.oauth_token
Esempio n. 10
0
def test_invalid_data(m_open, monkeypatch):
    fd = io.StringIO(missing_attr_config)
    fd.close = mock.Mock(return_value=None, side_effect=lambda: fd.seek(0))
    m_open.return_value = fd

    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        config.GitlabConfigParser("one")
        config.GitlabConfigParser("one")
        with pytest.raises(config.GitlabDataError):
            config.GitlabConfigParser(gitlab_id="two")
        with pytest.raises(config.GitlabDataError):
            config.GitlabConfigParser(gitlab_id="three")
        with pytest.raises(config.GitlabDataError) as emgr:
            config.GitlabConfigParser("four")
        assert "Unsupported per_page number: 200" == emgr.value.args[0]
Esempio n. 11
0
def test_config_user_agent(m_open, path_exists, config_string, expected_agent):
    fd = io.StringIO(config_string)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd

    cp = config.GitlabConfigParser()
    assert cp.user_agent == expected_agent
Esempio n. 12
0
def test_data_from_helper(m_open, path_exists, tmp_path):
    helper = tmp_path / "helper.sh"
    helper.write_text(
        dedent(
            """\
            #!/bin/sh
            echo "secret"
            """
        )
    )
    helper.chmod(0o755)

    fd = io.StringIO(
        dedent(
            """\
            [global]
            default = helper

            [helper]
            url = https://helper.url
            oauth_token = helper: %s
            """
        )
        % helper
    )

    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd
    cp = config.GitlabConfigParser(gitlab_id="helper")
    assert "helper" == cp.gitlab_id
    assert "https://helper.url" == cp.url
    assert cp.private_token is None
    assert "secret" == cp.oauth_token
Esempio n. 13
0
def test_config_user_agent(m_open, monkeypatch, config_string, expected_agent):
    fd = io.StringIO(config_string)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd

    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        cp = config.GitlabConfigParser()
    assert cp.user_agent == expected_agent
Esempio n. 14
0
def test_config_retry_transient_errors_when_global_config_is_set(
    m_open, path_exists, config_string, expected
):
    fd = io.StringIO(config_string)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd

    cp = config.GitlabConfigParser()
    assert cp.retry_transient_errors == expected
Esempio n. 15
0
    def test_invalid_data(self, m_open, path_exists):
        fd = six.StringIO(missing_attr_config)
        fd.close = mock.Mock(return_value=None, side_effect=lambda: fd.seek(0))
        m_open.return_value = fd
        path_exists.return_value = True

        config.GitlabConfigParser("one")
        config.GitlabConfigParser("one")
        self.assertRaises(config.GitlabDataError,
                          config.GitlabConfigParser,
                          gitlab_id="two")
        self.assertRaises(config.GitlabDataError,
                          config.GitlabConfigParser,
                          gitlab_id="three")
        with self.assertRaises(config.GitlabDataError) as emgr:
            config.GitlabConfigParser("four")
        self.assertEqual("Unsupported per_page number: 200",
                         emgr.exception.args[0])
Esempio n. 16
0
def test_config_retry_transient_errors_when_global_config_is_set(
        m_open, monkeypatch, config_string, expected):
    fd = io.StringIO(config_string)
    fd.close = mock.Mock(return_value=None)
    m_open.return_value = fd

    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_existent_file)
        cp = config.GitlabConfigParser()
    assert cp.retry_transient_errors == expected
Esempio n. 17
0
 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
     config.GitlabConfigParser('one')
     self.assertRaises(config.GitlabDataError,
                       config.GitlabConfigParser,
                       gitlab_id='two')
     self.assertRaises(config.GitlabDataError,
                       config.GitlabConfigParser,
                       gitlab_id='three')
Esempio n. 18
0
    def test_invalid_id(self, m_open, path_exists):
        fd = six.StringIO(no_default_config)
        fd.close = mock.Mock(return_value=None)
        m_open.return_value = fd
        path_exists.return_value = True
        config.GitlabConfigParser("there")
        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")
Esempio n. 19
0
def test_default_config(mock_clean_env, monkeypatch):
    with monkeypatch.context() as m:
        m.setattr(Path, "resolve", _mock_nonexistent_file)
        cp = config.GitlabConfigParser()

    assert cp.gitlab_id is None
    assert cp.http_username is None
    assert cp.http_password is None
    assert cp.job_token is None
    assert cp.oauth_token is None
    assert cp.private_token is None
    assert cp.api_version == "4"
    assert cp.order_by is None
    assert cp.pagination is None
    assert cp.per_page is None
    assert cp.retry_transient_errors is False
    assert cp.ssl_verify is True
    assert cp.timeout == 60
    assert cp.url is None
    assert cp.user_agent == const.USER_AGENT
Esempio n. 20
0
def test_missing_config(path_exists):
    path_exists.return_value = False
    with pytest.raises(config.GitlabConfigMissingError):
        config.GitlabConfigParser("test")
Esempio n. 21
0
 def test_missing_config(self, path_exists):
     path_exists.return_value = False
     with self.assertRaises(config.GitlabConfigMissingError):
         config.GitlabConfigParser("test")