OSDN Git Service

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