OSDN Git Service

Merge branch 'feature/refactoring_scopes_pr' of https://github.com/Undev/gitlabhq...
[wvm/gitlab.git] / spec / models / project_spec.rb
1 # == Schema Information
2 #
3 # Table name: projects
4 #
5 #  id                     :integer          not null, primary key
6 #  name                   :string(255)
7 #  path                   :string(255)
8 #  description            :text
9 #  created_at             :datetime         not null
10 #  updated_at             :datetime         not null
11 #  creator_id             :integer
12 #  default_branch         :string(255)
13 #  issues_enabled         :boolean          default(TRUE), not null
14 #  wall_enabled           :boolean          default(TRUE), not null
15 #  merge_requests_enabled :boolean          default(TRUE), not null
16 #  wiki_enabled           :boolean          default(TRUE), not null
17 #  namespace_id           :integer
18 #  public                 :boolean          default(FALSE), not null
19 #  issues_tracker         :string(255)      default("gitlab"), not null
20 #  issues_tracker_id      :string(255)
21 #  snippets_enabled       :boolean          default(TRUE), not null
22 #  last_activity_at       :datetime
23 #
24
25 require 'spec_helper'
26
27 describe Project do
28   describe "Associations" do
29     it { should belong_to(:group) }
30     it { should belong_to(:namespace) }
31     it { should belong_to(:creator).class_name('User') }
32     it { should have_many(:users) }
33     it { should have_many(:events).dependent(:destroy) }
34     it { should have_many(:merge_requests).dependent(:destroy) }
35     it { should have_many(:issues).dependent(:destroy) }
36     it { should have_many(:milestones).dependent(:destroy) }
37     it { should have_many(:users_projects).dependent(:destroy) }
38     it { should have_many(:notes).dependent(:destroy) }
39     it { should have_many(:snippets).dependent(:destroy) }
40     it { should have_many(:deploy_keys).dependent(:destroy) }
41     it { should have_many(:hooks).dependent(:destroy) }
42     it { should have_many(:wikis).dependent(:destroy) }
43     it { should have_many(:protected_branches).dependent(:destroy) }
44   end
45
46   describe "Mass assignment" do
47     it { should_not allow_mass_assignment_of(:namespace_id) }
48     it { should_not allow_mass_assignment_of(:creator_id) }
49   end
50
51   describe "Validation" do
52     let!(:project) { create(:project) }
53
54     it { should validate_presence_of(:name) }
55     it { should validate_uniqueness_of(:name) }
56     it { should ensure_length_of(:name).is_within(0..255) }
57
58     it { should validate_presence_of(:path) }
59     it { should validate_uniqueness_of(:path) }
60     it { should ensure_length_of(:path).is_within(0..255) }
61     it { should ensure_length_of(:description).is_within(0..2000) }
62     it { should validate_presence_of(:creator) }
63     it { should ensure_inclusion_of(:issues_enabled).in_array([true, false]) }
64     it { should ensure_inclusion_of(:wall_enabled).in_array([true, false]) }
65     it { should ensure_inclusion_of(:merge_requests_enabled).in_array([true, false]) }
66     it { should ensure_inclusion_of(:wiki_enabled).in_array([true, false]) }
67     it { should ensure_length_of(:issues_tracker_id).is_within(0..255) }
68
69     it "should not allow new projects beyond user limits" do
70       project.stub(:creator).and_return(double(can_create_project?: false, projects_limit: 1))
71       project.should_not be_valid
72       project.errors[:limit_reached].first.should match(/Your own projects limit is 1/)
73     end
74   end
75
76   describe "Respond to" do
77     it { should respond_to(:url_to_repo) }
78     it { should respond_to(:repo_exists?) }
79     it { should respond_to(:satellite) }
80     it { should respond_to(:update_merge_requests) }
81     it { should respond_to(:execute_hooks) }
82     it { should respond_to(:transfer) }
83     it { should respond_to(:name_with_namespace) }
84     it { should respond_to(:namespace_owner) }
85     it { should respond_to(:owner) }
86     it { should respond_to(:path_with_namespace) }
87   end
88
89   it "should return valid url to repo" do
90     project = Project.new(path: "somewhere")
91     project.url_to_repo.should == Gitlab.config.gitlab_shell.ssh_path_prefix + "somewhere.git"
92   end
93
94   it "returns the full web URL for this repo" do
95     project = Project.new(path: "somewhere")
96     project.web_url.should == "#{Gitlab.config.gitlab.url}/somewhere"
97   end
98
99   describe "last_activity methods" do
100     let(:project)    { create(:project) }
101     let(:last_event) { double(created_at: Time.now) }
102
103     describe "last_activity" do
104       it "should alias last_activity to last_event"do
105         project.stub(last_event: last_event)
106         project.last_activity.should == last_event
107       end
108     end
109
110     describe 'last_activity_date' do
111       it 'returns the creation date of the project\'s last event if present' do
112         last_activity_event = create(:event, project: project)
113         project.last_activity_date.to_s(:db).should == last_event.created_at.to_s(:db)
114       end
115
116       it 'returns the project\'s last update date if it has no events' do
117         project.last_activity_date.should == project.updated_at
118       end
119     end
120   end
121
122   describe :update_merge_requests do
123     let(:project) { create(:project_with_code) }
124
125     before do
126       @merge_request = create(:merge_request, project: project)
127       @key = create(:key, user_id: project.owner.id)
128     end
129
130     it "should close merge request if last commit from source branch was pushed to target branch" do
131       @merge_request.reloaded_commits
132       @merge_request.last_commit.id.should == "bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a"
133       project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a", "refs/heads/stable", @key.user)
134       @merge_request.reload
135       @merge_request.merged?.should be_true
136     end
137
138     it "should update merge request commits with new one if pushed to source branch" do
139       @merge_request.last_commit.should == nil
140       project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a", "refs/heads/master", @key.user)
141       @merge_request.reload
142       @merge_request.last_commit.id.should == "bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a"
143     end
144   end
145
146
147   describe :find_with_namespace do
148     context 'with namespace' do
149       before do
150         @group = create :group, name: 'gitlab'
151         @project = create(:project, name: 'gitlab-ci', namespace: @group)
152       end
153
154       it { Project.find_with_namespace('gitlab/gitlab-ci').should == @project }
155       it { Project.find_with_namespace('gitlab-ci').should be_nil }
156     end
157
158     context 'w/o namespace' do
159       before do
160         @project = create(:project, name: 'gitlab-ci')
161       end
162
163       it { Project.find_with_namespace('gitlab-ci').should == @project }
164       it { Project.find_with_namespace('gitlab/gitlab-ci').should be_nil }
165     end
166   end
167
168   describe :to_param do
169     context 'with namespace' do
170       before do
171         @group = create :group, name: 'gitlab'
172         @project = create(:project, name: 'gitlab-ci', namespace: @group)
173       end
174
175       it { @project.to_param.should == "gitlab/gitlab-ci" }
176     end
177
178     context 'w/o namespace' do
179       before do
180         @project = create(:project, name: 'gitlab-ci')
181       end
182
183       it { @project.to_param.should == "gitlab-ci" }
184     end
185   end
186
187   describe :repository do
188     let(:project) { create(:project) }
189
190     it "should return valid repo" do
191       project.repository.should be_kind_of(Repository)
192     end
193   end
194
195   describe :issue_exists? do
196     let(:project) { create(:project) }
197     let(:existed_issue) { create(:issue, project: project) }
198     let(:not_existed_issue) { create(:issue) }
199     let(:ext_project) { create(:redmine_project) }
200
201     it "should be true or if used internal tracker and issue exists" do
202       project.issue_exists?(existed_issue.id).should be_true
203     end
204
205     it "should be false or if used internal tracker and issue not exists" do
206       project.issue_exists?(not_existed_issue.id).should be_false
207     end
208
209     it "should always be true if used other tracker" do
210       ext_project.issue_exists?(rand(100)).should be_true
211     end
212   end
213
214   describe :used_default_issues_tracker? do
215     let(:project) { create(:project) }
216     let(:ext_project) { create(:redmine_project) }
217
218     it "should be true if used internal tracker" do
219       project.used_default_issues_tracker?.should be_true
220     end
221
222     it "should be false if used other tracker" do
223       ext_project.used_default_issues_tracker?.should be_false
224     end
225   end
226
227   describe :can_have_issues_tracker_id? do
228     let(:project) { create(:project) }
229     let(:ext_project) { create(:redmine_project) }
230
231     it "should be true for projects with external issues tracker if issues enabled" do
232       ext_project.can_have_issues_tracker_id?.should be_true
233     end
234
235     it "should be false for projects with internal issue tracker if issues enabled" do
236       project.can_have_issues_tracker_id?.should be_false
237     end
238
239     it "should be always false if issues disbled" do
240       project.issues_enabled = false
241       ext_project.issues_enabled = false
242
243       project.can_have_issues_tracker_id?.should be_false
244       ext_project.can_have_issues_tracker_id?.should be_false
245     end
246   end
247
248   describe :open_branches do
249     let(:project) { create(:project_with_code) }
250
251     before do
252       project.protected_branches.create(name: 'master')
253     end
254
255     it { project.open_branches.map(&:name).should include('bootstrap') }
256     it { project.open_branches.map(&:name).should_not include('master') }
257   end
258 end