Retrospring/spec/integration/role_constrained_routes_spe...

53 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2022-06-21 12:33:07 -07:00
require "rails_helper"
2022-06-21 12:33:46 -07:00
require "support/pghero_stubby"
2022-06-21 12:33:07 -07:00
describe "role-constrained routes", type: :request do
shared_examples_for "fails to access route" do
it "fails to access route" do
# 404 = no user found -- we have a fallback route if something could not be matched
expect(subject).to eq 404
end
end
2022-06-21 12:33:07 -07:00
shared_examples_for "routes for" do |roles, subject_block, skip_reason: nil|
before { skip(skip_reason) } if skip_reason
subject(&subject_block)
2022-06-21 12:33:07 -07:00
context "not signed in" do
include_examples "fails to access route"
end
roles.each do |role|
context "signed in user without #{role} role" do
2022-06-21 12:33:07 -07:00
let(:user) { FactoryBot.create(:user, password: "test1234") }
before(:each) do
2022-06-21 12:33:07 -07:00
post "/sign_in", params: { user: { login: user.email, password: user.password } }
end
2022-06-21 12:33:07 -07:00
include_examples "fails to access route"
end
context "signed in user with #{role} role" do
2022-06-21 12:33:07 -07:00
let(:user) { FactoryBot.create(:user, password: "test1234", roles: [role]) }
before(:each) do
2022-06-21 12:33:07 -07:00
post "/sign_in", params: { user: { login: user.email, password: user.password } }
end
2022-06-21 12:33:07 -07:00
it "can access route" do
expect(subject).to be_in 200..299
end
end
end
end
2022-06-21 12:33:07 -07:00
it_behaves_like("routes for", [:administrator], -> { get("/justask_admin") })
it_behaves_like("routes for", [:administrator], -> { get("/sidekiq") })
2022-06-21 12:33:46 -07:00
it_behaves_like("routes for", [:administrator], -> { get("/pghero") })
2022-06-21 12:33:07 -07:00
it_behaves_like("routes for", %i[administrator moderator], -> { get("/moderation") })
end