diff --git a/plugins/oauth_client/lib/ext/user.rb b/plugins/oauth_client/lib/ext/user.rb index 014013b..3ca6649 100644 --- a/plugins/oauth_client/lib/ext/user.rb +++ b/plugins/oauth_client/lib/ext/user.rb @@ -28,11 +28,7 @@ class User end def make_activation_code_with_oauth - if oauth_providers.blank? - nil - else - make_activation_code_without_oauth - end + oauth_providers.blank? ? make_activation_code_without_oauth : nil end alias_method_chain :make_activation_code, :oauth diff --git a/plugins/oauth_client/lib/oauth_client_plugin.rb b/plugins/oauth_client/lib/oauth_client_plugin.rb index 924376e..2ffa16b 100644 --- a/plugins/oauth_client/lib/oauth_client_plugin.rb +++ b/plugins/oauth_client/lib/oauth_client_plugin.rb @@ -20,7 +20,7 @@ class OauthClientPlugin < Noosfero::Plugin proc do unless (plugin.context.params[:user]||{})[:oauth_providers].blank? - render :partial => 'oauth_signup' + render :partial => 'account/oauth_signup' else '' end diff --git a/plugins/oauth_client/test/test_helper.rb b/plugins/oauth_client/test/test_helper.rb new file mode 100644 index 0000000..cca1fd3 --- /dev/null +++ b/plugins/oauth_client/test/test_helper.rb @@ -0,0 +1 @@ +require File.dirname(__FILE__) + '/../../../test/test_helper' diff --git a/plugins/oauth_client/test/unit/oauth_client_plugin_test.rb b/plugins/oauth_client/test/unit/oauth_client_plugin_test.rb new file mode 100644 index 0000000..db6d8a9 --- /dev/null +++ b/plugins/oauth_client/test/unit/oauth_client_plugin_test.rb @@ -0,0 +1,74 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class OauthClientPluginTest < ActiveSupport::TestCase + + def setup + @plugin = OauthClientPlugin.new + @params = {} + @plugin.stubs(:context).returns(self) + @environment = Environment.default + end + + attr_reader :params, :plugin, :environment + + should 'has extra contents for login' do + assert plugin.login_extra_contents + end + + should 'has no signup extra contents if no provider was enabled' do + assert_equal '', instance_eval(&plugin.signup_extra_contents) + end + + should 'has signup extra contents if there is enabled providers' do + params[:user] = {:oauth_providers => [:provider]} + expects(:render).with(:partial => 'account/oauth_signup').once + instance_eval(&plugin.signup_extra_contents) + end + + should 'list enabled providers' do + settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin) + providers = {:test => {:enabled => true}, :test2 => {:enabled => false}} + settings.set_setting(:providers, providers) + assert_equal({:test => {:enabled => true}}, plugin.enabled_providers) + end + + should 'define before filter for account controller' do + assert plugin.account_controller_filters + end + + should 'raise error if oauth email was changed' do + request = mock + stubs(:request).returns(request) + request.expects(:post?).returns(true) + stubs(:session).returns({:oauth_email => 'test@example.com'}) + params[:user] = {:email => 'test2@example.com'} + assert_raises RuntimeError do + instance_eval(&plugin.account_controller_filters[:block]) + end + end + + should 'do not raise error if oauth email was not changed' do + request = mock + stubs(:request).returns(request) + request.expects(:post?).returns(true) + stubs(:session).returns({:oauth_email => 'test@example.com'}) + params[:user] = {:email => 'test@example.com'} + instance_eval(&plugin.account_controller_filters[:block]) + end + + should 'do not raise error if oauth email is not set' do + request = mock + stubs(:request).returns(request) + request.expects(:post?).returns(true) + stubs(:session).returns({}) + instance_eval(&plugin.account_controller_filters[:block]) + end + + should 'do not raise error if it is not a post' do + request = mock + stubs(:request).returns(request) + request.expects(:post?).returns(false) + instance_eval(&plugin.account_controller_filters[:block]) + end + +end diff --git a/plugins/oauth_client/test/unit/user_test.rb b/plugins/oauth_client/test/unit/user_test.rb new file mode 100644 index 0000000..be46ef5 --- /dev/null +++ b/plugins/oauth_client/test/unit/user_test.rb @@ -0,0 +1,49 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class UserTest < ActiveSupport::TestCase + + should 'find with omniauth params' do + user = fast_create(User) + user.settings[:oauth_providers] = [:test => {}] + user.save! + auth = {:info => OpenStruct.new({:email => user.email})} + assert_equal user, User.find_with_omniauth(OpenStruct.new(auth)) + end + + should 'do not return user if there is no provider' do + user = fast_create(User) + auth = {:info => OpenStruct.new({:email => user.email})} + assert_equal nil, User.find_with_omniauth(OpenStruct.new(auth)) + end + + should 'password is not required if there is a oauth provider' do + User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) + end + + should 'password is required if there is a oauth provider' do + user = User.new(:email => 'testoauth@example.com', :login => 'testoauth') + user.save + assert user.errors[:password].present? + end + + should 'activate user when created with oauth' do + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) + assert user.activated? + end + + should 'not activate user when created without oauth' do + user = fast_create(User) + assert !user.activated? + end + + should 'not make activation code when created with oauth' do + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) + assert !user.activation_code + end + + should 'make activation code when created without oauth' do + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :password => 'test', :password_confirmation => 'test') + assert user.activation_code + end + +end -- libgit2 0.21.2