pipelines_finder_spec.rb 7.17 KB
Newer Older
1 2 3
require 'spec_helper'

describe PipelinesFinder do
4 5 6 7
  let(:project) { create(:project, :public, :repository) }
  let(:current_user) { nil }
  let(:params) { {} }
  subject { described_class.new(project, current_user, params).execute }
8 9

  describe "#execute" do
10
    context 'when params is empty' do
11
      let(:params) { {} }
12
      let!(:pipelines) { create_list(:ci_pipeline, 2, project: project) }
13

Shinya Maeda's avatar
Shinya Maeda committed
14
      it 'returns all pipelines' do
Shinya Maeda's avatar
Shinya Maeda committed
15
        is_expected.to match_array(pipelines)
16 17 18
      end
    end

19 20 21 22
    %w[running pending].each do |target|
      context "when scope is #{target}" do
        let(:params) { { scope: target } }
        let!(:pipeline) { create(:ci_pipeline, project: project, status: target) }
23

Shinya Maeda's avatar
Shinya Maeda committed
24
        it 'returns matched pipelines' do
25
          is_expected.to eq([pipeline])
26 27
        end
      end
28
    end
29

30 31
    context 'when scope is finished' do
      let(:params) { { scope: 'finished' } }
32 33 34 35 36
      let!(:pipelines) do
        [create(:ci_pipeline, project: project, status: 'success'),
         create(:ci_pipeline, project: project, status: 'failed'),
         create(:ci_pipeline, project: project, status: 'canceled')]
      end
37

38
      it 'returns matched pipelines' do
Shinya Maeda's avatar
Shinya Maeda committed
39
        is_expected.to match_array(pipelines)
40
      end
41
    end
42

43 44 45
    context 'when scope is branches or tags' do
      let!(:pipeline_branch) { create(:ci_pipeline, project: project) }
      let!(:pipeline_tag) { create(:ci_pipeline, project: project, ref: 'v1.0.0', tag: true) }
46

Shinya Maeda's avatar
Shinya Maeda committed
47
      context 'when scope is branches' do
48 49
        let(:params) { { scope: 'branches' } }

Shinya Maeda's avatar
Shinya Maeda committed
50
        it 'returns matched pipelines' do
51
          is_expected.to eq([pipeline_branch])
52 53 54
        end
      end

Shinya Maeda's avatar
Shinya Maeda committed
55
      context 'when scope is tags' do
56 57
        let(:params) { { scope: 'tags' } }

Shinya Maeda's avatar
Shinya Maeda committed
58
        it 'returns matched pipelines' do
59
          is_expected.to eq([pipeline_tag])
60 61 62 63
        end
      end
    end

64
    HasStatus::AVAILABLE_STATUSES.each do |target|
65 66 67
      context "when status is #{target}" do
        let(:params) { { status: target } }
        let!(:pipeline) { create(:ci_pipeline, project: project, status: target) }
68

69
        before do
70
          exception_status = HasStatus::AVAILABLE_STATUSES - [target]
Shinya Maeda's avatar
Shinya Maeda committed
71
          create(:ci_pipeline, project: project, status: exception_status.first)
72 73
        end

Shinya Maeda's avatar
Shinya Maeda committed
74
        it 'returns matched pipelines' do
75
          is_expected.to eq([pipeline])
76 77
        end
      end
78
    end
79

80 81
    context 'when ref is specified' do
      let!(:pipeline) { create(:ci_pipeline, project: project) }
82

Shinya Maeda's avatar
Shinya Maeda committed
83
      context 'when ref exists' do
84 85
        let(:params) { { ref: 'master' } }

Shinya Maeda's avatar
Shinya Maeda committed
86
        it 'returns matched pipelines' do
87
          is_expected.to eq([pipeline])
88 89 90
        end
      end

Shinya Maeda's avatar
Shinya Maeda committed
91
      context 'when ref does not exist' do
Shinya Maeda's avatar
Shinya Maeda committed
92
        let(:params) { { ref: 'invalid-ref' } }
93

Shinya Maeda's avatar
Shinya Maeda committed
94
        it 'returns empty' do
95
          is_expected.to be_empty
96 97
        end
      end
Shinya Maeda's avatar
Shinya Maeda committed
98 99
    end

100 101 102 103
    context 'when name is specified' do
      let(:user) { create(:user) }
      let!(:pipeline) { create(:ci_pipeline, project: project, user: user) }

Shinya Maeda's avatar
Shinya Maeda committed
104
      context 'when name exists' do
105
        let(:params) { { name: user.name } }
Shinya Maeda's avatar
Shinya Maeda committed
106

Shinya Maeda's avatar
Shinya Maeda committed
107
        it 'returns matched pipelines' do
108
          is_expected.to eq([pipeline])
Shinya Maeda's avatar
Shinya Maeda committed
109 110 111
        end
      end

Shinya Maeda's avatar
Shinya Maeda committed
112
      context 'when name does not exist' do
Shinya Maeda's avatar
Shinya Maeda committed
113 114
        let(:params) { { name: 'invalid-name' } }

Shinya Maeda's avatar
Shinya Maeda committed
115
        it 'returns empty' do
116
          is_expected.to be_empty
Shinya Maeda's avatar
Shinya Maeda committed
117 118 119
        end
      end
    end
120

121 122 123 124
    context 'when username is specified' do
      let(:user) { create(:user) }
      let!(:pipeline) { create(:ci_pipeline, project: project, user: user) }

Shinya Maeda's avatar
Shinya Maeda committed
125
      context 'when username exists' do
126
        let(:params) { { username: user.username } }
127

Shinya Maeda's avatar
Shinya Maeda committed
128
        it 'returns matched pipelines' do
129
          is_expected.to eq([pipeline])
130 131 132
        end
      end

Shinya Maeda's avatar
Shinya Maeda committed
133
      context 'when username does not exist' do
Shinya Maeda's avatar
Shinya Maeda committed
134
        let(:params) { { username: 'invalid-username' } }
135

Shinya Maeda's avatar
Shinya Maeda committed
136
        it 'returns empty' do
137
          is_expected.to be_empty
138 139
        end
      end
Shinya Maeda's avatar
Shinya Maeda committed
140
    end
141

142 143 144 145
    context 'when yaml_errors is specified' do
      let!(:pipeline1) { create(:ci_pipeline, project: project, yaml_errors: 'Syntax error') }
      let!(:pipeline2) { create(:ci_pipeline, project: project) }

146 147 148
      context 'when yaml_errors is true' do
        let(:params) { { yaml_errors: true } }

Shinya Maeda's avatar
Shinya Maeda committed
149
        it 'returns matched pipelines' do
150
          is_expected.to eq([pipeline1])
151 152 153 154 155 156
        end
      end

      context 'when yaml_errors is false' do
        let(:params) { { yaml_errors: false } }

Shinya Maeda's avatar
Shinya Maeda committed
157
        it 'returns matched pipelines' do
158
          is_expected.to eq([pipeline2])
159 160
        end
      end
Shinya Maeda's avatar
Shinya Maeda committed
161

Shinya Maeda's avatar
Shinya Maeda committed
162
      context 'when yaml_errors is invalid' do
163
        let(:params) { { yaml_errors: "invalid-yaml_errors" } }
Shinya Maeda's avatar
Shinya Maeda committed
164

Shinya Maeda's avatar
Shinya Maeda committed
165
        it 'returns all pipelines' do
Shinya Maeda's avatar
Shinya Maeda committed
166
          is_expected.to match_array([pipeline1, pipeline2])
Shinya Maeda's avatar
Shinya Maeda committed
167 168
        end
      end
169 170
    end

171 172
    context 'when order_by and sort are specified' do
      context 'when order_by user_id' do
173 174 175
        let(:params)     { { order_by: 'user_id', sort: 'asc' } }
        let(:users)      { Array.new(2) { create(:user, developer_projects: [project]) } }
        let!(:pipelines) { users.map { |user| create(:ci_pipeline, project: project, user: user) } }
176

177
        it 'sorts as user_id: :asc' do
Shinya Maeda's avatar
Shinya Maeda committed
178
          is_expected.to match_array(pipelines)
179 180
        end

181 182
        context 'when sort is invalid' do
          let(:params) { { order_by: 'user_id', sort: 'invalid_sort' } }
Shinya Maeda's avatar
Shinya Maeda committed
183

184
          it 'sorts as user_id: :desc' do
Shinya Maeda's avatar
Shinya Maeda committed
185
            is_expected.to eq(pipelines.sort_by { |p| -p.user.id })
186
          end
Shinya Maeda's avatar
Shinya Maeda committed
187 188 189
        end
      end

190 191 192
      context 'when order_by is invalid' do
        let(:params) { { order_by: 'invalid_column', sort: 'asc' } }
        let!(:pipelines) { create_list(:ci_pipeline, 2, project: project) }
Shinya Maeda's avatar
Shinya Maeda committed
193

194
        it 'sorts as id: :asc' do
Shinya Maeda's avatar
Shinya Maeda committed
195
          is_expected.to eq(pipelines.sort_by { |p| p.id })
Shinya Maeda's avatar
Shinya Maeda committed
196 197
        end
      end
198 199 200

      context 'when both are nil' do
        let(:params) { { order_by: nil, sort: nil } }
201
        let!(:pipelines) { create_list(:ci_pipeline, 2, project: project) }
202

203
        it 'sorts as id: :desc' do
Shinya Maeda's avatar
Shinya Maeda committed
204
          is_expected.to eq(pipelines.sort_by { |p| -p.id })
205 206
        end
      end
207
    end
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

    context 'when sha is specified' do
      let!(:pipeline) { create(:ci_pipeline, project: project, sha: '97de212e80737a608d939f648d959671fb0a0142') }

      context 'when sha exists' do
        let(:params) { { sha: '97de212e80737a608d939f648d959671fb0a0142' } }

        it 'returns matched pipelines' do
          is_expected.to eq([pipeline])
        end
      end

      context 'when sha does not exist' do
        let(:params) { { sha: 'invalid-sha' } }

        it 'returns empty' do
          is_expected.to be_empty
        end
      end
    end
228

jumpyoshim's avatar
jumpyoshim committed
229
    context 'when the project has limited access to pipelines' do
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      let(:project) { create(:project, :private, :repository) }
      let(:current_user) { create(:user) }
      let!(:pipelines) { create_list(:ci_pipeline, 2, project: project) }

      context 'when the user has access' do
        before do
          project.add_developer(current_user)
        end

        it 'is expected to return pipelines' do
          is_expected.to contain_exactly(*pipelines)
        end
      end

      context 'the user is not allowed to read pipelines' do
        it 'returns empty' do
          is_expected.to be_empty
        end
      end
    end
250 251
  end
end