From 5404a7e75a7bfd5c7c4927f49bcf18dc25739888 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 22 May 2014 22:08:43 +0400 Subject: [PATCH] #390: added specs --- .../api/v1/build_lists_controller.rb | 6 +- app/views/projects/build_lists/show.html.haml | 2 +- config/routes.rb | 1 + .../api/v1/build_lists_controller_spec.rb | 193 +++++++++--------- .../projects/build_lists_controller_spec.rb | 98 +++++++-- spec/spec_helper.rb | 4 +- 6 files changed, 176 insertions(+), 128 deletions(-) diff --git a/app/controllers/api/v1/build_lists_controller.rb b/app/controllers/api/v1/build_lists_controller.rb index 0655967ab..efcb5fff4 100644 --- a/app/controllers/api/v1/build_lists_controller.rb +++ b/app/controllers/api/v1/build_lists_controller.rb @@ -4,7 +4,7 @@ class Api::V1::BuildListsController < Api::V1::BaseController skip_before_filter :authenticate_user!, only: [:show, :index] if APP_CONFIG['anonymous_access'] load_resource :project, only: :index, parent: false - load_and_authorize_resource :build_list, only: [:show, :create, :cancel, :publish, :reject_publish, :create_container, :publish_into_testing] + load_and_authorize_resource :build_list, only: [:show, :create, :cancel, :publish, :reject_publish, :create_container, :publish_into_testing, :rerun_tests] def index authorize!(:show, @project) if @project @@ -43,6 +43,10 @@ class Api::V1::BuildListsController < Api::V1::BaseController render_json :create_container, :publish_container end + def rerun_tests + render_json :rerun_tests + end + def publish_into_testing @build_list.publisher = current_user render_json :publish_into_testing diff --git a/app/views/projects/build_lists/show.html.haml b/app/views/projects/build_lists/show.html.haml index 9b9420b4f..224771806 100644 --- a/app/views/projects/build_lists/show.html.haml +++ b/app/views/projects/build_lists/show.html.haml @@ -181,7 +181,7 @@ data: { confirm: t("layout.publish_again_warning") }, name: 'publish', 'ng-show' => "build_list.can_publish && build_list.status == #{BuildList::BUILD_PUBLISHED}" - - if can? :rerun_tests, @build_list + - if (current_user.tester? || current_user.admin?) && can?(:rerun_tests, @build_list) = link_to t('layout.build_lists.rerun_tests'), rerun_tests_build_list_path(@build_list), method: :put, diff --git a/config/routes.rb b/config/routes.rb index bcd78fd53..dd1db55ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,6 +37,7 @@ Rosa::Application.routes.draw do member { put :publish put :reject_publish + put :rerun_tests put :cancel put :create_container put :publish_into_testing diff --git a/spec/controllers/api/v1/build_lists_controller_spec.rb b/spec/controllers/api/v1/build_lists_controller_spec.rb index 14bdd57c2..8ccb22ccc 100644 --- a/spec/controllers/api/v1/build_lists_controller_spec.rb +++ b/spec/controllers/api/v1/build_lists_controller_spec.rb @@ -25,10 +25,6 @@ shared_examples_for 'not show build list via api' do end shared_examples_for 'create build list via api' do - before { - #@project.update_attributes({repositories: @platform.repositories}) - #test_git_commit(@project) - } it 'should create one more build list' do lambda { post :create, @create_params }.should change{ BuildList.count }.by(1) @@ -79,31 +75,24 @@ shared_examples_for 'not create build list via api' do end shared_examples_for 'validation error via build list api' do |message| - it 'should return 422 response code' do - response.status.should == 422 - end - - it "should return correct json error message" do - response.body.should == { build_list: {id: nil, message: message} }.to_json + it 'should return 422 response code and correct json error message' do + expect(response.status).to eq(422) + expect(response.body).to eq({ build_list: {id: nil, message: message} }.to_json) end end describe Api::V1::BuildListsController do - before(:each) { stub_symlink_methods } + before { stub_symlink_methods } context 'create and update abilities' do context 'for user' do - before(:each) do - Arch.destroy_all - User.destroy_all - + before do @build_list = FactoryGirl.create(:build_list) @params = @build_list.attributes.symbolize_keys @project = @build_list.project @platform = @build_list.save_to_platform #@platform = FactoryGirl.create(:platform_with_repos) - stub_symlink_methods @user = FactoryGirl.create(:user) @owner_user = @project.owner @member_user = FactoryGirl.create(:user) @@ -113,65 +102,71 @@ describe Api::V1::BuildListsController do # Create and show params: @create_params = {build_list: @build_list.attributes.symbolize_keys.merge(:qwerty=>'!')} # wrong parameter @create_params = @create_params.merge(arches: [@params[:arch_id]], build_for_platform_id: @platform.id, format: :json) - any_instance_of(Project, versions: ['v1.0', 'v2.0']) + allow_any_instance_of(Project).to receive(:versions).and_return(%w(v1.0 v2.0)) http_login(@user) end + context 'do rerun_tests' do + def do_rerun_tests + put :rerun_tests, id: @build_list, format: :json + end + + context 'if user is project owner' do + before { http_login(@owner_user) } + + it 'reruns tests' do + expect_any_instance_of(BuildList).to receive(:rerun_tests).and_return(true) + do_rerun_tests + expect(response.body).to eq({ build_list: {id: @build_list.id, message: I18n.t('layout.build_lists.rerun_tests_success')} }.to_json) + expect(response).to be_success + end + + context 'returns an error if the can not rerun_tests' do + before do + allow_any_instance_of(BuildList).to receive(:rerun_tests).and_return(false) + end + + it_should_behave_like 'validation error via build list api', I18n.t('layout.build_lists.rerun_tests_fail') + end + end + + it 'returns an error if user is not project owner' do + expect_any_instance_of(BuildList).to_not receive(:rerun_tests) + do_rerun_tests + expect(response.body).to eq({"message" => "Access violation to this page!"}.to_json) + end + end + context "do cancel" do def do_cancel put :cancel, id: @build_list, format: :json end context 'if user is project owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} - context "if it has :build_pending status" do - before do - @build_list.update_column(:status, BuildList::BUILD_PENDING) - do_cancel - end - - it "should return correct json message" do - response.body.should == { build_list: {id: @build_list.id, message: I18n.t('layout.build_lists.cancel_success')} }.to_json - end - - it 'should return 200 response code' do - response.should be_success - end - - it "should cancel build list" do - @build_list.reload.status.should == BuildList::BUILD_CANCELING - end + it 'cancels build' do + expect_any_instance_of(BuildList).to receive(:cancel).and_return(true) + do_cancel + expect(response.body).to eq({ build_list: {id: @build_list.id, message: I18n.t('layout.build_lists.cancel_success')} }.to_json) + expect(response).to be_success end - context "if it has another status" do + context 'returns an error if the can not cancel' do before do - @build_list.update_column(:status, BuildList::SUCCESS) + allow_any_instance_of(BuildList).to receive(:cancel).and_return(false) do_cancel end it_should_behave_like 'validation error via build list api', I18n.t('layout.build_lists.cancel_fail') - - it "should not change status of build list" do - @build_list.reload.status.should == BuildList::SUCCESS - end end end - context 'if user is not project owner' do - before(:each) do - @build_list.update_column(:status, BuildList::BUILD_PENDING) - do_cancel - end - - it "should return access violation message" do - response.body.should == {"message" => "Access violation to this page!"}.to_json - end - - it "should not change status of build list" do - @build_list.reload.status.should == BuildList::BUILD_PENDING - end + it 'returns an error if user is not project owner' do + expect_any_instance_of(BuildList).to_not receive(:cancel) + do_cancel + expect(response.body).to eq({"message" => "Access violation to this page!"}.to_json) end end @@ -204,7 +199,7 @@ describe Api::V1::BuildListsController do end context "if it has another status" do - before(:each) do + before do @build_list.update_column(:status, BuildList::BUILD_ERROR) do_create_container end @@ -218,7 +213,7 @@ describe Api::V1::BuildListsController do end context 'if user is not project owner' do - before(:each) do + before do @build_list.update_column(:status, BuildList::SUCCESS) do_create_container end @@ -239,7 +234,7 @@ describe Api::V1::BuildListsController do end context 'if user is project && platform owner' do - before(:each) do + before do http_login(@owner_user) end @@ -281,7 +276,7 @@ describe Api::V1::BuildListsController do end context "if it has another status" do - before(:each) do + before do @build_list.update_column(:status, BuildList::BUILD_CANCELED) do_publish_into_testing end @@ -340,7 +335,7 @@ describe Api::V1::BuildListsController do end context 'if user is project && platform owner' do - before(:each) do + before do http_login(@owner_user) end @@ -382,7 +377,7 @@ describe Api::V1::BuildListsController do end context "if it has another status" do - before(:each) do + before do @build_list.update_column(:status, BuildList::BUILD_CANCELED) do_publish end @@ -450,7 +445,7 @@ describe Api::V1::BuildListsController do end context "do reject_publish" do - before(:each) do + before do any_instance_of(BuildList, current_duration: 100) @build_list.save_to_repository.update_column(:publish_without_qa, false) end @@ -460,7 +455,7 @@ describe Api::V1::BuildListsController do end context 'if user is project owner' do - before(:each) do + before do http_login(@owner_user) @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_platform.update_column(:released, true) @@ -482,7 +477,7 @@ describe Api::V1::BuildListsController do end context "if it has another status" do - before(:each) do + before do @build_list.update_column(:status, BuildList::BUILD_CANCELED) do_reject_publish end @@ -496,7 +491,7 @@ describe Api::V1::BuildListsController do end context 'if user is not project owner' do - before(:each) do + before do @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_platform.update_column(:released, true) do_reject_publish @@ -513,7 +508,7 @@ describe Api::V1::BuildListsController do end context 'if user is project reader' do - before(:each) do + before do @another_user = FactoryGirl.create(:user) @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_repository.update_column(:publish_without_qa, true) @@ -533,7 +528,7 @@ describe Api::V1::BuildListsController do end context 'if user is project writer' do - before(:each) do + before do @another_user = FactoryGirl.create(:user) @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_repository.update_column(:publish_without_qa, true) @@ -560,7 +555,7 @@ describe Api::V1::BuildListsController do it_should_behave_like 'not create build list via api' context 'if user is project owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} it_should_behave_like 'create build list via api' context 'no ability to read build_for_platform' do @@ -579,36 +574,33 @@ describe Api::V1::BuildListsController do end context 'if user is project read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'not create build list via api' end end context 'for hidden project' do - before(:each) do + before do @project.update_column(:visibility, 'hidden') end it_should_behave_like 'not create build list via api' context 'if user is project owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} it_should_behave_like 'create build list via api' end context 'if user is project read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'not create build list via api' end end end context 'for group' do - before(:each) do - Arch.destroy_all - User.destroy_all - + before do @build_list = FactoryGirl.create(:build_list) @params = @build_list.attributes.symbolize_keys @project = @build_list.project @@ -651,30 +643,30 @@ describe Api::V1::BuildListsController do it_should_behave_like 'not create build list via api' context 'if user is group owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} it_should_behave_like 'create build list via api' end context 'if user is group read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'not create build list via api' end end context 'for hidden project' do - before(:each) do + before do @build_list.project.update_column(:visibility, 'hidden') end it_should_behave_like 'not create build list via api' context 'if user is group owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} it_should_behave_like 'create build list via api' end context 'if user is group read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'not create build list via api' end end @@ -683,10 +675,7 @@ describe Api::V1::BuildListsController do end context 'read and accessible abilities' do - before(:each) do - Arch.destroy_all - User.destroy_all - + before do @user = FactoryGirl.create(:user) # Build Lists: @@ -723,7 +712,7 @@ describe Api::V1::BuildListsController do end context 'for all build lists' do - before(:each) { + before { http_login(@user) } @@ -743,7 +732,7 @@ describe Api::V1::BuildListsController do end context 'filter' do - before(:each) do + before do http_login FactoryGirl.create(:admin) end @@ -775,7 +764,7 @@ describe Api::V1::BuildListsController do end context "for user" do - before(:each) do + before do @build_list = FactoryGirl.create(:build_list) @params = @build_list.attributes.symbolize_keys @project = @build_list.project @@ -792,45 +781,45 @@ describe Api::V1::BuildListsController do context 'for open project' do context 'for simple user' do - before(:each) {http_login(@user)} + before {http_login(@user)} it_should_behave_like 'show build list via api' end context 'if user is project owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} it_should_behave_like 'show build list via api' end context 'if user is project read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'show build list via api' end end context 'for hidden project' do - before(:each) do + before do @project.update_column(:visibility, 'hidden') end context 'for simple user' do - before(:each) {http_login(@user)} + before {http_login(@user)} it_should_behave_like 'not show build list via api' end context 'if user is project owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} it_should_behave_like 'show build list via api' end context 'if user is project read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'show build list via api' end end end context "for group" do - before(:each) do + before do @platform = FactoryGirl.create(:platform_with_repos) @build_list = FactoryGirl.create(:build_list, save_to_platform: @platform) @project = @build_list.project @@ -854,38 +843,38 @@ describe Api::V1::BuildListsController do context 'for open project' do context 'for simple user' do - before(:each) {http_login(@user)} + before {http_login(@user)} it_should_behave_like 'show build list via api' end context 'if user is group owner' do - before(:each) {http_login(@owner_user)} + before {http_login(@owner_user)} it_should_behave_like 'show build list via api' end context 'if user is group read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'show build list via api' end end context 'for hidden project' do - before(:each) do + before do @build_list.project.update_column(:visibility, 'hidden') end context 'for simple user' do - before(:each) {http_login(@user)} + before {http_login(@user)} it_should_behave_like 'not show build list via api' end context 'if user is group owner' do - before(:each) { http_login(@owner_user) } + before { http_login(@owner_user) } it_should_behave_like 'show build list via api' end context 'if user is group read member' do - before(:each) {http_login(@member_user)} + before {http_login(@member_user)} it_should_behave_like 'show build list via api' end end diff --git a/spec/controllers/projects/build_lists_controller_spec.rb b/spec/controllers/projects/build_lists_controller_spec.rb index f23a8b942..07a5a1fea 100644 --- a/spec/controllers/projects/build_lists_controller_spec.rb +++ b/spec/controllers/projects/build_lists_controller_spec.rb @@ -80,7 +80,7 @@ describe Projects::BuildListsController do before { stub_symlink_methods } context 'crud' do - before(:each) do + before do @platform = FactoryGirl.create(:platform_with_repos) @create_params = { build_list: { @@ -109,7 +109,7 @@ describe Projects::BuildListsController do end context 'for user' do - before(:each) do + before do any_instance_of(BuildList, current_duration: 100) @build_list = FactoryGirl.create(:build_list) @project = @build_list.project @@ -124,15 +124,69 @@ describe Projects::BuildListsController do @request.env['HTTP_REFERER'] = build_list_path(@build_list) end + context 'do rerun_tests' do + def do_rerun_tests + put :rerun_tests, id: @build_list + end + + context 'if user is project owner' do + before { set_session_for(@owner_user) } + + it 'reruns tests' do + expect_any_instance_of(BuildList).to receive(:rerun_tests).and_return(true) + do_rerun_tests + + expect(response).to redirect_to(build_list_path(@build_list)) + expect(flash[:notice]).to match I18n.t('layout.build_lists.rerun_tests_success') + end + + it 'returns an error if the can not rerun_tests' do + allow_any_instance_of(BuildList).to receive(:rerun_tests).and_return(false) + do_rerun_tests + + expect(response).to redirect_to(build_list_path(@build_list)) + expect(flash[:error]).to match I18n.t('layout.build_lists.rerun_tests_fail') + end + end + + it 'returns an error if user is not project owner' do + expect_any_instance_of(BuildList).to_not receive(:rerun_tests) + do_rerun_tests + expect(response).to redirect_to(forbidden_url) + end + + it 'returns an error if user is project reader' do + @another_user = FactoryGirl.create(:user) + @build_list.project.collaborators.create(actor_type: 'User', actor_id: @another_user.id, role: 'reader') + set_session_for(@another_user) + + expect_any_instance_of(BuildList).to_not receive(:rerun_tests) + do_rerun_tests + expect(response).to redirect_to(forbidden_url) + end + + it 'reruns tests if user is project writer' do + @writer_user = FactoryGirl.create(:user) + create_relation(@build_list.project, @writer_user, 'writer') + set_session_for(@writer_user) + + expect_any_instance_of(BuildList).to receive(:rerun_tests).and_return(true) + do_rerun_tests + expect(response).to redirect_to(build_list_path(@build_list)) + expect(flash[:notice]).to match I18n.t('layout.build_lists.rerun_tests_success') + end + + end + context "do reject_publish" do - before(:each) {@build_list.save_to_repository.update_column(:publish_without_qa, true)} + before {@build_list.save_to_repository.update_column(:publish_without_qa, true)} def do_reject_publish put :reject_publish, id: @build_list end context 'if user is project owner' do - before(:each) do + before do set_session_for(@owner_user) @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_platform.update_column(:released, true) @@ -150,7 +204,7 @@ describe Projects::BuildListsController do end context "if it has another status" do - before(:each) do + before do @build_list.update_column(:status, BuildList::BUILD_ERROR) do_reject_publish end @@ -162,7 +216,7 @@ describe Projects::BuildListsController do end context 'if user is not project owner' do - before(:each) do + before do @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_platform.update_column(:released, true) do_reject_publish @@ -179,7 +233,7 @@ describe Projects::BuildListsController do end context 'if user is project reader' do - before(:each) do + before do @another_user = FactoryGirl.create(:user) @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_repository.update_column(:publish_without_qa, true) @@ -199,7 +253,7 @@ describe Projects::BuildListsController do end context 'if user is project writer' do - before(:each) do + before do @writer_user = FactoryGirl.create(:user) @build_list.update_column(:status, BuildList::SUCCESS) @build_list.save_to_repository.update_column(:publish_without_qa, true) @@ -219,7 +273,7 @@ describe Projects::BuildListsController do end context 'for all build lists' do - before(:each) do + before do @build_list1 = FactoryGirl.create(:build_list) @build_list2 = FactoryGirl.create(:build_list) @@ -252,7 +306,7 @@ describe Projects::BuildListsController do it_should_behave_like 'not create build list' context 'if user is project owner' do - before(:each) {set_session_for(@owner_user)} + before {set_session_for(@owner_user)} it_should_behave_like 'show build list' it_should_behave_like 'create build list' @@ -270,14 +324,14 @@ describe Projects::BuildListsController do end context 'if user is project read member' do - before(:each) {set_session_for(@member_user)} + before {set_session_for(@member_user)} it_should_behave_like 'show build list' it_should_behave_like 'not create build list' end end context 'for hidden project' do - before(:each) do + before do @project.visibility = 'hidden' @project.save end @@ -286,13 +340,13 @@ describe Projects::BuildListsController do it_should_behave_like 'not create build list' context 'if user is project owner' do - before(:each) {set_session_for(@owner_user)} + before {set_session_for(@owner_user)} it_should_behave_like 'show build list' it_should_behave_like 'create build list' end context 'if user is project read member' do - before(:each) {set_session_for(@member_user)} + before {set_session_for(@member_user)} it_should_behave_like 'show build list' it_should_behave_like 'not create build list' end @@ -300,7 +354,7 @@ describe Projects::BuildListsController do end context 'for group' do - before(:each) do + before do @user = FactoryGirl.create(:user) set_session_for(@user) @@ -319,7 +373,7 @@ describe Projects::BuildListsController do end context 'for all build lists' do - before(:each) do + before do @build_list1 = FactoryGirl.create(:build_list) @build_list2 = FactoryGirl.create(:build_list) @@ -352,20 +406,20 @@ describe Projects::BuildListsController do it_should_behave_like 'not create build list' context 'if user is group owner' do - before(:each) {set_session_for(@owner_user)} + before {set_session_for(@owner_user)} it_should_behave_like 'show build list' it_should_behave_like 'create build list' end context 'if user is group read member' do - before(:each) {set_session_for(@member_user)} + before {set_session_for(@member_user)} it_should_behave_like 'show build list' it_should_behave_like 'not create build list' end end context 'for hidden project' do - before(:each) do + before do @project.visibility = 'hidden' @project.save end @@ -374,13 +428,13 @@ describe Projects::BuildListsController do it_should_behave_like 'not create build list' context 'if user is group owner' do - before(:each) {set_session_for(@owner_user)} + before {set_session_for(@owner_user)} it_should_behave_like 'show build list' it_should_behave_like 'create build list' end context 'if user is group read member' do - before(:each) {set_session_for(@member_user)} + before {set_session_for(@member_user)} it_should_behave_like 'show build list' it_should_behave_like 'not create build list' end @@ -391,7 +445,7 @@ describe Projects::BuildListsController do context 'filter' do - before(:each) do + before do set_session_for FactoryGirl.create(:admin) @build_list1 = FactoryGirl.create(:build_list) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 433505b88..dcae6ef9d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -45,8 +45,8 @@ def http_login(user=nil, password = '123456') end def stub_symlink_methods - any_instance_of(Platform, symlink_directory: true) - any_instance_of(Platform, remove_symlink_directory: true) + allow_any_instance_of(Platform).to receive(:symlink_directory).and_return(true) + allow_any_instance_of(Platform).to receive(:remove_symlink_directory).and_return(true) end Resque.inline = true