summaryrefslogtreecommitdiff
path: root/spec/support/stub_gitlab_calls.rb
blob: 286c6c99a8714825144450805c3523b6cdb9fc45 (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
module StubGitlabCalls
  def stub_gitlab_calls
    stub_session
    stub_user
    stub_oauth_user
    stub_project_8
    stub_project_8_hooks
    stub_projects
    stub_projects_owned
    stub_ci_enable
  end

  def stub_js_gitlab_calls
    Network.any_instance.stub(:projects) { project_hash_array }
  end

  private

  def gitlab_url
    GitlabCi.config.gitlab_server.url
  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").
      with(:body => "{\"email\":\"test@test.com\",\"password\":\"123456\"}",
           :headers => {'Content-Type'=>'application/json'}).
           to_return(:status => 201, :body => f, :headers => {'Content-Type'=>'application/json'})
  end

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

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

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

    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'})
  end

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

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

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

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

  def stub_projects
    f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
    stub_request(:get, "#{gitlab_url}api/v3/projects.json?archived=false&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/v3/projects/owned.json?archived=false&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/v3/projects/2/services/gitlab-ci.json?private_token=Wvjy2Krpb7y8xi93owUz").
      with(:headers => {'Content-Type'=>'application/json'}).
      to_return(:status => 200, :body => "", :headers => {})
  end

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