BigW Consortium Gitlab

stub_gitlab_calls.rb 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
module StubGitlabCalls
  def stub_gitlab_calls
    stub_session
    stub_user
    stub_project_8
    stub_project_8_hooks
    stub_projects
    stub_projects_owned
    stub_ci_enable
  end

  def stub_js_gitlab_calls
13
    allow_any_instance_of(Network).to receive(:projects) { project_hash_array }
14 15
  end

16 17
  def stub_ci_pipeline_to_return_yaml_file
    stub_ci_pipeline_yaml_file(gitlab_ci_yaml)
18 19
  end

20
  def stub_ci_pipeline_yaml_file(ci_yaml)
21
    allow_any_instance_of(Ci::Pipeline).to receive(:ci_yaml_file) { ci_yaml }
22 23
  end

24 25 26 27 28 29
  def stub_repository_ci_yaml_file(sha:, path: '.gitlab-ci.yml')
    allow_any_instance_of(Repository)
      .to receive(:gitlab_ci_yml_for).with(sha, path)
      .and_return(gitlab_ci_yaml)
  end

30 31
  def stub_ci_builds_disabled
    allow_any_instance_of(Project).to receive(:builds_enabled?).and_return(false)
32 33
  end

34
  def stub_container_registry_config(registry_settings)
35
    allow(Gitlab.config.registry).to receive_messages(registry_settings)
36 37
    allow(Auth::ContainerRegistryAuthenticationService)
      .to receive(:full_access_token).and_return('token')
38 39
  end

40 41 42
  def stub_container_registry_tags(repository: :any, tags:)
    repository = any_args if repository == :any

43
    allow_any_instance_of(ContainerRegistry::Client)
44 45
      .to receive(:repository_tags).with(repository)
      .and_return({ 'tags' => tags })
46 47

    allow_any_instance_of(ContainerRegistry::Client)
48
      .to receive(:repository_manifest).with(repository, anything)
49
      .and_return(stub_container_registry_tag_manifest)
50

51
    allow_any_instance_of(ContainerRegistry::Client)
52
      .to receive(:blob).with(repository, anything, 'application/octet-stream')
53
      .and_return(stub_container_registry_blob)
54 55
  end

56 57
  private

58 59 60 61 62 63 64 65 66 67 68 69
  def stub_container_registry_tag_manifest
    fixture_path = 'spec/fixtures/container_registry/tag_manifest.json'

    JSON.parse(File.read(Rails.root + fixture_path))
  end

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

    File.read(Rails.root + fixture_path)
  end

70
  def gitlab_url
71
    Gitlab.config.gitlab.url
72 73 74 75 76
  end

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

77 78 79 80
    stub_request(:post, "#{gitlab_url}api/v3/session.json")
      .with(body: "{\"email\":\"test@test.com\",\"password\":\"123456\"}",
            headers: { 'Content-Type' => 'application/json' })
      .to_return(status: 201, body: f, headers: { 'Content-Type' => 'application/json' })
81 82 83 84 85
  end

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

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

90 91 92
    stub_request(:get, "#{gitlab_url}api/v3/user?access_token=some_token")
      .with(headers: { 'Content-Type' => 'application/json' })
      .to_return(status: 200, body: f, headers: { 'Content-Type' => 'application/json' })
93 94 95 96
  end

  def stub_project_8
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8.json'))
97
    allow_any_instance_of(Network).to receive(:project).and_return(JSON.parse(data))
98 99 100 101
  end

  def stub_project_8_hooks
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8_hooks.json'))
102
    allow_any_instance_of(Network).to receive(:project_hooks).and_return(JSON.parse(data))
103 104 105 106
  end

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

108 109 110
    stub_request(:get, "#{gitlab_url}api/v3/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' })
111 112 113
  end

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

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

  def project_hash_array
    f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
Valery Sizov committed
127
    JSON.parse f
128 129
  end
end