Commit 472d9047b9e0aa20fe8d3cca2d135a0ddce2ae0b
Committed by
Eduardo Silva Araújo
1 parent
ffb9d386
Exists in
colab
[Colab] Implement callbacks and initial tests for Omniauth Remote User
Signed-off-by: Heitor Reis Ribeiro <marcheing@gmail.com>
Showing
8 changed files
with
64 additions
and
7 deletions
Show diff stats
@@ -0,0 +1,16 @@ | @@ -0,0 +1,16 @@ | ||
1 | +module Users | ||
2 | + class OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
3 | + def all | ||
4 | + auth = request.env["omniauth.auth"] | ||
5 | + user = User.find_or_create_by(email: auth.info.email, name: auth.info.name, provider: auth.provider, uid: auth.uid) | ||
6 | + | ||
7 | + if user.valid? | ||
8 | + sign_in_and_redirect user | ||
9 | + else | ||
10 | + raise "Fuck you: #{user.errors.full_messages}" | ||
11 | + end | ||
12 | + end | ||
13 | + | ||
14 | + alias_method :RemoteUser, :all | ||
15 | + end | ||
16 | +end | ||
0 | \ No newline at end of file | 17 | \ No newline at end of file |
app/views/devise/shared/_links.erb
@@ -20,6 +20,6 @@ | @@ -20,6 +20,6 @@ | ||
20 | 20 | ||
21 | <%- if devise_mapping.omniauthable? %> | 21 | <%- if devise_mapping.omniauthable? %> |
22 | <%- resource_class.omniauth_providers.each do |provider| %> | 22 | <%- resource_class.omniauth_providers.each do |provider| %> |
23 | - <%= 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' %> | 23 | + <%= 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' %> |
24 | <% end -%> | 24 | <% end -%> |
25 | <% end -%> | 25 | <% end -%> |
26 | \ No newline at end of file | 26 | \ No newline at end of file |
config/initializers/devise.rb
@@ -231,6 +231,7 @@ Devise.setup do |config| | @@ -231,6 +231,7 @@ Devise.setup do |config| | ||
231 | # Add a new OmniAuth provider. Check the wiki for more information on setting | 231 | # Add a new OmniAuth provider. Check the wiki for more information on setting |
232 | # up on your models and hooks. | 232 | # up on your models and hooks. |
233 | # config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo' | 233 | # config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo' |
234 | + config.omniauth :RemoteUser | ||
234 | 235 | ||
235 | # ==> Warden configuration | 236 | # ==> Warden configuration |
236 | # If you want to use other strategies, that are not supported by Devise, or | 237 | # If you want to use other strategies, that are not supported by Devise, or |
config/routes.rb
@@ -51,7 +51,7 @@ Rails.application.routes.draw do | @@ -51,7 +51,7 @@ Rails.application.routes.draw do | ||
51 | 51 | ||
52 | 52 | ||
53 | # See comment above for devise_for | 53 | # See comment above for devise_for |
54 | - devise_for :users, only: :omniauth_callbacks | 54 | + devise_for :users, only: :omniauth_callbacks, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } |
55 | # The priority is based upon order of creation: first created -> highest priority. | 55 | # The priority is based upon order of creation: first created -> highest priority. |
56 | # See how all your routes lay out with "rake routes". | 56 | # See how all your routes lay out with "rake routes". |
57 | 57 |
features/step_definitions/user_steps.rb
@@ -31,3 +31,25 @@ When(/^I click the "(.*?)" icon$/) do |icon| | @@ -31,3 +31,25 @@ When(/^I click the "(.*?)" icon$/) do |icon| | ||
31 | find('#' + icon).click # the hashtag symbol is necessary to find the id of a HTML element | 31 | find('#' + icon).click # the hashtag symbol is necessary to find the id of a HTML element |
32 | sleep(1) #This sleep is essential to make the popup visible when we take a picture of the page | 32 | sleep(1) #This sleep is essential to make the popup visible when we take a picture of the page |
33 | end | 33 | end |
34 | + | ||
35 | +Given(/^I am logged in as a Colab user$/) do | ||
36 | + colab_user = FactoryGirl.build(:colab_user) | ||
37 | + first_name, last_name = colab_user.name.partition(' ') | ||
38 | + | ||
39 | + set_header('HTTP_REMOTE_USER', colab_user.uid) | ||
40 | + set_header('HTTP_REMOTE_USER_DATA', { | ||
41 | + 'nickname': colab_user.uid, | ||
42 | + 'name': colab_user.name, | ||
43 | + 'firstname': first_name, | ||
44 | + 'lastname': last_name, | ||
45 | + 'email': colab_user.email | ||
46 | + }.to_json) | ||
47 | +end | ||
48 | + | ||
49 | +Then(/^I should be at the Home page$/) do | ||
50 | + expect(current_path).to be("/") | ||
51 | +end | ||
52 | + | ||
53 | +Then(/^I should be logged in$/) do | ||
54 | + expect(page).to have_no_link("Sign In") | ||
55 | +end | ||
34 | \ No newline at end of file | 56 | \ No newline at end of file |
features/support/hooks.rb
1 | +require_relative 'header' | ||
2 | + | ||
1 | After do |scenario| | 3 | After do |scenario| |
2 | # Do something after each scenario. | 4 | # Do something after each scenario. |
3 | # The +scenario+ argument is optional, but | 5 | # The +scenario+ argument is optional, but |
@@ -8,11 +10,7 @@ end | @@ -8,11 +10,7 @@ end | ||
8 | 10 | ||
9 | # Run all acceptance tests on the default language | 11 | # Run all acceptance tests on the default language |
10 | Before do |scenario| | 12 | Before do |scenario| |
11 | - if defined?(page) && ! page.driver.nil? | ||
12 | - header_method = [:add_header, :header].find(&page.driver.method(:respond_to?)) | ||
13 | - page.driver.send(header_method, 'Accept-Language', I18n.default_locale) if header_method | ||
14 | - end | ||
15 | - | 13 | + set_header('Accept-Language', I18n.default_locale) |
16 | I18n.locale = I18n.default_locale | 14 | I18n.locale = I18n.default_locale |
17 | end | 15 | end |
18 | 16 |
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +Feature: Omniauth authentication | ||
2 | + In order to sign in more easily | ||
3 | + As a regular user | ||
4 | + I want to authenticate with an external provider | ||
5 | + | ||
6 | + Scenario: through Colab | ||
7 | + Given I am logged in as a Colab user | ||
8 | + And I am at the homepage | ||
9 | + And I click the Sign In link | ||
10 | + When I click the Sign in with Remote User link | ||
11 | + Then I should be at the Home page | ||
12 | + And I should be logged in | ||
0 | \ No newline at end of file | 13 | \ No newline at end of file |
spec/factories/users.rb
@@ -16,4 +16,12 @@ FactoryGirl.define do | @@ -16,4 +16,12 @@ FactoryGirl.define do | ||
16 | sequence(:id, 1) | 16 | sequence(:id, 1) |
17 | end | 17 | end |
18 | end | 18 | end |
19 | + | ||
20 | + factory :colab_user, class: User do | ||
21 | + id 2 | ||
22 | + name "Eric Clapton" | ||
23 | + email "eric@clapton.com" | ||
24 | + provider "colab" | ||
25 | + uid "eric_clapton" | ||
26 | + end | ||
19 | end | 27 | end |