def test_unchanged_existing_with_token(capfd, fetch_url_mock, module_mock): fetch_url_mock.return_value = [FakeReader(fake_server_state), {'status': 200}] set_module_args({ 'api_url': 'https://gitlab.example.com/api', 'access_token': 'test-access-token', 'project': '10', 'hook_url': 'https://notification-server.example.com/gitlab-hook', 'push_events': 'yes', 'issues_events': 'yes', 'merge_requests_events': 'yes', 'tag_push_events': 'yes', 'note_events': 'yes', 'job_events': 'yes', 'pipeline_events': 'yes', 'wiki_page_events': 'yes', 'enable_ssl_verification': 'yes', 'state': 'present', 'token': 'secret-token', }) with pytest.raises(AnsibleExitJson) as result: gitlab_hooks.main() second_call = fetch_url_mock.call_args_list[1][1] assert second_call['url'] == 'https://gitlab.example.com/api/v4/projects/10/hooks/1' assert second_call['method'] == 'PUT' assert second_call['data'] == ('{"enable_ssl_verification": true, "issues_events": true, "job_events": true, ' '"merge_requests_events": true, "note_events": true, "pipeline_events": true, "push_events": true, ' '"tag_push_events": true, "token": "secret-token", "url": "https://notification-server.example.com/gitlab-hook", ' '"wiki_page_events": true}') assert result.value.args[0]['changed'] is True
def test_unchanged_existing(capfd, fetch_url_mock, module_mock): fetch_url_mock.return_value = [ FakeReader(fake_server_state), { 'status': 200 } ] set_module_args({ 'api_url': 'https://gitlab.example.com/api', 'access_token': 'test-access-token', 'project': '10', 'hook_url': 'https://notification-server.example.com/gitlab-hook', 'push_events': 'yes', 'issues_events': 'yes', 'merge_requests_events': 'yes', 'tag_push_events': 'yes', 'note_events': 'yes', 'job_events': 'yes', 'pipeline_events': 'yes', 'wiki_page_events': 'yes', 'enable_ssl_verification': 'yes', 'state': 'present' }) with pytest.raises(AnsibleExitJson) as result: gitlab_hooks.main() assert result.value.args[0]['changed'] is False assert fetch_url_mock.call_count == 1
def test_bad_http_second_response(capfd, fetch_url_mock, module_mock): fetch_url_mock.side_effect = [[FakeReader(fake_server_state), {'status': 200}], [FakeReader("Permission denied"), {'status': 403}]] set_module_args({ 'api_url': 'https://gitlab.example.com/api', 'access_token': 'test-access-token', 'project': '10', 'hook_url': 'https://my-ci-server.example.com/gitlab-hook', 'state': 'present' }) with pytest.raises(AnsibleFailJson): gitlab_hooks.main()
def test_delete_non_existing(capfd, fetch_url_mock, module_mock): fetch_url_mock.return_value = [FakeReader(fake_server_state), {'status': 200}] set_module_args({ 'api_url': 'https://gitlab.example.com/api', 'access_token': 'test-access-token', 'project': '10', 'hook_url': 'https://my-ci-server.example.com/gitlab-hook', 'state': 'absent' }) with pytest.raises(AnsibleExitJson) as result: gitlab_hooks.main() assert result.value.args[0]['changed'] is False
def test_private_token_output(capfd, fetch_url_mock, module_mock): fetch_url_mock.return_value = [FakeReader(fake_server_state), {'status': 200}] set_module_args({ 'api_url': 'https://gitlab.example.com/api', 'private_token': 'test-private-token', 'project': 'foo/bar', 'hook_url': 'https://my-ci-server.example.com/gitlab-hook', 'state': 'absent' }) with pytest.raises(AnsibleExitJson) as result: gitlab_hooks.main() first_call = fetch_url_mock.call_args_list[0][1] assert first_call['url'] == 'https://gitlab.example.com/api/v4/projects/foo%2Fbar/hooks' assert first_call['headers']['Private-Token'] == 'test-private-token' assert 'Authorization' not in first_call['headers'] assert first_call['method'] == 'GET'
def test_delete_existing(capfd, fetch_url_mock, module_mock): fetch_url_mock.return_value = [FakeReader(fake_server_state), {'status': 200}] set_module_args({ 'api_url': 'https://gitlab.example.com/api', 'access_token': 'test-access-token', 'project': '10', 'hook_url': 'https://notification-server.example.com/gitlab-hook', 'state': 'absent' }) with pytest.raises(AnsibleExitJson) as result: gitlab_hooks.main() second_call = fetch_url_mock.call_args_list[1][1] assert second_call['url'] == 'https://gitlab.example.com/api/v4/projects/10/hooks/1' assert second_call['method'] == 'DELETE' assert result.value.args[0]['changed'] is True