2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Admin::ImpersonateController do
|
2022-11-02 23:42:44 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:another_admin) { Fabricate(:admin) }
|
2018-06-11 00:47:14 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
describe "#index" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2018-06-11 00:47:14 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
it "returns success" do
|
|
|
|
get "/admin/impersonate.json"
|
2018-06-11 00:47:14 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2018-06-11 00:47:14 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "impersonation inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
2018-06-11 00:47:14 -04:00
|
|
|
get "/admin/impersonate.json"
|
2022-11-02 23:42:44 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
2018-06-11 00:47:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "impersonation inaccessible"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "impersonation inaccessible"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#create" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
2018-06-11 00:47:14 -04:00
|
|
|
it "requires a username_or_email parameter" do
|
|
|
|
post "/admin/impersonate.json"
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when that user does not exist" do
|
|
|
|
post "/admin/impersonate.json", params: { username_or_email: "hedonismbot" }
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an invalid access error if the user can't be impersonated" do
|
|
|
|
post "/admin/impersonate.json", params: { username_or_email: another_admin.email }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with success" do
|
2018-06-11 00:47:14 -04:00
|
|
|
it "succeeds and logs the impersonation" do
|
|
|
|
expect do
|
|
|
|
post "/admin/impersonate.json", params: { username_or_email: user.username }
|
|
|
|
end.to change { UserHistory.where(action: UserHistory.actions[:impersonate]).count }.by(1)
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "also works with an email address" do
|
|
|
|
post "/admin/impersonate.json", params: { username_or_email: user.email }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-11-02 23:42:44 -04:00
|
|
|
|
|
|
|
shared_examples "impersonation not allowed" do
|
|
|
|
it "prevents impersonation with a with 404 response" do
|
|
|
|
expect do
|
|
|
|
post "/admin/impersonate.json", params: { username_or_email: user.username }
|
|
|
|
end.not_to change { UserHistory.where(action: UserHistory.actions[:impersonate]).count }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(session[:current_user_id]).to eq(current_user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "impersonation not allowed" do
|
|
|
|
let(:current_user) { moderator }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "impersonation not allowed" do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
|
|
|
end
|
2018-06-11 00:47:14 -04:00
|
|
|
end
|
|
|
|
end
|