Last active
February 25, 2017 01:27
-
-
Save skamithi/5699812 to your computer and use it in GitHub Desktop.
bypassing sorcery authentication for rspec feature and cucumber tests for my external authentication setup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# feature/support/login_helper.rb | |
require 'spec/support/login_helper' | |
Before do | |
extend LoginHelper | |
@user = FactoryGirl.create(:user) | |
login_as @user | |
end | |
# @user can then be used in step definitions for a particular scenario. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#spec/support/login_helper.rb | |
# Cannot remember where I found this code. When I do, I'll post the credit where it is due. | |
module LoginHelper | |
def stub_login!(user) | |
# Stub '#login_at' in '/oauth/:provider' | |
override_once(OauthsController, :login_at) do |provider| | |
redirect_to action: :callback, provider: provider | |
end | |
# Stub `#login_from` in '/oauth/callback' | |
override_once(OauthsController, :login_from) do |provider| | |
auto_login(user) | |
after_login!(user) | |
user | |
end | |
end | |
# Test user will always login via Google | |
def login_as(user) | |
stub_login!(user) | |
visit login_path | |
click_link(I18n.t('user_sessions.new.google')) | |
end | |
end | |
def override_once(receiver, method_name, &block) | |
original_method = receiver.instance_method(method_name) | |
receiver.send(:define_method, method_name) do |*args| | |
value = instance_exec(*args, &block) | |
receiver.send(:define_method, method_name) do |*args| | |
original_method.bind(self).call(*args) | |
end | |
value | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/spec_helper.rb | |
RSpec.configure do |config| | |
config.include Sorcery::TestHelpers::Rails | |
config.include LoginHelper, type: :feature |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment