Skip to content

Commit

Permalink
Improve ExcludeRequestsControllerSpec
Browse files Browse the repository at this point in the history
- Add tests for policies
- Improve current tests (naming, use to_change, etc.)
  • Loading branch information
Ana06 committed Nov 26, 2018
1 parent 116a4a6 commit c0e99b9
Showing 1 changed file with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe Webui::Staging::ExcludedRequestsController, type: :controller do
let(:user) { create(:confirmed_user, login: 'tom') }
let(:another_user) { create(:confirmed_user, login: 'another_user') }
let(:project) { user.home_project }
let(:staging_workflow) { create(:staging_workflow_with_staging_projects, project: project) }

Expand All @@ -22,49 +23,99 @@
describe '#create' do
let(:description) { Faker::Lorem.sentence }

context 'success' do
before do
post :create, params: { staging_workflow_id: staging_workflow, staging_request_exclusion: { number: bs_request, description: description } }
context 'succeeds' do
subject { post :create, params: { staging_workflow_id: staging_workflow, staging_request_exclusion: { number: bs_request, description: description } } }

it { expect { subject }.to(change { staging_workflow.request_exclusions.count }.by(1)) }

context 'response' do
before { subject }

it { expect(staging_workflow.request_exclusions.first.description).to eq(description) }
it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
it { expect(flash[:success]).not_to be_nil }
end
end

context 'fails: invalid exclusion request' do
subject { post :create, params: { staging_workflow_id: staging_workflow, staging_request_exclusion: { number: bs_request } } }

it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) }

it { expect(Staging::RequestExclusion.count).to eq(1) }
it { expect(staging_workflow.request_exclusions.first.description).to eq(description) }
it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
it { expect(flash[:success]).not_to be_nil }
context 'response' do
before { subject }

it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
it { expect(flash[:error]).not_to be_nil }
end
end

context "doesn't exist" do
context "fails: user doesn't have permissions" do
subject { post :create, params: { staging_workflow_id: staging_workflow, staging_request_exclusion: { number: bs_request, description: description } } }

before do
allow_any_instance_of(Staging::RequestExclusion).to receive(:save).and_return(false)
post :create, params: { staging_workflow_id: staging_workflow, staging_request_exclusion: { number: bs_request, description: description } }
staging_workflow
login(another_user)
end

it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
it { expect(flash[:error]).not_to be_nil }
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) }

context 'response' do
before { subject }

it { expect(response).to redirect_to(root_path) }
it { expect(flash[:error]).not_to be_nil }
end
end
end

describe '#destroy' do
let(:request_exclusion) { create(:request_exclusion, staging_workflow: staging_workflow, bs_request: bs_request) }
let!(:request_exclusion) { create(:request_exclusion, staging_workflow: staging_workflow, bs_request: bs_request) }

context 'success' do
before do
delete :destroy, params: { staging_workflow_id: staging_workflow, id: request_exclusion }
context 'succeeds' do
subject { delete :destroy, params: { staging_workflow_id: staging_workflow, id: request_exclusion } }

it { expect { subject }.to(change { staging_workflow.request_exclusions.count }.by(-1)) }

context 'response' do
before { subject }

it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
it { expect(flash[:success]).not_to be_nil }
end
end

context 'fails: destroy not possible' do
subject { delete :destroy, params: { staging_workflow_id: staging_workflow, id: request_exclusion } }

before { allow_any_instance_of(Staging::RequestExclusion).to receive(:destroy).and_return(false) }

it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) }

it { expect(Staging::RequestExclusion.count).to eq(0) }
it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
it { expect(flash[:success]).not_to be_nil }
context 'response' do
before { subject }

it { expect(flash[:error]).not_to be_nil }
it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
end
end

context 'error' do
context "fails: users doesn't have permissions" do
subject { delete :destroy, params: { staging_workflow_id: staging_workflow, id: request_exclusion } }

before do
allow_any_instance_of(Staging::RequestExclusion).to receive(:destroy).and_return(false)
delete :destroy, params: { staging_workflow_id: staging_workflow, id: request_exclusion }
staging_workflow
login(another_user)
end

it { expect(flash[:error]).not_to be_nil }
it { expect(response).to redirect_to(staging_workflow_excluded_requests_path(staging_workflow)) }
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) }

context 'response' do
before { subject }

it { expect(response).to redirect_to(root_path) }
it { expect(flash[:error]).not_to be_nil }
end
end
end
end

0 comments on commit c0e99b9

Please sign in to comment.