From e9c353c4c26f973935d2bb3177023d7e27149c2c Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Fri, 18 Dec 2015 15:29:05 -0300 Subject: [PATCH] Allow multi environment on remote user plugin --- plugins/remote_user/lib/remote_user_plugin.rb | 8 ++++---- plugins/remote_user/test/functional/remote_user_plugin_test.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/plugins/remote_user/lib/remote_user_plugin.rb b/plugins/remote_user/lib/remote_user_plugin.rb index 5e013df..6f8cfa5 100644 --- a/plugins/remote_user/lib/remote_user_plugin.rb +++ b/plugins/remote_user/lib/remote_user_plugin.rb @@ -28,9 +28,9 @@ class RemoteUserPlugin < Noosfero::Plugin end if !logged_in? - self.current_user = User.find_by_login(remote_user) + self.current_user = User.where(environment_id: environment, login: remote_user).first unless self.current_user - self.current_user = User.create!(:login => remote_user, :email => remote_user_email, :name => remote_user_name, :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) + self.current_user = User.create!(:environment => environment, :login => remote_user, :email => remote_user_email, :name => remote_user_name, :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) self.current_user.activate end self.current_user.save! @@ -39,9 +39,9 @@ class RemoteUserPlugin < Noosfero::Plugin self.current_user.forget_me reset_session - self.current_user = User.find_by_login(remote_user) + self.current_user = User.where(environment_id: environment, login: remote_user).first unless self.current_user - self.current_user = User.create!(:login => remote_user, :email => remote_user_email, :name => remote_user_name, :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) + self.current_user = User.create!(:environment => environment, :login => remote_user, :email => remote_user_email, :name => remote_user_name, :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) self.current_user.activate end self.current_user.save! diff --git a/plugins/remote_user/test/functional/remote_user_plugin_test.rb b/plugins/remote_user/test/functional/remote_user_plugin_test.rb index d544cc7..2b527f3 100644 --- a/plugins/remote_user/test/functional/remote_user_plugin_test.rb +++ b/plugins/remote_user/test/functional/remote_user_plugin_test.rb @@ -6,6 +6,10 @@ class AccountControllerTest < ActionController::TestCase @environment.enabled_plugins = ['RemoteUserPlugin'] @environment.save + @another_environment = Environment.new(name: "AnotherEnvironment") + @another_environment.enabled_plugins = ['RemoteUserPlugin'] + @another_environment.save + @controller = AccountController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @@ -136,4 +140,26 @@ class AccountControllerTest < ActionController::TestCase assert session[:user].blank? assert_response 404 end + + should "create an user in the correct environment" do + @controller.stubs(:environment).returns(@another_environment) + @request.env["HTTP_REMOTE_USER"] = "testuser" + + get :index + user = User.last + assert_equal user.environment, @another_environment + end + + should "create an user in both environments" do + user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') + @controller.stubs(:environment).returns(@another_environment) + @request.env["HTTP_REMOTE_USER"] = "testuser" + + users = User.where(:login => 'testuser') + assert_equal users.count, 1 + + get :index + users = User.where(:login => 'testuser') + assert_equal users.count, 2 + end end -- libgit2 0.21.2