summaryrefslogtreecommitdiff
path: root/spec/controllers/projects_controller_spec.rb
blob: cea3986f72a1680c9d42e2593b64f58f7ad5da40 (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
require "spec_helper"

describe ProjectsController do
  before do
    @project = FactoryGirl.create :project
  end

  describe "POST #build" do
    it 'should respond 200 if params is ok' do
      post :build, id: @project.id,
        ref: 'master',
        before: '2aa371379db71ac89ae20843fcff3b3477cf1a1d',
        after: '1c8a9df454ef68c22c2a33cca8232bb50849e5c5',
        token: @project.token


      expect(response).to be_success
      expect(response.code).to eq('201')
    end

    it 'should respond 400 if push about removed branch' do
      post :build, id: @project.id,
        ref: 'master',
        before: '2aa371379db71ac89ae20843fcff3b3477cf1a1d',
        after: '0000000000000000000000000000000000000000',
        token: @project.token

      expect(response).not_to be_success
      expect(response.code).to eq('400')
    end

    it 'should respond 400 if some params missed' do
      post :build, id: @project.id, token: @project.token
      expect(response).not_to be_success
      expect(response.code).to eq('400')
    end

    it 'should respond 403 if token is wrong' do
      post :build, id: @project.id, token: 'invalid-token'
      expect(response).not_to be_success
      expect(response.code).to eq('403')
    end
  end

  describe "POST /projects" do
    let(:project_dump) { File.read(Rails.root.join('spec/support/gitlab_stubs/raw_project.yml')) }
    let(:gitlab_url) { GitlabCi.config.gitlab_server.url }

    let (:user_data) do
      data = JSON.parse File.read(Rails.root.join('spec/support/gitlab_stubs/user.json'))
      data.merge("url" => gitlab_url)
    end

    let(:user) do
      User.new(user_data)
    end

    it "creates project" do
      allow(controller).to receive(:reset_cache) { true }
      allow(controller).to receive(:current_user) { user }
      Network.any_instance.stub(:enable_ci).and_return(true)

      post :create, { project: project_dump }.with_indifferent_access

      Project.exists?(gitlab_id: 189).should be_true
      expect(response.code).to eq('302')
    end
  end

  describe "GET /gitlab" do
    let(:gitlab_url) { GitlabCi.config.gitlab_server.url }

    let (:user_data) do
      data = JSON.parse File.read(Rails.root.join('spec/support/gitlab_stubs/user.json'))
      data.merge("url" => gitlab_url)
    end

    let(:user) do
      User.new(user_data)
    end

    it "searches projects" do
      allow(controller).to receive(:reset_cache) { true }
      allow(controller).to receive(:current_user) { user }
      Network.any_instance.should_receive(:projects).with(hash_including(search: 'str'), :authorized)

      xhr :get, :gitlab, { search: "str", format: "js" }.with_indifferent_access

      expect(response).to be_success
      expect(response.code).to eq('200')
    end
  end
end