users_spec.rb 34.4 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
    it "creates non-admin user" do
141
      post api('/users', admin), attributes_for(:user, admin: false, can_create_group: false)
Z
Z.J. van de Weg 已提交
142
      expect(response).to have_http_status(201)
143 144
      user_id = json_response['id']
      new_user = User.find(user_id)
145 146 147
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
      expect(new_user.can_create_group).to eq(false)
148 149
    end

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

159
    it "returns 201 Created on success" do
160
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
Z
Z.J. van de Weg 已提交
161
      expect(response).to have_http_status(201)
162 163
    end

Z
Zeger-Jan van de Weg 已提交
164 165
    it 'creates non-external users by default' do
      post api("/users", admin), attributes_for(:user)
Z
Z.J. van de Weg 已提交
166
      expect(response).to have_http_status(201)
Z
Zeger-Jan van de Weg 已提交
167 168 169 170 171 172 173

      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

174
    it 'allows an external user to be created' do
Z
Zeger-Jan van de Weg 已提交
175
      post api("/users", admin), attributes_for(:user, external: true)
Z
Z.J. van de Weg 已提交
176
      expect(response).to have_http_status(201)
Z
Zeger-Jan van de Weg 已提交
177 178 179 180 181 182 183

      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

184
    it "does not create user with invalid email" do
J
jubianchi 已提交
185
      post api('/users', admin),
186 187 188
        email: 'invalid email',
        password: 'password',
        name: 'test'
Z
Z.J. van de Weg 已提交
189
      expect(response).to have_http_status(400)
190 191
    end

192
    it 'returns 400 error if name not given' do
193
      post api('/users', admin), attributes_for(:user).except(:name)
Z
Z.J. van de Weg 已提交
194
      expect(response).to have_http_status(400)
J
jubianchi 已提交
195 196
    end

197
    it 'returns 400 error if password not given' do
198
      post api('/users', admin), attributes_for(:user).except(:password)
Z
Z.J. van de Weg 已提交
199
      expect(response).to have_http_status(400)
200 201
    end

202
    it 'returns 400 error if email not given' do
203
      post api('/users', admin), attributes_for(:user).except(:email)
Z
Z.J. van de Weg 已提交
204
      expect(response).to have_http_status(400)
205 206
    end

207
    it 'returns 400 error if username not given' do
208
      post api('/users', admin), attributes_for(:user).except(:username)
Z
Z.J. van de Weg 已提交
209
      expect(response).to have_http_status(400)
J
jubianchi 已提交
210 211
    end

212
    it 'returns 400 error if user does not validate' do
J
jubianchi 已提交
213
      post api('/users', admin),
214 215 216 217 218 219
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
Z
Z.J. van de Weg 已提交
220
      expect(response).to have_http_status(400)
221
      expect(json_response['message']['password']).
222
        to eq(['is too short (minimum is 8 characters)'])
223
      expect(json_response['message']['bio']).
224
        to eq(['is too long (maximum is 255 characters)'])
225
      expect(json_response['message']['projects_limit']).
226
        to eq(['must be greater than or equal to 0'])
227
      expect(json_response['message']['username']).
R
Robert Speicher 已提交
228
        to eq([Gitlab::Regex.namespace_regex_message])
229 230
    end

231
    it "is not available for non admin users" do
232
      post api("/users", user), attributes_for(:user)
Z
Z.J. van de Weg 已提交
233
      expect(response).to have_http_status(403)
V
Valeriy Sizov 已提交
234
    end
235

J
jubianchi 已提交
236 237 238
    context 'with existing user' do
      before do
        post api('/users', admin),
239 240 241 242
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
J
jubianchi 已提交
243
      end
244

245
      it 'returns 409 conflict error if user with same email exists' do
246
        expect do
J
jubianchi 已提交
247
          post api('/users', admin),
248 249 250 251 252
            name: 'foo',
            email: 'test@example.com',
            password: 'password',
            username: 'foo'
        end.to change { User.count }.by(0)
Z
Z.J. van de Weg 已提交
253
        expect(response).to have_http_status(409)
254
        expect(json_response['message']).to eq('Email has already been taken')
255 256
      end

257
      it 'returns 409 conflict error if same username exists' do
J
jubianchi 已提交
258 259
        expect do
          post api('/users', admin),
260 261 262 263
            name: 'foo',
            email: 'foo@example.com',
            password: 'password',
            username: 'test'
J
jubianchi 已提交
264
        end.to change { User.count }.by(0)
Z
Z.J. van de Weg 已提交
265
        expect(response).to have_http_status(409)
266
        expect(json_response['message']).to eq('Username has already been taken')
267 268
      end
    end
V
Valeriy Sizov 已提交
269 270
  end

M
Marin Jankovski 已提交
271
  describe "GET /users/sign_up" do
272
    it "redirects to sign in page" do
273
      get "/users/sign_up"
Z
Z.J. van de Weg 已提交
274
      expect(response).to have_http_status(302)
275
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
276 277 278
    end
  end

279
  describe "PUT /users/:id" do
280 281
    let!(:admin_user) { create(:admin) }

282 283
    before { admin }

284
    it "updates user with new bio" do
285
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
Z
Z.J. van de Weg 已提交
286
      expect(response).to have_http_status(200)
287 288
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
289 290
    end

291 292
    it "updates user with organization" do
      put api("/users/#{user.id}", admin), { organization: 'GitLab' }
293

294 295 296 297 298
      expect(response).to have_http_status(200)
      expect(json_response['organization']).to eq('GitLab')
      expect(user.reload.organization).to eq('GitLab')
    end

299
    it 'updates user with his own email' do
J
jubianchi 已提交
300
      put api("/users/#{user.id}", admin), email: user.email
Z
Z.J. van de Weg 已提交
301
      expect(response).to have_http_status(200)
302 303
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
J
jubianchi 已提交
304 305
    end

306
    it 'updates user with his own username' do
J
jubianchi 已提交
307
      put api("/users/#{user.id}", admin), username: user.username
Z
Z.J. van de Weg 已提交
308
      expect(response).to have_http_status(200)
309 310
      expect(json_response['username']).to eq(user.username)
      expect(user.reload.username).to eq(user.username)
J
jubianchi 已提交
311 312
    end

313
    it "updates user's existing identity" do
314
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
Z
Z.J. van de Weg 已提交
315
      expect(response).to have_http_status(200)
316 317 318
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

319
    it 'updates user with new identity' do
320
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: '67890'
Z
Z.J. van de Weg 已提交
321
      expect(response).to have_http_status(200)
322 323 324 325
      expect(user.reload.identities.first.extern_uid).to eq('67890')
      expect(user.reload.identities.first.provider).to eq('github')
    end

326
    it "updates admin status" do
327
      put api("/users/#{user.id}", admin), { admin: true }
Z
Z.J. van de Weg 已提交
328
      expect(response).to have_http_status(200)
329 330
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
331 332
    end

333
    it "updates external status" do
334 335 336 337 338 339
      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

340
    it "does not update admin status" do
341
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
Z
Z.J. van de Weg 已提交
342
      expect(response).to have_http_status(200)
343 344 345
      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)
346 347
    end

348
    it "does not allow invalid update" do
349
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
Z
Z.J. van de Weg 已提交
350
      expect(response).to have_http_status(400)
351
      expect(user.reload.email).not_to eq('invalid email')
352 353
    end

354
    it "is not available for non admin users" do
355
      put api("/users/#{user.id}", user), attributes_for(:user)
Z
Z.J. van de Weg 已提交
356
      expect(response).to have_http_status(403)
357 358
    end

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

365
    it "returns a 404 if invalid ID" do
366 367
      put api("/users/ASDF", admin)

368
      expect(response).to have_http_status(404)
369 370
    end

371
    it 'returns 400 error if user does not validate' do
J
jubianchi 已提交
372
      put api("/users/#{user.id}", admin),
373 374 375 376 377 378
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
Z
Z.J. van de Weg 已提交
379
      expect(response).to have_http_status(400)
380
      expect(json_response['message']['password']).
381
        to eq(['is too short (minimum is 8 characters)'])
382
      expect(json_response['message']['bio']).
383
        to eq(['is too long (maximum is 255 characters)'])
384
      expect(json_response['message']['projects_limit']).
385
        to eq(['must be greater than or equal to 0'])
386
      expect(json_response['message']['username']).
R
Robert Speicher 已提交
387
        to eq([Gitlab::Regex.namespace_regex_message])
388
    end
389

R
Robert Schilling 已提交
390 391 392 393 394 395 396 397 398 399 400 401
    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

402
    context "with existing user" do
403
      before do
404 405
        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 已提交
406
        @user = User.all.last
407
      end
408

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

415
      it 'returns 409 conflict error if username taken' do
J
jubianchi 已提交
416 417
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
Z
Z.J. van de Weg 已提交
418
        expect(response).to have_http_status(409)
419
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
420
      end
421
    end
422 423
  end

A
Angus MacArthur 已提交
424 425 426
  describe "POST /users/:id/keys" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
430
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
431
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
432 433
    end

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

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

441
    it "creates ssh key" do
A
Angus MacArthur 已提交
442
      key_attrs = attributes_for :key
443
      expect do
A
Angus MacArthur 已提交
444
        post api("/users/#{user.id}/keys", admin), key_attrs
445
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
446
    end
447

448
    it "returns 400 for invalid ID" do
C
Connor Shea 已提交
449
      post api("/users/999999/keys", admin)
450
      expect(response).to have_http_status(400)
451
    end
A
Angus MacArthur 已提交
452 453
  end

R
Robert Schilling 已提交
454
  describe 'GET /user/:id/keys' do
455 456 457
    before { admin }

    context 'when unauthenticated' do
458
      it 'returns authentication error' do
459
        get api("/users/#{user.id}/keys")
Z
Z.J. van de Weg 已提交
460
        expect(response).to have_http_status(401)
461 462 463 464
      end
    end

    context 'when authenticated' do
465
      it 'returns 404 for non-existing user' do
466
        get api('/users/999999/keys', admin)
Z
Z.J. van de Weg 已提交
467
        expect(response).to have_http_status(404)
468
        expect(json_response['message']).to eq('404 User Not Found')
469 470
      end

471
      it 'returns array of ssh keys' do
472 473 474
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
Z
Z.J. van de Weg 已提交
475
        expect(response).to have_http_status(200)
476 477
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
478 479 480 481
      end
    end
  end

R
Robert Schilling 已提交
482
  describe 'DELETE /user/:id/keys/:key_id' do
483 484 485
    before { admin }

    context 'when unauthenticated' do
486
      it 'returns authentication error' do
487
        delete api("/users/#{user.id}/keys/42")
Z
Z.J. van de Weg 已提交
488
        expect(response).to have_http_status(401)
489 490 491 492
      end
    end

    context 'when authenticated' do
493
      it 'deletes existing key' do
494 495
        user.keys << key
        user.save
496
        expect do
497
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
498
        end.to change { user.keys.count }.by(-1)
Z
Z.J. van de Weg 已提交
499
        expect(response).to have_http_status(200)
500 501
      end

502
      it 'returns 404 error if user not found' do
503 504 505
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
506
        expect(response).to have_http_status(404)
507
        expect(json_response['message']).to eq('404 User Not Found')
508 509
      end

510
      it 'returns 404 error if key not foud' do
511
        delete api("/users/#{user.id}/keys/42", admin)
Z
Z.J. van de Weg 已提交
512
        expect(response).to have_http_status(404)
513
        expect(json_response['message']).to eq('404 Key Not Found')
514 515 516 517
      end
    end
  end

518 519 520
  describe "POST /users/:id/emails" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
524
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
525
      expect(json_response['error']).to eq('email is missing')
526 527
    end

528
    it "creates email" do
529 530 531 532 533
      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
534

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

538
      expect(response).to have_http_status(400)
539
    end
540 541
  end

R
Robert Schilling 已提交
542
  describe 'GET /user/:id/emails' do
543 544 545
    before { admin }

    context 'when unauthenticated' do
546
      it 'returns authentication error' do
547
        get api("/users/#{user.id}/emails")
Z
Z.J. van de Weg 已提交
548
        expect(response).to have_http_status(401)
549 550 551 552
      end
    end

    context 'when authenticated' do
553
      it 'returns 404 for non-existing user' do
554
        get api('/users/999999/emails', admin)
Z
Z.J. van de Weg 已提交
555
        expect(response).to have_http_status(404)
556 557 558
        expect(json_response['message']).to eq('404 User Not Found')
      end

559
      it 'returns array of emails' do
560 561 562
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
Z
Z.J. van de Weg 已提交
563
        expect(response).to have_http_status(200)
564 565 566
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
567

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

571
        expect(response).to have_http_status(404)
572
      end
573 574 575
    end
  end

R
Robert Schilling 已提交
576
  describe 'DELETE /user/:id/emails/:email_id' do
577 578 579
    before { admin }

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

    context 'when authenticated' do
587
      it 'deletes existing email' do
588 589 590 591 592
        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 已提交
593
        expect(response).to have_http_status(200)
594 595
      end

596
      it 'returns 404 error if user not found' do
597 598 599
        user.emails << email
        user.save
        delete api("/users/999999/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
600
        expect(response).to have_http_status(404)
601 602 603
        expect(json_response['message']).to eq('404 User Not Found')
      end

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

610
      it "returns a 404 for invalid ID" do
611 612
        delete api("/users/ASDF/emails/bar", admin)

613
        expect(response).to have_http_status(404)
614
      end
615 616 617
    end
  end

618
  describe "DELETE /users/:id" do
619
    let!(:namespace) { user.namespace }
620 621
    before { admin }

622
    it "deletes user" do
623
      delete api("/users/#{user.id}", admin)
Z
Z.J. van de Weg 已提交
624
      expect(response).to have_http_status(200)
625
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
626
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
627
      expect(json_response['email']).to eq(user.email)
628 629
    end

630
    it "does not delete for unauthenticated user" do
631
      delete api("/users/#{user.id}")
Z
Z.J. van de Weg 已提交
632
      expect(response).to have_http_status(401)
633 634
    end

635
    it "is not available for non admin users" do
636
      delete api("/users/#{user.id}", user)
Z
Z.J. van de Weg 已提交
637
      expect(response).to have_http_status(403)
638 639
    end

640
    it "returns 404 for non-existing user" do
641
      delete api("/users/999999", admin)
Z
Z.J. van de Weg 已提交
642
      expect(response).to have_http_status(404)
643
      expect(json_response['message']).to eq('404 User Not Found')
644
    end
645

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

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

N
Nihad Abbasov 已提交
653
  describe "GET /user" do
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    let(:personal_access_token) { create(:personal_access_token, user: user) }
    let(:private_token) { user.private_token }

    context 'with regular user' do
      context 'with personal access token' do
        it 'returns 403 without private token when sudo is defined' do
          get api("/user?private_token=#{personal_access_token.token}&sudo=#{user.id}")

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

      context 'with private token' do
        it 'returns 403 without private token when sudo defined' do
          get api("/user?private_token=#{private_token}&sudo=#{user.id}")

          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')
      end
N
Nihad Abbasov 已提交
680
    end
681

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    context 'with admin' do
      let(:user) { create(:admin) }

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

          expect(response).to have_http_status(403)
        end

        it 'returns current user without private token when sudo not defined' do
          get api("/user?private_token=#{personal_access_token.token}")

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
        end
      end

      context 'with private token' do
        it 'returns current user with private token when sudo defined' do
          get api("/user?private_token=#{private_token}&sudo=#{user.id}")

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/login')
        end

        it 'returns current user without private token when sudo not defined' do
          get api("/user?private_token=#{private_token}")

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
        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
723
    end
N
Nihad Abbasov 已提交
724
  end
725 726 727

  describe "GET /user/keys" do
    context "when unauthenticated" do
728
      it "returns authentication error" do
729
        get api("/user/keys")
Z
Z.J. van de Weg 已提交
730
        expect(response).to have_http_status(401)
731 732
      end
    end
N
Nihad Abbasov 已提交
733

734
    context "when authenticated" do
735
      it "returns array of ssh keys" do
736 737 738
        user.keys << key
        user.save
        get api("/user/keys", user)
Z
Z.J. van de Weg 已提交
739
        expect(response).to have_http_status(200)
740 741
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
742 743 744 745
      end
    end
  end

R
Robert Schilling 已提交
746
  describe "GET /user/keys/:key_id" do
747
    it "returns single key" do
748 749 750
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
Z
Z.J. van de Weg 已提交
751
      expect(response).to have_http_status(200)
752
      expect(json_response["title"]).to eq(key.title)
753
    end
N
Nihad Abbasov 已提交
754

755
    it "returns 404 Not Found within invalid ID" do
756
      get api("/user/keys/42", user)
757

Z
Z.J. van de Weg 已提交
758
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
759
      expect(json_response['message']).to eq('404 Key Not Found')
760 761
    end

762
    it "returns 404 error if admin accesses user's ssh key" do
763 764 765 766
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
767
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
768
      expect(json_response['message']).to eq('404 Key Not Found')
769
    end
770

771
    it "returns 404 for invalid ID" do
772
      get api("/users/keys/ASDF", admin)
773

774
      expect(response).to have_http_status(404)
775
    end
776
  end
N
Nihad Abbasov 已提交
777

778
  describe "POST /user/keys" do
779
    it "creates ssh key" do
780
      key_attrs = attributes_for :key
781
      expect do
782
        post api("/user/keys", user), key_attrs
783
      end.to change{ user.keys.count }.by(1)
Z
Z.J. van de Weg 已提交
784
      expect(response).to have_http_status(201)
785 786
    end

787
    it "returns a 401 error if unauthorized" do
788
      post api("/user/keys"), title: 'some title', key: 'some key'
Z
Z.J. van de Weg 已提交
789
      expect(response).to have_http_status(401)
790 791
    end

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

Z
Z.J. van de Weg 已提交
795
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
796
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
797 798
    end

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

Z
Z.J. van de Weg 已提交
802
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
803
      expect(json_response['error']).to eq('title is missing')
804 805
    end

806
    it "does not create ssh key without title" do
807
      post api("/user/keys", user), key: "somekey"
Z
Z.J. van de Weg 已提交
808
      expect(response).to have_http_status(400)
809 810 811
    end
  end

R
Robert Schilling 已提交
812
  describe "DELETE /user/keys/:key_id" do
813
    it "deletes existed key" do
814 815
      user.keys << key
      user.save
816
      expect do
817
        delete api("/user/keys/#{key.id}", user)
818
      end.to change{user.keys.count}.by(-1)
Z
Z.J. van de Weg 已提交
819
      expect(response).to have_http_status(200)
820
    end
N
Nihad Abbasov 已提交
821

R
Robert Schilling 已提交
822
    it "returns 404 if key ID not found" do
823
      delete api("/user/keys/42", user)
R
Robert Schilling 已提交
824 825 826

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

829
    it "returns 401 error if unauthorized" do
830 831 832
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
Z
Z.J. van de Weg 已提交
833
      expect(response).to have_http_status(401)
834
    end
835

836
    it "returns a 404 for invalid ID" do
837 838
      delete api("/users/keys/ASDF", admin)

839
      expect(response).to have_http_status(404)
840
    end
841
  end
842

843 844
  describe "GET /user/emails" do
    context "when unauthenticated" do
845
      it "returns authentication error" do
846
        get api("/user/emails")
Z
Z.J. van de Weg 已提交
847
        expect(response).to have_http_status(401)
848 849 850 851
      end
    end

    context "when authenticated" do
852
      it "returns array of emails" do
853 854 855
        user.emails << email
        user.save
        get api("/user/emails", user)
Z
Z.J. van de Weg 已提交
856
        expect(response).to have_http_status(200)
857 858 859 860 861 862
        expect(json_response).to be_an Array
        expect(json_response.first["email"]).to eq(email.email)
      end
    end
  end

R
Robert Schilling 已提交
863
  describe "GET /user/emails/:email_id" do
864
    it "returns single email" do
865 866 867
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
Z
Z.J. van de Weg 已提交
868
      expect(response).to have_http_status(200)
869 870 871
      expect(json_response["email"]).to eq(email.email)
    end

872
    it "returns 404 Not Found within invalid ID" do
873
      get api("/user/emails/42", user)
Z
Z.J. van de Weg 已提交
874
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
875
      expect(json_response['message']).to eq('404 Email Not Found')
876 877
    end

878
    it "returns 404 error if admin accesses user's email" do
879 880 881 882
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
883
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
884
      expect(json_response['message']).to eq('404 Email Not Found')
885
    end
886

887
    it "returns 404 for invalid ID" do
888
      get api("/users/emails/ASDF", admin)
889

890
      expect(response).to have_http_status(404)
891
    end
892 893 894
  end

  describe "POST /user/emails" do
895
    it "creates email" do
896 897 898 899
      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 已提交
900
      expect(response).to have_http_status(201)
901 902
    end

903
    it "returns a 401 error if unauthorized" do
904
      post api("/user/emails"), email: 'some email'
Z
Z.J. van de Weg 已提交
905
      expect(response).to have_http_status(401)
906 907
    end

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

Z
Z.J. van de Weg 已提交
911
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
912
      expect(json_response['error']).to eq('email is missing')
913 914 915
    end
  end

R
Robert Schilling 已提交
916
  describe "DELETE /user/emails/:email_id" do
917
    it "deletes existed email" do
918 919 920 921 922
      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 已提交
923
      expect(response).to have_http_status(200)
924 925
    end

R
Robert Schilling 已提交
926
    it "returns 404 if email ID not found" do
927
      delete api("/user/emails/42", user)
R
Robert Schilling 已提交
928 929 930

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

933
    it "returns 401 error if unauthorized" do
934 935 936
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
Z
Z.J. van de Weg 已提交
937
      expect(response).to have_http_status(401)
938
    end
939

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

R
Robert Schilling 已提交
943
      expect(response).to have_http_status(400)
944
    end
945 946
  end

947
  describe 'PUT /users/:id/block' do
948
    before { admin }
949
    it 'blocks existing user' do
950
      put api("/users/#{user.id}/block", admin)
Z
Z.J. van de Weg 已提交
951
      expect(response).to have_http_status(200)
952 953 954
      expect(user.reload.state).to eq('blocked')
    end

955
    it 'does not re-block ldap blocked users' do
956
      put api("/users/#{ldap_blocked_user.id}/block", admin)
Z
Z.J. van de Weg 已提交
957
      expect(response).to have_http_status(403)
958 959 960
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

961
    it 'does not be available for non admin users' do
962
      put api("/users/#{user.id}/block", user)
Z
Z.J. van de Weg 已提交
963
      expect(response).to have_http_status(403)
964 965 966
      expect(user.reload.state).to eq('active')
    end

967
    it 'returns a 404 error if user id not found' do
968
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
969
      expect(response).to have_http_status(404)
970 971 972 973
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

974
  describe 'PUT /users/:id/unblock' do
975
    let(:blocked_user)  { create(:user, state: 'blocked') }
976
    before { admin }
977

978
    it 'unblocks existing user' do
979
      put api("/users/#{user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
980
      expect(response).to have_http_status(200)
981 982 983
      expect(user.reload.state).to eq('active')
    end

984
    it 'unblocks a blocked user' do
985
      put api("/users/#{blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
986
      expect(response).to have_http_status(200)
987 988 989
      expect(blocked_user.reload.state).to eq('active')
    end

990
    it 'does not unblock ldap blocked users' do
991
      put api("/users/#{ldap_blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
992
      expect(response).to have_http_status(403)
993
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
994 995
    end

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

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

1008
    it "returns a 404 for invalid ID" do
1009 1010
      put api("/users/ASDF/block", admin)

1011
      expect(response).to have_http_status(404)
1012
    end
1013
  end
1014

1015
  describe 'GET /users/:id/events' do
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    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 已提交
1027 1028 1029
        other_user = create(:user)

        get api("/users/#{user.id}/events", other_user)
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

        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 已提交
1045
          comment_event = json_response.find { |e| e['action_name'] == 'commented on' }
1046

R
Rémy Coutable 已提交
1047 1048 1049 1050
          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!')
1051

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

R
Rémy Coutable 已提交
1054 1055 1056
          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)
1057 1058
        end
      end
A
Airat Shigapov 已提交
1059

1060
      context 'when there are multiple events from different projects' do
1061 1062
        let(:second_note) { create(:note_on_issue, project: create(:empty_project)) }
        let(:third_note) { create(:note_on_issue, project: project) }
A
Airat Shigapov 已提交
1063 1064

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

1067 1068 1069
          [second_note, third_note].each do |note|
            EventCreateService.new.leave_note(note, user)
          end
A
Airat Shigapov 已提交
1070 1071
        end

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

1075 1076
          comment_events = json_response.select { |e| e['action_name'] == 'commented on' }

1077 1078 1079
          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 已提交
1080 1081
        end
      end
1082 1083 1084 1085 1086 1087 1088 1089 1090
    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 已提交
1091
end