summaryrefslogtreecommitdiff
path: root/spec/support/helpers/stub_gitlab_calls.rb
blob: ae031f58bd400a467289194d061d9969b5d517cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true

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
    allow_any_instance_of(Network).to receive(:projects) { project_hash_array }
  end

  def stub_ci_pipeline_to_return_yaml_file
    stub_ci_pipeline_yaml_file(gitlab_ci_yaml)
  end

  def gitlab_ci_yaml
    File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
  end

  def stub_ci_pipeline_yaml_file(ci_yaml_content)
    allow_any_instance_of(Repository)
      .to receive(:gitlab_ci_yml_for)
      .and_return(ci_yaml_content)

    # 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

    # Stub the first call to `include:[local: .gitlab-ci.yml]` when
    # evaluating the CI root config content.
    allow_any_instance_of(Gitlab::Ci::Config::External::File::Local)
      .to receive(:content)
      .and_return(ci_yaml_content)
  end

  def stub_pipeline_modified_paths(pipeline, modified_paths)
    allow(pipeline).to receive(:modified_paths).and_return(modified_paths)
  end

  def stub_ci_builds_disabled
    allow_any_instance_of(Project).to receive(:builds_enabled?).and_return(false)
  end

  def stub_container_registry_config(registry_settings)
    allow(Gitlab.config.registry).to receive_messages(registry_settings)
    allow(Auth::ContainerRegistryAuthenticationService)
      .to receive(:full_access_token).and_return('token')
  end

  def stub_container_registry_tags(repository: :any, tags: [], with_manifest: false)
    repository = any_args if repository == :any

    allow_any_instance_of(ContainerRegistry::Client)
      .to receive(:repository_tags).with(repository)
      .and_return({ 'tags' => tags })

    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
  end

  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

  def stub_commonmark_sourcepos_disabled
    render_options =
      if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
        Banzai::Filter::MarkdownEngines::CommonMark::RENDER_OPTIONS_C
      else
        Banzai::Filter::MarkdownEngines::CommonMark::RENDER_OPTIONS_RUBY
      end

    allow_any_instance_of(Banzai::Filter::MarkdownEngines::CommonMark)
      .to receive(:render_options)
      .and_return(render_options)
  end

  private

  def stub_container_registry_tag_manifest_content
    fixture_path = 'spec/fixtures/container_registry/tag_manifest.json'

    Gitlab::Json.parse(File.read(Rails.root + fixture_path))
  end

  def stub_container_registry_blob_content
    fixture_path = 'spec/fixtures/container_registry/config_blob.json'

    File.read(Rails.root + fixture_path)
  end

  def gitlab_url
    Gitlab.config.gitlab.url
  end

  def stub_user
    f = File.read(Rails.root.join('spec/support/gitlab_stubs/user.json'))

    stub_request(:get, "#{gitlab_url}api/v4/user?private_token=Wvjy2Krpb7y8xi93owUz")
      .with(headers: { 'Content-Type' => 'application/json' })
      .to_return(status: 200, body: f, headers: { 'Content-Type' => 'application/json' })

    stub_request(:get, "#{gitlab_url}api/v4/user?access_token=some_token")
      .with(headers: { 'Content-Type' => 'application/json' })
      .to_return(status: 200, body: f, headers: { 'Content-Type' => 'application/json' })
  end

  def stub_project_8
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8.json'))
    allow_any_instance_of(Network).to receive(:project).and_return(Gitlab::Json.parse(data))
  end

  def stub_project_8_hooks
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8_hooks.json'))
    allow_any_instance_of(Network).to receive(:project_hooks).and_return(Gitlab::Json.parse(data))
  end

  def stub_projects
    f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))

    stub_request(:get, "#{gitlab_url}api/v4/projects.json?archived=false&ci_enabled_first=true&private_token=Wvjy2Krpb7y8xi93owUz")
      .with(headers: { 'Content-Type' => 'application/json' })
      .to_return(status: 200, body: f, headers: { 'Content-Type' => 'application/json' })
  end

  def stub_projects_owned
    stub_request(:get, "#{gitlab_url}api/v4/projects?owned=true&archived=false&ci_enabled_first=true&private_token=Wvjy2Krpb7y8xi93owUz")
      .with(headers: { 'Content-Type' => 'application/json' })
      .to_return(status: 200, body: "", headers: {})
  end

  def stub_ci_enable
    stub_request(:put, "#{gitlab_url}api/v4/projects/2/services/gitlab-ci.json?private_token=Wvjy2Krpb7y8xi93owUz")
      .with(headers: { 'Content-Type' => 'application/json' })
      .to_return(status: 200, body: "", headers: {})
  end

  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

  def project_hash_array
    f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
    Gitlab::Json.parse(f)
  end
end

StubGitlabCalls.prepend_mod_with('StubGitlabCalls')