Retrospring/spec/integration/role_constrained_routes_spe...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.8 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
# 302 = redirect to login
# 404 = no user found -- we have a fallback route if something could not be matched
result = begin; subject; rescue ActionController::RoutingError; :no_route; end
expect(result).to be_in [302, 404, :no_route]
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-08-20 12:31:40 -07:00
it_behaves_like("routes for", %i[administrator moderator], -> { get("/moderation/reports") })
end