Commit 8297b72b45c20ce6c91ed64cabf68bc84c048816

Authored by Braulio Bhavamitra
2 parents d7c346f7 09fe0be7

Merge branch 'fix_oauth_client' into 'master'

Fix oauth client plugin

Fix broken tests: https://travis-ci.org/vfcosta/noosfero/jobs/104698347#L1254
Downgrade to a working version of omniauth-oauth2: https://github.com/intridea/omniauth-oauth2/issues/81

See merge request !775
plugins/oauth_client/Gemfile
1 gem 'omniauth', '~> 1.2.2' 1 gem 'omniauth', '~> 1.2.2'
2 gem 'omniauth-facebook', '~> 2.0.0' 2 gem 'omniauth-facebook', '~> 2.0.0'
3 gem "omniauth-google-oauth2", '~> 0.2.6' 3 gem "omniauth-google-oauth2", '~> 0.2.6'
  4 +gem "omniauth-oauth2", '~> 1.3.1'
plugins/oauth_client/lib/ext/user.rb
@@ -6,19 +6,32 @@ class User @@ -6,19 +6,32 @@ class User
6 has_many :oauth_providers, through: :oauth_auths, source: :provider 6 has_many :oauth_providers, through: :oauth_auths, source: :provider
7 7
8 after_create :activate_oauth_user 8 after_create :activate_oauth_user
  9 + after_create :store_oauth_providers
  10 +
  11 + def initialize_with_oauth_client(attributes = {}, options = {})
  12 + @oauth_providers = attributes.delete(:oauth_providers) || []
  13 + initialize_without_oauth_client(attributes, options)
  14 + end
  15 + alias_method_chain :initialize, :oauth_client
  16 +
  17 + def store_oauth_providers
  18 + @oauth_providers.each do |provider|
  19 + self.person.oauth_auths.create!(profile: self.person, provider: provider, enabled: true)
  20 + end
  21 + end
9 22
10 def activate_oauth_user 23 def activate_oauth_user
11 - self.activate if oauth_providers.present? 24 + self.activate if oauth_providers.present? || @oauth_providers.present?
12 end 25 end
13 26
14 def password_required_with_oauth? 27 def password_required_with_oauth?
15 - password_required_without_oauth? && oauth_providers.empty? 28 + password_required_without_oauth? && oauth_providers.empty? && @oauth_providers.blank?
16 end 29 end
17 30
18 alias_method_chain :password_required?, :oauth 31 alias_method_chain :password_required?, :oauth
19 32
20 def make_activation_code_with_oauth 33 def make_activation_code_with_oauth
21 - oauth_providers.blank? ? make_activation_code_without_oauth : nil 34 + @oauth_providers.blank? && oauth_providers.blank? ? make_activation_code_without_oauth : nil
22 end 35 end
23 36
24 alias_method_chain :make_activation_code, :oauth 37 alias_method_chain :make_activation_code, :oauth
plugins/oauth_client/test/unit/user_test.rb
@@ -11,6 +11,11 @@ class UserTest < ActiveSupport::TestCase @@ -11,6 +11,11 @@ class UserTest < ActiveSupport::TestCase
11 User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [provider]) 11 User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [provider])
12 end 12 end
13 13
  14 + should 'associate the oauth provider with the created user' do
  15 + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [provider])
  16 + assert_equal user.oauth_providers.reload, [provider]
  17 + end
  18 +
14 should 'password is required if there is a oauth provider' do 19 should 'password is required if there is a oauth provider' do
15 user = User.new(:email => 'testoauth@example.com', :login => 'testoauth') 20 user = User.new(:email => 'testoauth@example.com', :login => 'testoauth')
16 user.save 21 user.save