cirandas.net

ref: master

plugins/remote_user/test/functional/remote_user_plugin_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require 'test_helper'

class AccountControllerTest < ActionController::TestCase
  def setup
    @controller = AccountController.new

    @environment = Environment.default
    @environment.enabled_plugins = ['RemoteUserPlugin']
    @environment.save

    @another_environment = Environment.new(name: "AnotherEnvironment")
    @another_environment.enabled_plugins = ['RemoteUserPlugin']
    @another_environment.save
  end

  should 'not authenticate user if there is no remote user' do
    get :index
    assert_nil session[:user]
  end

  should 'authenticate user if its a valid remote user' do
    user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
    user.activate
    @request.env["HTTP_REMOTE_USER"] = user.login
    get :index
    assert session[:user]
  end

  should 'authenticate another user if the remote user doesnt belong to the current user' do
    user1 = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
    user1.activate
    user2 = create_user('anotheruser', :email => 'anotheruser@example.com', :password => 'test', :password_confirmation => 'test')
    user2.activate

    login_as user1.login
    assert_equal user1.id, session[:user]

    @request.env["HTTP_REMOTE_USER"] = user2.login
    get :index

    assert_equal user2.id, session[:user]
  end

  should 'create a new user with remote_user_data if the remote user does not exist' do
    User.destroy_all

    assert_equal 0, User.count

    @request.env["HTTP_REMOTE_USER"] = "testuser"
    @request.env["CONTENT_TYPE"] = "application/json"
    @request.env["HTTP_REMOTE_USER_DATA"] = '{"email":"testuser@domain.com", "name":"Test User"}'
    get :index

    assert_equal 1, User.count
    assert_equal "testuser", User.last.login
    assert_equal true, User.last.activated?
    assert_equal User.last.id, session[:user]
    assert_equal "Test User", User.last.name
    assert_equal "testuser@domain.com", User.last.email
  end

  should 'create a new user with remote_user_data even if there is a logged user but the remote user is different' do
    users = User.count

    user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
    user.activate

    login_as user.login

    @request.env["HTTP_REMOTE_USER"] = 'another_user'
    @request.env["CONTENT_TYPE"] = "application/json"
    @request.env["HTTP_REMOTE_USER_DATA"] = '{"email":"another_user@domain.com", "name":"Another User"}'
    get :index

    assert_equal users + 2, User.count
    assert_equal "another_user", User.last.login
    assert_equal true, User.last.activated?
    assert_equal User.last.id, session[:user]
    assert_equal "Another User", User.last.name
    assert_equal "another_user@domain.com", User.last.email
  end

  should 'create a new user without remote_user_data if the remote user does not exist' do
    User.destroy_all

    assert_equal 0, User.count

    @request.env["HTTP_REMOTE_USER"] = "testuser"
    get :index

    assert_equal 1, User.count
    assert_equal "testuser", User.last.login
    assert_equal true, User.last.activated?
    assert_equal User.last.id, session[:user]
    assert_equal "testuser", User.last.name
    assert_equal "testuser@remote.user", User.last.email
  end

  should 'create a new user without remote_user_data even if there is a logged user but the remote user is different' do
    users = User.count

    user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
    user.activate

    login_as user.login

    @request.env["HTTP_REMOTE_USER"] = 'another_user'
    get :index

    assert_equal users + 2, User.count
    assert_equal "another_user", User.last.login
    assert_equal true, User.last.activated?
    assert_equal User.last.id, session[:user]
    assert_equal "another_user", User.last.name
    assert_equal "another_user@remote.user", User.last.email
  end

  should 'logout if there is a current logged user but not a remote user' do
    user1 = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
    user1.activate

    login_as user1.login

    get :index

    assert session[:user].blank?

    @request.env["HTTP_REMOTE_USER"] = ""
    get :index

    assert session[:user].blank?
  end

  should 'not create a new user if his informations is invalid' do
    @request.env["HTTP_REMOTE_USER"] = "*%&invalid user name&%*"
    get :index

    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