Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebuild list of current user's virtual groups stored in user_session #4101

Merged
merged 2 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/samvera/persona/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# Copied here in full from samvera-persona 0.1.7
# Added prepend_view_path
# Added LDAP group recalculation in impersonate and stop_impersonating
# TODO: Determine why this is necessary and remove this override
module Samvera
class Persona::UsersController < ApplicationController
Expand Down Expand Up @@ -77,11 +78,15 @@ def update
def impersonate
user = User.find(params[:id])
impersonate_user(user)
# Recalculate user_session[:virtual_groups]
user_session[:virtual_groups] = current_user.ldap_groups
redirect_to main_app.root_path
end

def stop_impersonating
stop_impersonating_user
# Recalculate user_session[:virtual_groups]
user_session[:virtual_groups] = current_user.ldap_groups
redirect_to main_app.persona_users_path, notice: t('.become.over')
end

Expand Down
101 changes: 101 additions & 0 deletions spec/controllers/samvera/persona/user_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'rails_helper'

RSpec.describe Samvera::Persona::UsersController, type: :controller do
describe "#index" do
let(:user) {FactoryBot.create(:admin)}
before do
sign_in(user)
end

it "is successful" do
get :index
expect(response).to be_successful
expect(assigns[:presenter]).to be_kind_of Samvera::Persona::UsersPresenter
end
end

context 'as an anonymous user' do
let(:user) { FactoryBot.create(:user) }

describe 'DELETE #destroy' do
subject { User.find_by(id: user.id) }

before { delete :destroy, params: { id: user.id } }

it "doesn't delete the user and redirects to login" do
expect(subject).not_to be_nil
expect(response).to redirect_to controller.main_app.new_user_session_path
end
end
end

context 'as an admin user' do
let(:user) { FactoryBot.create(:admin) }
before { sign_in user }

describe 'DELETE #destroy' do
before { delete :destroy, params: { id: user.to_param } }

it "deletes the user and displays success message" do
expect{ User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect(flash[:notice]).to match "has been successfully deleted."
end

it "deletes the user without paranoia gem" do
# expect{User.with_deleted.find(user.id)}.to_not raise_error
expect(User.exists?(user.id)).to eq false
end
end
end

context 'pretender' do
let(:admin) { FactoryBot.create(:admin) }
let(:ldap_groups) { [] }
before do
sign_in admin
allow(current_user).to receive(:ldap_groups).and_return(ldap_groups)
end

describe 'POST #impersonate' do
let(:current_user) { FactoryBot.create(:user) }

it 'allows you to impersonate another user' do
post :impersonate, params: { id: current_user.id }
expect(response).to redirect_to(controller.main_app.root_path)
end

context 'with external groups' do
let(:ldap_groups) { ['ldap_group1', 'ldap_group2'] }

before do
allow(User).to receive(:find).with(current_user.to_param).and_return(current_user)
end

it 'sets the virtual groups for the new user' do
expect { post :impersonate, params: { id: current_user.id }}.to change { @controller.user_session[:virtual_groups] }.from(nil).to(ldap_groups)
end
end
end

describe 'POST #stop_impersonating' do
let(:current_user) { FactoryBot.create(:user) }

it 'allows you to stop impersonating' do
post :stop_impersonating, params: { id: current_user.id }
expect(response).to redirect_to(controller.main_app.persona_users_path)
end

context 'with external groups' do
let(:old_ldap_groups) { ['ldap_group1', 'ldap_group2'] }

before do
@controller.user_session[:virtual_groups] = old_ldap_groups
end

it 'sets the virtual groups for the new user' do
expect { post :stop_impersonating, params: { id: current_user.id }}.to change { @controller.user_session[:virtual_groups] }.from(old_ldap_groups).to([])
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
username { [Faker::Name.last_name.gsub("'",""),Faker::Name.first_name.gsub("'","")].join('.').downcase }
password { 'testing123' }

factory :administrator do
factory :administrator, aliases: [:admin] do
after(:create) do |user|
begin
Avalon::RoleControls.add_user_role(user.user_key, 'administrator')
Expand Down
4 changes: 2 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@

describe "#ldap_groups" do
before do
Avalon::GROUP_LDAP = Net::LDAP.new unless defined?(Avalon::GROUP_LDAP)
Avalon::GROUP_LDAP_TREE = 'ou=Test,dc=avalonmediasystem,dc=org'.freeze unless defined?(Avalon::GROUP_LDAP_TREE)
stub_const("Avalon::GROUP_LDAP", Net::LDAP.new)
stub_const("Avalon::GROUP_LDAP_TREE", 'ou=Test,dc=avalonmediasystem,dc=org'.freeze)
end
it "should return [] if LDAP is not configured" do
hide_const("Avalon::GROUP_LDAP")
Expand Down