users_spec.rb 32 KB
Newer Older
Nihad Abbasov's avatar
Nihad Abbasov committed
1 2
require 'spec_helper'

Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
3
describe API::API, 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') }
Nihad Abbasov's avatar
Nihad Abbasov committed
13 14

  describe "GET /users" do
15
    context "when unauthenticated" do
16
      it "returns authentication error" do
17
        get api("/users")
18
        expect(response).to have_http_status(401)
19
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
20 21
    end

22
    context "when authenticated" do
23
      # These specs are written just in case API authentication is not required anymore
Felipe Artur's avatar
Felipe Artur committed
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")
32
          expect(response).to have_http_status(403)
Felipe Artur's avatar
Felipe Artur committed
33 34 35 36
        end

        it "renders 404" do
          get api("/users/#{user.id}")
37
          expect(response).to have_http_status(404)
Felipe Artur's avatar
Felipe Artur committed
38 39 40
        end
      end

41
      it "returns an array of users" do
Robert Speicher's avatar
Robert Speicher committed
42
        get api("/users", user)
43
        expect(response).to have_http_status(200)
44
        expect(json_response).to be_an Array
Marin Jankovski's avatar
Marin Jankovski committed
45
        username = user.username
46 47 48
        expect(json_response.detect do |user|
          user['username'] == username
        end['username']).to eq(username)
Nihad Abbasov's avatar
Nihad Abbasov committed
49
      end
50

51
      it "returns one user" do
52
        get api("/users?username=#{omniauth_user.username}", user)
53
        expect(response).to have_http_status(200)
54 55 56
        expect(json_response).to be_an Array
        expect(json_response.first['username']).to eq(omniauth_user.username)
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
57
    end
58 59

    context "when admin" do
60
      it "returns an array of users" do
61
        get api("/users", admin)
62
        expect(response).to have_http_status(200)
63 64
        expect(json_response).to be_an Array
        expect(json_response.first.keys).to include 'email'
65
        expect(json_response.first.keys).to include 'organization'
66 67
        expect(json_response.first.keys).to include 'identities'
        expect(json_response.first.keys).to include 'can_create_project'
Stan Hu's avatar
Stan Hu committed
68
        expect(json_response.first.keys).to include 'two_factor_enabled'
69 70
        expect(json_response.first.keys).to include 'last_sign_in_at'
        expect(json_response.first.keys).to include 'confirmed_at'
71 72
      end
    end
73 74

    context "when authenticated and ldap is enabled" do
Valery Sizov's avatar
Valery Sizov committed
75
      it "returns non-ldap user" do
76
        User.delete_all
77 78
        create :omniauth_user, provider: "ldapserver1"
        get api("/users", user), skip_ldap: "true"
Robert Speicher's avatar
Robert Speicher committed
79 80
        expect(response.status).to eq 200
        expect(json_response).to be_an Array
81
        username = user.username
Robert Speicher's avatar
Robert Speicher committed
82
        expect(json_response.first["username"]).to eq username
83 84
      end
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
85 86 87
  end

  describe "GET /users/:id" do
88
    it "returns a user by id" do
Robert Speicher's avatar
Robert Speicher committed
89
      get api("/users/#{user.id}", user)
90
      expect(response).to have_http_status(200)
91
      expect(json_response['username']).to eq(user.username)
Nihad Abbasov's avatar
Nihad Abbasov committed
92 93
    end

94
    it "returns a 401 if unauthenticated" do
95
      get api("/users/9998")
96
      expect(response).to have_http_status(401)
97
    end
98

99
    it "returns a 404 error if user id not found" do
100
      get api("/users/9999", user)
101
      expect(response).to have_http_status(404)
102
      expect(json_response['message']).to eq('404 Not found')
103
    end
104

105
    it "returns a 404 for invalid ID" do
106
      get api("/users/1ASDF", user)
107

108
      expect(response).to have_http_status(404)
109
    end
110 111 112 113
  end

  describe "POST /users" do
    before{ admin }
114

115
    it "creates user" do
116
      expect do
117
        post api("/users", admin), attributes_for(:user, projects_limit: 3)
118
      end.to change { User.count }.by(1)
119 120
    end

121
    it "creates user with correct attributes" do
122
      post api('/users', admin), attributes_for(:user, admin: true, can_create_group: true)
123
      expect(response).to have_http_status(201)
124 125
      user_id = json_response['id']
      new_user = User.find(user_id)
126 127 128
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(true)
      expect(new_user.can_create_group).to eq(true)
129 130
    end

131
    it "creates non-admin user" do
132
      post api('/users', admin), attributes_for(:user, admin: false, can_create_group: false)
133
      expect(response).to have_http_status(201)
134 135
      user_id = json_response['id']
      new_user = User.find(user_id)
136 137 138
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
      expect(new_user.can_create_group).to eq(false)
139 140
    end

141
    it "creates non-admin users by default" do
142
      post api('/users', admin), attributes_for(:user)
143
      expect(response).to have_http_status(201)
144 145
      user_id = json_response['id']
      new_user = User.find(user_id)
146 147
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
148 149
    end

150
    it "returns 201 Created on success" do
151
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
152
      expect(response).to have_http_status(201)
153 154
    end

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
155 156
    it 'creates non-external users by default' do
      post api("/users", admin), attributes_for(:user)
157
      expect(response).to have_http_status(201)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
158 159 160 161 162 163 164

      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

165
    it 'allows an external user to be created' do
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
166
      post api("/users", admin), attributes_for(:user, external: true)
167
      expect(response).to have_http_status(201)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
168 169 170 171 172 173 174

      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

175
    it "does not create user with invalid email" do
176
      post api('/users', admin),
177 178 179
        email: 'invalid email',
        password: 'password',
        name: 'test'
180
      expect(response).to have_http_status(400)
181 182
    end

183
    it 'returns 400 error if name not given' do
184
      post api('/users', admin), attributes_for(:user).except(:name)
185
      expect(response).to have_http_status(400)
186 187
    end

188
    it 'returns 400 error if password not given' do
189
      post api('/users', admin), attributes_for(:user).except(:password)
190
      expect(response).to have_http_status(400)
191 192
    end

193
    it 'returns 400 error if email not given' do
194
      post api('/users', admin), attributes_for(:user).except(:email)
195
      expect(response).to have_http_status(400)
196 197
    end

198
    it 'returns 400 error if username not given' do
199
      post api('/users', admin), attributes_for(:user).except(:username)
200
      expect(response).to have_http_status(400)
201 202
    end

203
    it 'returns 400 error if user does not validate' do
204
      post api('/users', admin),
205 206 207 208 209 210
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
211
      expect(response).to have_http_status(400)
212
      expect(json_response['message']['password']).
213
        to eq(['is too short (minimum is 8 characters)'])
214
      expect(json_response['message']['bio']).
215
        to eq(['is too long (maximum is 255 characters)'])
216
      expect(json_response['message']['projects_limit']).
217
        to eq(['must be greater than or equal to 0'])
218
      expect(json_response['message']['username']).
219
        to eq([Gitlab::Regex.namespace_regex_message])
220 221
    end

222
    it "is not available for non admin users" do
223
      post api("/users", user), attributes_for(:user)
224
      expect(response).to have_http_status(403)
225
    end
226

227 228 229
    context 'with existing user' do
      before do
        post api('/users', admin),
230 231 232 233
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
234
      end
235

236
      it 'returns 409 conflict error if user with same email exists' do
237
        expect do
238
          post api('/users', admin),
239 240 241 242 243
            name: 'foo',
            email: 'test@example.com',
            password: 'password',
            username: 'foo'
        end.to change { User.count }.by(0)
244
        expect(response).to have_http_status(409)
245
        expect(json_response['message']).to eq('Email has already been taken')
246 247
      end

248
      it 'returns 409 conflict error if same username exists' do
249 250
        expect do
          post api('/users', admin),
251 252 253 254
            name: 'foo',
            email: 'foo@example.com',
            password: 'password',
            username: 'test'
255
        end.to change { User.count }.by(0)
256
        expect(response).to have_http_status(409)
257
        expect(json_response['message']).to eq('Username has already been taken')
258 259
      end
    end
260 261
  end

Marin Jankovski's avatar
Marin Jankovski committed
262
  describe "GET /users/sign_up" do
263
    it "redirects to sign in page" do
264
      get "/users/sign_up"
265
      expect(response).to have_http_status(302)
266
      expect(response).to redirect_to(new_user_session_path)
Marin Jankovski's avatar
Marin Jankovski committed
267 268 269
    end
  end

270
  describe "PUT /users/:id" do
271 272
    let!(:admin_user) { create(:admin) }

273 274
    before { admin }

275
    it "updates user with new bio" do
276
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
277
      expect(response).to have_http_status(200)
278 279
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
280 281
    end

282 283
    it "updates user with organization" do
      put api("/users/#{user.id}", admin), { organization: 'GitLab' }
284

285 286 287 288 289
      expect(response).to have_http_status(200)
      expect(json_response['organization']).to eq('GitLab')
      expect(user.reload.organization).to eq('GitLab')
    end

290
    it 'updates user with his own email' do
291
      put api("/users/#{user.id}", admin), email: user.email
292
      expect(response).to have_http_status(200)
293 294
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
295 296
    end

297
    it 'updates user with his own username' do
298
      put api("/users/#{user.id}", admin), username: user.username
299
      expect(response).to have_http_status(200)
300 301
      expect(json_response['username']).to eq(user.username)
      expect(user.reload.username).to eq(user.username)
302 303
    end

304
    it "updates user's existing identity" do
305
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
306
      expect(response).to have_http_status(200)
307 308 309
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

310
    it 'updates user with new identity' do
311
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: '67890'
312
      expect(response).to have_http_status(200)
313 314 315 316
      expect(user.reload.identities.first.extern_uid).to eq('67890')
      expect(user.reload.identities.first.provider).to eq('github')
    end

317
    it "updates admin status" do
318
      put api("/users/#{user.id}", admin), { admin: true }
319
      expect(response).to have_http_status(200)
320 321
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
322 323
    end

324
    it "updates external status" do
325 326 327 328 329 330
      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

331
    it "does not update admin status" do
332
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
333
      expect(response).to have_http_status(200)
334 335 336
      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)
337 338
    end

339
    it "does not allow invalid update" do
340
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
341
      expect(response).to have_http_status(400)
342
      expect(user.reload.email).not_to eq('invalid email')
343 344
    end

345
    it "is not available for non admin users" do
346
      put api("/users/#{user.id}", user), attributes_for(:user)
347
      expect(response).to have_http_status(403)
348 349
    end

350
    it "returns 404 for non-existing user" do
351
      put api("/users/999999", admin), { bio: 'update should fail' }
352
      expect(response).to have_http_status(404)
353
      expect(json_response['message']).to eq('404 Not found')
354 355
    end

356
    it "returns a 404 if invalid ID" do
357 358
      put api("/users/ASDF", admin)

359
      expect(response).to have_http_status(404)
360 361
    end

362
    it 'returns 400 error if user does not validate' do
363
      put api("/users/#{user.id}", admin),
364 365 366 367 368 369
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
370
      expect(response).to have_http_status(400)
371
      expect(json_response['message']['password']).
372
        to eq(['is too short (minimum is 8 characters)'])
373
      expect(json_response['message']['bio']).
374
        to eq(['is too long (maximum is 255 characters)'])
375
      expect(json_response['message']['projects_limit']).
376
        to eq(['must be greater than or equal to 0'])
377
      expect(json_response['message']['username']).
378
        to eq([Gitlab::Regex.namespace_regex_message])
379
    end
380 381

    context "with existing user" do
382
      before do
383 384
        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' }
385
        @user = User.all.last
386
      end
387

388
      it 'returns 409 conflict error if email address exists' do
389
        put api("/users/#{@user.id}", admin), email: 'test@example.com'
390
        expect(response).to have_http_status(409)
391
        expect(@user.reload.email).to eq(@user.email)
392 393
      end

394
      it 'returns 409 conflict error if username taken' do
395 396
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
397
        expect(response).to have_http_status(409)
398
        expect(@user.reload.username).to eq(@user.username)
399
      end
400
    end
401 402
  end

Angus MacArthur's avatar
Angus MacArthur committed
403 404 405
  describe "POST /users/:id/keys" do
    before { admin }

406
    it "does not create invalid ssh key" do
Angus MacArthur's avatar
Angus MacArthur committed
407
      post api("/users/#{user.id}/keys", admin), { title: "invalid key" }
408
      expect(response).to have_http_status(400)
409
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
410 411
    end

412
    it 'does not create key without title' do
413
      post api("/users/#{user.id}/keys", admin), key: 'some key'
414
      expect(response).to have_http_status(400)
415
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
Angus MacArthur's avatar
Angus MacArthur committed
416 417
    end

418
    it "creates ssh key" do
Angus MacArthur's avatar
Angus MacArthur committed
419
      key_attrs = attributes_for :key
420
      expect do
Angus MacArthur's avatar
Angus MacArthur committed
421
        post api("/users/#{user.id}/keys", admin), key_attrs
422
      end.to change{ user.keys.count }.by(1)
Angus MacArthur's avatar
Angus MacArthur committed
423
    end
424

Connor Shea's avatar
Connor Shea committed
425 426 427
    it "returns 400 for invalid ID" do
      post api("/users/999999/keys", admin)
      expect(response).to have_http_status(400)
428
    end
Angus MacArthur's avatar
Angus MacArthur committed
429 430
  end

431 432 433 434
  describe 'GET /user/:uid/keys' do
    before { admin }

    context 'when unauthenticated' do
435
      it 'returns authentication error' do
436
        get api("/users/#{user.id}/keys")
437
        expect(response).to have_http_status(401)
438 439 440 441
      end
    end

    context 'when authenticated' do
442
      it 'returns 404 for non-existing user' do
443
        get api('/users/999999/keys', admin)
444
        expect(response).to have_http_status(404)
445
        expect(json_response['message']).to eq('404 User Not Found')
446 447
      end

448
      it 'returns array of ssh keys' do
449 450 451
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
452
        expect(response).to have_http_status(200)
453 454
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
455 456 457 458 459 460 461 462
      end
    end
  end

  describe 'DELETE /user/:uid/keys/:id' do
    before { admin }

    context 'when unauthenticated' do
463
      it 'returns authentication error' do
464
        delete api("/users/#{user.id}/keys/42")
465
        expect(response).to have_http_status(401)
466 467 468 469
      end
    end

    context 'when authenticated' do
470
      it 'deletes existing key' do
471 472
        user.keys << key
        user.save
473
        expect do
474
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
475
        end.to change { user.keys.count }.by(-1)
476
        expect(response).to have_http_status(200)
477 478
      end

479
      it 'returns 404 error if user not found' do
480 481 482
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
483
        expect(response).to have_http_status(404)
484
        expect(json_response['message']).to eq('404 User Not Found')
485 486
      end

487
      it 'returns 404 error if key not foud' do
488
        delete api("/users/#{user.id}/keys/42", admin)
489
        expect(response).to have_http_status(404)
490
        expect(json_response['message']).to eq('404 Key Not Found')
491 492 493 494
      end
    end
  end

495 496 497
  describe "POST /users/:id/emails" do
    before { admin }

498
    it "does not create invalid email" do
Douwe Maan's avatar
Douwe Maan committed
499
      post api("/users/#{user.id}/emails", admin), {}
500
      expect(response).to have_http_status(400)
501 502 503
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end

504
    it "creates email" do
505 506 507 508 509
      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
510

511
    it "returns a 400 for invalid ID" do
Connor Shea's avatar
Connor Shea committed
512
      post api("/users/999999/emails", admin)
513

Connor Shea's avatar
Connor Shea committed
514
      expect(response).to have_http_status(400)
515
    end
516 517 518 519 520 521
  end

  describe 'GET /user/:uid/emails' do
    before { admin }

    context 'when unauthenticated' do
522
      it 'returns authentication error' do
523
        get api("/users/#{user.id}/emails")
524
        expect(response).to have_http_status(401)
525 526 527 528
      end
    end

    context 'when authenticated' do
529
      it 'returns 404 for non-existing user' do
530
        get api('/users/999999/emails', admin)
531
        expect(response).to have_http_status(404)
532 533 534
        expect(json_response['message']).to eq('404 User Not Found')
      end

535
      it 'returns array of emails' do
536 537 538
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
539
        expect(response).to have_http_status(200)
540 541 542
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
543

544
      it "returns a 404 for invalid ID" do
545
        put api("/users/ASDF/emails", admin)
546

547
        expect(response).to have_http_status(404)
548
      end
549 550 551 552 553 554 555
    end
  end

  describe 'DELETE /user/:uid/emails/:id' do
    before { admin }

    context 'when unauthenticated' do
556
      it 'returns authentication error' do
557
        delete api("/users/#{user.id}/emails/42")
558
        expect(response).to have_http_status(401)
559 560 561 562
      end
    end

    context 'when authenticated' do
563
      it 'deletes existing email' do
564 565 566 567 568
        user.emails << email
        user.save
        expect do
          delete api("/users/#{user.id}/emails/#{email.id}", admin)
        end.to change { user.emails.count }.by(-1)
569
        expect(response).to have_http_status(200)
570 571
      end

572
      it 'returns 404 error if user not found' do
573 574 575
        user.emails << email
        user.save
        delete api("/users/999999/emails/#{email.id}", admin)
576
        expect(response).to have_http_status(404)
577 578 579
        expect(json_response['message']).to eq('404 User Not Found')
      end

580
      it 'returns 404 error if email not foud' do
581
        delete api("/users/#{user.id}/emails/42", admin)
582
        expect(response).to have_http_status(404)
583 584
        expect(json_response['message']).to eq('404 Email Not Found')
      end
585

586
      it "returns a 404 for invalid ID" do
587 588
        delete api("/users/ASDF/emails/bar", admin)

589
        expect(response).to have_http_status(404)
590
      end
591 592 593
    end
  end

594
  describe "DELETE /users/:id" do
595
    let!(:namespace) { user.namespace }
596 597
    before { admin }

598
    it "deletes user" do
599
      delete api("/users/#{user.id}", admin)
600
      expect(response).to have_http_status(200)
601
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
602
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
603
      expect(json_response['email']).to eq(user.email)
604 605
    end

606
    it "does not delete for unauthenticated user" do
607
      delete api("/users/#{user.id}")
608
      expect(response).to have_http_status(401)
609 610
    end

611
    it "is not available for non admin users" do
612
      delete api("/users/#{user.id}", user)
613
      expect(response).to have_http_status(403)
614 615
    end

616
    it "returns 404 for non-existing user" do
617
      delete api("/users/999999", admin)
618
      expect(response).to have_http_status(404)
619
      expect(json_response['message']).to eq('404 User Not Found')
620
    end
621

622
    it "returns a 404 for invalid ID" do
623 624
      delete api("/users/ASDF", admin)

625
      expect(response).to have_http_status(404)
626
    end
627 628
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
629
  describe "GET /user" do
630
    it "returns current user" do
Robert Speicher's avatar
Robert Speicher committed
631
      get api("/user", user)
632
      expect(response).to have_http_status(200)
633 634 635 636 637
      expect(json_response['email']).to eq(user.email)
      expect(json_response['is_admin']).to eq(user.is_admin?)
      expect(json_response['can_create_project']).to eq(user.can_create_project?)
      expect(json_response['can_create_group']).to eq(user.can_create_group?)
      expect(json_response['projects_limit']).to eq(user.projects_limit)
638
      expect(json_response['private_token']).to be_blank
Nihad Abbasov's avatar
Nihad Abbasov committed
639
    end
640

641
    it "returns 401 error if user is unauthenticated" do
642
      get api("/user")
643
      expect(response).to have_http_status(401)
644
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
645
  end
646 647 648

  describe "GET /user/keys" do
    context "when unauthenticated" do
649
      it "returns authentication error" do
650
        get api("/user/keys")
651
        expect(response).to have_http_status(401)
652 653
      end
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
654

655
    context "when authenticated" do
656
      it "returns array of ssh keys" do
657 658 659
        user.keys << key
        user.save
        get api("/user/keys", user)
660
        expect(response).to have_http_status(200)
661 662
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
663 664 665 666 667
      end
    end
  end

  describe "GET /user/keys/:id" do
668
    it "returns single key" do
669 670 671
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
672
      expect(response).to have_http_status(200)
673
      expect(json_response["title"]).to eq(key.title)
674
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
675

676
    it "returns 404 Not Found within invalid ID" do
677
      get api("/user/keys/42", user)
678

679
      expect(response).to have_http_status(404)
680
      expect(json_response['message']).to eq('404 Not found')
681 682
    end

683
    it "returns 404 error if admin accesses user's ssh key" do
684 685 686 687
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
688
      expect(response).to have_http_status(404)
689
      expect(json_response['message']).to eq('404 Not found')
690
    end
691

692
    it "returns 404 for invalid ID" do
693
      get api("/users/keys/ASDF", admin)
694

695
      expect(response).to have_http_status(404)
696
    end
697
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
698

699
  describe "POST /user/keys" do
700
    it "creates ssh key" do
701
      key_attrs = attributes_for :key
702
      expect do
703
        post api("/user/keys", user), key_attrs
704
      end.to change{ user.keys.count }.by(1)
705
      expect(response).to have_http_status(201)
706 707
    end

708
    it "returns a 401 error if unauthorized" do
709
      post api("/user/keys"), title: 'some title', key: 'some key'
710
      expect(response).to have_http_status(401)
711 712
    end

713
    it "does not create ssh key without key" do
714
      post api("/user/keys", user), title: 'title'
715
      expect(response).to have_http_status(400)
716
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
717 718
    end

719
    it 'does not create ssh key without title' do
720
      post api('/user/keys', user), key: 'some key'
721
      expect(response).to have_http_status(400)
722
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
723 724
    end

725
    it "does not create ssh key without title" do
726
      post api("/user/keys", user), key: "somekey"
727
      expect(response).to have_http_status(400)
728 729 730 731
    end
  end

  describe "DELETE /user/keys/:id" do
732
    it "deletes existed key" do
733 734
      user.keys << key
      user.save
735
      expect do
736
        delete api("/user/keys/#{key.id}", user)
737
      end.to change{user.keys.count}.by(-1)
738
      expect(response).to have_http_status(200)
739
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
740

741
    it "returns success if key ID not found" do
742
      delete api("/user/keys/42", user)
743
      expect(response).to have_http_status(200)
744 745
    end

746
    it "returns 401 error if unauthorized" do
747 748 749
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
750
      expect(response).to have_http_status(401)
751
    end
752

753
    it "returns a 404 for invalid ID" do
754 755
      delete api("/users/keys/ASDF", admin)

756
      expect(response).to have_http_status(404)
757
    end
758
  end
759

760 761
  describe "GET /user/emails" do
    context "when unauthenticated" do
762
      it "returns authentication error" do
763
        get api("/user/emails")
764
        expect(response).to have_http_status(401)
765 766 767 768
      end
    end

    context "when authenticated" do
769
      it "returns array of emails" do
770 771 772
        user.emails << email
        user.save
        get api("/user/emails", user)
773
        expect(response).to have_http_status(200)
774 775 776 777 778 779 780
        expect(json_response).to be_an Array
        expect(json_response.first["email"]).to eq(email.email)
      end
    end
  end

  describe "GET /user/emails/:id" do
781
    it "returns single email" do
782 783 784
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
785
      expect(response).to have_http_status(200)
786 787 788
      expect(json_response["email"]).to eq(email.email)
    end

789
    it "returns 404 Not Found within invalid ID" do
790
      get api("/user/emails/42", user)
791
      expect(response).to have_http_status(404)
792 793 794
      expect(json_response['message']).to eq('404 Not found')
    end

795
    it "returns 404 error if admin accesses user's email" do
796 797 798 799
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
800
      expect(response).to have_http_status(404)
801 802
      expect(json_response['message']).to eq('404 Not found')
    end
803

804
    it "returns 404 for invalid ID" do
805
      get api("/users/emails/ASDF", admin)
806

807
      expect(response).to have_http_status(404)
808
    end
809 810 811
  end

  describe "POST /user/emails" do
812
    it "creates email" do
813 814 815 816
      email_attrs = attributes_for :email
      expect do
        post api("/user/emails", user), email_attrs
      end.to change{ user.emails.count }.by(1)
817
      expect(response).to have_http_status(201)
818 819
    end

820
    it "returns a 401 error if unauthorized" do
821
      post api("/user/emails"), email: 'some email'
822
      expect(response).to have_http_status(401)
823 824
    end

825
    it "does not create email with invalid email" do
826
      post api("/user/emails", user), {}
827
      expect(response).to have_http_status(400)
828 829 830 831 832
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end
  end

  describe "DELETE /user/emails/:id" do
833
    it "deletes existed email" do
834 835 836 837 838
      user.emails << email
      user.save
      expect do
        delete api("/user/emails/#{email.id}", user)
      end.to change{user.emails.count}.by(-1)
839
      expect(response).to have_http_status(200)
840 841
    end

842
    it "returns success if email ID not found" do
843
      delete api("/user/emails/42", user)
844
      expect(response).to have_http_status(200)
845 846
    end

847
    it "returns 401 error if unauthorized" do
848 849 850
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
851
      expect(response).to have_http_status(401)
852
    end
853

854
    it "returns a 404 for invalid ID" do
855 856
      delete api("/users/emails/ASDF", admin)

857
      expect(response).to have_http_status(404)
858
    end
859 860
  end

861
  describe 'PUT /users/:id/block' do
862
    before { admin }
863
    it 'blocks existing user' do
864
      put api("/users/#{user.id}/block", admin)
865
      expect(response).to have_http_status(200)
866 867 868
      expect(user.reload.state).to eq('blocked')
    end

869
    it 'does not re-block ldap blocked users' do
870
      put api("/users/#{ldap_blocked_user.id}/block", admin)
871
      expect(response).to have_http_status(403)
872 873 874
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

875
    it 'does not be available for non admin users' do
876
      put api("/users/#{user.id}/block", user)
877
      expect(response).to have_http_status(403)
878 879 880
      expect(user.reload.state).to eq('active')
    end

881
    it 'returns a 404 error if user id not found' do
882
      put api('/users/9999/block', admin)
883
      expect(response).to have_http_status(404)
884 885 886 887
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

888
  describe 'PUT /users/:id/unblock' do
889
    let(:blocked_user)  { create(:user, state: 'blocked') }
890
    before { admin }
891

892
    it 'unblocks existing user' do
893
      put api("/users/#{user.id}/unblock", admin)
894
      expect(response).to have_http_status(200)
895 896 897
      expect(user.reload.state).to eq('active')
    end

898
    it 'unblocks a blocked user' do
899
      put api("/users/#{blocked_user.id}/unblock", admin)
900
      expect(response).to have_http_status(200)
901 902 903
      expect(blocked_user.reload.state).to eq('active')
    end

904
    it 'does not unblock ldap blocked users' do
905
      put api("/users/#{ldap_blocked_user.id}/unblock", admin)
906
      expect(response).to have_http_status(403)
907
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
908 909
    end

910
    it 'does not be available for non admin users' do
911
      put api("/users/#{user.id}/unblock", user)
912
      expect(response).to have_http_status(403)
913 914 915
      expect(user.reload.state).to eq('active')
    end

916
    it 'returns a 404 error if user id not found' do
917
      put api('/users/9999/block', admin)
918
      expect(response).to have_http_status(404)
919 920
      expect(json_response['message']).to eq('404 User Not Found')
    end
921

922
    it "returns a 404 for invalid ID" do
923 924
      put api("/users/ASDF/block", admin)

925
      expect(response).to have_http_status(404)
926
    end
927
  end
928

929
  describe 'GET /users/:id/events' do
930 931 932 933 934 935 936 937 938 939 940
    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
941 942 943
        other_user = create(:user)

        get api("/users/#{user.id}/events", other_user)
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

        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émy Coutable's avatar
Rémy Coutable committed
959
          comment_event = json_response.find { |e| e['action_name'] == 'commented on' }
960

Rémy Coutable's avatar
Rémy Coutable committed
961 962 963 964
          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!')
965

Rémy Coutable's avatar
Rémy Coutable committed
966
          joined_event = json_response.find { |e| e['action_name'] == 'joined' }
967

Rémy Coutable's avatar
Rémy Coutable committed
968 969 970
          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)
971 972
        end
      end
973

974
      context 'when there are multiple events from different projects' do
975 976
        let(:second_note) { create(:note_on_issue, project: create(:empty_project)) }
        let(:third_note) { create(:note_on_issue, project: project) }
977 978

        before do
979
          second_note.project.add_user(user, :developer)
980

981 982 983
          [second_note, third_note].each do |note|
            EventCreateService.new.leave_note(note, user)
          end
984 985
        end

986
        it 'returns events in the correct order (from newest to oldest)' do
987 988
          get api("/users/#{user.id}/events", user)

989 990
          comment_events = json_response.select { |e| e['action_name'] == 'commented on' }

991 992 993
          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)
994 995
        end
      end
996 997 998 999 1000 1001 1002
    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')
1003
    end
1004
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
1005
end