project_spec.rb 7.54 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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
# == Schema Information
#
# Table name: projects
#
#  id                       :integer          not null, primary key
#  name                     :string(255)      not null
#  timeout                  :integer          default(3600), not null
#  created_at               :datetime
#  updated_at               :datetime
#  token                    :string(255)
#  default_ref              :string(255)
#  path                     :string(255)
#  always_build             :boolean          default(FALSE), not null
#  polling_interval         :integer
#  public                   :boolean          default(FALSE), not null
#  ssh_url_to_repo          :string(255)
#  gitlab_id                :integer
#  allow_git_fetch          :boolean          default(TRUE), not null
#  email_recipients         :string(255)      default(""), not null
#  email_add_pusher         :boolean          default(TRUE), not null
#  email_only_broken_builds :boolean          default(TRUE), not null
#  skip_refs                :string(255)
#  coverage_regex           :string(255)
#  shared_runners_enabled   :boolean          default(FALSE)
#  generated_yaml_config    :text
#

require 'spec_helper'

30
describe Ci::Project do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
31
32
  let(:gl_project) { FactoryGirl.create :empty_project }
  subject { FactoryGirl.create :ci_project, gl_project: gl_project }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33

Kamil Trzcinski's avatar
Kamil Trzcinski committed
34
35
36
37
38
39
40
  it { is_expected.to have_many(:runner_projects) }
  it { is_expected.to have_many(:runners) }
  it { is_expected.to have_many(:web_hooks) }
  it { is_expected.to have_many(:events) }
  it { is_expected.to have_many(:variables) }
  it { is_expected.to have_many(:triggers) }
  it { is_expected.to have_many(:services) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
42
  it { is_expected.to validate_presence_of :timeout }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
43
44
45

  describe 'before_validation' do
    it 'should set an random token if none provided' do
46
      project = FactoryGirl.create :ci_project_without_token
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
47
      expect(project.token).not_to eq("")
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
48
49
50
    end

    it 'should not set an random toke if one provided' do
51
      project = FactoryGirl.create :ci_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
52
      expect(project.token).to eq("iPWx6WM4lhHNedGfBpPJNP")
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
53
54
55
56
57
    end
  end

  describe "ordered_by_last_commit_date" do
    it "returns ordered projects" do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
58
59
60
61
62
63
      newest_project = FactoryGirl.create :empty_project
      newest_project.ensure_ci_project
      oldest_project = FactoryGirl.create :empty_project
      oldest_project.ensure_ci_project
      project_without_commits = FactoryGirl.create :empty_project
      project_without_commits.ensure_ci_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
64

Kamil Trzcinski's avatar
Kamil Trzcinski committed
65
66
      FactoryGirl.create :ci_commit, committed_at: 1.hour.ago, gl_project: newest_project
      FactoryGirl.create :ci_commit, committed_at: 2.hour.ago, gl_project: oldest_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
67

Kamil Trzcinski's avatar
Kamil Trzcinski committed
68
      expect(Ci::Project.ordered_by_last_commit_date).to eq([newest_project.gitlab_ci_project, oldest_project.gitlab_ci_project, project_without_commits.gitlab_ci_project])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
69
70
71
    end
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
72
  describe 'ordered commits' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
73
    let(:project) { FactoryGirl.create :empty_project }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
74
75

    it 'returns ordered list of commits' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
76
77
      commit1 = FactoryGirl.create :ci_commit, committed_at: 1.hour.ago, gl_project: project
      commit2 = FactoryGirl.create :ci_commit, committed_at: 2.hour.ago, gl_project: project
Kamil Trzcinski's avatar
Kamil Trzcinski committed
78
      expect(project.commits).to eq([commit2, commit1])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
79
80
81
    end

    it 'returns commits ordered by committed_at and id, with nulls last' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
82
83
84
85
      commit1 = FactoryGirl.create :ci_commit, committed_at: 1.hour.ago, gl_project: project
      commit2 = FactoryGirl.create :ci_commit, committed_at: nil, gl_project: project
      commit3 = FactoryGirl.create :ci_commit, committed_at: 2.hour.ago, gl_project: project
      commit4 = FactoryGirl.create :ci_commit, committed_at: nil, gl_project: project
Kamil Trzcinski's avatar
Kamil Trzcinski committed
86
      expect(project.commits).to eq([commit2, commit4, commit3, commit1])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
87
88
89
    end
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
90
  context :valid_project do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
91
    let(:commit) { FactoryGirl.create(:ci_commit) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
92
93

    context :project_with_commit_and_builds do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
94
95
      let(:project) { commit.project }

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
96
      before do
97
        FactoryGirl.create(:ci_build, commit: commit)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98
99
      end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
100
      it { expect(project.status).to eq('pending') }
101
      it { expect(project.last_commit).to be_kind_of(Ci::Commit)  }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
102
      it { expect(project.human_status).to eq('pending') }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
103
104
105
106
107
    end
  end

  describe '#email_notification?' do
    it do
108
      project = FactoryGirl.create :ci_project, email_add_pusher: true
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
109
      expect(project.email_notification?).to eq(true)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
110
111
112
    end

    it do
113
      project = FactoryGirl.create :ci_project, email_add_pusher: false, email_recipients: 'test tesft'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
114
      expect(project.email_notification?).to eq(true)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
115
116
117
    end

    it do
118
      project = FactoryGirl.create :ci_project, email_add_pusher: false, email_recipients: ''
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
119
      expect(project.email_notification?).to eq(false)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
120
121
122
123
    end
  end

  describe '#broken_or_success?' do
Valery Sizov's avatar
Valery Sizov committed
124
    it do
125
      project = FactoryGirl.create :ci_project, email_add_pusher: true
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
126
127
128
      allow(project).to receive(:broken?).and_return(true)
      allow(project).to receive(:success?).and_return(true)
      expect(project.broken_or_success?).to eq(true)
Valery Sizov's avatar
Valery Sizov committed
129
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
130

Valery Sizov's avatar
Valery Sizov committed
131
    it do
132
      project = FactoryGirl.create :ci_project, email_add_pusher: true
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
133
134
135
      allow(project).to receive(:broken?).and_return(true)
      allow(project).to receive(:success?).and_return(false)
      expect(project.broken_or_success?).to eq(true)
Valery Sizov's avatar
Valery Sizov committed
136
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
137

Valery Sizov's avatar
Valery Sizov committed
138
    it do
139
      project = FactoryGirl.create :ci_project, email_add_pusher: true
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
140
141
142
      allow(project).to receive(:broken?).and_return(false)
      allow(project).to receive(:success?).and_return(true)
      expect(project.broken_or_success?).to eq(true)
Valery Sizov's avatar
Valery Sizov committed
143
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
144

Valery Sizov's avatar
Valery Sizov committed
145
    it do
146
      project = FactoryGirl.create :ci_project, email_add_pusher: true
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
147
148
149
      allow(project).to receive(:broken?).and_return(false)
      allow(project).to receive(:success?).and_return(false)
      expect(project.broken_or_success?).to eq(false)
Valery Sizov's avatar
Valery Sizov committed
150
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
151
152
153
  end

  describe 'Project.parse' do
154
    let(:project) { FactoryGirl.create :project }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
155

156
    subject { Ci::Project.parse(project) }
157

158
159
160
    it { is_expected.to be_valid }
    it { is_expected.to be_kind_of(Ci::Project) }
    it { expect(subject.name).to eq(project.name_with_namespace) }
161
    it { expect(subject.gitlab_id).to eq(project.id) }
162
    it { expect(subject.gitlab_url).to eq(project.web_url) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
163
164
165
  end

  describe :repo_url_with_auth do
166
    let(:project) { FactoryGirl.create :ci_project }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
167
168
    subject { project.repo_url_with_auth }

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
169
170
171
172
173
174
    it { is_expected.to be_a(String) }
    it { is_expected.to end_with(".git") }
    it { is_expected.to start_with(project.gitlab_url[0..6]) }
    it { is_expected.to include(project.token) }
    it { is_expected.to include('gitlab-ci-token') }
    it { is_expected.to include(project.gitlab_url[7..-1]) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
175
176
177
  end

  describe :search do
178
    let!(:project) { FactoryGirl.create(:ci_project, name: "foo") }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
179

180
181
    it { expect(Ci::Project.search('fo')).to include(project) }
    it { expect(Ci::Project.search('bar')).to be_empty }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
182
183
184
185
  end

  describe :any_runners do
    it "there are no runners available" do
186
      project = FactoryGirl.create(:ci_project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
187
      expect(project.any_runners?).to be_falsey
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
188
189
190
    end

    it "there is a specific runner" do
191
192
      project = FactoryGirl.create(:ci_project)
      project.runners << FactoryGirl.create(:ci_specific_runner)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
193
      expect(project.any_runners?).to be_truthy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
194
195
196
    end

    it "there is a shared runner" do
197
198
      project = FactoryGirl.create(:ci_project, shared_runners_enabled: true)
      FactoryGirl.create(:ci_shared_runner)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
199
      expect(project.any_runners?).to be_truthy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
200
201
202
    end

    it "there is a shared runner, but they are prohibited to use" do
203
204
      project = FactoryGirl.create(:ci_project)
      FactoryGirl.create(:ci_shared_runner)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
205
      expect(project.any_runners?).to be_falsey
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
206
207
208
    end
  end
end