Skip to content

Instantly share code, notes, and snippets.

@Zeko369
Last active February 18, 2020 10:20
Show Gist options
  • Save Zeko369/2e81e78d1eca03ca35a5a1fc7d63b222 to your computer and use it in GitHub Desktop.
Save Zeko369/2e81e78d1eca03ca35a5a1fc7d63b222 to your computer and use it in GitHub Desktop.
FactoryBot.define do
factory :location do
name { Faker::TvShows::Simpsons.location }
end
end
describe LocationsController, type: :request do
let(:namespace) { create(:namespace) }
let(:user) { create(:user, namespace: namespace) }
let(:admin) { create(:user, namespace: namespace, role: :namespace_owner) }
let(:location) { create(:location, namespace: namespace) }
before do
location
sign_in(admin)
end
describe 'GET #index' do
context 'when user is admin' do
it 'returns OK' do
get locations_path
expect(response).to have_http_status(:ok)
end
end
context 'when user is not admin' do
before { sign_in(user) }
it 'returns 403' do
expect do
get locations_path
end.to raise_exception(Pundit::NotAuthorizedError)
end
end
end
describe 'GET #new' do
context 'when user is admin' do
it 'returns OK' do
get new_location_path
expect(response).to have_http_status(:ok)
end
end
context 'when user is not admin' do
before { sign_in(user) }
it 'returns 403' do
expect do
get new_location_path
end.to raise_exception(Pundit::NotAuthorizedError)
end
end
end
describe 'POST #create' do
let(:params) do
{
location: {
name: 'Foo Bar'
}
}
end
context 'when user is admin' do
it 'redirects to locations_path' do
post locations_path, params: params
expect(response).to redirect_to locations_path
end
it 'adds another one' do
expect do
post locations_path, params: params
end.to change(Location, :count).by(1)
end
it 'fails' do
expect do
post locations_path, params: { location: { foo: 'bar' } }
end.to change(Location, :count).by(0)
end
end
context 'when user is not admin' do
before { sign_in(user) }
it 'returns 403' do
expect do
post locations_path, params: params
end.to raise_exception(Pundit::NotAuthorizedError)
end
end
end
describe 'GET #edit' do
context 'when user is admin' do
it 'returns OK' do
get edit_location_path(location)
expect(response).to have_http_status(:ok)
end
end
context 'when user is not admin' do
before { sign_in(user) }
it 'returns 403' do
expect do
get edit_location_path(location)
end.to raise_exception(Pundit::NotAuthorizedError)
end
end
end
describe 'PATCH #update' do
let(:params) do
{
location: {
name: 'Foo Bar'
}
}
end
context 'when user is admin' do
it 'redirects to location' do
patch location_path(location), params: params
expect(response).to redirect_to locations_path
end
it 'adds another one' do
expect do
patch location_path(location), params: params
end.to change { location.reload.name }.from(location.name).to('Foo Bar')
end
end
context 'when user is not admin' do
before { sign_in(user) }
it 'returns 403' do
expect do
patch location_path(location), params: params
end.to raise_exception(Pundit::NotAuthorizedError)
end
end
end
describe 'DELETE #destroy' do
context 'when user is admin' do
it 'redirects to locations_path' do
delete location_path(location)
expect(response).to redirect_to locations_path
end
it 'deletes location' do
expect do
delete location_path(location)
end.to change(Location, :count).by(-1)
end
end
context 'when user is not admin' do
before { sign_in(user) }
it 'returns 403' do
expect do
get delete location_path(location)
end.to raise_exception(Pundit::NotAuthorizedError)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment