2019-04-15 10:17:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-22 21:19:55 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-04 03:08:05 +00:00
|
|
|
RSpec.describe Admin::ImpersonationsController do
|
2016-04-22 21:19:55 +00:00
|
|
|
let(:impersonator) { create(:admin) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe "DELETE destroy" do
|
|
|
|
context "when not signed in" do
|
|
|
|
it "redirects to the sign in page" do
|
|
|
|
delete :destroy
|
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when signed in" do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when not impersonating" do
|
|
|
|
it "responds with status 404" do
|
|
|
|
delete :destroy
|
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-04-22 21:19:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't sign us in" do
|
|
|
|
delete :destroy
|
|
|
|
|
|
|
|
expect(warden.user).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when impersonating" do
|
|
|
|
before do
|
|
|
|
session[:impersonator_id] = impersonator.id
|
|
|
|
end
|
|
|
|
|
2016-04-22 21:46:47 +00:00
|
|
|
context "when the impersonator is not admin (anymore)" do
|
2016-04-22 21:19:55 +00:00
|
|
|
before do
|
|
|
|
impersonator.admin = false
|
|
|
|
impersonator.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "responds with status 404" do
|
|
|
|
delete :destroy
|
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-04-22 21:19:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't sign us in as the impersonator" do
|
|
|
|
delete :destroy
|
|
|
|
|
|
|
|
expect(warden.user).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the impersonator is admin" do
|
|
|
|
context "when the impersonator is blocked" do
|
|
|
|
before do
|
|
|
|
impersonator.block!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "responds with status 404" do
|
|
|
|
delete :destroy
|
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-04-22 21:19:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't sign us in as the impersonator" do
|
|
|
|
delete :destroy
|
|
|
|
|
|
|
|
expect(warden.user).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the impersonator is not blocked" do
|
2016-11-18 04:21:02 +00:00
|
|
|
shared_examples_for "successfully stops impersonating" do
|
|
|
|
it "redirects to the impersonated user's page" do
|
|
|
|
expect(Gitlab::AppLogger).to receive(:info).with("User #{impersonator.username} has stopped impersonating #{user.username}").and_call_original
|
2016-08-23 08:06:54 +00:00
|
|
|
|
2016-11-18 04:21:02 +00:00
|
|
|
delete :destroy
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_user_path(user))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "signs us in as the impersonator" do
|
|
|
|
delete :destroy
|
2016-04-22 21:19:55 +00:00
|
|
|
|
2016-11-18 04:21:02 +00:00
|
|
|
expect(warden.user).to eq(impersonator)
|
|
|
|
end
|
2016-04-22 21:19:55 +00:00
|
|
|
end
|
|
|
|
|
2016-11-18 04:21:02 +00:00
|
|
|
# base case
|
|
|
|
it_behaves_like "successfully stops impersonating"
|
|
|
|
|
|
|
|
context "and the user has a temporary oauth e-mail address" do
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:temp_oauth_email?).and_return(true)
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
end
|
2016-04-22 21:19:55 +00:00
|
|
|
|
2016-11-18 04:21:02 +00:00
|
|
|
it_behaves_like "successfully stops impersonating"
|
2016-04-22 21:19:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|