I'm just starting with rails and testing, and not sure what's good way to dry code like this.
require 'rails_helper'
describe EventsController, type: :controller do
let(:admin) { create(:user_admin) }
let(:mod) { create(:user_moderator) }
let(:user) { create(:user) }
let(:link) { create(:link) }
let(:event) { create(:event) }
describe 'GET #new' do
it 'allows admin user to perform action' do
sign_in admin
get :new, link: link.id
expect(flash[:alert]).to_not match(/^You are not authorized to perform this action./)
end
it 'allows mod user to perform action' do
sign_in mod
get :new, link: link.id
expect(flash[:alert]).to_not match(/^You are not authorized to perform this action./)
end
it 'does not allow regular user to perform action' do
sign_in user
get :new, link: link.id
expect(flash[:alert]).to match(/^You are not authorized to perform this action./)
end
it 'does not allow guest to perform action' do
get :new, link: link.id
expect(flash[:alert]).to match(/^You are not authorized to perform this action./)
end
end
describe 'POST #create' do
it 'allows admin user to perform action' do
sign_in admin
post :create, event: attributes_for_event(event, link)
expect(flash[:alert]).to_not match(/^You are not authorized to perform this action./)
end
it 'allows mod user to perform action' do
sign_in mod
post :create, event: attributes_for_event(event, link)
expect(flash[:alert]).to_not match(/^You are not authorized to perform this action./)
end
it 'does not allow regular user to perform action' do
sign_in user
post :create, event: attributes_for_event(event, link)
expect(flash[:alert]).to match(/^You are not authorized to perform this action./)
end
it 'does not allow guest to perform action' do
post :create, event: attributes_for_event(event, link)
expect(flash[:alert]).to match(/^You are not authorized to perform this action./)
end
end
end