From 42c8f5c0d09d6f222f2ec995d169a6cfdcfb2fed Mon Sep 17 00:00:00 2001 From: Daniel Miranda Date: Wed, 29 Apr 2015 16:34:07 -0300 Subject: [PATCH] [Colab] Implement callbacks and initial tests for Omniauth Remote User --- app/controllers/users/omniauth_callbacks_controller.rb | 16 ++++++++++++++++ app/views/devise/shared/_links.erb | 2 +- config/initializers/devise.rb | 1 + config/routes.rb | 2 +- features/step_definitions/user_steps.rb | 22 ++++++++++++++++++++++ features/support/header.rb | 9 +++++++++ features/support/hooks.rb | 8 +++----- features/users/omniauth.feature | 12 ++++++++++++ spec/factories/users.rb | 8 ++++++++ 9 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 app/controllers/users/omniauth_callbacks_controller.rb create mode 100644 features/support/header.rb create mode 100644 features/users/omniauth.feature diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..7f20631 --- /dev/null +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -0,0 +1,16 @@ +module Users + class OmniauthCallbacksController < Devise::OmniauthCallbacksController + def all + auth = request.env["omniauth.auth"] + user = User.find_or_create_by(email: auth.info.email, name: auth.info.name, provider: auth.provider, uid: auth.uid) + + if user.valid? + sign_in_and_redirect user + else + raise "Fuck you: #{user.errors.full_messages}" + end + end + + alias_method :RemoteUser, :all + end +end \ No newline at end of file diff --git a/app/views/devise/shared/_links.erb b/app/views/devise/shared/_links.erb index e243d98..c42357d 100644 --- a/app/views/devise/shared/_links.erb +++ b/app/views/devise/shared/_links.erb @@ -20,6 +20,6 @@ <%- if devise_mapping.omniauthable? %> <%- resource_class.omniauth_providers.each do |provider| %> - <%= link_to t('.sign_in_with_provider', :provider => provider.to_s.titleize, :default => "Sign in with #{provider.to_s.titleize}"), omniauth_authorize_path(resource_name, provider), class: 'btn btn-info' %> + <%= link_to t('.sign_in_with_provider', :provider => provider.to_s.titleize, :default => "Sign in with #{provider.to_s.titleize}"), user_omniauth_authorize_path(provider), class: 'btn btn-info' %> <% end -%> <% end -%> \ No newline at end of file diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index cfa6393..48a2aa2 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -231,6 +231,7 @@ Devise.setup do |config| # Add a new OmniAuth provider. Check the wiki for more information on setting # up on your models and hooks. # config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo' + config.omniauth :RemoteUser # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or diff --git a/config/routes.rb b/config/routes.rb index aaac08f..72b0cb5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,7 +52,7 @@ Rails.application.routes.draw do # See comment above for devise_for - devise_for :users, only: :omniauth_callbacks + devise_for :users, only: :omniauth_callbacks, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb index 7a2ae88..1223a10 100644 --- a/features/step_definitions/user_steps.rb +++ b/features/step_definitions/user_steps.rb @@ -31,3 +31,25 @@ When(/^I click the "(.*?)" icon$/) do |icon| find('#' + icon).click # the hashtag symbol is necessary to find the id of a HTML element sleep(1) #This sleep is essential to make the popup visible when we take a picture of the page end + +Given(/^I am logged in as a Colab user$/) do + colab_user = FactoryGirl.build(:colab_user) + first_name, last_name = colab_user.name.partition(' ') + + set_header('HTTP_REMOTE_USER', colab_user.uid) + set_header('HTTP_REMOTE_USER_DATA', { + 'nickname': colab_user.uid, + 'name': colab_user.name, + 'firstname': first_name, + 'lastname': last_name, + 'email': colab_user.email + }.to_json) +end + +Then(/^I should be at the Home page$/) do + expect(current_path).to be("/") +end + +Then(/^I should be logged in$/) do + expect(page).to have_no_link("Sign In") +end \ No newline at end of file diff --git a/features/support/header.rb b/features/support/header.rb new file mode 100644 index 0000000..f5c1356 --- /dev/null +++ b/features/support/header.rb @@ -0,0 +1,9 @@ +def set_header(key, value) + header_method = nil + if defined?(page) && ! page.driver.nil? + header_method = [:add_header, :header].find(&page.driver.method(:respond_to?)) + end + + raise StandardError.new("No header setting method available in current driver: #{page.driver}") unless header_method + page.driver.send(header_method, key, value) +end \ No newline at end of file diff --git a/features/support/hooks.rb b/features/support/hooks.rb index 7ff442d..2606ea6 100644 --- a/features/support/hooks.rb +++ b/features/support/hooks.rb @@ -1,3 +1,5 @@ +require_relative 'header' + After do |scenario| # Do something after each scenario. # The +scenario+ argument is optional, but @@ -8,11 +10,7 @@ end # Run all acceptance tests on the default language Before do |scenario| - if defined?(page) && ! page.driver.nil? - header_method = [:add_header, :header].find(&page.driver.method(:respond_to?)) - page.driver.send(header_method, 'Accept-Language', I18n.default_locale) if header_method - end - + set_header('Accept-Language', I18n.default_locale) I18n.locale = I18n.default_locale end diff --git a/features/users/omniauth.feature b/features/users/omniauth.feature new file mode 100644 index 0000000..201990b --- /dev/null +++ b/features/users/omniauth.feature @@ -0,0 +1,12 @@ +Feature: Omniauth authentication + In order to sign in more easily + As a regular user + I want to authenticate with an external provider + + Scenario: through Colab + Given I am logged in as a Colab user + And I am at the homepage + And I click the Sign In link + When I click the Sign in with Remote User link + Then I should be at the Home page + And I should be logged in \ No newline at end of file diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 7420ff3..63eadf0 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -16,4 +16,12 @@ FactoryGirl.define do sequence(:id, 1) end end + + factory :colab_user, class: User do + id 2 + name "Eric Clapton" + email "eric@clapton.com" + provider "colab" + uid "eric_clapton" + end end -- libgit2 0.21.2