users_spec.rb 35.9 KB
Newer Older
N
Nihad Abbasov 已提交
1 2
require 'spec_helper'

3
describe API::Users, api: true  do
4 5
  include ApiHelpers

6 7 8
  let(:user)  { create(:user) }
  let(:admin) { create(:admin) }
  let(:key)   { create(:key, user: user) }
9
  let(:email)   { create(:email, user: user) }
10
  let(:omniauth_user) { create(:omniauth_user) }
11 12
  let(:ldap_user) { create(:omniauth_user, provider: 'ldapmain') }
  let(:ldap_blocked_user) { create(:omniauth_user, provider: 'ldapmain', state: 'ldap_blocked') }
N
Nihad Abbasov 已提交
13 14

  describe "GET /users" do
15
    context "when unauthenticated" do
16
      it "returns authentication error" do
17
        get api("/users")
Z
Z.J. van de Weg 已提交
18
        expect(response).to have_http_status(401)
19
      end
N
Nihad Abbasov 已提交
20 21
    end

22
    context "when authenticated" do
F
Felipe Artur 已提交
23
      # These specs are written just in case API authentication is not required anymore
F
Felipe Artur 已提交
24 25 26 27 28 29 30 31
      context "when public level is restricted" do
        before do
          stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
          allow_any_instance_of(API::Helpers).to receive(:authenticate!).and_return(true)
        end

        it "renders 403" do
          get api("/users")
Z
Z.J. van de Weg 已提交
32
          expect(response).to have_http_status(403)
F
Felipe Artur 已提交
33 34 35 36
        end

        it "renders 404" do
          get api("/users/#{user.id}")
Z
Z.J. van de Weg 已提交
37
          expect(response).to have_http_status(404)
F
Felipe Artur 已提交
38 39 40
        end
      end

41
      it "returns an array of users" do
R
Robert Speicher 已提交
42
        get api("/users", user)
Z
Z.J. van de Weg 已提交
43
        expect(response).to have_http_status(200)
44
        expect(json_response).to be_an Array
M
Marin Jankovski 已提交
45
        username = user.username
46 47 48
        expect(json_response.detect do |user|
          user['username'] == username
        end['username']).to eq(username)
N
Nihad Abbasov 已提交
49
      end
50

51 52 53 54 55 56 57 58 59 60 61
      it "returns an array of blocked users" do
        ldap_blocked_user
        create(:user, state: 'blocked')

        get api("/users?blocked=true", user)

        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array
        expect(json_response).to all(include('state' => /(blocked|ldap_blocked)/))
      end

62
      it "returns one user" do
63
        get api("/users?username=#{omniauth_user.username}", user)
Z
Z.J. van de Weg 已提交
64
        expect(response).to have_http_status(200)
65 66 67
        expect(json_response).to be_an Array
        expect(json_response.first['username']).to eq(omniauth_user.username)
      end
N
Nihad Abbasov 已提交
68
    end
69 70

    context "when admin" do
71
      it "returns an array of users" do
72
        get api("/users", admin)
Z
Z.J. van de Weg 已提交
73
        expect(response).to have_http_status(200)
74 75
        expect(json_response).to be_an Array
        expect(json_response.first.keys).to include 'email'
76
        expect(json_response.first.keys).to include 'organization'
77 78
        expect(json_response.first.keys).to include 'identities'
        expect(json_response.first.keys).to include 'can_create_project'
S
Stan Hu 已提交
79
        expect(json_response.first.keys).to include 'two_factor_enabled'
80 81
        expect(json_response.first.keys).to include 'last_sign_in_at'
        expect(json_response.first.keys).to include 'confirmed_at'
82
      end
83 84 85 86 87 88 89 90 91 92

      it "returns an array of external users" do
        create(:user, external: true)

        get api("/users?external=true", admin)

        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array
        expect(json_response).to all(include('external' => true))
      end
93
    end
N
Nihad Abbasov 已提交
94 95 96
  end

  describe "GET /users/:id" do
97
    it "returns a user by id" do
R
Robert Speicher 已提交
98
      get api("/users/#{user.id}", user)
Z
Z.J. van de Weg 已提交
99
      expect(response).to have_http_status(200)
100
      expect(json_response['username']).to eq(user.username)
N
Nihad Abbasov 已提交
101 102
    end

103
    it "returns a 401 if unauthenticated" do
104
      get api("/users/9998")
Z
Z.J. van de Weg 已提交
105
      expect(response).to have_http_status(401)
106
    end
V
Valeriy Sizov 已提交
107

108
    it "returns a 404 error if user id not found" do
109
      get api("/users/9999", user)
Z
Z.J. van de Weg 已提交
110
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
111
      expect(json_response['message']).to eq('404 User Not Found')
V
Valeriy Sizov 已提交
112
    end
113

114
    it "returns a 404 for invalid ID" do
115
      get api("/users/1ASDF", user)
116

117
      expect(response).to have_http_status(404)
118
    end
119 120 121 122
  end

  describe "POST /users" do
    before{ admin }
V
Valeriy Sizov 已提交
123

124
    it "creates user" do
125
      expect do
126
        post api("/users", admin), attributes_for(:user, projects_limit: 3)
127
      end.to change { User.count }.by(1)
V
Valeriy Sizov 已提交
128 129
    end

130
    it "creates user with correct attributes" do
131
      post api('/users', admin), attributes_for(:user, admin: true, can_create_group: true)
Z
Z.J. van de Weg 已提交
132
      expect(response).to have_http_status(201)
133 134
      user_id = json_response['id']
      new_user = User.find(user_id)
135 136 137
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(true)
      expect(new_user.can_create_group).to eq(true)
138 139
    end

140 141 142 143 144 145 146 147 148
    it "creates user with optional attributes" do
      optional_attributes = { confirm: true }
      attributes = attributes_for(:user).merge(optional_attributes)

      post api('/users', admin), attributes

      expect(response).to have_http_status(201)
    end

149
    it "creates non-admin user" do
150
      post api('/users', admin), attributes_for(:user, admin: false, can_create_group: false)
Z
Z.J. van de Weg 已提交
151
      expect(response).to have_http_status(201)
152 153
      user_id = json_response['id']
      new_user = User.find(user_id)
154 155 156
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
      expect(new_user.can_create_group).to eq(false)
157 158
    end

159
    it "creates non-admin users by default" do
160
      post api('/users', admin), attributes_for(:user)
Z
Z.J. van de Weg 已提交
161
      expect(response).to have_http_status(201)
162 163
      user_id = json_response['id']
      new_user = User.find(user_id)
164 165
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
166 167
    end

168
    it "returns 201 Created on success" do
169
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
Z
Z.J. van de Weg 已提交
170
      expect(response).to have_http_status(201)
171 172
    end

Z
Zeger-Jan van de Weg 已提交
173 174
    it 'creates non-external users by default' do
      post api("/users", admin), attributes_for(:user)
Z
Z.J. van de Weg 已提交
175
      expect(response).to have_http_status(201)
Z
Zeger-Jan van de Weg 已提交
176 177 178 179 180 181 182

      user_id = json_response['id']
      new_user = User.find(user_id)
      expect(new_user).not_to eq nil
      expect(new_user.external).to be_falsy
    end

183
    it 'allows an external user to be created' do
Z
Zeger-Jan van de Weg 已提交
184
      post api("/users", admin), attributes_for(:user, external: true)
Z
Z.J. van de Weg 已提交
185
      expect(response).to have_http_status(201)
Z
Zeger-Jan van de Weg 已提交
186 187 188 189 190 191 192

      user_id = json_response['id']
      new_user = User.find(user_id)
      expect(new_user).not_to eq nil
      expect(new_user.external).to be_truthy
    end

193 194 195 196 197 198 199 200 201 202 203 204
    it "creates user with reset password" do
      post api('/users', admin), attributes_for(:user, reset_password: true).except(:password)

      expect(response).to have_http_status(201)

      user_id = json_response['id']
      new_user = User.find(user_id)

      expect(new_user).not_to eq(nil)
      expect(new_user.recently_sent_password_reset?).to eq(true)
    end

205
    it "does not create user with invalid email" do
J
jubianchi 已提交
206
      post api('/users', admin),
207 208 209
        email: 'invalid email',
        password: 'password',
        name: 'test'
Z
Z.J. van de Weg 已提交
210
      expect(response).to have_http_status(400)
211 212
    end

213
    it 'returns 400 error if name not given' do
214
      post api('/users', admin), attributes_for(:user).except(:name)
Z
Z.J. van de Weg 已提交
215
      expect(response).to have_http_status(400)
J
jubianchi 已提交
216 217
    end

218
    it 'returns 400 error if password not given' do
219
      post api('/users', admin), attributes_for(:user).except(:password)
Z
Z.J. van de Weg 已提交
220
      expect(response).to have_http_status(400)
221 222
    end

223
    it 'returns 400 error if email not given' do
224
      post api('/users', admin), attributes_for(:user).except(:email)
Z
Z.J. van de Weg 已提交
225
      expect(response).to have_http_status(400)
226 227
    end

228
    it 'returns 400 error if username not given' do
229
      post api('/users', admin), attributes_for(:user).except(:username)
Z
Z.J. van de Weg 已提交
230
      expect(response).to have_http_status(400)
J
jubianchi 已提交
231 232
    end

233
    it 'returns 400 error if user does not validate' do
J
jubianchi 已提交
234
      post api('/users', admin),
235 236 237 238 239 240
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
Z
Z.J. van de Weg 已提交
241
      expect(response).to have_http_status(400)
242
      expect(json_response['message']['password']).
243
        to eq(['is too short (minimum is 8 characters)'])
244
      expect(json_response['message']['bio']).
245
        to eq(['is too long (maximum is 255 characters)'])
246
      expect(json_response['message']['projects_limit']).
247
        to eq(['must be greater than or equal to 0'])
248
      expect(json_response['message']['username']).
R
Robert Speicher 已提交
249
        to eq([Gitlab::Regex.namespace_regex_message])
250 251
    end

252
    it "is not available for non admin users" do
253
      post api("/users", user), attributes_for(:user)
Z
Z.J. van de Weg 已提交
254
      expect(response).to have_http_status(403)
V
Valeriy Sizov 已提交
255
    end
256

J
jubianchi 已提交
257 258 259
    context 'with existing user' do
      before do
        post api('/users', admin),
260 261 262 263
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
J
jubianchi 已提交
264
      end
265

266
      it 'returns 409 conflict error if user with same email exists' do
267
        expect do
J
jubianchi 已提交
268
          post api('/users', admin),
269 270 271 272 273
            name: 'foo',
            email: 'test@example.com',
            password: 'password',
            username: 'foo'
        end.to change { User.count }.by(0)
Z
Z.J. van de Weg 已提交
274
        expect(response).to have_http_status(409)
275
        expect(json_response['message']).to eq('Email has already been taken')
276 277
      end

278
      it 'returns 409 conflict error if same username exists' do
J
jubianchi 已提交
279 280
        expect do
          post api('/users', admin),
281 282 283 284
            name: 'foo',
            email: 'foo@example.com',
            password: 'password',
            username: 'test'
J
jubianchi 已提交
285
        end.to change { User.count }.by(0)
Z
Z.J. van de Weg 已提交
286
        expect(response).to have_http_status(409)
287
        expect(json_response['message']).to eq('Username has already been taken')
288
      end
289 290 291 292 293 294 295 296

      it 'creates user with new identity' do
        post api("/users", admin), attributes_for(:user, provider: 'github', extern_uid: '67890')

        expect(response).to have_http_status(201)
        expect(json_response['identities'].first['extern_uid']).to eq('67890')
        expect(json_response['identities'].first['provider']).to eq('github')
      end
297
    end
V
Valeriy Sizov 已提交
298 299
  end

M
Marin Jankovski 已提交
300
  describe "GET /users/sign_up" do
301
    it "redirects to sign in page" do
302
      get "/users/sign_up"
Z
Z.J. van de Weg 已提交
303
      expect(response).to have_http_status(302)
304
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
305 306 307
    end
  end

308
  describe "PUT /users/:id" do
309 310
    let!(:admin_user) { create(:admin) }

311 312
    before { admin }

313
    it "updates user with new bio" do
314
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
Z
Z.J. van de Weg 已提交
315
      expect(response).to have_http_status(200)
316 317
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
318 319
    end

320
    it "updates user with new password and forces reset on next login" do
321 322
      put api("/users/#{user.id}", admin), password: '12345678'

323
      expect(response).to have_http_status(200)
324
      expect(user.reload.password_expires_at).to be <= Time.now
325 326
    end

327 328
    it "updates user with organization" do
      put api("/users/#{user.id}", admin), { organization: 'GitLab' }
329

330 331 332 333 334
      expect(response).to have_http_status(200)
      expect(json_response['organization']).to eq('GitLab')
      expect(user.reload.organization).to eq('GitLab')
    end

335
    it 'updates user with his own email' do
J
jubianchi 已提交
336
      put api("/users/#{user.id}", admin), email: user.email
Z
Z.J. van de Weg 已提交
337
      expect(response).to have_http_status(200)
338 339
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
J
jubianchi 已提交
340 341
    end

342
    it 'updates user with his own username' do
J
jubianchi 已提交
343
      put api("/users/#{user.id}", admin), username: user.username
Z
Z.J. van de Weg 已提交
344
      expect(response).to have_http_status(200)
345 346
      expect(json_response['username']).to eq(user.username)
      expect(user.reload.username).to eq(user.username)
J
jubianchi 已提交
347 348
    end

349
    it "updates user's existing identity" do
350
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
Z
Z.J. van de Weg 已提交
351
      expect(response).to have_http_status(200)
352 353 354
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

355
    it 'updates user with new identity' do
R
Robert Schilling 已提交
356
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: 'john'
Z
Z.J. van de Weg 已提交
357
      expect(response).to have_http_status(200)
R
Robert Schilling 已提交
358
      expect(user.reload.identities.first.extern_uid).to eq('john')
359 360 361
      expect(user.reload.identities.first.provider).to eq('github')
    end

362
    it "updates admin status" do
363
      put api("/users/#{user.id}", admin), { admin: true }
Z
Z.J. van de Weg 已提交
364
      expect(response).to have_http_status(200)
365 366
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
367 368
    end

369
    it "updates external status" do
370 371 372 373 374 375
      put api("/users/#{user.id}", admin), { external: true }
      expect(response.status).to eq 200
      expect(json_response['external']).to eq(true)
      expect(user.reload.external?).to be_truthy
    end

376
    it "does not update admin status" do
377
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
Z
Z.J. van de Weg 已提交
378
      expect(response).to have_http_status(200)
379 380 381
      expect(json_response['is_admin']).to eq(true)
      expect(admin_user.reload.admin).to eq(true)
      expect(admin_user.can_create_group).to eq(false)
382 383
    end

384
    it "does not allow invalid update" do
385
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
Z
Z.J. van de Weg 已提交
386
      expect(response).to have_http_status(400)
387
      expect(user.reload.email).not_to eq('invalid email')
388 389
    end

390
    it "is not available for non admin users" do
391
      put api("/users/#{user.id}", user), attributes_for(:user)
Z
Z.J. van de Weg 已提交
392
      expect(response).to have_http_status(403)
393 394
    end

395
    it "returns 404 for non-existing user" do
396
      put api("/users/999999", admin), { bio: 'update should fail' }
Z
Z.J. van de Weg 已提交
397
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
398
      expect(json_response['message']).to eq('404 User Not Found')
J
jubianchi 已提交
399 400
    end

401
    it "returns a 404 if invalid ID" do
402 403
      put api("/users/ASDF", admin)

404
      expect(response).to have_http_status(404)
405 406
    end

407
    it 'returns 400 error if user does not validate' do
J
jubianchi 已提交
408
      put api("/users/#{user.id}", admin),
409 410 411 412 413 414
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
Z
Z.J. van de Weg 已提交
415
      expect(response).to have_http_status(400)
416
      expect(json_response['message']['password']).
417
        to eq(['is too short (minimum is 8 characters)'])
418
      expect(json_response['message']['bio']).
419
        to eq(['is too long (maximum is 255 characters)'])
420
      expect(json_response['message']['projects_limit']).
421
        to eq(['must be greater than or equal to 0'])
422
      expect(json_response['message']['username']).
R
Robert Speicher 已提交
423
        to eq([Gitlab::Regex.namespace_regex_message])
424
    end
425

R
Robert Schilling 已提交
426 427 428 429 430 431 432 433 434 435 436 437
    it 'returns 400 if provider is missing for identity update' do
      put api("/users/#{omniauth_user.id}", admin), extern_uid: '654321'

      expect(response).to have_http_status(400)
    end

    it 'returns 400 if external UID is missing for identity update' do
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldap'

      expect(response).to have_http_status(400)
    end

438
    context "with existing user" do
439
      before do
440 441
        post api("/users", admin), { email: 'test@example.com', password: 'password', username: 'test', name: 'test' }
        post api("/users", admin), { email: 'foo@bar.com', password: 'password', username: 'john', name: 'john' }
J
jubianchi 已提交
442
        @user = User.all.last
443
      end
444

445
      it 'returns 409 conflict error if email address exists' do
J
jubianchi 已提交
446
        put api("/users/#{@user.id}", admin), email: 'test@example.com'
Z
Z.J. van de Weg 已提交
447
        expect(response).to have_http_status(409)
448
        expect(@user.reload.email).to eq(@user.email)
J
jubianchi 已提交
449 450
      end

451
      it 'returns 409 conflict error if username taken' do
J
jubianchi 已提交
452 453
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
Z
Z.J. van de Weg 已提交
454
        expect(response).to have_http_status(409)
455
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
456
      end
457
    end
458 459
  end

A
Angus MacArthur 已提交
460 461 462
  describe "POST /users/:id/keys" do
    before { admin }

463
    it "does not create invalid ssh key" do
A
Angus MacArthur 已提交
464
      post api("/users/#{user.id}/keys", admin), { title: "invalid key" }
R
Robert Schilling 已提交
465

Z
Z.J. van de Weg 已提交
466
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
467
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
468 469
    end

470
    it 'does not create key without title' do
J
jubianchi 已提交
471
      post api("/users/#{user.id}/keys", admin), key: 'some key'
R
Robert Schilling 已提交
472

Z
Z.J. van de Weg 已提交
473
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
474
      expect(json_response['error']).to eq('title is missing')
A
Angus MacArthur 已提交
475 476
    end

477
    it "creates ssh key" do
A
Angus MacArthur 已提交
478
      key_attrs = attributes_for :key
479
      expect do
A
Angus MacArthur 已提交
480
        post api("/users/#{user.id}/keys", admin), key_attrs
481
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
482
    end
483

484
    it "returns 400 for invalid ID" do
C
Connor Shea 已提交
485
      post api("/users/999999/keys", admin)
486
      expect(response).to have_http_status(400)
487
    end
A
Angus MacArthur 已提交
488 489
  end

R
Robert Schilling 已提交
490
  describe 'GET /user/:id/keys' do
491 492 493
    before { admin }

    context 'when unauthenticated' do
494
      it 'returns authentication error' do
495
        get api("/users/#{user.id}/keys")
Z
Z.J. van de Weg 已提交
496
        expect(response).to have_http_status(401)
497 498 499 500
      end
    end

    context 'when authenticated' do
501
      it 'returns 404 for non-existing user' do
502
        get api('/users/999999/keys', admin)
Z
Z.J. van de Weg 已提交
503
        expect(response).to have_http_status(404)
504
        expect(json_response['message']).to eq('404 User Not Found')
505 506
      end

507
      it 'returns array of ssh keys' do
508 509 510
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
Z
Z.J. van de Weg 已提交
511
        expect(response).to have_http_status(200)
512 513
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
514 515 516 517
      end
    end
  end

R
Robert Schilling 已提交
518
  describe 'DELETE /user/:id/keys/:key_id' do
519 520 521
    before { admin }

    context 'when unauthenticated' do
522
      it 'returns authentication error' do
523
        delete api("/users/#{user.id}/keys/42")
Z
Z.J. van de Weg 已提交
524
        expect(response).to have_http_status(401)
525 526 527 528
      end
    end

    context 'when authenticated' do
529
      it 'deletes existing key' do
530 531
        user.keys << key
        user.save
532
        expect do
533
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
534
        end.to change { user.keys.count }.by(-1)
Z
Z.J. van de Weg 已提交
535
        expect(response).to have_http_status(200)
536 537
      end

538
      it 'returns 404 error if user not found' do
539 540 541
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
542
        expect(response).to have_http_status(404)
543
        expect(json_response['message']).to eq('404 User Not Found')
544 545
      end

546
      it 'returns 404 error if key not foud' do
547
        delete api("/users/#{user.id}/keys/42", admin)
Z
Z.J. van de Weg 已提交
548
        expect(response).to have_http_status(404)
549
        expect(json_response['message']).to eq('404 Key Not Found')
550 551 552 553
      end
    end
  end

554 555 556
  describe "POST /users/:id/emails" do
    before { admin }

557
    it "does not create invalid email" do
D
Douwe Maan 已提交
558
      post api("/users/#{user.id}/emails", admin), {}
R
Robert Schilling 已提交
559

Z
Z.J. van de Weg 已提交
560
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
561
      expect(json_response['error']).to eq('email is missing')
562 563
    end

564
    it "creates email" do
565 566 567 568 569
      email_attrs = attributes_for :email
      expect do
        post api("/users/#{user.id}/emails", admin), email_attrs
      end.to change{ user.emails.count }.by(1)
    end
570

571
    it "returns a 400 for invalid ID" do
C
Connor Shea 已提交
572
      post api("/users/999999/emails", admin)
573

574
      expect(response).to have_http_status(400)
575
    end
576 577
  end

R
Robert Schilling 已提交
578
  describe 'GET /user/:id/emails' do
579 580 581
    before { admin }

    context 'when unauthenticated' do
582
      it 'returns authentication error' do
583
        get api("/users/#{user.id}/emails")
Z
Z.J. van de Weg 已提交
584
        expect(response).to have_http_status(401)
585 586 587 588
      end
    end

    context 'when authenticated' do
589
      it 'returns 404 for non-existing user' do
590
        get api('/users/999999/emails', admin)
Z
Z.J. van de Weg 已提交
591
        expect(response).to have_http_status(404)
592 593 594
        expect(json_response['message']).to eq('404 User Not Found')
      end

595
      it 'returns array of emails' do
596 597 598
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
Z
Z.J. van de Weg 已提交
599
        expect(response).to have_http_status(200)
600 601 602
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
603

604
      it "returns a 404 for invalid ID" do
K
Kamil Trzcinski 已提交
605
        put api("/users/ASDF/emails", admin)
606

607
        expect(response).to have_http_status(404)
608
      end
609 610 611
    end
  end

R
Robert Schilling 已提交
612
  describe 'DELETE /user/:id/emails/:email_id' do
613 614 615
    before { admin }

    context 'when unauthenticated' do
616
      it 'returns authentication error' do
617
        delete api("/users/#{user.id}/emails/42")
Z
Z.J. van de Weg 已提交
618
        expect(response).to have_http_status(401)
619 620 621 622
      end
    end

    context 'when authenticated' do
623
      it 'deletes existing email' do
624 625 626 627 628
        user.emails << email
        user.save
        expect do
          delete api("/users/#{user.id}/emails/#{email.id}", admin)
        end.to change { user.emails.count }.by(-1)
Z
Z.J. van de Weg 已提交
629
        expect(response).to have_http_status(200)
630 631
      end

632
      it 'returns 404 error if user not found' do
633 634 635
        user.emails << email
        user.save
        delete api("/users/999999/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
636
        expect(response).to have_http_status(404)
637 638 639
        expect(json_response['message']).to eq('404 User Not Found')
      end

640
      it 'returns 404 error if email not foud' do
641
        delete api("/users/#{user.id}/emails/42", admin)
Z
Z.J. van de Weg 已提交
642
        expect(response).to have_http_status(404)
643 644
        expect(json_response['message']).to eq('404 Email Not Found')
      end
645

646
      it "returns a 404 for invalid ID" do
647 648
        delete api("/users/ASDF/emails/bar", admin)

649
        expect(response).to have_http_status(404)
650
      end
651 652 653
    end
  end

654
  describe "DELETE /users/:id" do
655
    let!(:namespace) { user.namespace }
656 657
    before { admin }

658
    it "deletes user" do
659
      delete api("/users/#{user.id}", admin)
Z
Z.J. van de Weg 已提交
660
      expect(response).to have_http_status(200)
661
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
662
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
663
      expect(json_response['email']).to eq(user.email)
664 665
    end

666
    it "does not delete for unauthenticated user" do
667
      delete api("/users/#{user.id}")
Z
Z.J. van de Weg 已提交
668
      expect(response).to have_http_status(401)
669 670
    end

671
    it "is not available for non admin users" do
672
      delete api("/users/#{user.id}", user)
Z
Z.J. van de Weg 已提交
673
      expect(response).to have_http_status(403)
674 675
    end

676
    it "returns 404 for non-existing user" do
677
      delete api("/users/999999", admin)
Z
Z.J. van de Weg 已提交
678
      expect(response).to have_http_status(404)
679
      expect(json_response['message']).to eq('404 User Not Found')
680
    end
681

682
    it "returns a 404 for invalid ID" do
683 684
      delete api("/users/ASDF", admin)

685
      expect(response).to have_http_status(404)
686
    end
687 688
  end

N
Nihad Abbasov 已提交
689
  describe "GET /user" do
690
    let(:personal_access_token) { create(:personal_access_token, user: user).token }
691 692 693 694

    context 'with regular user' do
      context 'with personal access token' do
        it 'returns 403 without private token when sudo is defined' do
695
          get api("/user?private_token=#{personal_access_token}&sudo=123")
696 697 698 699 700 701 702

          expect(response).to have_http_status(403)
        end
      end

      context 'with private token' do
        it 'returns 403 without private token when sudo defined' do
703
          get api("/user?private_token=#{user.private_token}&sudo=123")
704 705 706 707 708 709 710 711 712 713

          expect(response).to have_http_status(403)
        end
      end

      it 'returns current user without private token when sudo not defined' do
        get api("/user", user)

        expect(response).to have_http_status(200)
        expect(response).to match_response_schema('user/public')
714
        expect(json_response['id']).to eq(user.id)
715
      end
N
Nihad Abbasov 已提交
716
    end
717

718
    context 'with admin' do
719
      let(:admin_personal_access_token) { create(:personal_access_token, user: admin).token }
720 721 722

      context 'with personal access token' do
        it 'returns 403 without private token when sudo defined' do
723
          get api("/user?private_token=#{admin_personal_access_token}&sudo=#{user.id}")
724 725 726 727

          expect(response).to have_http_status(403)
        end

728 729
        it 'returns initial current user without private token when sudo not defined' do
          get api("/user?private_token=#{admin_personal_access_token}")
730 731 732

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
733
          expect(json_response['id']).to eq(admin.id)
734 735 736 737
        end
      end

      context 'with private token' do
738 739
        it 'returns sudoed user with private token when sudo defined' do
          get api("/user?private_token=#{admin.private_token}&sudo=#{user.id}")
740 741 742

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/login')
743
          expect(json_response['id']).to eq(user.id)
744 745
        end

746 747
        it 'returns initial current user without private token when sudo not defined' do
          get api("/user?private_token=#{admin.private_token}")
748 749 750

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
751
          expect(json_response['id']).to eq(admin.id)
752 753 754 755 756 757 758 759 760 761
        end
      end
    end

    context 'with unauthenticated user' do
      it "returns 401 error if user is unauthenticated" do
        get api("/user")

        expect(response).to have_http_status(401)
      end
762
    end
N
Nihad Abbasov 已提交
763
  end
764 765 766

  describe "GET /user/keys" do
    context "when unauthenticated" do
767
      it "returns authentication error" do
768
        get api("/user/keys")
Z
Z.J. van de Weg 已提交
769
        expect(response).to have_http_status(401)
770 771
      end
    end
N
Nihad Abbasov 已提交
772

773
    context "when authenticated" do
774
      it "returns array of ssh keys" do
775 776 777
        user.keys << key
        user.save
        get api("/user/keys", user)
Z
Z.J. van de Weg 已提交
778
        expect(response).to have_http_status(200)
779 780
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
781 782 783 784
      end
    end
  end

R
Robert Schilling 已提交
785
  describe "GET /user/keys/:key_id" do
786
    it "returns single key" do
787 788 789
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
Z
Z.J. van de Weg 已提交
790
      expect(response).to have_http_status(200)
791
      expect(json_response["title"]).to eq(key.title)
792
    end
N
Nihad Abbasov 已提交
793

794
    it "returns 404 Not Found within invalid ID" do
795
      get api("/user/keys/42", user)
796

Z
Z.J. van de Weg 已提交
797
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
798
      expect(json_response['message']).to eq('404 Key Not Found')
799 800
    end

801
    it "returns 404 error if admin accesses user's ssh key" do
802 803 804 805
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
806
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
807
      expect(json_response['message']).to eq('404 Key Not Found')
808
    end
809

810
    it "returns 404 for invalid ID" do
811
      get api("/users/keys/ASDF", admin)
812

813
      expect(response).to have_http_status(404)
814
    end
815
  end
N
Nihad Abbasov 已提交
816

817
  describe "POST /user/keys" do
818
    it "creates ssh key" do
819
      key_attrs = attributes_for :key
820
      expect do
821
        post api("/user/keys", user), key_attrs
822
      end.to change{ user.keys.count }.by(1)
Z
Z.J. van de Weg 已提交
823
      expect(response).to have_http_status(201)
824 825
    end

826
    it "returns a 401 error if unauthorized" do
827
      post api("/user/keys"), title: 'some title', key: 'some key'
Z
Z.J. van de Weg 已提交
828
      expect(response).to have_http_status(401)
829 830
    end

831
    it "does not create ssh key without key" do
832
      post api("/user/keys", user), title: 'title'
R
Robert Schilling 已提交
833

Z
Z.J. van de Weg 已提交
834
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
835
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
836 837
    end

838
    it 'does not create ssh key without title' do
J
jubianchi 已提交
839
      post api('/user/keys', user), key: 'some key'
R
Robert Schilling 已提交
840

Z
Z.J. van de Weg 已提交
841
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
842
      expect(json_response['error']).to eq('title is missing')
843 844
    end

845
    it "does not create ssh key without title" do
846
      post api("/user/keys", user), key: "somekey"
Z
Z.J. van de Weg 已提交
847
      expect(response).to have_http_status(400)
848 849 850
    end
  end

R
Robert Schilling 已提交
851
  describe "DELETE /user/keys/:key_id" do
852
    it "deletes existed key" do
853 854
      user.keys << key
      user.save
855
      expect do
856
        delete api("/user/keys/#{key.id}", user)
857
      end.to change{user.keys.count}.by(-1)
Z
Z.J. van de Weg 已提交
858
      expect(response).to have_http_status(200)
859
    end
N
Nihad Abbasov 已提交
860

R
Robert Schilling 已提交
861
    it "returns 404 if key ID not found" do
862
      delete api("/user/keys/42", user)
R
Robert Schilling 已提交
863 864 865

      expect(response).to have_http_status(404)
      expect(json_response['message']).to eq('404 Key Not Found')
866 867
    end

868
    it "returns 401 error if unauthorized" do
869 870 871
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
Z
Z.J. van de Weg 已提交
872
      expect(response).to have_http_status(401)
873
    end
874

875
    it "returns a 404 for invalid ID" do
876 877
      delete api("/users/keys/ASDF", admin)

878
      expect(response).to have_http_status(404)
879
    end
880
  end
881

882 883
  describe "GET /user/emails" do
    context "when unauthenticated" do
884
      it "returns authentication error" do
885
        get api("/user/emails")
Z
Z.J. van de Weg 已提交
886
        expect(response).to have_http_status(401)
887 888 889 890
      end
    end

    context "when authenticated" do
891
      it "returns array of emails" do
892 893 894
        user.emails << email
        user.save
        get api("/user/emails", user)
Z
Z.J. van de Weg 已提交
895
        expect(response).to have_http_status(200)
896 897 898 899 900 901
        expect(json_response).to be_an Array
        expect(json_response.first["email"]).to eq(email.email)
      end
    end
  end

R
Robert Schilling 已提交
902
  describe "GET /user/emails/:email_id" do
903
    it "returns single email" do
904 905 906
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
Z
Z.J. van de Weg 已提交
907
      expect(response).to have_http_status(200)
908 909 910
      expect(json_response["email"]).to eq(email.email)
    end

911
    it "returns 404 Not Found within invalid ID" do
912
      get api("/user/emails/42", user)
Z
Z.J. van de Weg 已提交
913
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
914
      expect(json_response['message']).to eq('404 Email Not Found')
915 916
    end

917
    it "returns 404 error if admin accesses user's email" do
918 919 920 921
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
922
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
923
      expect(json_response['message']).to eq('404 Email Not Found')
924
    end
925

926
    it "returns 404 for invalid ID" do
927
      get api("/users/emails/ASDF", admin)
928

929
      expect(response).to have_http_status(404)
930
    end
931 932 933
  end

  describe "POST /user/emails" do
934
    it "creates email" do
935 936 937 938
      email_attrs = attributes_for :email
      expect do
        post api("/user/emails", user), email_attrs
      end.to change{ user.emails.count }.by(1)
Z
Z.J. van de Weg 已提交
939
      expect(response).to have_http_status(201)
940 941
    end

942
    it "returns a 401 error if unauthorized" do
943
      post api("/user/emails"), email: 'some email'
Z
Z.J. van de Weg 已提交
944
      expect(response).to have_http_status(401)
945 946
    end

947
    it "does not create email with invalid email" do
948
      post api("/user/emails", user), {}
R
Robert Schilling 已提交
949

Z
Z.J. van de Weg 已提交
950
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
951
      expect(json_response['error']).to eq('email is missing')
952 953 954
    end
  end

R
Robert Schilling 已提交
955
  describe "DELETE /user/emails/:email_id" do
956
    it "deletes existed email" do
957 958 959 960 961
      user.emails << email
      user.save
      expect do
        delete api("/user/emails/#{email.id}", user)
      end.to change{user.emails.count}.by(-1)
Z
Z.J. van de Weg 已提交
962
      expect(response).to have_http_status(200)
963 964
    end

R
Robert Schilling 已提交
965
    it "returns 404 if email ID not found" do
966
      delete api("/user/emails/42", user)
R
Robert Schilling 已提交
967 968 969

      expect(response).to have_http_status(404)
      expect(json_response['message']).to eq('404 Email Not Found')
970 971
    end

972
    it "returns 401 error if unauthorized" do
973 974 975
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
Z
Z.J. van de Weg 已提交
976
      expect(response).to have_http_status(401)
977
    end
978

R
Robert Schilling 已提交
979 980
    it "returns 400 for invalid ID" do
      delete api("/user/emails/ASDF", admin)
981

R
Robert Schilling 已提交
982
      expect(response).to have_http_status(400)
983
    end
984 985
  end

986
  describe 'PUT /users/:id/block' do
987
    before { admin }
988
    it 'blocks existing user' do
989
      put api("/users/#{user.id}/block", admin)
Z
Z.J. van de Weg 已提交
990
      expect(response).to have_http_status(200)
991 992 993
      expect(user.reload.state).to eq('blocked')
    end

994
    it 'does not re-block ldap blocked users' do
995
      put api("/users/#{ldap_blocked_user.id}/block", admin)
Z
Z.J. van de Weg 已提交
996
      expect(response).to have_http_status(403)
997 998 999
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

1000
    it 'does not be available for non admin users' do
1001
      put api("/users/#{user.id}/block", user)
Z
Z.J. van de Weg 已提交
1002
      expect(response).to have_http_status(403)
1003 1004 1005
      expect(user.reload.state).to eq('active')
    end

1006
    it 'returns a 404 error if user id not found' do
1007
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
1008
      expect(response).to have_http_status(404)
1009 1010 1011 1012
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

1013
  describe 'PUT /users/:id/unblock' do
1014
    let(:blocked_user)  { create(:user, state: 'blocked') }
1015
    before { admin }
1016

1017
    it 'unblocks existing user' do
1018
      put api("/users/#{user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1019
      expect(response).to have_http_status(200)
1020 1021 1022
      expect(user.reload.state).to eq('active')
    end

1023
    it 'unblocks a blocked user' do
1024
      put api("/users/#{blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1025
      expect(response).to have_http_status(200)
1026 1027 1028
      expect(blocked_user.reload.state).to eq('active')
    end

1029
    it 'does not unblock ldap blocked users' do
1030
      put api("/users/#{ldap_blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1031
      expect(response).to have_http_status(403)
1032
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
1033 1034
    end

1035
    it 'does not be available for non admin users' do
1036
      put api("/users/#{user.id}/unblock", user)
Z
Z.J. van de Weg 已提交
1037
      expect(response).to have_http_status(403)
1038 1039 1040
      expect(user.reload.state).to eq('active')
    end

1041
    it 'returns a 404 error if user id not found' do
1042
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
1043
      expect(response).to have_http_status(404)
1044 1045
      expect(json_response['message']).to eq('404 User Not Found')
    end
1046

1047
    it "returns a 404 for invalid ID" do
1048 1049
      put api("/users/ASDF/block", admin)

1050
      expect(response).to have_http_status(404)
1051
    end
1052
  end
1053

1054
  describe 'GET /users/:id/events' do
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    let(:user) { create(:user) }
    let(:project) { create(:empty_project) }
    let(:note) { create(:note_on_issue, note: 'What an awesome day!', project: project) }

    before do
      project.add_user(user, :developer)
      EventCreateService.new.leave_note(note, user)
    end

    context "as a user than cannot see the event's project" do
      it 'returns no events' do
R
Rémy Coutable 已提交
1066 1067 1068
        other_user = create(:user)

        get api("/users/#{user.id}/events", other_user)
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

        expect(response).to have_http_status(200)
        expect(json_response).to be_empty
      end
    end

    context "as a user than can see the event's project" do
      it_behaves_like 'a paginated resources' do
        let(:request) { get api("/users/#{user.id}/events", user) }
      end

      context 'joined event' do
        it 'returns the "joined" event' do
          get api("/users/#{user.id}/events", user)

R
Rémy Coutable 已提交
1084
          comment_event = json_response.find { |e| e['action_name'] == 'commented on' }
1085

R
Rémy Coutable 已提交
1086 1087 1088 1089
          expect(comment_event['project_id'].to_i).to eq(project.id)
          expect(comment_event['author_username']).to eq(user.username)
          expect(comment_event['note']['id']).to eq(note.id)
          expect(comment_event['note']['body']).to eq('What an awesome day!')
1090

R
Rémy Coutable 已提交
1091
          joined_event = json_response.find { |e| e['action_name'] == 'joined' }
1092

R
Rémy Coutable 已提交
1093 1094 1095
          expect(joined_event['project_id'].to_i).to eq(project.id)
          expect(joined_event['author_username']).to eq(user.username)
          expect(joined_event['author']['name']).to eq(user.name)
1096 1097
        end
      end
A
Airat Shigapov 已提交
1098

1099
      context 'when there are multiple events from different projects' do
1100 1101
        let(:second_note) { create(:note_on_issue, project: create(:empty_project)) }
        let(:third_note) { create(:note_on_issue, project: project) }
A
Airat Shigapov 已提交
1102 1103

        before do
1104
          second_note.project.add_user(user, :developer)
A
Airat Shigapov 已提交
1105

1106 1107 1108
          [second_note, third_note].each do |note|
            EventCreateService.new.leave_note(note, user)
          end
A
Airat Shigapov 已提交
1109 1110
        end

1111
        it 'returns events in the correct order (from newest to oldest)' do
A
Airat Shigapov 已提交
1112 1113
          get api("/users/#{user.id}/events", user)

1114 1115
          comment_events = json_response.select { |e| e['action_name'] == 'commented on' }

1116 1117 1118
          expect(comment_events[0]['target_id']).to eq(third_note.id)
          expect(comment_events[1]['target_id']).to eq(second_note.id)
          expect(comment_events[2]['target_id']).to eq(note.id)
A
Airat Shigapov 已提交
1119 1120
        end
      end
1121 1122 1123 1124 1125 1126 1127 1128 1129
    end

    it 'returns a 404 error if not found' do
      get api('/users/42/events', user)

      expect(response).to have_http_status(404)
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end
N
Nihad Abbasov 已提交
1130
end