2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
module StubGitlabCalls
|
|
|
|
def stub_gitlab_calls
|
|
|
|
stub_user
|
|
|
|
stub_project_8
|
|
|
|
stub_project_8_hooks
|
|
|
|
stub_projects
|
|
|
|
stub_projects_owned
|
|
|
|
stub_ci_enable
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_js_gitlab_calls
|
2015-09-10 10:55:09 -04:00
|
|
|
allow_any_instance_of(Network).to receive(:projects) { project_hash_array }
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-06-03 10:22:26 -04:00
|
|
|
def stub_ci_pipeline_to_return_yaml_file
|
|
|
|
stub_ci_pipeline_yaml_file(gitlab_ci_yaml)
|
2015-10-05 07:12:00 -04:00
|
|
|
end
|
|
|
|
|
2021-08-25 20:09:31 -04:00
|
|
|
def gitlab_ci_yaml
|
|
|
|
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
|
|
|
|
end
|
|
|
|
|
2019-11-18 13:06:53 -05:00
|
|
|
def stub_ci_pipeline_yaml_file(ci_yaml_content)
|
2019-12-05 13:07:51 -05:00
|
|
|
allow_any_instance_of(Repository)
|
|
|
|
.to receive(:gitlab_ci_yml_for)
|
|
|
|
.and_return(ci_yaml_content)
|
2019-11-18 13:06:53 -05:00
|
|
|
|
|
|
|
# Ensure we don't hit auto-devops when config not found in repository
|
|
|
|
unless ci_yaml_content
|
|
|
|
allow_any_instance_of(Project).to receive(:auto_devops_enabled?).and_return(false)
|
|
|
|
end
|
2019-12-05 13:07:51 -05:00
|
|
|
|
|
|
|
# Stub the first call to `include:[local: .gitlab-ci.yml]` when
|
|
|
|
# evaluating the CI root config content.
|
2020-03-31 11:07:53 -04:00
|
|
|
allow_any_instance_of(Gitlab::Ci::Config::External::File::Local)
|
|
|
|
.to receive(:content)
|
|
|
|
.and_return(ci_yaml_content)
|
2015-10-05 07:12:00 -04:00
|
|
|
end
|
|
|
|
|
2019-08-20 16:03:43 -04:00
|
|
|
def stub_pipeline_modified_paths(pipeline, modified_paths)
|
|
|
|
allow(pipeline).to receive(:modified_paths).and_return(modified_paths)
|
|
|
|
end
|
|
|
|
|
2015-12-14 06:24:45 -05:00
|
|
|
def stub_ci_builds_disabled
|
|
|
|
allow_any_instance_of(Project).to receive(:builds_enabled?).and_return(false)
|
2015-12-08 05:59:46 -05:00
|
|
|
end
|
|
|
|
|
2016-05-16 19:03:55 -04:00
|
|
|
def stub_container_registry_config(registry_settings)
|
2017-09-15 11:43:49 -04:00
|
|
|
allow(Gitlab.config.registry).to receive_messages(registry_settings)
|
2017-03-24 09:58:25 -04:00
|
|
|
allow(Auth::ContainerRegistryAuthenticationService)
|
|
|
|
.to receive(:full_access_token).and_return('token')
|
2016-05-16 19:03:55 -04:00
|
|
|
end
|
|
|
|
|
2019-01-10 09:22:58 -05:00
|
|
|
def stub_container_registry_tags(repository: :any, tags: [], with_manifest: false)
|
2017-04-03 09:52:24 -04:00
|
|
|
repository = any_args if repository == :any
|
|
|
|
|
2017-04-03 04:38:37 -04:00
|
|
|
allow_any_instance_of(ContainerRegistry::Client)
|
2017-04-03 09:52:24 -04:00
|
|
|
.to receive(:repository_tags).with(repository)
|
|
|
|
.and_return({ 'tags' => tags })
|
2017-04-03 04:38:37 -04:00
|
|
|
|
2019-01-10 09:22:58 -05:00
|
|
|
if with_manifest
|
|
|
|
tags.each do |tag|
|
|
|
|
allow_any_instance_of(ContainerRegistry::Client)
|
|
|
|
.to receive(:repository_tag_digest)
|
|
|
|
.with(repository, tag)
|
|
|
|
.and_return('sha256:4c8e63ca4cb663ce6c688cb06f1c3' \
|
|
|
|
'72b088dac5b6d7ad7d49cd620d85cf72a15')
|
|
|
|
end
|
|
|
|
|
|
|
|
allow_any_instance_of(ContainerRegistry::Client)
|
|
|
|
.to receive(:repository_manifest).with(repository, anything)
|
|
|
|
.and_return(stub_container_registry_tag_manifest_content)
|
|
|
|
|
|
|
|
allow_any_instance_of(ContainerRegistry::Client)
|
|
|
|
.to receive(:blob).with(repository, anything, 'application/octet-stream')
|
|
|
|
.and_return(stub_container_registry_blob_content)
|
|
|
|
end
|
2016-05-09 16:32:18 -04:00
|
|
|
end
|
|
|
|
|
2021-10-11 14:09:46 -04:00
|
|
|
def stub_container_registry_info(info: {})
|
|
|
|
allow(ContainerRegistry::Client)
|
|
|
|
.to receive(:registry_info)
|
|
|
|
.and_return(info)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_container_registry_network_error(client_method:)
|
|
|
|
allow_next_instance_of(ContainerRegistry::Client) do |client|
|
|
|
|
allow(client).to receive(client_method).and_raise(::Faraday::Error, nil, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-11 19:31:00 -05:00
|
|
|
def stub_commonmark_sourcepos_disabled
|
|
|
|
allow_any_instance_of(Banzai::Filter::MarkdownEngines::CommonMark)
|
|
|
|
.to receive(:render_options)
|
|
|
|
.and_return(Banzai::Filter::MarkdownEngines::CommonMark::RENDER_OPTIONS)
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
private
|
|
|
|
|
2019-01-10 09:22:58 -05:00
|
|
|
def stub_container_registry_tag_manifest_content
|
2017-04-03 09:52:24 -04:00
|
|
|
fixture_path = 'spec/fixtures/container_registry/tag_manifest.json'
|
|
|
|
|
2020-04-30 14:09:38 -04:00
|
|
|
Gitlab::Json.parse(File.read(Rails.root + fixture_path))
|
2017-04-03 09:52:24 -04:00
|
|
|
end
|
|
|
|
|
2019-01-10 09:22:58 -05:00
|
|
|
def stub_container_registry_blob_content
|
2017-04-03 09:52:24 -04:00
|
|
|
fixture_path = 'spec/fixtures/container_registry/config_blob.json'
|
|
|
|
|
|
|
|
File.read(Rails.root + fixture_path)
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
def gitlab_url
|
2015-09-10 10:04:06 -04:00
|
|
|
Gitlab.config.gitlab.url
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_user
|
|
|
|
f = File.read(Rails.root.join('spec/support/gitlab_stubs/user.json'))
|
|
|
|
|
2018-05-15 09:39:33 -04:00
|
|
|
stub_request(:get, "#{gitlab_url}api/v4/user?private_token=Wvjy2Krpb7y8xi93owUz")
|
2017-06-21 09:48:12 -04:00
|
|
|
.with(headers: { 'Content-Type' => 'application/json' })
|
|
|
|
.to_return(status: 200, body: f, headers: { 'Content-Type' => 'application/json' })
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2018-05-15 09:39:33 -04:00
|
|
|
stub_request(:get, "#{gitlab_url}api/v4/user?access_token=some_token")
|
2017-06-21 09:48:12 -04:00
|
|
|
.with(headers: { 'Content-Type' => 'application/json' })
|
|
|
|
.to_return(status: 200, body: f, headers: { 'Content-Type' => 'application/json' })
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_project_8
|
|
|
|
data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8.json'))
|
2020-04-30 14:09:38 -04:00
|
|
|
allow_any_instance_of(Network).to receive(:project).and_return(Gitlab::Json.parse(data))
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_project_8_hooks
|
|
|
|
data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8_hooks.json'))
|
2020-04-30 14:09:38 -04:00
|
|
|
allow_any_instance_of(Network).to receive(:project_hooks).and_return(Gitlab::Json.parse(data))
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_projects
|
|
|
|
f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
|
2015-09-10 10:04:06 -04:00
|
|
|
|
2018-05-15 09:39:33 -04:00
|
|
|
stub_request(:get, "#{gitlab_url}api/v4/projects.json?archived=false&ci_enabled_first=true&private_token=Wvjy2Krpb7y8xi93owUz")
|
2017-06-21 09:48:12 -04:00
|
|
|
.with(headers: { 'Content-Type' => 'application/json' })
|
|
|
|
.to_return(status: 200, body: f, headers: { 'Content-Type' => 'application/json' })
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_projects_owned
|
2018-05-15 09:39:33 -04:00
|
|
|
stub_request(:get, "#{gitlab_url}api/v4/projects?owned=true&archived=false&ci_enabled_first=true&private_token=Wvjy2Krpb7y8xi93owUz")
|
2017-06-21 09:48:12 -04:00
|
|
|
.with(headers: { 'Content-Type' => 'application/json' })
|
|
|
|
.to_return(status: 200, body: "", headers: {})
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_ci_enable
|
2018-05-15 09:39:33 -04:00
|
|
|
stub_request(:put, "#{gitlab_url}api/v4/projects/2/services/gitlab-ci.json?private_token=Wvjy2Krpb7y8xi93owUz")
|
2017-06-21 09:48:12 -04:00
|
|
|
.with(headers: { 'Content-Type' => 'application/json' })
|
|
|
|
.to_return(status: 200, body: "", headers: {})
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2020-05-25 11:07:58 -04:00
|
|
|
def stub_webide_config_file(content, sha: anything)
|
|
|
|
allow_any_instance_of(Repository)
|
|
|
|
.to receive(:blob_data_at).with(sha, '.gitlab/.gitlab-webide.yml')
|
|
|
|
.and_return(content)
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
def project_hash_array
|
|
|
|
f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
|
2020-04-30 14:09:38 -04:00
|
|
|
Gitlab::Json.parse(f)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
StubGitlabCalls.prepend_mod_with('StubGitlabCalls')
|