Commit e9c353c4c26f973935d2bb3177023d7e27149c2c

Authored by Daniela Feitosa
1 parent 5607f066

Allow multi environment on remote user plugin

Adding tests for multi env remote user

    Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
    Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
    Signed-off-by: Macartur Sousa <macartur.sc@gmail.com

Closes !753
plugins/remote_user/lib/remote_user_plugin.rb
@@ -28,9 +28,9 @@ class RemoteUserPlugin &lt; Noosfero::Plugin @@ -28,9 +28,9 @@ class RemoteUserPlugin &lt; Noosfero::Plugin
28 end 28 end
29 29
30 if !logged_in? 30 if !logged_in?
31 - self.current_user = User.find_by_login(remote_user) 31 + self.current_user = User.where(environment_id: environment, login: remote_user).first
32 unless self.current_user 32 unless self.current_user
33 - 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)) 33 + 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))
34 self.current_user.activate 34 self.current_user.activate
35 end 35 end
36 self.current_user.save! 36 self.current_user.save!
@@ -39,9 +39,9 @@ class RemoteUserPlugin &lt; Noosfero::Plugin @@ -39,9 +39,9 @@ class RemoteUserPlugin &lt; Noosfero::Plugin
39 self.current_user.forget_me 39 self.current_user.forget_me
40 reset_session 40 reset_session
41 41
42 - self.current_user = User.find_by_login(remote_user) 42 + self.current_user = User.where(environment_id: environment, login: remote_user).first
43 unless self.current_user 43 unless self.current_user
44 - 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)) 44 + 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))
45 self.current_user.activate 45 self.current_user.activate
46 end 46 end
47 self.current_user.save! 47 self.current_user.save!
plugins/remote_user/test/functional/remote_user_plugin_test.rb
@@ -6,6 +6,10 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -6,6 +6,10 @@ class AccountControllerTest &lt; ActionController::TestCase
6 @environment.enabled_plugins = ['RemoteUserPlugin'] 6 @environment.enabled_plugins = ['RemoteUserPlugin']
7 @environment.save 7 @environment.save
8 8
  9 + @another_environment = Environment.new(name: "AnotherEnvironment")
  10 + @another_environment.enabled_plugins = ['RemoteUserPlugin']
  11 + @another_environment.save
  12 +
9 @controller = AccountController.new 13 @controller = AccountController.new
10 @request = ActionController::TestRequest.new 14 @request = ActionController::TestRequest.new
11 @response = ActionController::TestResponse.new 15 @response = ActionController::TestResponse.new
@@ -136,4 +140,26 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -136,4 +140,26 @@ class AccountControllerTest &lt; ActionController::TestCase
136 assert session[:user].blank? 140 assert session[:user].blank?
137 assert_response 404 141 assert_response 404
138 end 142 end
  143 +
  144 + should "create an user in the correct environment" do
  145 + @controller.stubs(:environment).returns(@another_environment)
  146 + @request.env["HTTP_REMOTE_USER"] = "testuser"
  147 +
  148 + get :index
  149 + user = User.last
  150 + assert_equal user.environment, @another_environment
  151 + end
  152 +
  153 + should "create an user in both environments" do
  154 + user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
  155 + @controller.stubs(:environment).returns(@another_environment)
  156 + @request.env["HTTP_REMOTE_USER"] = "testuser"
  157 +
  158 + users = User.where(:login => 'testuser')
  159 + assert_equal users.count, 1
  160 +
  161 + get :index
  162 + users = User.where(:login => 'testuser')
  163 + assert_equal users.count, 2
  164 + end
139 end 165 end