users_spec.rb 30.7 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
Nihad Abbasov's avatar
Nihad Abbasov committed
73 74 75
  end

  describe "GET /users/:id" do
76
    it "returns a user by id" do
Robert Speicher's avatar
Robert Speicher committed
77
      get api("/users/#{user.id}", user)
78
      expect(response).to have_http_status(200)
79
      expect(json_response['username']).to eq(user.username)
Nihad Abbasov's avatar
Nihad Abbasov committed
80 81
    end

82
    it "returns a 401 if unauthenticated" do
83
      get api("/users/9998")
84
      expect(response).to have_http_status(401)
85
    end
86

87
    it "returns a 404 error if user id not found" do
88
      get api("/users/9999", user)
89
      expect(response).to have_http_status(404)
90
      expect(json_response['message']).to eq('404 Not found')
91
    end
92

93
    it "returns a 404 for invalid ID" do
94
      get api("/users/1ASDF", user)
95

96
      expect(response).to have_http_status(404)
97
    end
98 99 100 101
  end

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

103
    it "creates user" do
104
      expect do
105
        post api("/users", admin), attributes_for(:user, projects_limit: 3)
106
      end.to change { User.count }.by(1)
107 108
    end

109
    it "creates user with correct attributes" do
110
      post api('/users', admin), attributes_for(:user, admin: true, can_create_group: true)
111
      expect(response).to have_http_status(201)
112 113
      user_id = json_response['id']
      new_user = User.find(user_id)
114 115 116
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(true)
      expect(new_user.can_create_group).to eq(true)
117 118
    end

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

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

138
    it "returns 201 Created on success" do
139
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
140
      expect(response).to have_http_status(201)
141 142
    end

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
143 144
    it 'creates non-external users by default' do
      post api("/users", admin), attributes_for(:user)
145
      expect(response).to have_http_status(201)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
146 147 148 149 150 151 152

      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

153
    it 'allows an external user to be created' do
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
154
      post api("/users", admin), attributes_for(:user, external: true)
155
      expect(response).to have_http_status(201)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
156 157 158 159 160 161 162

      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

163
    it "does not create user with invalid email" do
164
      post api('/users', admin),
165 166 167
        email: 'invalid email',
        password: 'password',
        name: 'test'
168
      expect(response).to have_http_status(400)
169 170
    end

171
    it 'returns 400 error if name not given' do
172
      post api('/users', admin), attributes_for(:user).except(:name)
173
      expect(response).to have_http_status(400)
174 175
    end

176
    it 'returns 400 error if password not given' do
177
      post api('/users', admin), attributes_for(:user).except(:password)
178
      expect(response).to have_http_status(400)
179 180
    end

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

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

191
    it 'returns 400 error if user does not validate' do
192
      post api('/users', admin),
193 194 195 196 197 198
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
199
      expect(response).to have_http_status(400)
200
      expect(json_response['message']['password']).
201
        to eq(['is too short (minimum is 8 characters)'])
202
      expect(json_response['message']['bio']).
203
        to eq(['is too long (maximum is 255 characters)'])
204
      expect(json_response['message']['projects_limit']).
205
        to eq(['must be greater than or equal to 0'])
206
      expect(json_response['message']['username']).
207
        to eq([Gitlab::Regex.namespace_regex_message])
208 209
    end

210
    it "is not available for non admin users" do
211
      post api("/users", user), attributes_for(:user)
212
      expect(response).to have_http_status(403)
213
    end
214

215 216 217
    context 'with existing user' do
      before do
        post api('/users', admin),
218 219 220 221
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
222
      end
223

224
      it 'returns 409 conflict error if user with same email exists' do
225
        expect do
226
          post api('/users', admin),
227 228 229 230 231
            name: 'foo',
            email: 'test@example.com',
            password: 'password',
            username: 'foo'
        end.to change { User.count }.by(0)
232
        expect(response).to have_http_status(409)
233
        expect(json_response['message']).to eq('Email has already been taken')
234 235
      end

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

Marin Jankovski's avatar
Marin Jankovski committed
250
  describe "GET /users/sign_up" do
251
    it "redirects to sign in page" do
252
      get "/users/sign_up"
253
      expect(response).to have_http_status(302)
254
      expect(response).to redirect_to(new_user_session_path)
Marin Jankovski's avatar
Marin Jankovski committed
255 256 257
    end
  end

258
  describe "PUT /users/:id" do
259 260
    let!(:admin_user) { create(:admin) }

261 262
    before { admin }

263
    it "updates user with new bio" do
264
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
265
      expect(response).to have_http_status(200)
266 267
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
268 269
    end

270 271
    it "updates user with organization" do
      put api("/users/#{user.id}", admin), { organization: 'GitLab' }
272

273 274 275 276 277
      expect(response).to have_http_status(200)
      expect(json_response['organization']).to eq('GitLab')
      expect(user.reload.organization).to eq('GitLab')
    end

278
    it 'updates user with his own email' do
279
      put api("/users/#{user.id}", admin), email: user.email
280
      expect(response).to have_http_status(200)
281 282
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
283 284
    end

285
    it 'updates user with his own username' do
286
      put api("/users/#{user.id}", admin), username: user.username
287
      expect(response).to have_http_status(200)
288 289
      expect(json_response['username']).to eq(user.username)
      expect(user.reload.username).to eq(user.username)
290 291
    end

292
    it "updates user's existing identity" do
293
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
294
      expect(response).to have_http_status(200)
295 296 297
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

298
    it 'updates user with new identity' do
299
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: '67890'
300
      expect(response).to have_http_status(200)
301 302 303 304
      expect(user.reload.identities.first.extern_uid).to eq('67890')
      expect(user.reload.identities.first.provider).to eq('github')
    end

305
    it "updates admin status" do
306
      put api("/users/#{user.id}", admin), { admin: true }
307
      expect(response).to have_http_status(200)
308 309
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
310 311
    end

312
    it "updates external status" do
313 314 315 316 317 318
      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

319
    it "does not update admin status" do
320
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
321
      expect(response).to have_http_status(200)
322 323 324
      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)
325 326
    end

327
    it "does not allow invalid update" do
328
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
329
      expect(response).to have_http_status(400)
330
      expect(user.reload.email).not_to eq('invalid email')
331 332
    end

333
    it "is not available for non admin users" do
334
      put api("/users/#{user.id}", user), attributes_for(:user)
335
      expect(response).to have_http_status(403)
336 337
    end

338
    it "returns 404 for non-existing user" do
339
      put api("/users/999999", admin), { bio: 'update should fail' }
340
      expect(response).to have_http_status(404)
341
      expect(json_response['message']).to eq('404 Not found')
342 343
    end

344
    it "returns a 404 if invalid ID" do
345 346
      put api("/users/ASDF", admin)

347
      expect(response).to have_http_status(404)
348 349
    end

350
    it 'returns 400 error if user does not validate' do
351
      put api("/users/#{user.id}", admin),
352 353 354 355 356 357
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
358
      expect(response).to have_http_status(400)
359
      expect(json_response['message']['password']).
360
        to eq(['is too short (minimum is 8 characters)'])
361
      expect(json_response['message']['bio']).
362
        to eq(['is too long (maximum is 255 characters)'])
363
      expect(json_response['message']['projects_limit']).
364
        to eq(['must be greater than or equal to 0'])
365
      expect(json_response['message']['username']).
366
        to eq([Gitlab::Regex.namespace_regex_message])
367
    end
368 369

    context "with existing user" do
370
      before do
371 372
        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' }
373
        @user = User.all.last
374
      end
375

376
      it 'returns 409 conflict error if email address exists' do
377
        put api("/users/#{@user.id}", admin), email: 'test@example.com'
378
        expect(response).to have_http_status(409)
379
        expect(@user.reload.email).to eq(@user.email)
380 381
      end

382
      it 'returns 409 conflict error if username taken' do
383 384
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
385
        expect(response).to have_http_status(409)
386
        expect(@user.reload.username).to eq(@user.username)
387
      end
388
    end
389 390
  end

Angus MacArthur's avatar
Angus MacArthur committed
391 392 393
  describe "POST /users/:id/keys" do
    before { admin }

394
    it "does not create invalid ssh key" do
Angus MacArthur's avatar
Angus MacArthur committed
395
      post api("/users/#{user.id}/keys", admin), { title: "invalid key" }
396
      expect(response).to have_http_status(400)
397
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
398 399
    end

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

406
    it "creates ssh key" do
Angus MacArthur's avatar
Angus MacArthur committed
407
      key_attrs = attributes_for :key
408
      expect do
Angus MacArthur's avatar
Angus MacArthur committed
409
        post api("/users/#{user.id}/keys", admin), key_attrs
410
      end.to change{ user.keys.count }.by(1)
Angus MacArthur's avatar
Angus MacArthur committed
411
    end
412

413
    it "returns 400 for invalid ID" do
Connor Shea's avatar
Connor Shea committed
414
      post api("/users/999999/keys", admin)
415
      expect(response).to have_http_status(400)
416
    end
Angus MacArthur's avatar
Angus MacArthur committed
417 418
  end

419 420 421 422
  describe 'GET /user/:uid/keys' do
    before { admin }

    context 'when unauthenticated' do
423
      it 'returns authentication error' do
424
        get api("/users/#{user.id}/keys")
425
        expect(response).to have_http_status(401)
426 427 428 429
      end
    end

    context 'when authenticated' do
430
      it 'returns 404 for non-existing user' do
431
        get api('/users/999999/keys', admin)
432
        expect(response).to have_http_status(404)
433
        expect(json_response['message']).to eq('404 User Not Found')
434 435
      end

436
      it 'returns array of ssh keys' do
437 438 439
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
440
        expect(response).to have_http_status(200)
441 442
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
443 444 445 446 447 448 449 450
      end
    end
  end

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

    context 'when unauthenticated' do
451
      it 'returns authentication error' do
452
        delete api("/users/#{user.id}/keys/42")
453
        expect(response).to have_http_status(401)
454 455 456 457
      end
    end

    context 'when authenticated' do
458
      it 'deletes existing key' do
459 460
        user.keys << key
        user.save
461
        expect do
462
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
463
        end.to change { user.keys.count }.by(-1)
464
        expect(response).to have_http_status(200)
465 466
      end

467
      it 'returns 404 error if user not found' do
468 469 470
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
471
        expect(response).to have_http_status(404)
472
        expect(json_response['message']).to eq('404 User Not Found')
473 474
      end

475
      it 'returns 404 error if key not foud' do
476
        delete api("/users/#{user.id}/keys/42", admin)
477
        expect(response).to have_http_status(404)
478
        expect(json_response['message']).to eq('404 Key Not Found')
479 480 481 482
      end
    end
  end

483 484 485
  describe "POST /users/:id/emails" do
    before { admin }

486
    it "does not create invalid email" do
Douwe Maan's avatar
Douwe Maan committed
487
      post api("/users/#{user.id}/emails", admin), {}
488
      expect(response).to have_http_status(400)
489 490 491
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end

492
    it "creates email" do
493 494 495 496 497
      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
498

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

502
      expect(response).to have_http_status(400)
503
    end
504 505 506 507 508 509
  end

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

    context 'when unauthenticated' do
510
      it 'returns authentication error' do
511
        get api("/users/#{user.id}/emails")
512
        expect(response).to have_http_status(401)
513 514 515 516
      end
    end

    context 'when authenticated' do
517
      it 'returns 404 for non-existing user' do
518
        get api('/users/999999/emails', admin)
519
        expect(response).to have_http_status(404)
520 521 522
        expect(json_response['message']).to eq('404 User Not Found')
      end

523
      it 'returns array of emails' do
524 525 526
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
527
        expect(response).to have_http_status(200)
528 529 530
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
531

532
      it "returns a 404 for invalid ID" do
533
        put api("/users/ASDF/emails", admin)
534

535
        expect(response).to have_http_status(404)
536
      end
537 538 539 540 541 542 543
    end
  end

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

    context 'when unauthenticated' do
544
      it 'returns authentication error' do
545
        delete api("/users/#{user.id}/emails/42")
546
        expect(response).to have_http_status(401)
547 548 549 550
      end
    end

    context 'when authenticated' do
551
      it 'deletes existing email' do
552 553 554 555 556
        user.emails << email
        user.save
        expect do
          delete api("/users/#{user.id}/emails/#{email.id}", admin)
        end.to change { user.emails.count }.by(-1)
557
        expect(response).to have_http_status(200)
558 559
      end

560
      it 'returns 404 error if user not found' do
561 562 563
        user.emails << email
        user.save
        delete api("/users/999999/emails/#{email.id}", admin)
564
        expect(response).to have_http_status(404)
565 566 567
        expect(json_response['message']).to eq('404 User Not Found')
      end

568
      it 'returns 404 error if email not foud' do
569
        delete api("/users/#{user.id}/emails/42", admin)
570
        expect(response).to have_http_status(404)
571 572
        expect(json_response['message']).to eq('404 Email Not Found')
      end
573

574
      it "returns a 404 for invalid ID" do
575 576
        delete api("/users/ASDF/emails/bar", admin)

577
        expect(response).to have_http_status(404)
578
      end
579 580 581
    end
  end

582
  describe "DELETE /users/:id" do
583
    let!(:namespace) { user.namespace }
584 585
    before { admin }

586
    it "deletes user" do
587
      delete api("/users/#{user.id}", admin)
588
      expect(response).to have_http_status(200)
589
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
590
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
591
      expect(json_response['email']).to eq(user.email)
592 593
    end

594
    it "does not delete for unauthenticated user" do
595
      delete api("/users/#{user.id}")
596
      expect(response).to have_http_status(401)
597 598
    end

599
    it "is not available for non admin users" do
600
      delete api("/users/#{user.id}", user)
601
      expect(response).to have_http_status(403)
602 603
    end

604
    it "returns 404 for non-existing user" do
605
      delete api("/users/999999", admin)
606
      expect(response).to have_http_status(404)
607
      expect(json_response['message']).to eq('404 User Not Found')
608
    end
609

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

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

Nihad Abbasov's avatar
Nihad Abbasov committed
617
  describe "GET /user" do
618
    it "returns current user" do
Robert Speicher's avatar
Robert Speicher committed
619
      get api("/user", user)
620
      expect(response).to have_http_status(200)
621 622 623 624 625
      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)
626
      expect(json_response['private_token']).to be_blank
Nihad Abbasov's avatar
Nihad Abbasov committed
627
    end
628

629
    it "returns 401 error if user is unauthenticated" do
630
      get api("/user")
631
      expect(response).to have_http_status(401)
632
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
633
  end
634 635 636

  describe "GET /user/keys" do
    context "when unauthenticated" do
637
      it "returns authentication error" do
638
        get api("/user/keys")
639
        expect(response).to have_http_status(401)
640 641
      end
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
642

643
    context "when authenticated" do
644
      it "returns array of ssh keys" do
645 646 647
        user.keys << key
        user.save
        get api("/user/keys", user)
648
        expect(response).to have_http_status(200)
649 650
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
651 652 653 654 655
      end
    end
  end

  describe "GET /user/keys/:id" do
656
    it "returns single key" do
657 658 659
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
660
      expect(response).to have_http_status(200)
661
      expect(json_response["title"]).to eq(key.title)
662
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
663

664
    it "returns 404 Not Found within invalid ID" do
665
      get api("/user/keys/42", user)
666

667
      expect(response).to have_http_status(404)
668
      expect(json_response['message']).to eq('404 Not found')
669 670
    end

671
    it "returns 404 error if admin accesses user's ssh key" do
672 673 674 675
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
676
      expect(response).to have_http_status(404)
677
      expect(json_response['message']).to eq('404 Not found')
678
    end
679

680
    it "returns 404 for invalid ID" do
681
      get api("/users/keys/ASDF", admin)
682

683
      expect(response).to have_http_status(404)
684
    end
685
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
686

687
  describe "POST /user/keys" do
688
    it "creates ssh key" do
689
      key_attrs = attributes_for :key
690
      expect do
691
        post api("/user/keys", user), key_attrs
692
      end.to change{ user.keys.count }.by(1)
693
      expect(response).to have_http_status(201)
694 695
    end

696
    it "returns a 401 error if unauthorized" do
697
      post api("/user/keys"), title: 'some title', key: 'some key'
698
      expect(response).to have_http_status(401)
699 700
    end

701
    it "does not create ssh key without key" do
702
      post api("/user/keys", user), title: 'title'
703
      expect(response).to have_http_status(400)
704
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
705 706
    end

707
    it 'does not create ssh key without title' do
708
      post api('/user/keys', user), key: 'some key'
709
      expect(response).to have_http_status(400)
710
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
711 712
    end

713
    it "does not create ssh key without title" do
714
      post api("/user/keys", user), key: "somekey"
715
      expect(response).to have_http_status(400)
716 717 718 719
    end
  end

  describe "DELETE /user/keys/:id" do
720
    it "deletes existed key" do
721 722
      user.keys << key
      user.save
723
      expect do
724
        delete api("/user/keys/#{key.id}", user)
725
      end.to change{user.keys.count}.by(-1)
726
      expect(response).to have_http_status(200)
727
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
728

729
    it "returns success if key ID not found" do
730
      delete api("/user/keys/42", user)
731
      expect(response).to have_http_status(200)
732 733
    end

734
    it "returns 401 error if unauthorized" do
735 736 737
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
738
      expect(response).to have_http_status(401)
739
    end
740

741
    it "returns a 404 for invalid ID" do
742 743
      delete api("/users/keys/ASDF", admin)

744
      expect(response).to have_http_status(404)
745
    end
746
  end
747

748 749
  describe "GET /user/emails" do
    context "when unauthenticated" do
750
      it "returns authentication error" do
751
        get api("/user/emails")
752
        expect(response).to have_http_status(401)
753 754 755 756
      end
    end

    context "when authenticated" do
757
      it "returns array of emails" do
758 759 760
        user.emails << email
        user.save
        get api("/user/emails", user)
761
        expect(response).to have_http_status(200)
762 763 764 765 766 767 768
        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
769
    it "returns single email" do
770 771 772
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
773
      expect(response).to have_http_status(200)
774 775 776
      expect(json_response["email"]).to eq(email.email)
    end

777
    it "returns 404 Not Found within invalid ID" do
778
      get api("/user/emails/42", user)
779
      expect(response).to have_http_status(404)
780 781 782
      expect(json_response['message']).to eq('404 Not found')
    end

783
    it "returns 404 error if admin accesses user's email" do
784 785 786 787
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
788
      expect(response).to have_http_status(404)
789 790
      expect(json_response['message']).to eq('404 Not found')
    end
791

792
    it "returns 404 for invalid ID" do
793
      get api("/users/emails/ASDF", admin)
794

795
      expect(response).to have_http_status(404)
796
    end
797 798 799
  end

  describe "POST /user/emails" do
800
    it "creates email" do
801 802 803 804
      email_attrs = attributes_for :email
      expect do
        post api("/user/emails", user), email_attrs
      end.to change{ user.emails.count }.by(1)
805
      expect(response).to have_http_status(201)
806 807
    end

808
    it "returns a 401 error if unauthorized" do
809
      post api("/user/emails"), email: 'some email'
810
      expect(response).to have_http_status(401)
811 812
    end

813
    it "does not create email with invalid email" do
814
      post api("/user/emails", user), {}
815
      expect(response).to have_http_status(400)
816 817 818 819 820
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end
  end

  describe "DELETE /user/emails/:id" do
821
    it "deletes existed email" do
822 823 824 825 826
      user.emails << email
      user.save
      expect do
        delete api("/user/emails/#{email.id}", user)
      end.to change{user.emails.count}.by(-1)
827
      expect(response).to have_http_status(200)
828 829
    end

830
    it "returns success if email ID not found" do
831
      delete api("/user/emails/42", user)
832
      expect(response).to have_http_status(200)
833 834
    end

835
    it "returns 401 error if unauthorized" do
836 837 838
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
839
      expect(response).to have_http_status(401)
840
    end
841

842
    it "returns a 404 for invalid ID" do
843 844
      delete api("/users/emails/ASDF", admin)

845
      expect(response).to have_http_status(404)
846
    end
847 848
  end

849
  describe 'PUT /users/:id/block' do
850
    before { admin }
851
    it 'blocks existing user' do
852
      put api("/users/#{user.id}/block", admin)
853
      expect(response).to have_http_status(200)
854 855 856
      expect(user.reload.state).to eq('blocked')
    end

857
    it 'does not re-block ldap blocked users' do
858
      put api("/users/#{ldap_blocked_user.id}/block", admin)
859
      expect(response).to have_http_status(403)
860 861 862
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

863
    it 'does not be available for non admin users' do
864
      put api("/users/#{user.id}/block", user)
865
      expect(response).to have_http_status(403)
866 867 868
      expect(user.reload.state).to eq('active')
    end

869
    it 'returns a 404 error if user id not found' do
870
      put api('/users/9999/block', admin)
871
      expect(response).to have_http_status(404)
872 873 874 875
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

876
  describe 'PUT /users/:id/unblock' do
877
    let(:blocked_user)  { create(:user, state: 'blocked') }
878
    before { admin }
879

880
    it 'unblocks existing user' do
881
      put api("/users/#{user.id}/unblock", admin)
882
      expect(response).to have_http_status(200)
883 884 885
      expect(user.reload.state).to eq('active')
    end

886
    it 'unblocks a blocked user' do
887
      put api("/users/#{blocked_user.id}/unblock", admin)
888
      expect(response).to have_http_status(200)
889 890 891
      expect(blocked_user.reload.state).to eq('active')
    end

892
    it 'does not unblock ldap blocked users' do
893
      put api("/users/#{ldap_blocked_user.id}/unblock", admin)
894
      expect(response).to have_http_status(403)
895
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
896 897
    end

898
    it 'does not be available for non admin users' do
899
      put api("/users/#{user.id}/unblock", user)
900
      expect(response).to have_http_status(403)
901 902 903
      expect(user.reload.state).to eq('active')
    end

904
    it 'returns a 404 error if user id not found' do
905
      put api('/users/9999/block', admin)
906
      expect(response).to have_http_status(404)
907 908
      expect(json_response['message']).to eq('404 User Not Found')
    end
909

910
    it "returns a 404 for invalid ID" do
911 912
      put api("/users/ASDF/block", admin)

913
      expect(response).to have_http_status(404)
914
    end
915
  end
916

917
  describe 'GET /users/:id/events' do
918 919 920 921 922 923 924 925 926 927 928
    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
929 930 931
        other_user = create(:user)

        get api("/users/#{user.id}/events", other_user)
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

        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
947
          comment_event = json_response.find { |e| e['action_name'] == 'commented on' }
948

Rémy Coutable's avatar
Rémy Coutable committed
949 950 951 952
          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!')
953

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

Rémy Coutable's avatar
Rémy Coutable committed
956 957 958
          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)
959 960 961 962 963 964 965 966 967 968 969
        end
      end
    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
Nihad Abbasov's avatar
Nihad Abbasov committed
970
end