summaryrefslogtreecommitdiffstats
path: root/test/integration/sudo_mode_test.rb
blob: 07a4d9e9e4464187f838fda7435f8ed9525f4162 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# frozen_string_literal: true

require_relative '../test_helper'

class SudoModeTest < Redmine::IntegrationTest
  fixtures :projects, :members, :member_roles, :roles, :users, :email_addresses

  def setup
    Redmine::SudoMode.stubs(:enabled?).returns(true)
  end

  def teardown
    travel_back
  end

  def test_sudo_mode_should_be_active_after_login
    log_user("admin", "admin")
    get "/users/new"
    assert_response :success
    post(
      "/users",
      :params => {
        :user => {
          :login => "psmith", :firstname => "Paul",
          :lastname => "Smith", :mail => "psmith@somenet.foo",
          :language => "en", :password => "psmith09",
          :password_confirmation => "psmith09"
        }
      }
    )
    assert_response 302

    user = User.find_by_login("psmith")
    assert_kind_of User, user
  end

  def test_add_user
    log_user("admin", "admin")
    expire_sudo_mode!
    get "/users/new"
    assert_response :success
    post(
      "/users",
      :params => {
        :user => {
          :login => "psmith", :firstname => "Paul",
          :lastname => "Smith", :mail => "psmith@somenet.foo",
          :language => "en", :password => "psmith09",
          :password_confirmation => "psmith09"
        }
      }
    )
    assert_response :success
    assert_nil User.find_by_login("psmith")

    assert_select 'input[name=?][value=?]', 'user[login]', 'psmith'
    assert_select 'input[name=?][value=?]', 'user[firstname]', 'Paul'

    post(
      "/users",
      :params => {
        :user => {
          :login => "psmith", :firstname => "Paul",
          :lastname => "Smith", :mail => "psmith@somenet.foo",
          :language => "en", :password => "psmith09",
          :password_confirmation => "psmith09"
        },
        :sudo_password => 'admin'
      }
    )
    assert_response 302

    user = User.find_by_login("psmith")
    assert_kind_of User, user
  end

  def test_create_member_xhr
    log_user 'admin', 'admin'
    expire_sudo_mode!
    get '/projects/ecookbook/settings/members'
    assert_response :success

    assert_no_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}}, :xhr => true
    end

    assert_no_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}, sudo_password: ''}, :xhr => true
    end

    assert_no_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}, sudo_password: 'wrong'}, :xhr => true
    end

    assert_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}, sudo_password: 'admin'}, :xhr => true
    end
    assert User.find(7).member_of?(Project.find(1))
  end

  def test_create_member
    log_user 'admin', 'admin'
    expire_sudo_mode!
    get '/projects/ecookbook/settings/members'
    assert_response :success

    assert_no_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}}
    end

    assert_no_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}, sudo_password: ''}
    end

    assert_no_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}, sudo_password: 'wrong'}
    end

    assert_difference 'Member.count' do
      post '/projects/ecookbook/memberships', :params => {membership: {role_ids: [1], user_id: 7}, sudo_password: 'admin'}
    end

    assert_redirected_to '/projects/ecookbook/settings/members'
    assert User.find(7).member_of?(Project.find(1))
  end

  def test_create_role
    log_user 'admin', 'admin'
    expire_sudo_mode!
    get '/roles'
    assert_response :success

    get '/roles/new'
    assert_response :success

    post('/roles', :params => {:role => {}})
    assert_response :success
    assert_select 'h2', 'Confirm your password to continue'
    assert_select 'form[action="/roles"]'
    assert_select '#flash_error', 0

    post(
      '/roles',
      :params => {
        :role => {
          :name => 'new role',
          :issues_visibility => 'all'
        }
      }
    )
    assert_response :success
    assert_select 'h2', 'Confirm your password to continue'
    assert_select 'form[action="/roles"]'
    assert_select 'input[type=hidden][name=?][value=?]', 'role[name]', 'new role'
    assert_select '#flash_error', 0

    post(
      '/roles',
      :params => {
        :role => {
          :name => 'new role',
          :issues_visibility => 'all'
        },
        :sudo_password => 'wrong'
      }
    )
    assert_response :success
    assert_select 'h2', 'Confirm your password to continue'
    assert_select 'form[action="/roles"]'
    assert_select 'input[type=hidden][name=?][value=?]', 'role[name]', 'new role'
    assert_select '#flash_error'

    assert_difference 'Role.count' do
      post(
        '/roles',
        :params => {
          :role => {
            :name => 'new role',
            :issues_visibility => 'all',
            :assignable => '1',
            :permissions => %w(view_calendar)
          },
          :sudo_password => 'admin'
        }
      )
    end
    assert_redirected_to '/roles'
  end

  def test_update_email_address
    log_user 'jsmith', 'jsmith'
    expire_sudo_mode!
    get '/my/account'
    assert_response :success
    put('/my/account', :params => {:user => {:mail => 'newmail@test.com'}})
    assert_response :success
    assert_select 'h2', 'Confirm your password to continue'
    assert_select 'form[action="/my/account"]'
    assert_select 'input[type=hidden][name=?][value=?]', 'user[mail]', 'newmail@test.com'
    assert_select '#flash_error', 0

    # wrong password
    put(
      '/my/account',
      :params => {
        :user => {
          :mail => 'newmail@test.com'
        },
        :sudo_password => 'wrong'
      }
    )
    assert_response :success
    assert_select 'h2', 'Confirm your password to continue'
    assert_select 'form[action="/my/account"]'
    assert_select 'input[type=hidden][name=?][value=?]', 'user[mail]', 'newmail@test.com'
    assert_select '#flash_error'

    # correct password
    put(
      '/my/account',
      :params => {
        :user => {
          :mail => 'newmail@test.com'
        },
        :sudo_password => 'jsmith'
      }
    )
    assert_redirected_to '/my/account'
    assert_equal 'newmail@test.com', User.find_by_login('jsmith').mail

    # sudo mode should now be active and not require password again
    put(
      '/my/account',
      :params => {
        :user => {
          :mail => 'even.newer.mail@test.com'
        }
      }
    )
    assert_redirected_to '/my/account'
    assert_equal 'even.newer.mail@test.com', User.find_by_login('jsmith').mail
  end

  def test_sudo_mode_should_skip_api_requests
    with_settings :rest_api_enabled => '1' do
      assert_difference('User.count') do
        post(
          '/users.json',
          :params => {
            :user => {
              :login => 'foo', :firstname => 'Firstname',
              :lastname => 'Lastname',
              :mail => 'foo@example.net', :password => 'secret123',
              :mail_notification => 'only_assigned'
            }
          },
          :headers => credentials('admin')
        )
        assert_response :created
      end
    end
  end

  private

  # sudo mode is active after sign, let it expire by advancing the time
  def expire_sudo_mode!
    travel_to 20.minutes.from_now
  end
end