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

Stop mutating given hash in #add_mock #828

Merged
merged 2 commits into from Dec 19, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 9 additions & 13 deletions lib/omniauth.rb
Expand Up @@ -82,19 +82,15 @@ def before_request_phase(&block)
end
end

def add_mock(provider, mock = {})
# Stringify keys recursively one level.
mock.keys.each do |key|
mock[key.to_s] = mock.delete(key)
end
mock.each_pair do |_key, val|
if val.is_a? Hash
val.keys.each do |subkey|
val[subkey.to_s] = val.delete(subkey)
end
else
next
end
def add_mock(provider, original = {})
# Create key-stringified new hash from given auth hash
mock = {}
original.each_pair do |key, val|
mock[key.to_s] = if val.is_a? Hash
Hash[val.each_pair { |k, v| [k.to_s, v] }]
else
val
end
end

# Merge with the default mock and ensure provider is correct.
Expand Down
10 changes: 9 additions & 1 deletion spec/omniauth_spec.rb
Expand Up @@ -90,7 +90,10 @@ class ExampleStrategy

describe 'mock auth' do
before do
OmniAuth.config.add_mock(:facebook, :uid => '12345', :info => {:name => 'Joe', :email => 'joe@example.com'})
@auth_hash = {:uid => '12345', :info => {:name => 'Joe', :email => 'joe@example.com'}}
@original_auth_hash = Marshal.load(Marshal.dump(@auth_hash))

OmniAuth.config.add_mock(:facebook, @auth_hash)
end
it 'default is AuthHash' do
OmniAuth.configure do |config|
Expand All @@ -109,6 +112,11 @@ class ExampleStrategy
expect(config.mock_auth[:facebook].info.email).to eq('joe@example.com')
end
end
it 'does not mutate given auth hash' do
OmniAuth.configure do
expect(@auth_hash).to eq @original_auth_hash
end
end
end
end

Expand Down