def test_poke_jenkins_hook_with_auth(hg_ui_with_repo_url,
                                     hg_ui_with_jenkins_base_url,
                                     hg_ui_with_jenkins_jobs,
                                     hg_ui_with_jenkins_auth, hg_ui_with_tag,
                                     hg_repo, hg_node, repo_url, tag):
    """Test poke_jenkins hook with jenkins base url and repo url and jenkins jobs being set up and with auth enabled."""
    with mock.patch.multiple('urllib2',
                             urlopen=mock.DEFAULT,
                             Request=mock.DEFAULT) as mocks:
        poke_jenkins.poke_jenkins_hook(hg_ui_with_jenkins_jobs, hg_repo,
                                       hg_node)
        node_id = hg_repo[hg_node].hex()
        mocks['Request'].assert_has_calls((
            mock.call(
                'http://example.ci.com/job/unit/buildWithParameters?TAG={tag}&NODE_ID={node_id}&{repo_url}'
                '&BRANCH={branch}'.format(repo_url=urllib.urlencode(
                    dict(REPO_URL=repo_url)),
                                          node_id=node_id,
                                          tag=tag,
                                          branch='default'), '',
                {'Authorization': 'Basic Zm9vOmJhcg=='}),
            mock.call(
                'http://example.ci.com/job/functional/buildWithParameters?TAG={tag}&NODE_ID={node_id}&'
                '{repo_url}&BRANCH={branch}'.format(repo_url=urllib.urlencode(
                    dict(REPO_URL=repo_url)),
                                                    node_id=node_id,
                                                    tag=tag,
                                                    branch='default'), '',
                {'Authorization': 'Basic Zm9vOmJhcg=='}),
        ),
                                          any_order=True)
def test_poke_jenkins_hook_with_branch_regex(
        hg_ui_with_repo_url, hg_ui_with_jenkins_base_url, hg_ui_with_jenkins_jobs, hg_ui_with_branch_regex,
        hg_ui_with_tag, hg_repo, hg_node, repo_url, tag):
    """Test poke_jenkins hook with jenkins base url and repo url and jenkins jobs being set up and with auth enabled."""
    with mock.patch.multiple('urllib2', urlopen=mock.DEFAULT, Request=mock.DEFAULT) as mocks:
        poke_jenkins.poke_jenkins_hook(hg_ui_with_jenkins_jobs, hg_repo, hg_node)
        mocks['Request'].assert_has_calls((), any_order=True)
def test_poke_jenkins_hook_no_repo_url(hg_ui_with_jenkins_base_url, hg_repo, hg_node):
    """Test poke_jenkins hook with jenkins base url being set up but without repo_url."""
    with pytest.raises(util.Abort) as exc:
        # should raise an exception to set up the hook url
        poke_jenkins.poke_jenkins_hook(hg_ui_with_jenkins_base_url, hg_repo, hg_node)

    assert exc.value.args == ('You have to specify the parameter repo_url in the section poke_jenkins.',)
def test_poke_jenkins_hook_no_jenkins_base_url(hg_ui, hg_repo, hg_node):
    """Test poke_jenkins hook without url being set up."""
    with pytest.raises(util.Abort) as exc:
        # should raise an exception to set up the jenkins base url
        poke_jenkins.poke_jenkins_hook(hg_ui, hg_repo, hg_node)

    assert exc.value.args == (
        'You have to specify the parameter jenkins_base_url in the section poke_jenkins.',
    )
def test_poke_jenkins_hook_with_branch_regex(hg_ui_with_repo_url,
                                             hg_ui_with_jenkins_base_url,
                                             hg_ui_with_jenkins_jobs,
                                             hg_ui_with_branch_regex,
                                             hg_ui_with_tag, hg_repo, hg_node,
                                             repo_url, tag):
    """Test poke_jenkins hook with jenkins base url and repo url and jenkins jobs being set up and with auth enabled."""
    with mock.patch.multiple('urllib2',
                             urlopen=mock.DEFAULT,
                             Request=mock.DEFAULT) as mocks:
        poke_jenkins.poke_jenkins_hook(hg_ui_with_jenkins_jobs, hg_repo,
                                       hg_node)
        mocks['Request'].assert_has_calls((), any_order=True)
def test_poke_jenkins_hook_with_auth(
        hg_ui_with_repo_url, hg_ui_with_jenkins_base_url, hg_ui_with_jenkins_jobs, hg_ui_with_jenkins_auth,
        hg_ui_with_tag, hg_repo, hg_node, repo_url, tag):
    """Test poke_jenkins hook with jenkins base url and repo url and jenkins jobs being set up and with auth enabled."""
    with mock.patch.multiple('urllib2', urlopen=mock.DEFAULT, Request=mock.DEFAULT) as mocks:
        poke_jenkins.poke_jenkins_hook(hg_ui_with_jenkins_jobs, hg_repo, hg_node)
        node_id = hg_repo[hg_node].hex()
        mocks['Request'].assert_has_calls((
            mock.call(
                'http://example.ci.com/job/unit/buildWithParameters?TAG={tag}&NODE_ID={node_id}&{repo_url}'
                '&BRANCH={branch}'.format(
                    repo_url=urllib.urlencode(dict(REPO_URL=repo_url)),
                    node_id=node_id, tag=tag, branch='default'), '',
                {'Authorization': 'Basic Zm9vOmJhcg=='}),
            mock.call(
                'http://example.ci.com/job/functional/buildWithParameters?TAG={tag}&NODE_ID={node_id}&'
                '{repo_url}&BRANCH={branch}'.format(
                    repo_url=urllib.urlencode(dict(REPO_URL=repo_url)),
                    node_id=node_id, tag=tag, branch='default'), '',
                {'Authorization': 'Basic Zm9vOmJhcg=='}),
        ), any_order=True)