2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Admin::ImpersonateController do
|
|
|
|
|
|
|
|
it "is a subclass of AdminController" do
|
2014-09-25 23:44:48 +08:00
|
|
|
(Admin::ImpersonateController < Admin::AdminController).should == true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in as an admin' do
|
|
|
|
let!(:admin) { log_in(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'index' do
|
|
|
|
it 'returns success' do
|
|
|
|
xhr :get, :index
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'create' do
|
|
|
|
|
|
|
|
it 'requires a username_or_email parameter' do
|
2014-11-06 17:58:47 +08:00
|
|
|
-> { xhr :put, :create }.should raise_error(ActionController::ParameterMissing)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 when that user does not exist' do
|
|
|
|
xhr :post, :create, username_or_email: 'hedonismbot'
|
|
|
|
response.status.should == 404
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an invalid access error if the user can't be impersonated" do
|
|
|
|
Guardian.any_instance.expects(:can_impersonate?).with(user).returns(false)
|
|
|
|
xhr :post, :create, username_or_email: user.email
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'success' do
|
|
|
|
|
2014-11-06 17:58:47 +08:00
|
|
|
it "logs the impersonation" do
|
|
|
|
StaffActionLogger.any_instance.expects(:log_impersonate)
|
|
|
|
xhr :post, :create, username_or_email: user.username
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "changes the current user session id" do
|
|
|
|
xhr :post, :create, username_or_email: user.username
|
|
|
|
session[:current_user_id].should == user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns success" do
|
|
|
|
xhr :post, :create, username_or_email: user.email
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "also works with an email address" do
|
|
|
|
xhr :post, :create, username_or_email: user.email
|
|
|
|
session[:current_user_id].should == user.id
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|