def test_update_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', 'title': 'Public key', 'key': 'ssh-rsa long/+base64//+string==', 'can_push': 'yes', 'state': 'present' }) with pytest.raises(AnsibleExitJson) as result: gitlab_deploy_key.main() second_call = fetch_url_mock.call_args_list[1][1] assert second_call[ 'url'] == 'https://gitlab.example.com/api/v4/projects/10/deploy_keys/1' assert second_call['method'] == 'PUT' assert second_call['data'] == ( '{"can_push": true, "key": "ssh-rsa long/+base64//+string==", "title": "Public key"}' ) assert result.value.args[0]['changed'] is True
def test_add_new(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', 'key': 'ssh-key foobar', 'title': 'a title', 'state': 'present' }) with pytest.raises(AnsibleExitJson) as result: gitlab_deploy_key.main() second_call = fetch_url_mock.call_args_list[1][1] assert second_call[ 'url'] == 'https://gitlab.example.com/api/v4/projects/10/deploy_keys' assert second_call['method'] == 'POST' assert second_call[ 'data'] == '{"can_push": false, "key": "ssh-key foobar", "title": "a title"}' assert result.value.args[0]['changed'] is True
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', 'key': 'ssh-key foobar', 'title': 'a title', 'state': 'present' }) with pytest.raises(AnsibleFailJson): gitlab_deploy_key.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', 'key': 'ssh-key foobar', 'title': 'a title', 'state': 'absent' }) with pytest.raises(AnsibleExitJson) as result: gitlab_deploy_key.main() assert result.value.args[0]['changed'] is False
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', 'title': 'Public key', 'key': 'ssh-rsa long/+base64//+string==', 'can_push': 'no', 'state': 'present' }) with pytest.raises(AnsibleExitJson) as result: gitlab_deploy_key.main() assert result.value.args[0]['changed'] is False assert fetch_url_mock.call_count == 1
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', 'key': 'ssh-key foobar', 'title': 'a title', 'state': 'absent' }) with pytest.raises(AnsibleExitJson) as result: gitlab_deploy_key.main() first_call = fetch_url_mock.call_args_list[0][1] assert first_call['url'] == 'https://gitlab.example.com/api/v4/projects/foo%2Fbar/deploy_keys' assert first_call['headers']['Private-Token'] == 'test-private-token' assert 'Authorization' not in first_call['headers'] assert first_call['method'] == 'GET'