I am trying to write integration tests that involve signing in using Omniauth, specifically Twitter. The error I get whenever running the Cucumber test is this:
When I sign in with "Twitter" features/step_definitions/steps.rb:5 undefined method
uid' for #<Hash:0x007fa891f95830> (NoMethodError) ./app/models/user.rb:26:in
from_omniauth' ./app/controllers/omniauth_callbacks_controller.rb:4:inall' ./features/step_definitions/steps.rb:6:in
/^I sign in with "(.*?)"$/' features/user_management.feature:15:in `When I sign in with "Twitter"'
Below is the code from my User model.
def self.from_omniauth(auth)
user = where(twitter_uid: auth.uid).first || check_for_non_twitter_login(auth)
user.twitter_oauth_token = auth.credentials.token
user.twitter_oauth_secret = auth.credentials.secret
user.save! if user.email != ""
user
end
def self.create_from_omniauth(auth)
create do |user|
user.provider = auth.provider
user.twitter_uid = auth.uid
user.twitter_username = auth.info.nickname
user.twitter_oauth_token = auth.credentials.token
user.twitter_oauth_secret = auth.credentials.secret
end
end
I tried changing how the uid value was accessed by changing the line in question to:
user = where(twitter_uid: auth['uid']).first || check_for_non_twitter_login(auth)
This got rid of that error and so I changed all of the mentions of data inside the auth hash to use the same method. This broke how the code worked in development (it didn't work at all) and the tests then threw up this error:
When I sign in with "Twitter" features/step_definitions/steps.rb:5 undefined method
[]' for nil:NilClass (NoMethodError) ./app/models/user.rb:56:in
block in create_from_omniauth' ./app/models/user.rb:53:increate_from_omniauth' ./app/models/user.rb:38:in
check_for_non_twitter_login' ./app/models/user.rb:26:infrom_omniauth' ./app/controllers/omniauth_callbacks_controller.rb:4:in
all' ./features/step_definitions/steps.rb:6:in/^I sign in with "(.*?)"$/' features/user_management.feature:15:in
When I sign in with "Twitter"'
Anyone know what I'm doing wrong?
Found a solution to this. Instead of using:
OmniAuth.config.mock_auth ...etc
I used:
OmniAuth.config.add_mock(:twitter, {"uid" => '12345', "credentials" => {"token" => "mytoken","secret" => "mysecret"} })
And now it all works.