Commit 79e936e632466c12222af00c08480e9798fe58bd

Authored by Dmitriy Zaporozhets
2 parents 6ff0652e 580ce4f2

Merge pull request #1300 from tsigo/spec_reorganize

Spec cleanup/reorganization
app/models/milestone.rb
@@ -28,17 +28,9 @@ class Milestone < ActiveRecord::Base @@ -28,17 +28,9 @@ class Milestone < ActiveRecord::Base
28 end 28 end
29 29
30 def percent_complete 30 def percent_complete
31 - @percent_complete ||= begin  
32 - total_i = self.issues.count  
33 - closed_i = self.issues.closed.count  
34 - if total_i > 0  
35 - (closed_i * 100) / total_i  
36 - else  
37 - 100  
38 - end  
39 - rescue => ex  
40 - 0  
41 - end 31 + ((self.issues.closed.count * 100) / self.issues.count).abs
  32 + rescue ZeroDivisionError
  33 + 100
42 end 34 end
43 35
44 def expires_at 36 def expires_at
features/step_definitions/project/projects_steps.rb
1 -include LoginMacros 1 +include LoginHelpers
2 2
3 Given /^I signin as a user$/ do 3 Given /^I signin as a user$/ do
4 login_as :user 4 login_as :user
features/support/env.rb
@@ -7,9 +7,9 @@ require 'cucumber/rails' @@ -7,9 +7,9 @@ require 'cucumber/rails'
7 require 'webmock/cucumber' 7 require 'webmock/cucumber'
8 WebMock.allow_net_connect! 8 WebMock.allow_net_connect!
9 9
10 -require Rails.root.join 'spec/monkeypatch'  
11 require Rails.root.join 'spec/factories' 10 require Rails.root.join 'spec/factories'
12 -require Rails.root.join 'spec/support/login' 11 +require Rails.root.join 'spec/support/monkeypatch'
  12 +require Rails.root.join 'spec/support/login_helpers'
13 require Rails.root.join 'spec/support/valid_commit' 13 require Rails.root.join 'spec/support/valid_commit'
14 14
15 Capybara.default_selector = :css 15 Capybara.default_selector = :css
spec/api/issues_spec.rb
@@ -1,71 +0,0 @@ @@ -1,71 +0,0 @@
1 -require 'spec_helper'  
2 -  
3 -describe Gitlab::API do  
4 - let(:user) { Factory :user }  
5 - let!(:project) { Factory :project, owner: user }  
6 - let!(:issue) { Factory :issue, author: user, assignee: user, project: project }  
7 - before { project.add_access(user, :read) }  
8 -  
9 - describe "GET /issues" do  
10 - it "should return authentication error" do  
11 - get "#{api_prefix}/issues"  
12 - response.status.should == 401  
13 - end  
14 -  
15 - describe "authenticated GET /issues" do  
16 - it "should return an array of issues" do  
17 - get "#{api_prefix}/issues?private_token=#{user.private_token}"  
18 - response.status.should == 200  
19 - json_response.should be_an Array  
20 - json_response.first['title'].should == issue.title  
21 - end  
22 - end  
23 - end  
24 -  
25 - describe "GET /projects/:id/issues" do  
26 - it "should return project issues" do  
27 - get "#{api_prefix}/projects/#{project.code}/issues?private_token=#{user.private_token}"  
28 - response.status.should == 200  
29 - json_response.should be_an Array  
30 - json_response.first['title'].should == issue.title  
31 - end  
32 - end  
33 -  
34 - describe "GET /projects/:id/issues/:issue_id" do  
35 - it "should return a project issue by id" do  
36 - get "#{api_prefix}/projects/#{project.code}/issues/#{issue.id}?private_token=#{user.private_token}"  
37 - response.status.should == 200  
38 - json_response['title'].should == issue.title  
39 - end  
40 - end  
41 -  
42 - describe "POST /projects/:id/issues" do  
43 - it "should create a new project issue" do  
44 - post "#{api_prefix}/projects/#{project.code}/issues?private_token=#{user.private_token}",  
45 - title: 'new issue', labels: 'label, label2'  
46 - response.status.should == 201  
47 - json_response['title'].should == 'new issue'  
48 - json_response['description'].should be_nil  
49 - json_response['labels'].should == ['label', 'label2']  
50 - end  
51 - end  
52 -  
53 - describe "PUT /projects/:id/issues/:issue_id" do  
54 - it "should update a project issue" do  
55 - put "#{api_prefix}/projects/#{project.code}/issues/#{issue.id}?private_token=#{user.private_token}",  
56 - title: 'updated title', labels: 'label2', closed: 1  
57 - response.status.should == 200  
58 - json_response['title'].should == 'updated title'  
59 - json_response['labels'].should == ['label2']  
60 - json_response['closed'].should be_true  
61 - end  
62 - end  
63 -  
64 - describe "DELETE /projects/:id/issues/:issue_id" do  
65 - it "should delete a project issue" do  
66 - expect {  
67 - delete "#{api_prefix}/projects/#{project.code}/issues/#{issue.id}?private_token=#{user.private_token}"  
68 - }.to change { Issue.count }.by(-1)  
69 - end  
70 - end  
71 -end  
spec/api/projects_spec.rb
@@ -1,136 +0,0 @@ @@ -1,136 +0,0 @@
1 -require 'spec_helper'  
2 -  
3 -describe Gitlab::API do  
4 - let(:user) { Factory :user }  
5 - let!(:project) { Factory :project, owner: user }  
6 - let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }  
7 - before { project.add_access(user, :read) }  
8 -  
9 - describe "GET /projects" do  
10 - it "should return authentication error" do  
11 - get "#{api_prefix}/projects"  
12 - response.status.should == 401  
13 - end  
14 -  
15 - describe "authenticated GET /projects" do  
16 - it "should return an array of projects" do  
17 - get "#{api_prefix}/projects?private_token=#{user.private_token}"  
18 - response.status.should == 200  
19 - json_response.should be_an Array  
20 - json_response.first['name'].should == project.name  
21 - json_response.first['owner']['email'].should == user.email  
22 - end  
23 - end  
24 - end  
25 -  
26 - describe "GET /projects/:id" do  
27 - it "should return a project by id" do  
28 - get "#{api_prefix}/projects/#{project.id}?private_token=#{user.private_token}"  
29 - response.status.should == 200  
30 - json_response['name'].should == project.name  
31 - json_response['owner']['email'].should == user.email  
32 - end  
33 -  
34 - it "should return a project by code name" do  
35 - get "#{api_prefix}/projects/#{project.code}?private_token=#{user.private_token}"  
36 - response.status.should == 200  
37 - json_response['name'].should == project.name  
38 - end  
39 -  
40 - it "should return a 404 error if not found" do  
41 - get "#{api_prefix}/projects/42?private_token=#{user.private_token}"  
42 - response.status.should == 404  
43 - json_response['message'].should == '404 Not found'  
44 - end  
45 - end  
46 -  
47 - describe "GET /projects/:id/repository/branches" do  
48 - it "should return an array of project branches" do  
49 - get "#{api_prefix}/projects/#{project.code}/repository/branches?private_token=#{user.private_token}"  
50 - response.status.should == 200  
51 - json_response.should be_an Array  
52 - json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name  
53 - end  
54 - end  
55 -  
56 - describe "GET /projects/:id/repository/branches/:branch" do  
57 - it "should return the branch information for a single branch" do  
58 - get "#{api_prefix}/projects/#{project.code}/repository/branches/new_design?private_token=#{user.private_token}"  
59 - response.status.should == 200  
60 -  
61 - json_response['name'].should == 'new_design'  
62 - json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'  
63 - end  
64 - end  
65 -  
66 - describe "GET /projects/:id/repository/tags" do  
67 - it "should return an array of project tags" do  
68 - get "#{api_prefix}/projects/#{project.code}/repository/tags?private_token=#{user.private_token}"  
69 - response.status.should == 200  
70 - json_response.should be_an Array  
71 - json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name  
72 - end  
73 - end  
74 -  
75 - describe "GET /projects/:id/snippets/:snippet_id" do  
76 - it "should return a project snippet" do  
77 - get "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}"  
78 - response.status.should == 200  
79 - json_response['title'].should == snippet.title  
80 - end  
81 - end  
82 -  
83 - describe "POST /projects/:id/snippets" do  
84 - it "should create a new project snippet" do  
85 - post "#{api_prefix}/projects/#{project.code}/snippets?private_token=#{user.private_token}",  
86 - title: 'api test', file_name: 'sample.rb', code: 'test'  
87 - response.status.should == 201  
88 - json_response['title'].should == 'api test'  
89 - end  
90 - end  
91 -  
92 - describe "PUT /projects/:id/snippets" do  
93 - it "should update an existing project snippet" do  
94 - put "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}",  
95 - code: 'updated code'  
96 - response.status.should == 200  
97 - json_response['title'].should == 'example'  
98 - snippet.reload.content.should == 'updated code'  
99 - end  
100 - end  
101 -  
102 - describe "DELETE /projects/:id/snippets/:snippet_id" do  
103 - it "should delete existing project snippet" do  
104 - expect {  
105 - delete "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}"  
106 - }.to change { Snippet.count }.by(-1)  
107 - end  
108 - end  
109 -  
110 - describe "GET /projects/:id/snippets/:snippet_id/raw" do  
111 - it "should get a raw project snippet" do  
112 - get "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}/raw?private_token=#{user.private_token}"  
113 - response.status.should == 200  
114 - end  
115 - end  
116 -  
117 - describe "GET /projects/:id/:sha/blob" do  
118 - it "should get the raw file contents" do  
119 - get "#{api_prefix}/projects/#{project.code}/repository/commits/master/blob?filepath=README.md&private_token=#{user.private_token}"  
120 -  
121 - response.status.should == 200  
122 - end  
123 -  
124 - it "should return 404 for invalid branch_name" do  
125 - get "#{api_prefix}/projects/#{project.code}/repository/commits/invalid_branch_name/blob?filepath=README.md&private_token=#{user.private_token}"  
126 -  
127 - response.status.should == 404  
128 - end  
129 -  
130 - it "should return 404 for invalid file" do  
131 - get "#{api_prefix}/projects/#{project.code}/repository/commits/master/blob?filepath=README.invalid&private_token=#{user.private_token}"  
132 -  
133 - response.status.should == 404  
134 - end  
135 - end  
136 -end  
spec/api/users_spec.rb
@@ -1,37 +0,0 @@ @@ -1,37 +0,0 @@
1 -require 'spec_helper'  
2 -  
3 -describe Gitlab::API do  
4 - let(:user) { Factory :user }  
5 -  
6 - describe "GET /users" do  
7 - it "should return authentication error" do  
8 - get "#{api_prefix}/users"  
9 - response.status.should == 401  
10 - end  
11 -  
12 - describe "authenticated GET /users" do  
13 - it "should return an array of users" do  
14 - get "#{api_prefix}/users?private_token=#{user.private_token}"  
15 - response.status.should == 200  
16 - json_response.should be_an Array  
17 - json_response.first['email'].should == user.email  
18 - end  
19 - end  
20 - end  
21 -  
22 - describe "GET /users/:id" do  
23 - it "should return a user by id" do  
24 - get "#{api_prefix}/users/#{user.id}?private_token=#{user.private_token}"  
25 - response.status.should == 200  
26 - json_response['email'].should == user.email  
27 - end  
28 - end  
29 -  
30 - describe "GET /user" do  
31 - it "should return current user" do  
32 - get "#{api_prefix}/user?private_token=#{user.private_token}"  
33 - response.status.should == 200  
34 - json_response['email'].should == user.email  
35 - end  
36 - end  
37 -end  
spec/models/activity_observer_spec.rb
@@ -1,48 +0,0 @@ @@ -1,48 +0,0 @@
1 -require 'spec_helper'  
2 -  
3 -describe ActivityObserver do  
4 - let(:project) { Factory :project }  
5 -  
6 - def self.it_should_be_valid_event  
7 - it { @event.should_not be_nil }  
8 - it { @event.project.should == project }  
9 - end  
10 -  
11 - describe "Merge Request created" do  
12 - before do  
13 - MergeRequest.observers.enable :activity_observer do  
14 - @merge_request = Factory :merge_request, project: project  
15 - @event = Event.last  
16 - end  
17 - end  
18 -  
19 - it_should_be_valid_event  
20 - it { @event.action.should == Event::Created }  
21 - it { @event.target.should == @merge_request }  
22 - end  
23 -  
24 - describe "Issue created" do  
25 - before do  
26 - Issue.observers.enable :activity_observer do  
27 - @issue = Factory :issue, project: project  
28 - @event = Event.last  
29 - end  
30 - end  
31 -  
32 - it_should_be_valid_event  
33 - it { @event.action.should == Event::Created }  
34 - it { @event.target.should == @issue }  
35 - end  
36 -  
37 - #describe "Issue commented" do  
38 - #before do  
39 - #@issue = Factory :issue, project: project  
40 - #@note = Factory :note, noteable: @issue, project: project  
41 - #@event = Event.last  
42 - #end  
43 -  
44 - #it_should_be_valid_event  
45 - #it { @event.action.should == Event::Commented }  
46 - #it { @event.target.should == @note }  
47 - #end  
48 -end  
spec/models/issue_observer_spec.rb
@@ -1,144 +0,0 @@ @@ -1,144 +0,0 @@
1 -require 'spec_helper'  
2 -  
3 -describe IssueObserver do  
4 - let(:some_user) { double(:user, id: 1) }  
5 - let(:assignee) { double(:user, id: 2) }  
6 - let(:issue) { double(:issue, id: 42, assignee: assignee) }  
7 -  
8 - before(:each) { subject.stub(:current_user).and_return(some_user) }  
9 -  
10 - subject { IssueObserver.instance }  
11 -  
12 - describe '#after_create' do  
13 -  
14 - it 'is called when an issue is created' do  
15 - subject.should_receive(:after_create)  
16 -  
17 - Issue.observers.enable :issue_observer do  
18 - Factory.create(:issue, project: Factory.create(:project))  
19 - end  
20 - end  
21 -  
22 - it 'sends an email to the assignee' do  
23 - Notify.should_receive(:new_issue_email).with(issue.id).  
24 - and_return(double(deliver: true))  
25 -  
26 - subject.after_create(issue)  
27 - end  
28 -  
29 - it 'does not send an email to the assignee if assignee created the issue' do  
30 - subject.stub(:current_user).and_return(assignee)  
31 - Notify.should_not_receive(:new_issue_email)  
32 -  
33 - subject.after_create(issue)  
34 - end  
35 - end  
36 -  
37 - context '#after_update' do  
38 - before(:each) do  
39 - issue.stub(:is_being_reassigned?).and_return(false)  
40 - issue.stub(:is_being_closed?).and_return(false)  
41 - issue.stub(:is_being_reopened?).and_return(false)  
42 - end  
43 -  
44 - it 'is called when an issue is changed' do  
45 - changed = Factory.create(:issue, project: Factory.create(:project))  
46 - subject.should_receive(:after_update)  
47 -  
48 - Issue.observers.enable :issue_observer do  
49 - changed.description = 'I changed'  
50 - changed.save  
51 - end  
52 - end  
53 -  
54 - context 'a reassigned email' do  
55 - it 'is sent if the issue is being reassigned' do  
56 - issue.should_receive(:is_being_reassigned?).and_return(true)  
57 - subject.should_receive(:send_reassigned_email).with(issue)  
58 -  
59 - subject.after_update(issue)  
60 - end  
61 -  
62 - it 'is not sent if the issue is not being reassigned' do  
63 - issue.should_receive(:is_being_reassigned?).and_return(false)  
64 - subject.should_not_receive(:send_reassigned_email)  
65 -  
66 - subject.after_update(issue)  
67 - end  
68 - end  
69 -  
70 - context 'a status "closed" note' do  
71 - it 'is created if the issue is being closed' do  
72 - issue.should_receive(:is_being_closed?).and_return(true)  
73 - Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')  
74 -  
75 - subject.after_update(issue)  
76 - end  
77 -  
78 - it 'is not created if the issue is not being closed' do  
79 - issue.should_receive(:is_being_closed?).and_return(false)  
80 - Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')  
81 -  
82 - subject.after_update(issue)  
83 - end  
84 - end  
85 -  
86 - context 'a status "reopened" note' do  
87 - it 'is created if the issue is being reopened' do  
88 - issue.should_receive(:is_being_reopened?).and_return(true)  
89 - Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')  
90 -  
91 - subject.after_update(issue)  
92 - end  
93 -  
94 - it 'is not created if the issue is not being reopened' do  
95 - issue.should_receive(:is_being_reopened?).and_return(false)  
96 - Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')  
97 -  
98 - subject.after_update(issue)  
99 - end  
100 - end  
101 - end  
102 -  
103 - describe '#send_reassigned_email' do  
104 - let(:previous_assignee) { double(:user, id: 3) }  
105 -  
106 - before(:each) do  
107 - issue.stub(:assignee_id).and_return(assignee.id)  
108 - issue.stub(:assignee_id_was).and_return(previous_assignee.id)  
109 - end  
110 -  
111 - def it_sends_a_reassigned_email_to(recipient)  
112 - Notify.should_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id).  
113 - and_return(double(deliver: true))  
114 - end  
115 -  
116 - def it_does_not_send_a_reassigned_email_to(recipient)  
117 - Notify.should_not_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id)  
118 - end  
119 -  
120 - it 'sends a reassigned email to the previous and current assignees' do  
121 - it_sends_a_reassigned_email_to assignee.id  
122 - it_sends_a_reassigned_email_to previous_assignee.id  
123 -  
124 - subject.send(:send_reassigned_email, issue)  
125 - end  
126 -  
127 - context 'does not send an email to the user who made the reassignment' do  
128 - it 'if the user is the assignee' do  
129 - subject.stub(:current_user).and_return(assignee)  
130 - it_sends_a_reassigned_email_to previous_assignee.id  
131 - it_does_not_send_a_reassigned_email_to assignee.id  
132 -  
133 - subject.send(:send_reassigned_email, issue)  
134 - end  
135 - it 'if the user is the previous assignee' do  
136 - subject.stub(:current_user).and_return(previous_assignee)  
137 - it_sends_a_reassigned_email_to assignee.id  
138 - it_does_not_send_a_reassigned_email_to previous_assignee.id  
139 -  
140 - subject.send(:send_reassigned_email, issue)  
141 - end  
142 - end  
143 - end  
144 -end  
spec/models/milestone_spec.rb
@@ -31,24 +31,33 @@ describe Milestone do @@ -31,24 +31,33 @@ describe Milestone do
31 31
32 it { milestone.should be_valid } 32 it { milestone.should be_valid }
33 33
34 - describe "Issues" do  
35 - before do 34 + describe "#percent_complete" do
  35 + it "should not count open issues" do
36 milestone.issues << issue 36 milestone.issues << issue
  37 + milestone.percent_complete.should == 0
37 end 38 end
38 39
39 - it { milestone.percent_complete.should == 0 } 40 + it "should count closed issues" do
  41 + issue.update_attributes(closed: true)
  42 + milestone.issues << issue
  43 + milestone.percent_complete.should == 100
  44 + end
40 45
41 - it do  
42 - issue.update_attributes closed: true 46 + it "should recover from dividing by zero" do
  47 + milestone.issues.should_receive(:count).and_return(0)
43 milestone.percent_complete.should == 100 48 milestone.percent_complete.should == 100
44 end 49 end
45 end 50 end
46 51
47 - describe :expires_at do  
48 - before do  
49 - milestone.update_attributes due_date: Date.today + 1.day 52 + describe "#expires_at" do
  53 + it "should be nil when due_date is unset" do
  54 + milestone.update_attributes(due_date: nil)
  55 + milestone.expires_at.should be_nil
50 end 56 end
51 57
52 - it { milestone.expires_at.should_not be_nil } 58 + it "should not be nil when due_date is set" do
  59 + milestone.update_attributes(due_date: Date.tomorrow)
  60 + milestone.expires_at.should be_present
  61 + end
53 end 62 end
54 end 63 end
spec/models/user_observer_spec.rb
@@ -1,26 +0,0 @@ @@ -1,26 +0,0 @@
1 -require 'spec_helper'  
2 -  
3 -describe UserObserver do  
4 - subject { UserObserver.instance }  
5 -  
6 - it 'calls #after_create when new users are created' do  
7 - new_user = Factory.new(:user)  
8 - subject.should_receive(:after_create).with(new_user)  
9 -  
10 - User.observers.enable :user_observer do  
11 - new_user.save  
12 - end  
13 - end  
14 -  
15 - context 'when a new user is created' do  
16 - let(:user) { double(:user, id: 42, password: 'P@ssword!') }  
17 - let(:notification) { double :notification }  
18 -  
19 - it 'sends an email' do  
20 - notification.should_receive(:deliver)  
21 - Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification)  
22 -  
23 - subject.after_create(user)  
24 - end  
25 - end  
26 -end  
spec/monkeypatch.rb
@@ -1,51 +0,0 @@ @@ -1,51 +0,0 @@
1 -# Stubbing Project <-> git host path  
2 -# create project using Factory only  
3 -class Project  
4 - def update_repository  
5 - true  
6 - end  
7 -  
8 - def destroy_repository  
9 - true  
10 - end  
11 -  
12 - def path_to_repo  
13 - File.join(Rails.root, "tmp", "tests", path)  
14 - end  
15 -  
16 - def satellite  
17 - @satellite ||= FakeSatellite.new  
18 - end  
19 -end  
20 -  
21 -class Key  
22 - def update_repository  
23 - true  
24 - end  
25 -  
26 - def repository_delete_key  
27 - true  
28 - end  
29 -end  
30 -  
31 -class UsersProject  
32 - def update_repository  
33 - true  
34 - end  
35 -end  
36 -  
37 -class FakeSatellite  
38 - def exists?  
39 - true  
40 - end  
41 -  
42 - def create  
43 - true  
44 - end  
45 -end  
46 -  
47 -class ProtectedBranch  
48 - def update_repository  
49 - true  
50 - end  
51 -end  
spec/observers/activity_observer_spec.rb 0 → 100644
@@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
  1 +require 'spec_helper'
  2 +
  3 +describe ActivityObserver do
  4 + let(:project) { Factory :project }
  5 +
  6 + def self.it_should_be_valid_event
  7 + it { @event.should_not be_nil }
  8 + it { @event.project.should == project }
  9 + end
  10 +
  11 + describe "Merge Request created" do
  12 + before do
  13 + MergeRequest.observers.enable :activity_observer do
  14 + @merge_request = Factory :merge_request, project: project
  15 + @event = Event.last
  16 + end
  17 + end
  18 +
  19 + it_should_be_valid_event
  20 + it { @event.action.should == Event::Created }
  21 + it { @event.target.should == @merge_request }
  22 + end
  23 +
  24 + describe "Issue created" do
  25 + before do
  26 + Issue.observers.enable :activity_observer do
  27 + @issue = Factory :issue, project: project
  28 + @event = Event.last
  29 + end
  30 + end
  31 +
  32 + it_should_be_valid_event
  33 + it { @event.action.should == Event::Created }
  34 + it { @event.target.should == @issue }
  35 + end
  36 +
  37 + #describe "Issue commented" do
  38 + #before do
  39 + #@issue = Factory :issue, project: project
  40 + #@note = Factory :note, noteable: @issue, project: project
  41 + #@event = Event.last
  42 + #end
  43 +
  44 + #it_should_be_valid_event
  45 + #it { @event.action.should == Event::Commented }
  46 + #it { @event.target.should == @note }
  47 + #end
  48 +end
spec/observers/issue_observer_spec.rb 0 → 100644
@@ -0,0 +1,144 @@ @@ -0,0 +1,144 @@
  1 +require 'spec_helper'
  2 +
  3 +describe IssueObserver do
  4 + let(:some_user) { double(:user, id: 1) }
  5 + let(:assignee) { double(:user, id: 2) }
  6 + let(:issue) { double(:issue, id: 42, assignee: assignee) }
  7 +
  8 + before(:each) { subject.stub(:current_user).and_return(some_user) }
  9 +
  10 + subject { IssueObserver.instance }
  11 +
  12 + describe '#after_create' do
  13 +
  14 + it 'is called when an issue is created' do
  15 + subject.should_receive(:after_create)
  16 +
  17 + Issue.observers.enable :issue_observer do
  18 + Factory.create(:issue, project: Factory.create(:project))
  19 + end
  20 + end
  21 +
  22 + it 'sends an email to the assignee' do
  23 + Notify.should_receive(:new_issue_email).with(issue.id).
  24 + and_return(double(deliver: true))
  25 +
  26 + subject.after_create(issue)
  27 + end
  28 +
  29 + it 'does not send an email to the assignee if assignee created the issue' do
  30 + subject.stub(:current_user).and_return(assignee)
  31 + Notify.should_not_receive(:new_issue_email)
  32 +
  33 + subject.after_create(issue)
  34 + end
  35 + end
  36 +
  37 + context '#after_update' do
  38 + before(:each) do
  39 + issue.stub(:is_being_reassigned?).and_return(false)
  40 + issue.stub(:is_being_closed?).and_return(false)
  41 + issue.stub(:is_being_reopened?).and_return(false)
  42 + end
  43 +
  44 + it 'is called when an issue is changed' do
  45 + changed = Factory.create(:issue, project: Factory.create(:project))
  46 + subject.should_receive(:after_update)
  47 +
  48 + Issue.observers.enable :issue_observer do
  49 + changed.description = 'I changed'
  50 + changed.save
  51 + end
  52 + end
  53 +
  54 + context 'a reassigned email' do
  55 + it 'is sent if the issue is being reassigned' do
  56 + issue.should_receive(:is_being_reassigned?).and_return(true)
  57 + subject.should_receive(:send_reassigned_email).with(issue)
  58 +
  59 + subject.after_update(issue)
  60 + end
  61 +
  62 + it 'is not sent if the issue is not being reassigned' do
  63 + issue.should_receive(:is_being_reassigned?).and_return(false)
  64 + subject.should_not_receive(:send_reassigned_email)
  65 +
  66 + subject.after_update(issue)
  67 + end
  68 + end
  69 +
  70 + context 'a status "closed" note' do
  71 + it 'is created if the issue is being closed' do
  72 + issue.should_receive(:is_being_closed?).and_return(true)
  73 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
  74 +
  75 + subject.after_update(issue)
  76 + end
  77 +
  78 + it 'is not created if the issue is not being closed' do
  79 + issue.should_receive(:is_being_closed?).and_return(false)
  80 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
  81 +
  82 + subject.after_update(issue)
  83 + end
  84 + end
  85 +
  86 + context 'a status "reopened" note' do
  87 + it 'is created if the issue is being reopened' do
  88 + issue.should_receive(:is_being_reopened?).and_return(true)
  89 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  90 +
  91 + subject.after_update(issue)
  92 + end
  93 +
  94 + it 'is not created if the issue is not being reopened' do
  95 + issue.should_receive(:is_being_reopened?).and_return(false)
  96 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  97 +
  98 + subject.after_update(issue)
  99 + end
  100 + end
  101 + end
  102 +
  103 + describe '#send_reassigned_email' do
  104 + let(:previous_assignee) { double(:user, id: 3) }
  105 +
  106 + before(:each) do
  107 + issue.stub(:assignee_id).and_return(assignee.id)
  108 + issue.stub(:assignee_id_was).and_return(previous_assignee.id)
  109 + end
  110 +
  111 + def it_sends_a_reassigned_email_to(recipient)
  112 + Notify.should_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id).
  113 + and_return(double(deliver: true))
  114 + end
  115 +
  116 + def it_does_not_send_a_reassigned_email_to(recipient)
  117 + Notify.should_not_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id)
  118 + end
  119 +
  120 + it 'sends a reassigned email to the previous and current assignees' do
  121 + it_sends_a_reassigned_email_to assignee.id
  122 + it_sends_a_reassigned_email_to previous_assignee.id
  123 +
  124 + subject.send(:send_reassigned_email, issue)
  125 + end
  126 +
  127 + context 'does not send an email to the user who made the reassignment' do
  128 + it 'if the user is the assignee' do
  129 + subject.stub(:current_user).and_return(assignee)
  130 + it_sends_a_reassigned_email_to previous_assignee.id
  131 + it_does_not_send_a_reassigned_email_to assignee.id
  132 +
  133 + subject.send(:send_reassigned_email, issue)
  134 + end
  135 + it 'if the user is the previous assignee' do
  136 + subject.stub(:current_user).and_return(previous_assignee)
  137 + it_sends_a_reassigned_email_to assignee.id
  138 + it_does_not_send_a_reassigned_email_to previous_assignee.id
  139 +
  140 + subject.send(:send_reassigned_email, issue)
  141 + end
  142 + end
  143 + end
  144 +end
spec/observers/user_observer_spec.rb 0 → 100644
@@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
  1 +require 'spec_helper'
  2 +
  3 +describe UserObserver do
  4 + subject { UserObserver.instance }
  5 +
  6 + it 'calls #after_create when new users are created' do
  7 + new_user = Factory.new(:user)
  8 + subject.should_receive(:after_create).with(new_user)
  9 +
  10 + User.observers.enable :user_observer do
  11 + new_user.save
  12 + end
  13 + end
  14 +
  15 + context 'when a new user is created' do
  16 + let(:user) { double(:user, id: 42, password: 'P@ssword!') }
  17 + let(:notification) { double :notification }
  18 +
  19 + it 'sends an email' do
  20 + notification.should_receive(:deliver)
  21 + Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification)
  22 +
  23 + subject.after_create(user)
  24 + end
  25 + end
  26 +end
spec/requests/admin/security_spec.rb
@@ -2,20 +2,26 @@ require &#39;spec_helper&#39; @@ -2,20 +2,26 @@ require &#39;spec_helper&#39;
2 2
3 describe "Admin::Projects" do 3 describe "Admin::Projects" do
4 describe "GET /admin/projects" do 4 describe "GET /admin/projects" do
5 - it { admin_projects_path.should be_allowed_for :admin }  
6 - it { admin_projects_path.should be_denied_for :user }  
7 - it { admin_projects_path.should be_denied_for :visitor } 5 + subject { admin_projects_path }
  6 +
  7 + it { should be_allowed_for :admin }
  8 + it { should be_denied_for :user }
  9 + it { should be_denied_for :visitor }
8 end 10 end
9 11
10 describe "GET /admin/users" do 12 describe "GET /admin/users" do
11 - it { admin_users_path.should be_allowed_for :admin }  
12 - it { admin_users_path.should be_denied_for :user }  
13 - it { admin_users_path.should be_denied_for :visitor } 13 + subject { admin_users_path }
  14 +
  15 + it { should be_allowed_for :admin }
  16 + it { should be_denied_for :user }
  17 + it { should be_denied_for :visitor }
14 end 18 end
15 19
16 describe "GET /admin/hooks" do 20 describe "GET /admin/hooks" do
17 - it { admin_hooks_path.should be_allowed_for :admin }  
18 - it { admin_hooks_path.should be_denied_for :user }  
19 - it { admin_hooks_path.should be_denied_for :visitor } 21 + subject { admin_hooks_path }
  22 +
  23 + it { should be_allowed_for :admin }
  24 + it { should be_denied_for :user }
  25 + it { should be_denied_for :visitor }
20 end 26 end
21 end 27 end
spec/requests/api/issues_spec.rb 0 → 100644
@@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Gitlab::API do
  4 + include ApiHelpers
  5 +
  6 + let(:user) { Factory :user }
  7 + let!(:project) { Factory :project, owner: user }
  8 + let!(:issue) { Factory :issue, author: user, assignee: user, project: project }
  9 + before { project.add_access(user, :read) }
  10 +
  11 + describe "GET /issues" do
  12 + it "should return authentication error" do
  13 + get api("/issues")
  14 + response.status.should == 401
  15 + end
  16 +
  17 + describe "authenticated GET /issues" do
  18 + it "should return an array of issues" do
  19 + get api("/issues", user)
  20 + response.status.should == 200
  21 + json_response.should be_an Array
  22 + json_response.first['title'].should == issue.title
  23 + end
  24 + end
  25 + end
  26 +
  27 + describe "GET /projects/:id/issues" do
  28 + it "should return project issues" do
  29 + get api("/projects/#{project.code}/issues", user)
  30 + response.status.should == 200
  31 + json_response.should be_an Array
  32 + json_response.first['title'].should == issue.title
  33 + end
  34 + end
  35 +
  36 + describe "GET /projects/:id/issues/:issue_id" do
  37 + it "should return a project issue by id" do
  38 + get api("/projects/#{project.code}/issues/#{issue.id}", user)
  39 + response.status.should == 200
  40 + json_response['title'].should == issue.title
  41 + end
  42 + end
  43 +
  44 + describe "POST /projects/:id/issues" do
  45 + it "should create a new project issue" do
  46 + post api("/projects/#{project.code}/issues", user),
  47 + title: 'new issue', labels: 'label, label2'
  48 + response.status.should == 201
  49 + json_response['title'].should == 'new issue'
  50 + json_response['description'].should be_nil
  51 + json_response['labels'].should == ['label', 'label2']
  52 + end
  53 + end
  54 +
  55 + describe "PUT /projects/:id/issues/:issue_id" do
  56 + it "should update a project issue" do
  57 + put api("/projects/#{project.code}/issues/#{issue.id}", user),
  58 + title: 'updated title', labels: 'label2', closed: 1
  59 + response.status.should == 200
  60 + json_response['title'].should == 'updated title'
  61 + json_response['labels'].should == ['label2']
  62 + json_response['closed'].should be_true
  63 + end
  64 + end
  65 +
  66 + describe "DELETE /projects/:id/issues/:issue_id" do
  67 + it "should delete a project issue" do
  68 + expect {
  69 + delete api("/projects/#{project.code}/issues/#{issue.id}", user)
  70 + }.to change { Issue.count }.by(-1)
  71 + end
  72 + end
  73 +end
spec/requests/api/projects_spec.rb 0 → 100644
@@ -0,0 +1,135 @@ @@ -0,0 +1,135 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Gitlab::API do
  4 + include ApiHelpers
  5 +
  6 + let(:user) { Factory :user }
  7 + let!(:project) { Factory :project, owner: user }
  8 + let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }
  9 + before { project.add_access(user, :read) }
  10 +
  11 + describe "GET /projects" do
  12 + it "should return authentication error" do
  13 + get api("/projects")
  14 + response.status.should == 401
  15 + end
  16 +
  17 + describe "authenticated GET /projects" do
  18 + it "should return an array of projects" do
  19 + get api("/projects", user)
  20 + response.status.should == 200
  21 + json_response.should be_an Array
  22 + json_response.first['name'].should == project.name
  23 + json_response.first['owner']['email'].should == user.email
  24 + end
  25 + end
  26 + end
  27 +
  28 + describe "GET /projects/:id" do
  29 + it "should return a project by id" do
  30 + get api("/projects/#{project.id}", user)
  31 + response.status.should == 200
  32 + json_response['name'].should == project.name
  33 + json_response['owner']['email'].should == user.email
  34 + end
  35 +
  36 + it "should return a project by code name" do
  37 + get api("/projects/#{project.code}", user)
  38 + response.status.should == 200
  39 + json_response['name'].should == project.name
  40 + end
  41 +
  42 + it "should return a 404 error if not found" do
  43 + get api("/projects/42", user)
  44 + response.status.should == 404
  45 + json_response['message'].should == '404 Not found'
  46 + end
  47 + end
  48 +
  49 + describe "GET /projects/:id/repository/branches" do
  50 + it "should return an array of project branches" do
  51 + get api("/projects/#{project.code}/repository/branches", user)
  52 + response.status.should == 200
  53 + json_response.should be_an Array
  54 + json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
  55 + end
  56 + end
  57 +
  58 + describe "GET /projects/:id/repository/branches/:branch" do
  59 + it "should return the branch information for a single branch" do
  60 + get api("/projects/#{project.code}/repository/branches/new_design", user)
  61 + response.status.should == 200
  62 +
  63 + json_response['name'].should == 'new_design'
  64 + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
  65 + end
  66 + end
  67 +
  68 + describe "GET /projects/:id/repository/tags" do
  69 + it "should return an array of project tags" do
  70 + get api("/projects/#{project.code}/repository/tags", user)
  71 + response.status.should == 200
  72 + json_response.should be_an Array
  73 + json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
  74 + end
  75 + end
  76 +
  77 + describe "GET /projects/:id/snippets/:snippet_id" do
  78 + it "should return a project snippet" do
  79 + get api("/projects/#{project.code}/snippets/#{snippet.id}", user)
  80 + response.status.should == 200
  81 + json_response['title'].should == snippet.title
  82 + end
  83 + end
  84 +
  85 + describe "POST /projects/:id/snippets" do
  86 + it "should create a new project snippet" do
  87 + post api("/projects/#{project.code}/snippets", user),
  88 + title: 'api test', file_name: 'sample.rb', code: 'test'
  89 + response.status.should == 201
  90 + json_response['title'].should == 'api test'
  91 + end
  92 + end
  93 +
  94 + describe "PUT /projects/:id/snippets" do
  95 + it "should update an existing project snippet" do
  96 + put api("/projects/#{project.code}/snippets/#{snippet.id}", user),
  97 + code: 'updated code'
  98 + response.status.should == 200
  99 + json_response['title'].should == 'example'
  100 + snippet.reload.content.should == 'updated code'
  101 + end
  102 + end
  103 +
  104 + describe "DELETE /projects/:id/snippets/:snippet_id" do
  105 + it "should delete existing project snippet" do
  106 + expect {
  107 + delete api("/projects/#{project.code}/snippets/#{snippet.id}", user)
  108 + }.to change { Snippet.count }.by(-1)
  109 + end
  110 + end
  111 +
  112 + describe "GET /projects/:id/snippets/:snippet_id/raw" do
  113 + it "should get a raw project snippet" do
  114 + get api("/projects/#{project.code}/snippets/#{snippet.id}/raw", user)
  115 + response.status.should == 200
  116 + end
  117 + end
  118 +
  119 + describe "GET /projects/:id/:sha/blob" do
  120 + it "should get the raw file contents" do
  121 + get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.md", user)
  122 + response.status.should == 200
  123 + end
  124 +
  125 + it "should return 404 for invalid branch_name" do
  126 + get api("/projects/#{project.code}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
  127 + response.status.should == 404
  128 + end
  129 +
  130 + it "should return 404 for invalid file" do
  131 + get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.invalid", user)
  132 + response.status.should == 404
  133 + end
  134 + end
  135 +end
spec/requests/api/users_spec.rb 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Gitlab::API do
  4 + include ApiHelpers
  5 +
  6 + let(:user) { Factory :user }
  7 +
  8 + describe "GET /users" do
  9 + it "should return authentication error" do
  10 + get api("/users")
  11 + response.status.should == 401
  12 + end
  13 +
  14 + describe "authenticated GET /users" do
  15 + it "should return an array of users" do
  16 + get api("/users", user)
  17 + response.status.should == 200
  18 + json_response.should be_an Array
  19 + json_response.first['email'].should == user.email
  20 + end
  21 + end
  22 + end
  23 +
  24 + describe "GET /users/:id" do
  25 + it "should return a user by id" do
  26 + get api("/users/#{user.id}", user)
  27 + response.status.should == 200
  28 + json_response['email'].should == user.email
  29 + end
  30 + end
  31 +
  32 + describe "GET /user" do
  33 + it "should return current user" do
  34 + get api("/user", user)
  35 + response.status.should == 200
  36 + json_response['email'].should == user.email
  37 + end
  38 + end
  39 +end
spec/requests/security/profile_access_spec.rb
@@ -11,24 +11,30 @@ describe &quot;Users Security&quot; do @@ -11,24 +11,30 @@ describe &quot;Users Security&quot; do
11 end 11 end
12 12
13 describe "GET /keys" do 13 describe "GET /keys" do
14 - it { keys_path.should be_allowed_for @u1 }  
15 - it { keys_path.should be_allowed_for :admin }  
16 - it { keys_path.should be_allowed_for :user }  
17 - it { keys_path.should be_denied_for :visitor } 14 + subject { keys_path }
  15 +
  16 + it { should be_allowed_for @u1 }
  17 + it { should be_allowed_for :admin }
  18 + it { should be_allowed_for :user }
  19 + it { should be_denied_for :visitor }
18 end 20 end
19 21
20 describe "GET /profile" do 22 describe "GET /profile" do
21 - it { profile_path.should be_allowed_for @u1 }  
22 - it { profile_path.should be_allowed_for :admin }  
23 - it { profile_path.should be_allowed_for :user }  
24 - it { profile_path.should be_denied_for :visitor } 23 + subject { profile_path }
  24 +
  25 + it { should be_allowed_for @u1 }
  26 + it { should be_allowed_for :admin }
  27 + it { should be_allowed_for :user }
  28 + it { should be_denied_for :visitor }
25 end 29 end
26 30
27 describe "GET /profile/password" do 31 describe "GET /profile/password" do
28 - it { profile_password_path.should be_allowed_for @u1 }  
29 - it { profile_password_path.should be_allowed_for :admin }  
30 - it { profile_password_path.should be_allowed_for :user }  
31 - it { profile_password_path.should be_denied_for :visitor } 32 + subject { profile_password_path }
  33 +
  34 + it { should be_allowed_for @u1 }
  35 + it { should be_allowed_for :admin }
  36 + it { should be_allowed_for :user }
  37 + it { should be_denied_for :visitor }
32 end 38 end
33 end 39 end
34 end 40 end
spec/requests/security/project_access_spec.rb
@@ -26,64 +26,76 @@ describe &quot;Application access&quot; do @@ -26,64 +26,76 @@ describe &quot;Application access&quot; do
26 end 26 end
27 27
28 describe "GET /project_code" do 28 describe "GET /project_code" do
29 - it { project_path(@project).should be_allowed_for @u1 }  
30 - it { project_path(@project).should be_allowed_for @u3 }  
31 - it { project_path(@project).should be_denied_for :admin }  
32 - it { project_path(@project).should be_denied_for @u2 }  
33 - it { project_path(@project).should be_denied_for :user }  
34 - it { project_path(@project).should be_denied_for :visitor } 29 + subject { project_path(@project) }
  30 +
  31 + it { should be_allowed_for @u1 }
  32 + it { should be_allowed_for @u3 }
  33 + it { should be_denied_for :admin }
  34 + it { should be_denied_for @u2 }
  35 + it { should be_denied_for :user }
  36 + it { should be_denied_for :visitor }
35 end 37 end
36 38
37 describe "GET /project_code/master/tree" do 39 describe "GET /project_code/master/tree" do
38 - it { tree_project_ref_path(@project, @project.root_ref).should be_allowed_for @u1 }  
39 - it { tree_project_ref_path(@project, @project.root_ref).should be_allowed_for @u3 }  
40 - it { tree_project_ref_path(@project, @project.root_ref).should be_denied_for :admin }  
41 - it { tree_project_ref_path(@project, @project.root_ref).should be_denied_for @u2 }  
42 - it { tree_project_ref_path(@project, @project.root_ref).should be_denied_for :user }  
43 - it { tree_project_ref_path(@project, @project.root_ref).should be_denied_for :visitor } 40 + subject { tree_project_ref_path(@project, @project.root_ref) }
  41 +
  42 + it { should be_allowed_for @u1 }
  43 + it { should be_allowed_for @u3 }
  44 + it { should be_denied_for :admin }
  45 + it { should be_denied_for @u2 }
  46 + it { should be_denied_for :user }
  47 + it { should be_denied_for :visitor }
44 end 48 end
45 49
46 describe "GET /project_code/commits" do 50 describe "GET /project_code/commits" do
47 - it { project_commits_path(@project).should be_allowed_for @u1 }  
48 - it { project_commits_path(@project).should be_allowed_for @u3 }  
49 - it { project_commits_path(@project).should be_denied_for :admin }  
50 - it { project_commits_path(@project).should be_denied_for @u2 }  
51 - it { project_commits_path(@project).should be_denied_for :user }  
52 - it { project_commits_path(@project).should be_denied_for :visitor } 51 + subject { project_commits_path(@project) }
  52 +
  53 + it { should be_allowed_for @u1 }
  54 + it { should be_allowed_for @u3 }
  55 + it { should be_denied_for :admin }
  56 + it { should be_denied_for @u2 }
  57 + it { should be_denied_for :user }
  58 + it { should be_denied_for :visitor }
53 end 59 end
54 60
55 describe "GET /project_code/commit" do 61 describe "GET /project_code/commit" do
56 - it { project_commit_path(@project, @project.commit.id).should be_allowed_for @u1 }  
57 - it { project_commit_path(@project, @project.commit.id).should be_allowed_for @u3 }  
58 - it { project_commit_path(@project, @project.commit.id).should be_denied_for :admin }  
59 - it { project_commit_path(@project, @project.commit.id).should be_denied_for @u2 }  
60 - it { project_commit_path(@project, @project.commit.id).should be_denied_for :user }  
61 - it { project_commit_path(@project, @project.commit.id).should be_denied_for :visitor } 62 + subject { project_commit_path(@project, @project.commit.id) }
  63 +
  64 + it { should be_allowed_for @u1 }
  65 + it { should be_allowed_for @u3 }
  66 + it { should be_denied_for :admin }
  67 + it { should be_denied_for @u2 }
  68 + it { should be_denied_for :user }
  69 + it { should be_denied_for :visitor }
62 end 70 end
63 71
64 describe "GET /project_code/team" do 72 describe "GET /project_code/team" do
65 - it { team_project_path(@project).should be_allowed_for @u1 }  
66 - it { team_project_path(@project).should be_allowed_for @u3 }  
67 - it { team_project_path(@project).should be_denied_for :admin }  
68 - it { team_project_path(@project).should be_denied_for @u2 }  
69 - it { team_project_path(@project).should be_denied_for :user }  
70 - it { team_project_path(@project).should be_denied_for :visitor } 73 + subject { team_project_path(@project) }
  74 +
  75 + it { should be_allowed_for @u1 }
  76 + it { should be_allowed_for @u3 }
  77 + it { should be_denied_for :admin }
  78 + it { should be_denied_for @u2 }
  79 + it { should be_denied_for :user }
  80 + it { should be_denied_for :visitor }
71 end 81 end
72 82
73 describe "GET /project_code/wall" do 83 describe "GET /project_code/wall" do
74 - it { wall_project_path(@project).should be_allowed_for @u1 }  
75 - it { wall_project_path(@project).should be_allowed_for @u3 }  
76 - it { wall_project_path(@project).should be_denied_for :admin }  
77 - it { wall_project_path(@project).should be_denied_for @u2 }  
78 - it { wall_project_path(@project).should be_denied_for :user }  
79 - it { wall_project_path(@project).should be_denied_for :visitor } 84 + subject { wall_project_path(@project) }
  85 +
  86 + it { should be_allowed_for @u1 }
  87 + it { should be_allowed_for @u3 }
  88 + it { should be_denied_for :admin }
  89 + it { should be_denied_for @u2 }
  90 + it { should be_denied_for :user }
  91 + it { should be_denied_for :visitor }
80 end 92 end
81 93
82 describe "GET /project_code/blob" do 94 describe "GET /project_code/blob" do
83 before do 95 before do
84 - @commit = @project.commit  
85 - @path = @commit.tree.contents.select { |i| i.is_a?(Grit::Blob)}.first.name  
86 - @blob_path = blob_project_ref_path(@project, @commit.id, path: @path) 96 + commit = @project.commit
  97 + path = commit.tree.contents.select { |i| i.is_a?(Grit::Blob)}.first.name
  98 + @blob_path = blob_project_ref_path(@project, commit.id, path: path)
87 end 99 end
88 100
89 it { @blob_path.should be_allowed_for @u1 } 101 it { @blob_path.should be_allowed_for @u1 }
@@ -95,93 +107,113 @@ describe &quot;Application access&quot; do @@ -95,93 +107,113 @@ describe &quot;Application access&quot; do
95 end 107 end
96 108
97 describe "GET /project_code/edit" do 109 describe "GET /project_code/edit" do
98 - it { edit_project_path(@project).should be_allowed_for @u1 }  
99 - it { edit_project_path(@project).should be_denied_for @u3 }  
100 - it { edit_project_path(@project).should be_denied_for :admin }  
101 - it { edit_project_path(@project).should be_denied_for @u2 }  
102 - it { edit_project_path(@project).should be_denied_for :user }  
103 - it { edit_project_path(@project).should be_denied_for :visitor } 110 + subject { edit_project_path(@project) }
  111 +
  112 + it { should be_allowed_for @u1 }
  113 + it { should be_denied_for @u3 }
  114 + it { should be_denied_for :admin }
  115 + it { should be_denied_for @u2 }
  116 + it { should be_denied_for :user }
  117 + it { should be_denied_for :visitor }
104 end 118 end
105 119
106 describe "GET /project_code/deploy_keys" do 120 describe "GET /project_code/deploy_keys" do
107 - it { project_deploy_keys_path(@project).should be_allowed_for @u1 }  
108 - it { project_deploy_keys_path(@project).should be_denied_for @u3 }  
109 - it { project_deploy_keys_path(@project).should be_denied_for :admin }  
110 - it { project_deploy_keys_path(@project).should be_denied_for @u2 }  
111 - it { project_deploy_keys_path(@project).should be_denied_for :user }  
112 - it { project_deploy_keys_path(@project).should be_denied_for :visitor } 121 + subject { project_deploy_keys_path(@project) }
  122 +
  123 + it { should be_allowed_for @u1 }
  124 + it { should be_denied_for @u3 }
  125 + it { should be_denied_for :admin }
  126 + it { should be_denied_for @u2 }
  127 + it { should be_denied_for :user }
  128 + it { should be_denied_for :visitor }
113 end 129 end
114 130
115 describe "GET /project_code/issues" do 131 describe "GET /project_code/issues" do
116 - it { project_issues_path(@project).should be_allowed_for @u1 }  
117 - it { project_issues_path(@project).should be_allowed_for @u3 }  
118 - it { project_issues_path(@project).should be_denied_for :admin }  
119 - it { project_issues_path(@project).should be_denied_for @u2 }  
120 - it { project_issues_path(@project).should be_denied_for :user }  
121 - it { project_issues_path(@project).should be_denied_for :visitor } 132 + subject { project_issues_path(@project) }
  133 +
  134 + it { should be_allowed_for @u1 }
  135 + it { should be_allowed_for @u3 }
  136 + it { should be_denied_for :admin }
  137 + it { should be_denied_for @u2 }
  138 + it { should be_denied_for :user }
  139 + it { should be_denied_for :visitor }
122 end 140 end
123 141
124 describe "GET /project_code/snippets" do 142 describe "GET /project_code/snippets" do
125 - it { project_snippets_path(@project).should be_allowed_for @u1 }  
126 - it { project_snippets_path(@project).should be_allowed_for @u3 }  
127 - it { project_snippets_path(@project).should be_denied_for :admin }  
128 - it { project_snippets_path(@project).should be_denied_for @u2 }  
129 - it { project_snippets_path(@project).should be_denied_for :user }  
130 - it { project_snippets_path(@project).should be_denied_for :visitor } 143 + subject { project_snippets_path(@project) }
  144 +
  145 + it { should be_allowed_for @u1 }
  146 + it { should be_allowed_for @u3 }
  147 + it { should be_denied_for :admin }
  148 + it { should be_denied_for @u2 }
  149 + it { should be_denied_for :user }
  150 + it { should be_denied_for :visitor }
131 end 151 end
132 152
133 describe "GET /project_code/merge_requests" do 153 describe "GET /project_code/merge_requests" do
134 - it { project_merge_requests_path(@project).should be_allowed_for @u1 }  
135 - it { project_merge_requests_path(@project).should be_allowed_for @u3 }  
136 - it { project_merge_requests_path(@project).should be_denied_for :admin }  
137 - it { project_merge_requests_path(@project).should be_denied_for @u2 }  
138 - it { project_merge_requests_path(@project).should be_denied_for :user }  
139 - it { project_merge_requests_path(@project).should be_denied_for :visitor } 154 + subject { project_merge_requests_path(@project) }
  155 +
  156 + it { should be_allowed_for @u1 }
  157 + it { should be_allowed_for @u3 }
  158 + it { should be_denied_for :admin }
  159 + it { should be_denied_for @u2 }
  160 + it { should be_denied_for :user }
  161 + it { should be_denied_for :visitor }
140 end 162 end
141 163
142 describe "GET /project_code/repository" do 164 describe "GET /project_code/repository" do
143 - it { project_repository_path(@project).should be_allowed_for @u1 }  
144 - it { project_repository_path(@project).should be_allowed_for @u3 }  
145 - it { project_repository_path(@project).should be_denied_for :admin }  
146 - it { project_repository_path(@project).should be_denied_for @u2 }  
147 - it { project_repository_path(@project).should be_denied_for :user }  
148 - it { project_repository_path(@project).should be_denied_for :visitor } 165 + subject { project_repository_path(@project) }
  166 +
  167 + it { should be_allowed_for @u1 }
  168 + it { should be_allowed_for @u3 }
  169 + it { should be_denied_for :admin }
  170 + it { should be_denied_for @u2 }
  171 + it { should be_denied_for :user }
  172 + it { should be_denied_for :visitor }
149 end 173 end
150 174
151 describe "GET /project_code/repository/branches" do 175 describe "GET /project_code/repository/branches" do
152 - it { branches_project_repository_path(@project).should be_allowed_for @u1 }  
153 - it { branches_project_repository_path(@project).should be_allowed_for @u3 }  
154 - it { branches_project_repository_path(@project).should be_denied_for :admin }  
155 - it { branches_project_repository_path(@project).should be_denied_for @u2 }  
156 - it { branches_project_repository_path(@project).should be_denied_for :user }  
157 - it { branches_project_repository_path(@project).should be_denied_for :visitor } 176 + subject { branches_project_repository_path(@project) }
  177 +
  178 + it { should be_allowed_for @u1 }
  179 + it { should be_allowed_for @u3 }
  180 + it { should be_denied_for :admin }
  181 + it { should be_denied_for @u2 }
  182 + it { should be_denied_for :user }
  183 + it { should be_denied_for :visitor }
158 end 184 end
159 185
160 describe "GET /project_code/repository/tags" do 186 describe "GET /project_code/repository/tags" do
161 - it { tags_project_repository_path(@project).should be_allowed_for @u1 }  
162 - it { tags_project_repository_path(@project).should be_allowed_for @u3 }  
163 - it { tags_project_repository_path(@project).should be_denied_for :admin }  
164 - it { tags_project_repository_path(@project).should be_denied_for @u2 }  
165 - it { tags_project_repository_path(@project).should be_denied_for :user }  
166 - it { tags_project_repository_path(@project).should be_denied_for :visitor } 187 + subject { tags_project_repository_path(@project) }
  188 +
  189 + it { should be_allowed_for @u1 }
  190 + it { should be_allowed_for @u3 }
  191 + it { should be_denied_for :admin }
  192 + it { should be_denied_for @u2 }
  193 + it { should be_denied_for :user }
  194 + it { should be_denied_for :visitor }
167 end 195 end
168 196
169 describe "GET /project_code/hooks" do 197 describe "GET /project_code/hooks" do
170 - it { project_hooks_path(@project).should be_allowed_for @u1 }  
171 - it { project_hooks_path(@project).should be_allowed_for @u3 }  
172 - it { project_hooks_path(@project).should be_denied_for :admin }  
173 - it { project_hooks_path(@project).should be_denied_for @u2 }  
174 - it { project_hooks_path(@project).should be_denied_for :user }  
175 - it { project_hooks_path(@project).should be_denied_for :visitor } 198 + subject { project_hooks_path(@project) }
  199 +
  200 + it { should be_allowed_for @u1 }
  201 + it { should be_allowed_for @u3 }
  202 + it { should be_denied_for :admin }
  203 + it { should be_denied_for @u2 }
  204 + it { should be_denied_for :user }
  205 + it { should be_denied_for :visitor }
176 end 206 end
177 207
178 describe "GET /project_code/files" do 208 describe "GET /project_code/files" do
179 - it { files_project_path(@project).should be_allowed_for @u1 }  
180 - it { files_project_path(@project).should be_allowed_for @u3 }  
181 - it { files_project_path(@project).should be_denied_for :admin }  
182 - it { files_project_path(@project).should be_denied_for @u2 }  
183 - it { files_project_path(@project).should be_denied_for :user }  
184 - it { files_project_path(@project).should be_denied_for :visitor } 209 + subject { files_project_path(@project) }
  210 +
  211 + it { should be_allowed_for @u1 }
  212 + it { should be_allowed_for @u3 }
  213 + it { should be_denied_for :admin }
  214 + it { should be_denied_for @u2 }
  215 + it { should be_denied_for :user }
  216 + it { should be_denied_for :visitor }
185 end 217 end
186 end 218 end
187 end 219 end
spec/spec_helper.rb
@@ -9,10 +9,8 @@ require File.expand_path(&quot;../../config/environment&quot;, __FILE__) @@ -9,10 +9,8 @@ require File.expand_path(&quot;../../config/environment&quot;, __FILE__)
9 require 'rspec/rails' 9 require 'rspec/rails'
10 require 'capybara/rails' 10 require 'capybara/rails'
11 require 'capybara/rspec' 11 require 'capybara/rspec'
12 -require 'capybara/dsl'  
13 require 'webmock/rspec' 12 require 'webmock/rspec'
14 require 'factories' 13 require 'factories'
15 -require 'monkeypatch'  
16 require 'email_spec' 14 require 'email_spec'
17 require 'headless' 15 require 'headless'
18 16
@@ -23,10 +21,12 @@ Dir[Rails.root.join(&quot;spec/support/**/*.rb&quot;)].each {|f| require f} @@ -23,10 +21,12 @@ Dir[Rails.root.join(&quot;spec/support/**/*.rb&quot;)].each {|f| require f}
23 # Use capybara-webkit 21 # Use capybara-webkit
24 Capybara.javascript_driver = :webkit 22 Capybara.javascript_driver = :webkit
25 23
  24 +WebMock.disable_net_connect!(allow_localhost: true)
  25 +
26 RSpec.configure do |config| 26 RSpec.configure do |config|
27 config.mock_with :rspec 27 config.mock_with :rspec
28 28
29 - config.include LoginMacros 29 + config.include LoginHelpers, type: :request
30 30
31 # If you're not using ActiveRecord, or you'd prefer not to run each of your 31 # If you're not using ActiveRecord, or you'd prefer not to run each of your
32 # examples within a transaction, remove the following line or assign false 32 # examples within a transaction, remove the following line or assign false
@@ -38,35 +38,9 @@ RSpec.configure do |config| @@ -38,35 +38,9 @@ RSpec.configure do |config|
38 headless.start 38 headless.start
39 end 39 end
40 40
41 - config.before :each, type: :integration do  
42 - DeviseSessionMock.disable  
43 - end  
44 -  
45 config.before do 41 config.before do
46 - if example.metadata[:js]  
47 - DatabaseCleaner.strategy = :truncation  
48 - Capybara::Selenium::Driver::DEFAULT_OPTIONS[:resynchronize] = true  
49 - else  
50 - DatabaseCleaner.strategy = :transaction  
51 - end  
52 -  
53 - DatabaseCleaner.start  
54 -  
55 - WebMock.disable_net_connect!(allow_localhost: true)  
56 -  
57 # !!! Observers disabled by default in tests 42 # !!! Observers disabled by default in tests
58 - #  
59 - # Use next code to enable observers  
60 - # before(:each) { ActiveRecord::Base.observers.enable(:all) }  
61 - #  
62 - ActiveRecord::Base.observers.disable :all  
63 - end  
64 -  
65 - config.after do  
66 - DatabaseCleaner.clean 43 + ActiveRecord::Base.observers.disable(:all)
  44 + # ActiveRecord::Base.observers.enable(:all)
67 end 45 end
68 -  
69 - config.include RSpec::Rails::RequestExampleGroup, type: :request, example_group: {  
70 - file_path: /spec\/api/  
71 - }  
72 end 46 end
spec/support/api.rb
@@ -1,7 +0,0 @@ @@ -1,7 +0,0 @@
1 -def api_prefix  
2 - "/api/#{Gitlab::API::VERSION}"  
3 -end  
4 -  
5 -def json_response  
6 - JSON.parse(response.body)  
7 -end  
spec/support/api_helpers.rb 0 → 100644
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
  1 +module ApiHelpers
  2 + # Public: Prepend a request path with the path to the API
  3 + #
  4 + # path - Path to append
  5 + # user - User object - If provided, automatically appends private_token query
  6 + # string for authenticated requests
  7 + #
  8 + # Examples
  9 + #
  10 + # >> api('/issues')
  11 + # => "/api/v2/issues"
  12 + #
  13 + # >> api('/issues', User.last)
  14 + # => "/api/v2/issues?private_token=..."
  15 + #
  16 + # >> api('/issues?foo=bar', User.last)
  17 + # => "/api/v2/issues?foo=bar&private_token=..."
  18 + #
  19 + # Returns the relative path to the requested API resource
  20 + def api(path, user = nil)
  21 + "/api/#{Gitlab::API::VERSION}#{path}" +
  22 +
  23 + # Normalize query string
  24 + (path.index('?') ? '' : '?') +
  25 +
  26 + # Append private_token if given a User object
  27 + (user.respond_to?(:private_token) ?
  28 + "&private_token=#{user.private_token}" : "")
  29 + end
  30 +
  31 + def json_response
  32 + JSON.parse(response.body)
  33 + end
  34 +end
spec/support/db_cleaner.rb 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +require 'database_cleaner'
  2 +
  3 +RSpec.configure do |config|
  4 + config.before do
  5 + if example.metadata[:js]
  6 + DatabaseCleaner.strategy = :truncation
  7 + Capybara::Selenium::Driver::DEFAULT_OPTIONS[:resynchronize] = true
  8 + else
  9 + DatabaseCleaner.strategy = :transaction
  10 + end
  11 +
  12 + DatabaseCleaner.start
  13 + end
  14 +
  15 + config.after do
  16 + DatabaseCleaner.clean
  17 + end
  18 +end
spec/support/js_patch.rb
@@ -1,6 +0,0 @@ @@ -1,6 +0,0 @@
1 -module JsPatch  
2 - def confirm_js_popup  
3 - page.evaluate_script("window.alert = function(msg) { return true; }")  
4 - page.evaluate_script("window.confirm = function(msg) { return true; }")  
5 - end  
6 -end  
spec/support/login.rb
@@ -1,30 +0,0 @@ @@ -1,30 +0,0 @@
1 -module LoginMacros  
2 - def login_as role  
3 - @user = User.create(email: "user#{User.count}@mail.com",  
4 - name: "John Smith",  
5 - password: "123456",  
6 - password_confirmation: "123456",  
7 - skype: 'user_skype')  
8 -  
9 - if role == :admin  
10 - @user.admin = true  
11 - @user.save!  
12 - end  
13 -  
14 - visit new_user_session_path  
15 - fill_in "user_email", with: @user.email  
16 - fill_in "user_password", with: "123456"  
17 - click_button "Sign in"  
18 - end  
19 -  
20 - def login_with(user)  
21 - visit new_user_session_path  
22 - fill_in "user_email", with: user.email  
23 - fill_in "user_password", with: "123456"  
24 - click_button "Sign in"  
25 - end  
26 -  
27 - def logout  
28 - click_link "Logout" rescue nil  
29 - end  
30 -end  
spec/support/login_helpers.rb 0 → 100644
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +module LoginHelpers
  2 + # Internal: Create and log in as a user of the specified role
  3 + #
  4 + # role - User role (e.g., :admin, :user)
  5 + def login_as(role)
  6 + @user = Factory(role)
  7 + login_with(@user)
  8 + end
  9 +
  10 + # Internal: Login as the specified user
  11 + #
  12 + # user - User instance to login with
  13 + def login_with(user)
  14 + visit new_user_session_path
  15 + fill_in "user_email", with: user.email
  16 + fill_in "user_password", with: "123456"
  17 + click_button "Sign in"
  18 + end
  19 +
  20 + def logout
  21 + click_link "Logout" rescue nil
  22 + end
  23 +end
spec/support/monkeypatch.rb 0 → 100644
@@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
  1 +# Stubbing Project <-> git host path
  2 +# create project using Factory only
  3 +class Project
  4 + def update_repository
  5 + true
  6 + end
  7 +
  8 + def destroy_repository
  9 + true
  10 + end
  11 +
  12 + def path_to_repo
  13 + File.join(Rails.root, "tmp", "tests", path)
  14 + end
  15 +
  16 + def satellite
  17 + @satellite ||= FakeSatellite.new
  18 + end
  19 +end
  20 +
  21 +class Key
  22 + def update_repository
  23 + true
  24 + end
  25 +
  26 + def repository_delete_key
  27 + true
  28 + end
  29 +end
  30 +
  31 +class UsersProject
  32 + def update_repository
  33 + true
  34 + end
  35 +end
  36 +
  37 +class FakeSatellite
  38 + def exists?
  39 + true
  40 + end
  41 +
  42 + def create
  43 + true
  44 + end
  45 +end
  46 +
  47 +class ProtectedBranch
  48 + def update_repository
  49 + true
  50 + end
  51 +end
spec/support/shared_examples.rb
@@ -1,16 +0,0 @@ @@ -1,16 +0,0 @@
1 -shared_examples_for :project_side_pane do  
2 - subject { page }  
3 - it { should have_content((@project || project).name) }  
4 - it { should have_content("Commits") }  
5 - it { should have_content("Files") }  
6 -end  
7 -  
8 -shared_examples_for :tree_view do  
9 - subject { page }  
10 -  
11 - it "should have Tree View of project" do  
12 - should have_content("app")  
13 - should have_content("History")  
14 - should have_content("Gemfile")  
15 - end  
16 -end