summaryrefslogtreecommitdiffstats
path: root/test/unit/project_test.rb
blob: e61fe313ce12f7232777abf57fa5893a273d4267 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++


require File.dirname(__FILE__) + '/../test_helper'

class ProjectTest < ActiveSupport::TestCase


  def create_project(options={})
    Project.new({
      :title => "foo project",
      :slug => "foo",
      :description => "my little project",
      :user => users(:johan),
      :owner => users(:johan)
    }.merge(options))
  end

  should_belong_to :containing_site
  should_have_many :merge_request_statuses

  should " have a title to be valid" do
    project = create_project(:title => nil)
    assert !project.valid?, 'valid? should be false'
    project.title = "foo"
    assert project.valid?
  end

  should " have a slug to be valid" do
    project = create_project(:slug => nil)
    assert !project.valid?, 'valid? should be false'
  end

  should " have a unique slug to be valid" do
    p1 = create_project
    p1.save!
    p2 = create_project(:slug => "FOO")
    assert !p2.valid?, 'valid? should be false'
    assert_not_nil p2.errors.on(:slug)
  end

  should " have an alphanumeric slug" do
    project = create_project(:slug => "asd asd")
    project.valid?
    assert !project.valid?, 'valid? should be false'
  end

  should " downcase the slug before validation" do
    project = create_project(:slug => "FOO")
    project.valid?
    assert_equal "foo", project.slug
  end

  should "cannot have a reserved name as slug" do
    project = create_project(:slug => Gitorious::Reservations.project_names.first)
    project.valid?
    assert_not_nil project.errors.on(:slug)

    project = create_project(:slug => "dashboard")
    project.valid?
    assert_not_nil project.errors.on(:slug)
  end

  should "creates the wiki repository on create" do
    project = create_project(:slug => "my-new-project")
    project.save!
    assert_instance_of Repository, project.wiki_repository
    assert_equal "my-new-project#{Repository::WIKI_NAME_SUFFIX}", project.wiki_repository.name
    assert_equal Repository::KIND_WIKI, project.wiki_repository.kind
    assert !project.repositories.include?(project.wiki_repository)
    assert_equal project.owner, project.wiki_repository.owner
  end

  should "finds a project by slug or raises" do
    assert_equal projects(:johans), Project.find_by_slug!(projects(:johans).slug)
    assert_raises(ActiveRecord::RecordNotFound) do
      Project.find_by_slug!("asdasdasd")
    end
  end

  should "has the slug as its params" do
    assert_equal projects(:johans).slug, projects(:johans).to_param
  end

  should "knows if a user is a admin on a project" do
    project = projects(:johans)
    assert project.admin?(users(:johan))
    project.owner = groups(:team_thunderbird)
    assert !project.admin?(users(:johan))
    project.owner.add_member(users(:johan), Role.admin)
    assert project.admin?(users(:johan))

    assert !project.admin?(users(:moe))
    project.owner.add_member(users(:moe), Role.member)
    assert !project.admin?(users(:moe))
    # be able to deal with AuthenticatedSystem's quirky design:
    assert !project.admin?(:false)
    assert !project.admin?(false)
    assert !project.admin?(nil)
  end

  should "knows if a user is a member on a project" do
    project = projects(:johans)
    assert project.member?(users(:johan))
    project.owner = groups(:team_thunderbird)
    assert !project.member?(users(:johan))
    project.owner.add_member(users(:johan), Role.member)
    assert project.member?(users(:johan))

    assert !project.member?(users(:moe))
    project.owner.add_member(users(:moe), Role.member)
    assert !project.admin?(users(:moe))
    # be able to deal with AuthenticatedSystem's quirky design:
    assert !project.member?(:false)
    assert !project.member?(false)
    assert !project.member?(nil)
  end

  should "knows if a user can delete the project" do
    project = projects(:johans)
    assert !project.can_be_deleted_by?(users(:moe))
    assert !project.can_be_deleted_by?(users(:johan)) # it has clones..
    project.repositories.clones.each(&:destroy)
    assert project.reload.can_be_deleted_by?(users(:johan)) # the clones are clone
  end

  should " strip html tags" do
    project = create_project(:description => "<h1>Project A</h1>\n<b>Project A</b> is a....")
    assert_equal "Project A\nProject A is a....", project.stripped_description
  end

  should " have a breadcrumb_parent method which returns nil" do
    project = create_project
    assert project.breadcrumb_parent.nil?
  end

  # should "strip html tags, except highlights" do
  #   project = create_project(:description => %Q{<h1>Project A</h1>\n<strong class="highlight">Project A</strong> is a....})
  #   assert_equal %Q(Project A\n<strong class="highlight">Project A</strong> is a....), #   project.stripped_description
  # end

  should "have valid urls ( prepending http:// if needed )" do
    project = projects(:johans)
    [ :home_url, :mailinglist_url, :bugtracker_url ].each do |attr|
      assert project.valid?
      project.send("#{attr}=", 'http://blah.com')
      assert project.valid?
      project.send("#{attr}=", 'ftp://blah.com')
      assert !project.valid?, 'valid? should be false'
      project.send("#{attr}=", 'blah.com')
      assert project.valid?
      assert_equal 'http://blah.com', project.send(attr)
    end
  end

  should " not prepend http:// to empty urls" do
    project = projects(:johans)
    [ :home_url, :mailinglist_url, :bugtracker_url ].each do |attr|
      project.send("#{attr}=", '')
      assert project.send(attr).blank?
      project.send("#{attr}=", nil)
      assert project.send(attr).blank?
    end
  end

  should " find or create an associated wiki repo" do
    project = projects(:johans)
    repo = repositories(:johans)
    repo.kind = Repository::KIND_WIKI
    project.wiki_repository = repo
    project.save!
    assert_equal repo, project.reload.wiki_repository
  end

  should " have a wiki repository" do
    project = projects(:johans)
    assert_equal repositories(:johans_wiki), project.wiki_repository
    assert !project.repositories.include?(repositories(:johans_wiki))
  end

  should "has to_param_with_prefix" do
    assert_equal projects(:johans).to_param, projects(:johans).to_param_with_prefix
  end

  should " change the owner of the wiki repo as well" do
    project = projects(:johans)
    project.change_owner_to(groups(:team_thunderbird))
    assert_equal groups(:team_thunderbird), project.owner
    assert_equal groups(:team_thunderbird), project.wiki_repository.owner
  end

  should " allow changing ownership from a user to a group, but not the other way around" do
    p = projects(:johans)
    p.change_owner_to(groups(:team_thunderbird))
    assert_equal groups(:team_thunderbird), p.owner
    p.change_owner_to(users(:johan))
    assert_equal groups(:team_thunderbird), p.owner
  end

  should "add group as admin to mainline repositories when changing ownership" do
    p = projects(:johans)
    assert_difference("Committership.count") { p.change_owner_to(groups(:team_thunderbird)) }
    committership = p.repositories.mainlines.first.committerships.detect { |c|
      c.committer == groups(:team_thunderbird)
    }
    assert_not_nil committership
    assert_equal(
      Committership::CAN_REVIEW | Committership::CAN_COMMIT | Committership::CAN_ADMIN,
      committership.permissions)
  end

  should "delegate wiki permissions to the wiki repository" do
    project = projects(:johans)
    assert_equal project.wiki_repository.wiki_permissions, project.wiki_permissions
    project.wiki_permissions = 2
    assert_equal 2, project.wiki_permissions
  end

  should "extract first paragraph from description" do
    project = projects(:johans)

    project.description = "Hello.\nWorld."
    assert_equal "Hello.", project.descriptions_first_paragraph

    project.description = "No newline."
    assert_equal "No newline.", project.descriptions_first_paragraph
  end

  context "Project events" do
    setup do
      @project = projects(:johans)
      @user = users(:johan)
      @repository = repositories(:johans)
    end

    should " create an event from the action name" do
      assert_not_equal nil, @project.create_event(Action::CREATE_PROJECT, @repository, @user, "", "")
    end

    should 'allow optional creation of events' do
      assert @project.new_event_required?(Action::UPDATE_WIKI_PAGE, @repository, @user, 'HomePage')
      event = @project.create_event(Action::UPDATE_WIKI_PAGE, @repository, @user, 'HomePage', Time.now)
      assert !@project.new_event_required?(Action::UPDATE_WIKI_PAGE, @repository, @user, 'HomePage')
      event.update_attributes(:created_at => 2.hours.ago)
      assert @project.new_event_required?(Action::UPDATE_WIKI_PAGE, @repository, @user, 'HomePage')
    end

    should " create an event even without a valid id" do
      assert_not_equal nil, @project.create_event(52342, @repository, @user)
    end

    should "creates valid attributes on the event" do
      e = @project.create_event(Action::COMMIT, @repository, @user, "somedata", "a body")
      assert e.valid?
      assert !e.new_record?, 'e.new_record? should be false'
      e.reload
      assert_equal Action::COMMIT, e.action
      assert_equal @repository, e.target
      assert_equal @project, e.project
      assert_equal @user, e.user
      assert_equal "somedata", e.data
      assert_equal "a body", e.body
    end
  end

  context "Containing Site" do
    should "have a site" do
      assert_equal sites(:qt), projects(:thunderbird).site
    end

    should "have a default site if site_id is nil" do
      assert_equal Site.default.title, projects(:johans).site.title
    end
  end

  context "Thottling" do
    setup{ Project.destroy_all }

    should "throttle on create" do
      assert_nothing_raised do
        5.times{|i| create_project(:slug => "wifebeater#{i}").save! }
      end

      assert_no_difference("Project.count") do
        assert_raises(RecordThrottling::LimitReachedError) do
          create_project(:slug => "wtf-are-you-doing-bro").save!
        end
      end
    end
  end

  context 'Oauth' do
    setup do
      @project = projects(:johans)
      @project.oauth_signoff_site = 'http://oauth.example'
    end

    should 'return oauth_consumer_options with default paths' do
      assert_equal({:site => @project.oauth_signoff_site}, @project.oauth_consumer_options)
    end

    should 'append correct paths when a prefix is supplied' do
      @project.oauth_path_prefix = "/path/to/oauth"
      consumer_options = @project.oauth_consumer_options
      assert_equal('/path/to/oauth/request_token', consumer_options[:request_token_path])
    end

    should 'append a correct path even with strange options' do
      @project.oauth_path_prefix = "path/to/oauth/"
      consumer_options = @project.oauth_consumer_options
      assert_equal('/path/to/oauth/request_token', consumer_options[:request_token_path])
    end

    should 'be able to set the oauth options from a hash' do
      new_settings = {
        :path_prefix    => '/foo',
        :signoff_key    => 'kee',
        :site           => 'http://oauth.example.com',
        :signoff_secret => 'secret'
      }
      @project.oauth_settings = new_settings
      expected = {
          :site                 => 'http://oauth.example.com',
          :request_token_path   => '/foo/request_token',
          :authorize_path       => '/foo/authorize',
          :access_token_path    => '/foo/access_token'
        }
      assert @project.merge_requests_need_signoff?
      assert_equal expected, @project.oauth_consumer_options
      assert_equal 'kee', @project.oauth_signoff_key
      assert_equal 'secret', @project.oauth_signoff_secret
      assert_equal new_settings, @project.oauth_settings
    end

    should 'deactivate signoff on merge requests when passing an empty :site option in oauth_settings' do
      @project.oauth_settings = {:site => ''}
      assert !@project.merge_requests_need_signoff?
      @project.oauth_settings = {}
      assert !@project.merge_requests_need_signoff?
    end
  end

  context "#to_xml" do
    setup do
      @project = projects(:johans)
    end

    should "not include oauth keys" do
      assert_no_match(/<oauth/, @project.to_xml)
      assert_no_match(/<merge-requests-need-signoff/, @project.to_xml)
    end

    should "include a list of the mainline repositories" do
      assert_match(/<mainlines/, @project.to_xml)
    end

    should "include a list of the repository clones" do
      assert_match(/<clones/, @project.to_xml)
    end
  end

  context 'Merge request status tags' do
    setup {@project = Factory.create(:user_project)}

    should 'serialize merge_request_state_options' do
      @project.merge_request_custom_states = %w(Merged Verifying)
      assert_equal %w(Merged Verifying), @project.merge_request_custom_states
    end

    should 'be serializible through a text-only version' do
      assert_equal "Open\nClosed\nVerifying", @project.merge_request_states
      @project.merge_request_states = "Foo\nBar"
      assert_equal ['Foo','Bar'], @project.merge_request_custom_states
    end
  end

  should "create default merge_request_statuses on creation" do
    project = Factory.build(:user_project)
    assert project.new_record?
    project.save!

    assert_equal 2, project.reload.merge_request_statuses.count
    open_status, closed_status = project.merge_request_statuses
    assert_equal MergeRequest::STATUS_OPEN, open_status.state
    assert_equal "Open", open_status.name
    assert_equal MergeRequest::STATUS_CLOSED, closed_status.state
    assert_equal "Closed", closed_status.name
  end


  context "Searching" do
    setup do
      @owner = Factory.create(:user, :login => "thejoker")
      @project = Factory.create(:project, :user => @owner,
        :owner => @owner)
      @repo = Factory.create(:repository, :project => @project, :owner => @owner,
        :user => @owner, :name => "thework", :description => "halloween")
      @group = Factory.create(:group, :creator => @owner,
        :name => "foo-hackers", :user_id => @owner.to_param)
      @group_repo = Factory.create(:repository, :project => @project,
        :owner => @group, :name => "group-repo", :user => @owner)
      @tracking_repo = @repo.create_tracking_repository
    end

    should "find repositories matching the repo name" do
      assert @project.search_repositories("work").include?(@repo)
    end

    should "find repositories with a matching description" do
      assert @project.search_repositories("ween").include?(@repo)
    end

    should "find repositories matching the owning user's name" do
      assert @project.search_repositories("joker").include?(@repo)
    end

    should "find repositories matching the owning group's name" do
      assert @project.search_repositories("hackers").include?(@group_repo)
    end

    should "only include regular repositories" do
      assert !@project.search_repositories("track").include?(@tracking_repo)
    end
  end

  should "be added to the creators favorites" do
    p = create_project
    p.save!
    assert p.watched_by?(p.user)
  end
end