stub_gitlab_calls.rb 3.6 KB
Newer Older
D
Douwe Maan 已提交
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 }
D
Douwe Maan 已提交
14 15
  end

K
Kamil Trzcinski 已提交
16 17 18 19 20 21 22 23
  def stub_ci_commit_to_return_yaml_file
    stub_ci_commit_yaml_file(gitlab_ci_yaml)
  end

  def stub_ci_commit_yaml_file(ci_yaml)
    allow_any_instance_of(Ci::Commit).to receive(:ci_yaml_file) { ci_yaml }
  end

24 25
  def stub_ci_builds_disabled
    allow_any_instance_of(Project).to receive(:builds_enabled?).and_return(false)
26 27
  end

28 29 30 31 32 33 34 35 36 37 38 39
  def stub_container_registry(*tags)
    allow_any_instance_of(ContainerRegistry::Client).to receive(:repository_tags).and_return(
      { "tags" => tags }
    )
    allow_any_instance_of(ContainerRegistry::Client).to receive(:repository_manifest).and_return(
      JSON.load(File.read(Rails.root + 'spec/fixtures/container_registry/tag_manifest.json'))
    )
    allow_any_instance_of(ContainerRegistry::Client).to receive(:blob).and_return(
      File.read(Rails.root + 'spec/fixtures/container_registry/config_blob.json')
    )
  end

D
Douwe Maan 已提交
40 41 42
  private

  def gitlab_url
D
Dmitriy Zaporozhets 已提交
43
    Gitlab.config.gitlab.url
D
Douwe Maan 已提交
44 45 46 47 48 49
  end

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

    stub_request(:post, "#{gitlab_url}api/v3/session.json").
V
Valery Sizov 已提交
50
      with(body: "{\"email\":\"test@test.com\",\"password\":\"123456\"}",
V
Valery Sizov 已提交
51 52
           headers: { 'Content-Type'=>'application/json' }).
           to_return(status: 201, body: f, headers: { 'Content-Type'=>'application/json' })
D
Douwe Maan 已提交
53 54 55 56 57 58
  end

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

    stub_request(:get, "#{gitlab_url}api/v3/user?private_token=Wvjy2Krpb7y8xi93owUz").
V
Valery Sizov 已提交
59 60
      with(headers: { 'Content-Type'=>'application/json' }).
      to_return(status: 200, body: f, headers: { 'Content-Type'=>'application/json' })
D
Douwe Maan 已提交
61 62

    stub_request(:get, "#{gitlab_url}api/v3/user?access_token=some_token").
V
Valery Sizov 已提交
63 64
      with(headers: { 'Content-Type'=>'application/json' }).
      to_return(status: 200, body: f, headers: { 'Content-Type'=>'application/json' })
D
Douwe Maan 已提交
65 66 67 68
  end

  def stub_project_8
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8.json'))
69
    allow_any_instance_of(Network).to receive(:project).and_return(JSON.parse(data))
D
Douwe Maan 已提交
70 71 72 73
  end

  def stub_project_8_hooks
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8_hooks.json'))
74
    allow_any_instance_of(Network).to receive(:project_hooks).and_return(JSON.parse(data))
D
Douwe Maan 已提交
75 76 77 78
  end

  def stub_projects
    f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
D
Dmitriy Zaporozhets 已提交
79

D
Douwe Maan 已提交
80
    stub_request(:get, "#{gitlab_url}api/v3/projects.json?archived=false&ci_enabled_first=true&private_token=Wvjy2Krpb7y8xi93owUz").
V
Valery Sizov 已提交
81 82
      with(headers: { 'Content-Type'=>'application/json' }).
      to_return(status: 200, body: f, headers: { 'Content-Type'=>'application/json' })
D
Douwe Maan 已提交
83 84 85 86
  end

  def stub_projects_owned
    stub_request(:get, "#{gitlab_url}api/v3/projects/owned.json?archived=false&ci_enabled_first=true&private_token=Wvjy2Krpb7y8xi93owUz").
V
Valery Sizov 已提交
87
      with(headers: { 'Content-Type'=>'application/json' }).
V
Valery Sizov 已提交
88
      to_return(status: 200, body: "", headers: {})
D
Douwe Maan 已提交
89 90 91 92
  end

  def stub_ci_enable
    stub_request(:put, "#{gitlab_url}api/v3/projects/2/services/gitlab-ci.json?private_token=Wvjy2Krpb7y8xi93owUz").
V
Valery Sizov 已提交
93
      with(headers: { 'Content-Type'=>'application/json' }).
V
Valery Sizov 已提交
94
      to_return(status: 200, body: "", headers: {})
D
Douwe Maan 已提交
95 96 97 98
  end

  def project_hash_array
    f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
V
Valery Sizov 已提交
99
    JSON.parse f
D
Douwe Maan 已提交
100 101
  end
end