BigW Consortium Gitlab

stub_gitlab_calls.rb 4.2 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
  def stub_ci_builds_disabled
    allow_any_instance_of(Project).to receive(:builds_enabled?).and_return(false)
26 27
  end

28 29
  def stub_container_registry_config(registry_settings)
    allow(Gitlab.config.registry).to receive_messages(registry_settings)
30 31
    allow(Auth::ContainerRegistryAuthenticationService)
      .to receive(:full_access_token).and_return('token')
32 33
  end

34 35 36
  def stub_container_registry_tags(repository: :any, tags:)
    repository = any_args if repository == :any

37
    allow_any_instance_of(ContainerRegistry::Client)
38 39
      .to receive(:repository_tags).with(repository)
      .and_return({ 'tags' => tags })
40 41

    allow_any_instance_of(ContainerRegistry::Client)
42 43
      .to receive(:repository_manifest).with(repository)
      .and_return(stub_container_registry_tag_manifest)
44

45 46 47
    allow_any_instance_of(ContainerRegistry::Client)
      .to receive(:blob).with(repository)
      .and_return(stub_container_registry_blob)
48 49
  end

50 51
  private

52 53 54 55 56 57 58 59 60 61 62 63
  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

64
  def gitlab_url
65
    Gitlab.config.gitlab.url
66 67 68 69 70
  end

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

71 72 73 74
    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' })
75 76 77 78 79
  end

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

80 81 82
    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' })
83

84 85 86
    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' })
87 88 89 90
  end

  def stub_project_8
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8.json'))
91
    allow_any_instance_of(Network).to receive(:project).and_return(JSON.parse(data))
92 93 94 95
  end

  def stub_project_8_hooks
    data = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8_hooks.json'))
96
    allow_any_instance_of(Network).to receive(:project_hooks).and_return(JSON.parse(data))
97 98 99 100
  end

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

102 103 104
    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' })
105 106 107
  end

  def stub_projects_owned
108 109 110
    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: {})
111 112 113
  end

  def stub_ci_enable
114 115 116
    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: {})
117 118 119 120
  end

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