2012-05-22 19:23:00 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
shared_context "pull request controller" do
|
2012-10-01 12:32:40 +01:00
|
|
|
after { FileUtils.rm_rf File.join(Rails.root, "tmp", Rails.env, "pull_requests") }
|
2012-09-28 16:08:51 +01:00
|
|
|
before do
|
2012-10-01 12:32:40 +01:00
|
|
|
FileUtils.rm_rf(APP_CONFIG['root_path'])
|
2012-05-22 19:23:00 +01:00
|
|
|
stub_symlink_methods
|
|
|
|
|
2012-12-18 18:53:41 +00:00
|
|
|
@project = FactoryGirl.create(:project_with_commit)
|
2012-05-22 19:23:00 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
@pull = @project.pull_requests.new issue_attributes: {title: 'test', body: 'testing'}
|
2012-10-03 13:18:02 +01:00
|
|
|
@pull.issue.user, @pull.issue.project = @project.owner, @pull.to_project
|
|
|
|
@pull.to_ref = 'master'
|
|
|
|
@pull.from_project, @pull.from_ref = @project, 'non_conflicts'
|
2012-05-22 19:23:00 +01:00
|
|
|
@pull.save
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
@create_params = {
|
2014-03-19 07:11:16 +00:00
|
|
|
pull_request: { issue_attributes: { title: 'create', body: 'creating' },
|
|
|
|
to_ref: 'non_conflicts',
|
|
|
|
from_ref: 'master' },
|
2014-01-21 04:51:49 +00:00
|
|
|
to_project: @project.name_with_owner,
|
2014-03-19 07:19:03 +00:00
|
|
|
name_with_owner: @project.name_with_owner
|
2014-03-19 07:11:16 +00:00
|
|
|
}
|
|
|
|
@update_params = @create_params.merge(pull_request_action: 'close', id: @pull.serial_id)
|
2012-10-02 16:19:50 +01:00
|
|
|
@wrong_update_params = @create_params.merge(
|
2014-03-19 07:11:16 +00:00
|
|
|
pull_request: { issue_attributes: { title: 'update', body: 'updating', id: @pull.issue.id }},
|
|
|
|
id: @pull.serial_id
|
|
|
|
)
|
2012-09-28 16:08:51 +01:00
|
|
|
|
|
|
|
@user = FactoryGirl.create(:user)
|
|
|
|
set_session_for(@user)
|
2013-07-22 16:19:25 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
@issue = FactoryGirl.create(:issue, project: @project)
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'pull request user with project guest rights' do
|
2012-10-01 12:32:40 +01:00
|
|
|
it 'should be able to perform show action when pull request has been created' do
|
|
|
|
@pull.check
|
2014-03-19 07:19:03 +00:00
|
|
|
get :show, name_with_owner: @project.name_with_owner, id: @pull.serial_id
|
2012-09-28 16:08:51 +01:00
|
|
|
response.should render_template(:show)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'pull request user with project reader rights' do
|
|
|
|
it 'should be able to perform create action' do
|
|
|
|
post :create, @create_params
|
|
|
|
response.should redirect_to(project_pull_request_path(@project, @project.pull_requests.last))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create pull request object into db' do
|
|
|
|
lambda{ post :create, @create_params }.should change{ PullRequest.joins(:issue).
|
2014-01-21 04:51:49 +00:00
|
|
|
where(issues: {title: 'create', body: 'creating'}).count }.by(1)
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not create same pull" do
|
2014-01-21 04:51:49 +00:00
|
|
|
post :create, @create_params.merge({pull_request: {issue_attributes: {title: 'same', body: 'creating'}, from_ref: 'non_conflicts', to_ref: 'master'}, to_project_id: @project.id})
|
|
|
|
PullRequest.joins(:issue).where(issues: {title: 'same', body: 'creating'}).count.should == 0
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not create already up-to-date pull" do
|
2014-08-29 10:54:15 +01:00
|
|
|
lambda{
|
|
|
|
post :create, @create_params.merge({pull_request: {issue_attributes: {title: 'already', body: 'creating'},
|
|
|
|
to_ref: 'master', from_ref: 'master'}, to_project_id: @project.id}) }.should
|
|
|
|
change{ PullRequest.count }.by(0)
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
2012-10-10 20:00:21 +01:00
|
|
|
|
|
|
|
it "should create pull request to the same project" do
|
|
|
|
@parent = FactoryGirl.create(:project)
|
2014-01-21 04:51:49 +00:00
|
|
|
@project.update_attributes({parent_id: @parent}, without_protection: true)
|
2012-10-10 20:00:21 +01:00
|
|
|
|
|
|
|
lambda{ post :create, @create_params }.should change{ PullRequest.joins(:issue).
|
2014-01-21 04:51:49 +00:00
|
|
|
where(issues: {user_id: @user}, to_project_id: @project, from_project_id: @project).count }.by(1)
|
2012-10-10 20:00:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should create pull request to the parent project" do
|
2012-12-18 18:53:41 +00:00
|
|
|
@parent = FactoryGirl.create(:project_with_commit)
|
2014-01-21 04:51:49 +00:00
|
|
|
@project.update_attributes({parent_id: @parent}, without_protection: true)
|
2012-10-10 20:00:21 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
lambda{ post :create, @create_params.merge({to_project: @parent.name_with_owner}) }.should change{ PullRequest.joins(:issue).
|
|
|
|
where(issues: {user_id: @user}, to_project_id: @parent, from_project_id: @project).count }.by(1)
|
2012-10-10 20:00:21 +01:00
|
|
|
end
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'user with pull request update rights' do
|
|
|
|
it 'should be able to perform update action' do
|
|
|
|
put :update, @update_params
|
2013-07-23 14:04:34 +01:00
|
|
|
response.should be_success
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
2012-10-02 16:19:50 +01:00
|
|
|
it 'should be able to perform merge action' do
|
2013-07-23 14:04:34 +01:00
|
|
|
@pull.check
|
2012-10-02 16:19:50 +01:00
|
|
|
put :merge, @update_params
|
2013-07-23 14:04:34 +01:00
|
|
|
response.should be_success
|
2012-10-01 12:32:40 +01:00
|
|
|
end
|
|
|
|
|
2012-10-02 16:19:50 +01:00
|
|
|
let(:pull) { @project.pull_requests.find(@pull) }
|
|
|
|
it 'should update pull request status' do
|
2012-10-01 12:32:40 +01:00
|
|
|
put :update, @update_params
|
2012-10-02 16:19:50 +01:00
|
|
|
pull.status.should =='closed'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not update pull request title' do
|
|
|
|
put :update, @wrong_update_params
|
|
|
|
pull.issue.title.should =='test'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not update pull request body' do
|
|
|
|
put :update, @wrong_update_params
|
|
|
|
pull.issue.body.should =='testing'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not update pull request title direct' do
|
|
|
|
put :update, @wrong_update_params
|
|
|
|
pull.issue.title.should_not =='update'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not update pull request body direct' do
|
|
|
|
put :update, @wrong_update_params
|
|
|
|
pull.issue.body.should_not =='updating'
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'user without pull request update rights' do
|
|
|
|
it 'should not be able to perform update action' do
|
|
|
|
put :update, @update_params
|
|
|
|
response.should redirect_to(controller.current_user ? forbidden_path : new_user_session_path)
|
|
|
|
end
|
|
|
|
|
2012-10-02 16:19:50 +01:00
|
|
|
let(:pull) { @project.pull_requests.find(@pull) }
|
|
|
|
it 'should not update pull request status' do
|
2012-09-28 16:08:51 +01:00
|
|
|
put :update, @update_params
|
2012-10-02 16:19:50 +01:00
|
|
|
pull.status.should_not =='closed'
|
|
|
|
end
|
|
|
|
it 'should not update pull request title' do
|
|
|
|
put :update, @wrong_update_params
|
|
|
|
pull.issue.title.should_not =='update'
|
2012-10-01 12:32:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not update pull request body' do
|
2012-10-02 16:19:50 +01:00
|
|
|
put :update, @wrong_update_params
|
|
|
|
pull.issue.body.should_not =='updating'
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
2013-07-23 14:04:34 +01:00
|
|
|
|
2014-06-17 20:48:22 +01:00
|
|
|
it 'should not be able to perform merge action' do
|
2013-07-23 14:04:34 +01:00
|
|
|
@pull.check
|
|
|
|
put :merge, @update_params
|
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
2012-10-01 12:40:52 +01:00
|
|
|
shared_examples_for 'pull request when project with issues turned off' do
|
2014-01-21 04:51:49 +00:00
|
|
|
before { @project.update_attributes(has_issues: false) }
|
2012-09-28 16:08:51 +01:00
|
|
|
|
2012-10-01 12:32:40 +01:00
|
|
|
it 'should be able to perform show action when pull request has been created' do
|
|
|
|
@pull.check
|
2014-03-19 07:19:03 +00:00
|
|
|
get :show, name_with_owner: @project.name_with_owner, id: @pull.serial_id
|
2012-09-28 16:08:51 +01:00
|
|
|
response.should render_template(:show)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-19 01:12:08 +00:00
|
|
|
describe Projects::PullRequestsController, type: :controller do
|
2012-09-28 16:08:51 +01:00
|
|
|
include_context "pull request controller"
|
|
|
|
|
|
|
|
context 'for global admin user' do
|
|
|
|
before do
|
|
|
|
@user.role = "admin"
|
|
|
|
@user.save
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it_should_behave_like 'pull request user with project guest rights'
|
|
|
|
it_should_behave_like 'pull request user with project reader rights'
|
|
|
|
it_should_behave_like 'user with pull request update rights'
|
2012-10-01 12:40:52 +01:00
|
|
|
it_should_behave_like 'pull request when project with issues turned off'
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project admin user' do
|
|
|
|
before do
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @user, 'admin')
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it_should_behave_like 'pull request user with project guest rights'
|
|
|
|
it_should_behave_like 'pull request user with project reader rights'
|
|
|
|
it_should_behave_like 'user with pull request update rights'
|
2012-10-01 12:40:52 +01:00
|
|
|
it_should_behave_like 'pull request when project with issues turned off'
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project owner user' do
|
|
|
|
before do
|
|
|
|
@user = @project.owner
|
|
|
|
set_session_for(@user)
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
2012-09-28 16:08:51 +01:00
|
|
|
|
|
|
|
it_should_behave_like 'pull request user with project guest rights'
|
|
|
|
it_should_behave_like 'pull request user with project reader rights'
|
|
|
|
it_should_behave_like 'user with pull request update rights'
|
2012-10-01 12:40:52 +01:00
|
|
|
it_should_behave_like 'pull request when project with issues turned off'
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
context 'for project reader user' do
|
|
|
|
before do
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @user, 'reader')
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it_should_behave_like 'pull request user with project guest rights'
|
|
|
|
it_should_behave_like 'pull request user with project reader rights'
|
|
|
|
it_should_behave_like 'user without pull request update rights'
|
2012-10-01 12:40:52 +01:00
|
|
|
it_should_behave_like 'pull request when project with issues turned off'
|
2013-07-22 16:19:25 +01:00
|
|
|
|
|
|
|
it 'should return 404' do
|
2014-03-19 07:19:03 +00:00
|
|
|
get :show, name_with_owner: @project.name_with_owner, id: 999999
|
2014-01-21 04:51:49 +00:00
|
|
|
render_template(file: "#{Rails.root}/public/404.html")
|
2013-07-22 16:19:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should redirect to issue page' do
|
2014-03-19 07:19:03 +00:00
|
|
|
get :show, name_with_owner: @project.name_with_owner, id: @issue.serial_id
|
2013-07-22 16:19:25 +01:00
|
|
|
response.should redirect_to(project_issue_path(@project, @issue))
|
|
|
|
end
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project writer user' do
|
|
|
|
before do
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @user, 'writer')
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it_should_behave_like 'pull request user with project guest rights'
|
|
|
|
it_should_behave_like 'pull request user with project reader rights'
|
2014-06-17 20:48:22 +01:00
|
|
|
it_should_behave_like 'user with pull request update rights'
|
2012-10-01 12:40:52 +01:00
|
|
|
it_should_behave_like 'pull request when project with issues turned off'
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
context 'for pull request assign user' do
|
|
|
|
before do
|
|
|
|
set_session_for(@pull request_user)
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it_should_behave_like 'user without pull request update rights'
|
2012-10-01 12:40:52 +01:00
|
|
|
it_should_behave_like 'pull request when project with issues turned off'
|
2012-09-28 16:08:51 +01:00
|
|
|
end
|
|
|
|
=end
|
|
|
|
|
2012-10-01 12:32:40 +01:00
|
|
|
context 'for guest' do
|
|
|
|
let(:guest) { User.new }
|
2012-09-28 16:08:51 +01:00
|
|
|
before do
|
2012-10-01 12:32:40 +01:00
|
|
|
set_session_for(guest)
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
2012-05-23 17:09:11 +01:00
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
if APP_CONFIG['anonymous_access']
|
2012-10-02 14:43:23 +01:00
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it_should_behave_like 'pull request user with project guest rights'
|
2012-10-01 12:40:52 +01:00
|
|
|
it_should_behave_like 'pull request when project with issues turned off'
|
2012-10-02 14:43:23 +01:00
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
else
|
|
|
|
it 'should not be able to perform show action' do
|
2012-10-01 12:32:40 +01:00
|
|
|
@pull.check
|
2014-03-19 07:19:03 +00:00
|
|
|
get :show, name_with_owner: @project.name_with_owner, id: @pull.serial_id
|
2012-09-28 16:08:51 +01:00
|
|
|
response.should redirect_to(new_user_session_path)
|
|
|
|
end
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
2012-05-23 17:09:11 +01:00
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it 'should not be able to perform create action' do
|
|
|
|
post :create, @create_params
|
|
|
|
response.should redirect_to(new_user_session_path)
|
2012-05-23 17:09:11 +01:00
|
|
|
end
|
|
|
|
|
2012-09-28 16:08:51 +01:00
|
|
|
it 'should not create pull request object into db' do
|
|
|
|
lambda{ post :create, @create_params }.should_not change{ PullRequest.count }
|
2012-05-23 17:09:11 +01:00
|
|
|
end
|
2012-09-28 16:08:51 +01:00
|
|
|
|
|
|
|
it_should_behave_like 'user without pull request update rights'
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|
2013-07-09 21:12:27 +01:00
|
|
|
|
2013-07-10 10:32:59 +01:00
|
|
|
context 'send email messages' do
|
|
|
|
before(:each) do
|
|
|
|
@project_reader = FactoryGirl.create :user
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @project_reader, 'reader')
|
2013-07-10 10:32:59 +01:00
|
|
|
@project_admin = FactoryGirl.create :user
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @project_admin, 'admin')
|
2013-07-10 10:32:59 +01:00
|
|
|
@project_writer = FactoryGirl.create :user
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @project_writer, 'writer')
|
2013-07-10 10:32:59 +01:00
|
|
|
|
|
|
|
set_session_for(@project_writer)
|
|
|
|
ActionMailer::Base.deliveries = []
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should send two email messages to project admins' do
|
|
|
|
post :create, @create_params
|
|
|
|
ActionMailer::Base.deliveries.count.should == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should send two email messages to admins and one to assignee' do
|
2014-01-21 04:51:49 +00:00
|
|
|
post :create, @create_params.deep_merge(issue: {assignee_id: @project_reader.id})
|
2013-07-10 10:32:59 +01:00
|
|
|
ActionMailer::Base.deliveries.count.should == 3
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not duplicate email message' do
|
2014-01-21 04:51:49 +00:00
|
|
|
post :create, @create_params.deep_merge(issue: {assignee_id: @project_admin.id})
|
2013-07-10 10:32:59 +01:00
|
|
|
ActionMailer::Base.deliveries.count.should == 2 # send only to admins
|
|
|
|
ActionMailer::Base.deliveries.first.to != ActionMailer::Base.deliveries.last.to
|
|
|
|
end
|
|
|
|
end
|
2012-05-22 19:23:00 +01:00
|
|
|
end
|