Skip to content

Commit

Permalink
Merge pull request #828 from fujimura/stop-mutating-given-hash-in-add…
Browse files Browse the repository at this point in the history
…-mock

Stop mutating given hash in #add_mock
  • Loading branch information
sferik committed Dec 19, 2015
2 parents 9a9ec5a + 9669152 commit f60a58e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
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

0 comments on commit f60a58e

Please sign in to comment.