2017-04-28 12:02:05 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 - present Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-10-12 06:59:38 +08:00
|
|
|
module Factories
|
|
|
|
def user_session(user, pseudonym=nil)
|
2016-12-06 06:06:14 +08:00
|
|
|
if caller.grep(/onceler\/recorder.*record!/).present?
|
|
|
|
raise "don't stub sessions in a `before(:once)` block; do it in a `before(:each)` so the stubbing works for all examples and not just the first one"
|
|
|
|
end
|
|
|
|
|
2016-10-12 06:59:38 +08:00
|
|
|
unless pseudonym
|
2017-07-21 06:10:04 +08:00
|
|
|
pseudonym = double(
|
2017-04-18 04:40:46 +08:00
|
|
|
'Pseudonym',
|
|
|
|
:record => user,
|
|
|
|
:user_id => user.id,
|
|
|
|
:user => user,
|
|
|
|
:login_count => 1,
|
|
|
|
:global_account_id => '10000000000001',
|
2018-09-20 04:06:02 +08:00
|
|
|
:sis_user_id => 'U001',
|
|
|
|
:shard => Shard.default
|
2017-04-18 04:40:46 +08:00
|
|
|
)
|
2016-10-12 06:59:38 +08:00
|
|
|
# at least one thing cares about the id of the pseudonym... using the
|
|
|
|
# object_id should make it unique (but obviously things will fail if
|
|
|
|
# it tries to load it from the db.)
|
2017-07-21 06:10:04 +08:00
|
|
|
allow(pseudonym).to receive(:id).and_return(pseudonym.object_id)
|
|
|
|
allow(pseudonym).to receive(:unique_id).and_return('unique_id')
|
2016-10-12 06:59:38 +08:00
|
|
|
end
|
2015-07-08 03:04:12 +08:00
|
|
|
|
2017-07-21 06:10:04 +08:00
|
|
|
session = double('PseudonymSession', :record => pseudonym, :session_credentials => nil)
|
2015-07-08 03:04:12 +08:00
|
|
|
|
2017-07-21 06:10:04 +08:00
|
|
|
@session_stubbed = true
|
|
|
|
allow(PseudonymSession).to receive(:find).and_wrap_original do |original|
|
|
|
|
next original.call unless @session_stubbed
|
|
|
|
session
|
|
|
|
end
|
2018-07-27 10:12:42 +08:00
|
|
|
session
|
2016-10-12 06:59:38 +08:00
|
|
|
end
|
2015-07-08 03:04:12 +08:00
|
|
|
|
2016-10-12 06:59:38 +08:00
|
|
|
def remove_user_session
|
2017-07-21 06:10:04 +08:00
|
|
|
@session_stubbed = false
|
2016-10-12 06:59:38 +08:00
|
|
|
end
|
2015-07-08 03:04:12 +08:00
|
|
|
end
|