2013-06-26 18:59:07 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
def create_pull to_ref, from_ref, owner, project = @project
|
2014-01-21 04:51:49 +00:00
|
|
|
pull = project.pull_requests.new issue_attributes: {title: 'test', body: 'testing'}
|
2013-06-26 18:59:07 +01:00
|
|
|
pull.issue.user, pull.issue.project = owner, pull.to_project
|
|
|
|
pull.to_ref, pull.from_ref, pull.from_project = to_ref, from_ref, project
|
2013-06-28 14:10:22 +01:00
|
|
|
pull.save; pull.check
|
2013-06-26 18:59:07 +01:00
|
|
|
pull
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Api::V1::PullRequestsController do
|
2013-06-28 14:10:22 +01:00
|
|
|
before(:all) do
|
2013-06-26 18:59:07 +01:00
|
|
|
stub_symlink_methods
|
2014-03-18 13:54:16 +00:00
|
|
|
|
2013-06-26 18:59:07 +01:00
|
|
|
@project = FactoryGirl.create(:project_with_commit)
|
|
|
|
@pull = create_pull 'master', 'non_conflicts', @project.owner
|
2013-06-28 14:10:22 +01:00
|
|
|
|
|
|
|
@another_project = FactoryGirl.create(:project_with_commit)
|
|
|
|
@another_pull = create_pull 'master', 'non_conflicts', @another_project.owner, @another_project
|
|
|
|
|
|
|
|
@hidden_project = FactoryGirl.create(:project_with_commit)
|
|
|
|
@hidden_project.update_column :visibility, 'hidden'
|
|
|
|
@hidden_pull = create_pull 'master', 'non_conflicts', @hidden_project.owner, @hidden_project
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
@own_hidden_project = FactoryGirl.create(:project_with_commit, owner: @project.owner)
|
2013-06-28 14:10:22 +01:00
|
|
|
@own_hidden_project.update_column :visibility, 'hidden'
|
|
|
|
@own_hidden_pull = create_pull 'master', 'non_conflicts', @own_hidden_project.owner, @own_hidden_project
|
|
|
|
@own_hidden_pull.issue.update_column :assignee_id, @project.owner.id
|
|
|
|
|
|
|
|
@membered_project = FactoryGirl.create(:project_with_commit)
|
|
|
|
@membered_pull = create_pull 'master', 'non_conflicts', @membered_project.owner, @membered_project
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@membered_project, @pull.user, 'reader')
|
2013-06-28 14:10:22 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
@create_params = {pull_request: {title: 'title', body: 'body',
|
|
|
|
from_ref: 'conflicts', to_ref: 'master'},
|
|
|
|
project_id: @project.id, format: :json}
|
2013-06-28 14:10:22 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
@update_params = {pull_request: {title: 'new title'},
|
|
|
|
project_id: @project.id, id: @pull.serial_id, format: :json}
|
2013-07-22 16:19:25 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
@issue = FactoryGirl.create(:issue, project: @project)
|
2013-06-26 18:59:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'read and accessible abilities' do
|
|
|
|
context 'for user' do
|
|
|
|
before(:each) do
|
|
|
|
http_login(@project.owner)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can show pull request in own project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should render right template for show action' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.should render_template('api/v1/pull_requests/show')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can show pull request in open project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @another_project.id, id: @another_pull.serial_id, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can show pull request in own hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @own_hidden_project.id, id: @own_hidden_pull.serial_id, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant show pull request in hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @hidden_project.id, id: @hidden_pull.serial_id, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.status.should == 403
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return three pull requests' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :all_index, filter: 'all', format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
assigns[:pulls].should include(@pull)
|
2013-06-28 14:10:22 +01:00
|
|
|
assigns[:pulls].should include(@own_hidden_pull)
|
|
|
|
assigns[:pulls].should include(@membered_pull)
|
2013-06-26 18:59:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should render right template for all index action' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :all_index, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.should render_template('api/v1/pull_requests/index')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return only assigned pull request' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :user_index, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
assigns[:pulls].should include(@own_hidden_pull)
|
2013-06-28 17:15:17 +01:00
|
|
|
assigns[:pulls].should have(1).item
|
2013-06-26 18:59:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should render right template for user index action' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :user_index, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.should render_template('api/v1/pull_requests/index')
|
|
|
|
end
|
2013-06-28 14:10:22 +01:00
|
|
|
|
|
|
|
%w(commits files).each do |action|
|
|
|
|
it "can show pull request #{action} in own project" do
|
2014-01-21 04:51:49 +00:00
|
|
|
get action, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should render right template for commits action" do
|
2014-01-21 04:51:49 +00:00
|
|
|
get action, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
response.should render_template("api/v1/pull_requests/#{action}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can't show pull request #{action} in hidden project" do
|
2014-01-21 04:51:49 +00:00
|
|
|
get action, project_id: @hidden_project.id, id: @hidden_pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
end
|
2013-07-22 16:19:25 +01:00
|
|
|
|
|
|
|
it 'should return 404' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @project.id, id: 999999, format: :json
|
2013-07-22 16:19:25 +01:00
|
|
|
response.status.should == 404
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should redirect to issue page' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @project.id, id: @issue.serial_id, format: :json
|
2013-07-22 16:19:25 +01:00
|
|
|
response.should redirect_to(api_v1_project_issue_path(@project.id, @issue.serial_id))
|
|
|
|
end
|
2013-06-26 18:59:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for anonymous user' do
|
2014-01-21 04:51:49 +00:00
|
|
|
it 'can show pull request in open project', anonymous_access: true do
|
|
|
|
get :show, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
it 'cant show pull request in hidden project', anonymous_access: true do
|
2013-06-26 18:59:07 +01:00
|
|
|
@project.update_column :visibility, 'hidden'
|
2014-01-21 04:51:49 +00:00
|
|
|
get :show, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.status.should == 403
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not return any pull requests' do
|
2014-01-21 04:51:49 +00:00
|
|
|
get :all_index, filter: 'all', format: :json
|
2013-06-26 18:59:07 +01:00
|
|
|
response.status.should == 401
|
|
|
|
end
|
2013-06-28 14:10:22 +01:00
|
|
|
|
|
|
|
%w(commits files).each do |action|
|
2014-01-21 04:51:49 +00:00
|
|
|
it "can show pull request #{action} in project", anonymous_access: true do
|
|
|
|
get action, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
it "should render right template for commits action", anonymous_access: true do
|
|
|
|
get action, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
response.should render_template("api/v1/pull_requests/#{action}")
|
|
|
|
end
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
it "can't show pull request #{action} in hidden project", anonymous_access: true do
|
|
|
|
get action, project_id: @hidden_project.id, id: @hidden_pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
end
|
2013-06-26 18:59:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'create accessibility' do
|
|
|
|
context 'for user' do
|
|
|
|
before(:each) do
|
|
|
|
http_login(@pull.user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can create pull request in own project' do
|
|
|
|
lambda { post :create, @create_params }.should change{ PullRequest.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can create pull request in own hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
lambda { post :create, @create_params.merge(project_id: @own_hidden_project.id) }.should
|
2013-06-26 18:59:07 +01:00
|
|
|
change{ PullRequest.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can create pull request in open project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
lambda { post :create, @create_params.merge(project_id: @another_project.id) }.should
|
2013-06-26 18:59:07 +01:00
|
|
|
change{ PullRequest.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant create pull request in hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
lambda { post :create, @create_params.merge(project_id: @hidden_project.id) }.should
|
2013-06-26 18:59:07 +01:00
|
|
|
change{ PullRequest.count }.by(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for anonymous user' do
|
2014-01-21 04:51:49 +00:00
|
|
|
it 'cant create pull request in project', anonymous_access: true do
|
2013-06-26 18:59:07 +01:00
|
|
|
lambda { post :create, @create_params }.should change{ PullRequest.count }.by(0)
|
|
|
|
end
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
it 'cant create pull request in hidden project', anonymous_access: true do
|
|
|
|
lambda { post :create, @create_params.merge(project_id: @hidden_project.id) }.should
|
2013-06-26 18:59:07 +01:00
|
|
|
change{ PullRequest.count }.by(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-06-28 14:10:22 +01:00
|
|
|
|
|
|
|
context 'update accessibility' do
|
|
|
|
context 'for user' do
|
|
|
|
before(:each) do
|
|
|
|
http_login(@project.owner)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can update pull request in own project' do
|
|
|
|
put :update, @update_params
|
|
|
|
@pull.reload.title.should == 'new title'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can update pull request in own hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :update, @update_params.merge(project_id: @own_hidden_project.id, id: @own_hidden_pull.serial_id)
|
2013-06-28 14:10:22 +01:00
|
|
|
@own_hidden_pull.reload.title.should == 'new title'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant update pull request in open project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :update, @update_params.merge(project_id: @another_project.id, id: @another_pull.serial_id)
|
2013-06-28 14:10:22 +01:00
|
|
|
@another_pull.reload.title.should_not == 'new title'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant update pull request in hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :update, @update_params.merge(project_id: @hidden_project.id, id: @hidden_pull.serial_id)
|
2013-06-28 14:10:22 +01:00
|
|
|
@hidden_pull.reload.title.should_not == 'title'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can merge pull request in own project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :merge, project_id: @project.id, id: @pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
@pull.reload.status.should == 'merged'
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can merge pull request in own hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :merge, project_id: @own_hidden_project.id, id: @own_hidden_pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
@own_hidden_pull.reload.status.should == 'merged'
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant merge pull request in open project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :merge, project_id: @another_project.id, id: @another_pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
@another_pull.reload.status.should == 'ready'
|
|
|
|
response.status.should == 403
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant merge pull request in hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :merge, project_id: @hidden_project.id, id: @hidden_pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
@hidden_pull.reload.status.should == 'ready'
|
|
|
|
response.status.should == 403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for anonymous user' do
|
2014-01-21 04:51:49 +00:00
|
|
|
it 'cant update pull request in project', anonymous_access: true do
|
2013-06-28 14:10:22 +01:00
|
|
|
put :update, @update_params
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
it 'cant update pull request in hidden project', anonymous_access: true do
|
|
|
|
put :update, @update_params.merge(project_id: @hidden_project.id, id: @hidden_pull.serial_id)
|
2013-06-28 14:10:22 +01:00
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant merge pull request in open project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :merge, project_id: @another_project.id, id: @another_pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
@another_pull.reload.status.should == 'ready'
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cant merge pull request in hidden project' do
|
2014-01-21 04:51:49 +00:00
|
|
|
put :merge, project_id: @hidden_project.id, id: @hidden_pull.serial_id, format: :json
|
2013-06-28 14:10:22 +01:00
|
|
|
@hidden_pull.reload.status.should == 'ready'
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
http_login(@project_writer)
|
|
|
|
ActionMailer::Base.deliveries = []
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should send two email messages to project admins' do
|
|
|
|
post :create, @create_params
|
|
|
|
@project.pull_requests.last.issue.send(:new_issue_notifications)
|
|
|
|
@project.pull_requests.last.issue.send(:send_assign_notifications)
|
|
|
|
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(pull_request: {assignee_id: @project_reader.id})
|
2013-07-10 10:32:59 +01:00
|
|
|
@project.pull_requests.last.issue.send(:new_issue_notifications)
|
|
|
|
@project.pull_requests.last.issue.send(:send_assign_notifications)
|
|
|
|
ActionMailer::Base.deliveries.count.should == 3
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should send email message to new assignee' do
|
2013-07-22 19:53:41 +01:00
|
|
|
http_login(@project_admin)
|
2014-01-21 04:51:49 +00:00
|
|
|
put :update, @update_params.deep_merge(pull_request: {assignee_id: @project_reader.id})
|
2013-07-10 10:32:59 +01:00
|
|
|
@project.pull_requests.last.issue.send(:send_assign_notifications)
|
|
|
|
ActionMailer::Base.deliveries.count.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not duplicate email message' do
|
2014-01-21 04:51:49 +00:00
|
|
|
post :create, @create_params.deep_merge(pull_request: {assignee_id: @project_admin.id})
|
2013-07-10 10:32:59 +01:00
|
|
|
@project.pull_requests.last.issue.send(:new_issue_notifications)
|
|
|
|
@project.pull_requests.last.issue.send(:send_assign_notifications)
|
|
|
|
ActionMailer::Base.deliveries.count.should == 2 # send only to admins
|
|
|
|
ActionMailer::Base.deliveries.first.to != ActionMailer::Base.deliveries.last.to
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-28 14:10:22 +01:00
|
|
|
after(:all) do
|
|
|
|
User.destroy_all
|
|
|
|
Platform.destroy_all
|
|
|
|
end
|
2013-06-26 18:59:07 +01:00
|
|
|
end
|