From 8fc2429496135a53b85a19a35f1a443b4d972614 Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Tue, 5 Sep 2017 20:12:45 +0100 Subject: [PATCH 1/2] FIX: make work with user_emails migration --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 837ea48..f9fbf68 100644 --- a/plugin.rb +++ b/plugin.rb @@ -104,7 +104,7 @@ class OAuth2BasicAuthenticator < ::Auth::OAuth2Authenticator if current_info result.user = User.where(id: current_info[:user_id]).first elsif SiteSetting.oauth2_email_verified? - result.user = User.where(email: Email.downcase(result.email)).first + result.user = User.find_by_email(result.email) if result.user && user_details[:user_id] ::PluginStore.set("oauth2_basic", "oauth2_basic_user_#{user_details[:user_id]}", user_id: result.user.id) end From f0151cdea6f03b54d05ab83cbd8e811d30e1b982 Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Wed, 6 Sep 2017 23:15:10 +0100 Subject: [PATCH 2/2] add spec --- spec/plugin_spec.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spec/plugin_spec.rb diff --git a/spec/plugin_spec.rb b/spec/plugin_spec.rb new file mode 100644 index 0000000..dfe7607 --- /dev/null +++ b/spec/plugin_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +# This is ugly... but it works! +# Need to load plugin.rb to avoid: +# +# NameError: +# uninitialized constant OAuth2BasicAuthenticator +# +# And need to mock various methods to avoid: +# +# NoMethodError: +# undefined method `enabled_site_setting' for main:Object +# +# etc. + +def enabled_site_setting(arg) +end + +def auth_provider(arg) +end + +def register_css(arg) +end + +require_relative '../plugin.rb' + +describe OAuth2BasicAuthenticator do + context 'after_authenticate' do + it 'finds user by email' do + authenticator = OAuth2BasicAuthenticator.new('oauth2_basic') + user = Fabricate(:user) + authenticator.expects(:fetch_user_details).returns(email: user.email) + SiteSetting.oauth2_email_verified = true + auth = { 'credentials' => { 'token': 'token' }, + 'info' => { id: 'id' }, + 'extra' => {} } + + result = authenticator.after_authenticate(auth) + + expect(result.user).to eq(user) + end + end +end