users_spec.rb 41.5 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') }
13
  let(:not_existing_user_id) { (User.maximum('id') || 0 ) + 10 }
N
Nihad Abbasov 已提交
14 15

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

23
    context "when authenticated" do
F
Felipe Artur 已提交
24
      # These specs are written just in case API authentication is not required anymore
F
Felipe Artur 已提交
25 26 27 28 29 30 31 32
      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 已提交
33
          expect(response).to have_http_status(403)
F
Felipe Artur 已提交
34 35 36 37
        end

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

42
      it "returns an array of users" do
R
Robert Speicher 已提交
43
        get api("/users", user)
44

Z
Z.J. van de Weg 已提交
45
        expect(response).to have_http_status(200)
46
        expect(response).to include_pagination_headers
47
        expect(json_response).to be_an Array
M
Marin Jankovski 已提交
48
        username = user.username
49 50 51
        expect(json_response.detect do |user|
          user['username'] == username
        end['username']).to eq(username)
N
Nihad Abbasov 已提交
52
      end
53

54 55 56 57 58 59 60
      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)
61
        expect(response).to include_pagination_headers
62 63 64 65
        expect(json_response).to be_an Array
        expect(json_response).to all(include('state' => /(blocked|ldap_blocked)/))
      end

66
      it "returns one user" do
67
        get api("/users?username=#{omniauth_user.username}", user)
68

Z
Z.J. van de Weg 已提交
69
        expect(response).to have_http_status(200)
70
        expect(response).to include_pagination_headers
71 72 73
        expect(json_response).to be_an Array
        expect(json_response.first['username']).to eq(omniauth_user.username)
      end
N
Nihad Abbasov 已提交
74
    end
75 76

    context "when admin" do
77
      it "returns an array of users" do
78
        get api("/users", admin)
79

Z
Z.J. van de Weg 已提交
80
        expect(response).to have_http_status(200)
81
        expect(response).to include_pagination_headers
82 83
        expect(json_response).to be_an Array
        expect(json_response.first.keys).to include 'email'
84
        expect(json_response.first.keys).to include 'organization'
85 86
        expect(json_response.first.keys).to include 'identities'
        expect(json_response.first.keys).to include 'can_create_project'
S
Stan Hu 已提交
87
        expect(json_response.first.keys).to include 'two_factor_enabled'
88 89
        expect(json_response.first.keys).to include 'last_sign_in_at'
        expect(json_response.first.keys).to include 'confirmed_at'
90
      end
91 92 93 94 95 96 97

      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)
98
        expect(response).to include_pagination_headers
99 100 101
        expect(json_response).to be_an Array
        expect(json_response).to all(include('external' => true))
      end
102
    end
N
Nihad Abbasov 已提交
103 104 105
  end

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

112
    it "returns a 401 if unauthenticated" do
113
      get api("/users/9998")
Z
Z.J. van de Weg 已提交
114
      expect(response).to have_http_status(401)
115
    end
V
Valeriy Sizov 已提交
116

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

123
    it "returns a 404 for invalid ID" do
124
      get api("/users/1ASDF", user)
125

126
      expect(response).to have_http_status(404)
127
    end
128 129 130 131
  end

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

133
    it "creates user" do
134
      expect do
135
        post api("/users", admin), attributes_for(:user, projects_limit: 3)
136
      end.to change { User.count }.by(1)
V
Valeriy Sizov 已提交
137 138
    end

139
    it "creates user with correct attributes" do
140
      post api('/users', admin), attributes_for(:user, admin: true, can_create_group: true)
Z
Z.J. van de Weg 已提交
141
      expect(response).to have_http_status(201)
142 143
      user_id = json_response['id']
      new_user = User.find(user_id)
144 145 146
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(true)
      expect(new_user.can_create_group).to eq(true)
147 148
    end

149 150 151 152 153 154 155 156 157
    it "creates user with optional attributes" do
      optional_attributes = { confirm: true }
      attributes = attributes_for(:user).merge(optional_attributes)

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

      expect(response).to have_http_status(201)
    end

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

168
    it "creates non-admin users by default" do
169
      post api('/users', admin), attributes_for(:user)
Z
Z.J. van de Weg 已提交
170
      expect(response).to have_http_status(201)
171 172
      user_id = json_response['id']
      new_user = User.find(user_id)
173 174
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
175 176
    end

177
    it "returns 201 Created on success" do
178
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
Z
Z.J. van de Weg 已提交
179
      expect(response).to have_http_status(201)
180 181
    end

Z
Zeger-Jan van de Weg 已提交
182 183
    it 'creates non-external users by default' do
      post api("/users", admin), attributes_for(:user)
Z
Z.J. van de Weg 已提交
184
      expect(response).to have_http_status(201)
Z
Zeger-Jan van de Weg 已提交
185 186 187 188 189 190 191

      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

192
    it 'allows an external user to be created' do
Z
Zeger-Jan van de Weg 已提交
193
      post api("/users", admin), attributes_for(:user, external: true)
Z
Z.J. van de Weg 已提交
194
      expect(response).to have_http_status(201)
Z
Zeger-Jan van de Weg 已提交
195 196 197 198 199 200 201

      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

202 203 204 205 206 207 208 209 210 211 212 213
    it "creates user with reset password" do
      post api('/users', admin), attributes_for(:user, reset_password: true).except(:password)

      expect(response).to have_http_status(201)

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

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

214
    it "does not create user with invalid email" do
J
jubianchi 已提交
215
      post api('/users', admin),
216 217 218
        email: 'invalid email',
        password: 'password',
        name: 'test'
Z
Z.J. van de Weg 已提交
219
      expect(response).to have_http_status(400)
220 221
    end

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

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

232
    it 'returns 400 error if email not given' do
233
      post api('/users', admin), attributes_for(:user).except(:email)
Z
Z.J. van de Weg 已提交
234
      expect(response).to have_http_status(400)
235 236
    end

237
    it 'returns 400 error if username not given' do
238
      post api('/users', admin), attributes_for(:user).except(:username)
Z
Z.J. van de Weg 已提交
239
      expect(response).to have_http_status(400)
J
jubianchi 已提交
240 241
    end

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

261
    it "is not available for non admin users" do
262
      post api("/users", user), attributes_for(:user)
Z
Z.J. van de Weg 已提交
263
      expect(response).to have_http_status(403)
V
Valeriy Sizov 已提交
264
    end
265

J
jubianchi 已提交
266 267 268
    context 'with existing user' do
      before do
        post api('/users', admin),
269 270 271 272
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
J
jubianchi 已提交
273
      end
274

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

287
      it 'returns 409 conflict error if same username exists' do
J
jubianchi 已提交
288 289
        expect do
          post api('/users', admin),
290 291 292 293
            name: 'foo',
            email: 'foo@example.com',
            password: 'password',
            username: 'test'
J
jubianchi 已提交
294
        end.to change { User.count }.by(0)
Z
Z.J. van de Weg 已提交
295
        expect(response).to have_http_status(409)
296
        expect(json_response['message']).to eq('Username has already been taken')
297
      end
298 299 300 301 302 303 304 305

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

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

M
Marin Jankovski 已提交
309
  describe "GET /users/sign_up" do
310
    it "redirects to sign in page" do
311
      get "/users/sign_up"
Z
Z.J. van de Weg 已提交
312
      expect(response).to have_http_status(302)
313
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
314 315 316
    end
  end

317
  describe "PUT /users/:id" do
318 319
    let!(:admin_user) { create(:admin) }

320 321
    before { admin }

322
    it "updates user with new bio" do
323
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
Z
Z.J. van de Weg 已提交
324
      expect(response).to have_http_status(200)
325 326
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
327 328
    end

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

332
      expect(response).to have_http_status(200)
333
      expect(user.reload.password_expires_at).to be <= Time.now
334 335
    end

336 337
    it "updates user with organization" do
      put api("/users/#{user.id}", admin), { organization: 'GitLab' }
338

339 340 341 342 343
      expect(response).to have_http_status(200)
      expect(json_response['organization']).to eq('GitLab')
      expect(user.reload.organization).to eq('GitLab')
    end

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

351
    it 'updates user with his own username' do
J
jubianchi 已提交
352
      put api("/users/#{user.id}", admin), username: user.username
Z
Z.J. van de Weg 已提交
353
      expect(response).to have_http_status(200)
354 355
      expect(json_response['username']).to eq(user.username)
      expect(user.reload.username).to eq(user.username)
J
jubianchi 已提交
356 357
    end

358
    it "updates user's existing identity" do
359
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
Z
Z.J. van de Weg 已提交
360
      expect(response).to have_http_status(200)
361 362 363
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

364
    it 'updates user with new identity' do
R
Robert Schilling 已提交
365
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: 'john'
Z
Z.J. van de Weg 已提交
366
      expect(response).to have_http_status(200)
R
Robert Schilling 已提交
367
      expect(user.reload.identities.first.extern_uid).to eq('john')
368 369 370
      expect(user.reload.identities.first.provider).to eq('github')
    end

371
    it "updates admin status" do
372
      put api("/users/#{user.id}", admin), { admin: true }
Z
Z.J. van de Weg 已提交
373
      expect(response).to have_http_status(200)
374 375
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
376 377
    end

378
    it "updates external status" do
379 380 381 382 383 384
      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

385
    it "does not update admin status" do
386
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
Z
Z.J. van de Weg 已提交
387
      expect(response).to have_http_status(200)
388 389 390
      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)
391 392
    end

393
    it "does not allow invalid update" do
394
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
Z
Z.J. van de Weg 已提交
395
      expect(response).to have_http_status(400)
396
      expect(user.reload.email).not_to eq('invalid email')
397 398
    end

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

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

410
    it "returns a 404 if invalid ID" do
411 412
      put api("/users/ASDF", admin)

413
      expect(response).to have_http_status(404)
414 415
    end

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

R
Robert Schilling 已提交
435 436 437 438 439 440 441 442 443 444 445 446
    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

447
    context "with existing user" do
448
      before do
449 450
        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 已提交
451
        @user = User.all.last
452
      end
453

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

460
      it 'returns 409 conflict error if username taken' do
J
jubianchi 已提交
461 462
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
Z
Z.J. van de Weg 已提交
463
        expect(response).to have_http_status(409)
464
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
465
      end
466
    end
467 468
  end

A
Angus MacArthur 已提交
469 470 471
  describe "POST /users/:id/keys" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
475
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
476
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
477 478
    end

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

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

486
    it "creates ssh key" do
A
Angus MacArthur 已提交
487
      key_attrs = attributes_for :key
488
      expect do
A
Angus MacArthur 已提交
489
        post api("/users/#{user.id}/keys", admin), key_attrs
490
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
491
    end
492

493
    it "returns 400 for invalid ID" do
C
Connor Shea 已提交
494
      post api("/users/999999/keys", admin)
495
      expect(response).to have_http_status(400)
496
    end
A
Angus MacArthur 已提交
497 498
  end

R
Robert Schilling 已提交
499
  describe 'GET /user/:id/keys' do
500 501 502
    before { admin }

    context 'when unauthenticated' do
503
      it 'returns authentication error' do
504
        get api("/users/#{user.id}/keys")
Z
Z.J. van de Weg 已提交
505
        expect(response).to have_http_status(401)
506 507 508 509
      end
    end

    context 'when authenticated' do
510
      it 'returns 404 for non-existing user' do
511
        get api('/users/999999/keys', admin)
Z
Z.J. van de Weg 已提交
512
        expect(response).to have_http_status(404)
513
        expect(json_response['message']).to eq('404 User Not Found')
514 515
      end

516
      it 'returns array of ssh keys' do
517 518
        user.keys << key
        user.save
519

520
        get api("/users/#{user.id}/keys", admin)
521

Z
Z.J. van de Weg 已提交
522
        expect(response).to have_http_status(200)
523
        expect(response).to include_pagination_headers
524 525
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
526 527 528 529
      end
    end
  end

R
Robert Schilling 已提交
530
  describe 'DELETE /user/:id/keys/:key_id' do
531 532 533
    before { admin }

    context 'when unauthenticated' do
534
      it 'returns authentication error' do
535
        delete api("/users/#{user.id}/keys/42")
Z
Z.J. van de Weg 已提交
536
        expect(response).to have_http_status(401)
537 538 539 540
      end
    end

    context 'when authenticated' do
541
      it 'deletes existing key' do
542 543
        user.keys << key
        user.save
544

545
        expect do
546
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
547 548

          expect(response).to have_http_status(204)
549
        end.to change { user.keys.count }.by(-1)
550 551
      end

552
      it 'returns 404 error if user not found' do
553 554 555
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
556
        expect(response).to have_http_status(404)
557
        expect(json_response['message']).to eq('404 User Not Found')
558 559
      end

560
      it 'returns 404 error if key not foud' do
561
        delete api("/users/#{user.id}/keys/42", admin)
Z
Z.J. van de Weg 已提交
562
        expect(response).to have_http_status(404)
563
        expect(json_response['message']).to eq('404 Key Not Found')
564 565 566 567
      end
    end
  end

568 569 570
  describe "POST /users/:id/emails" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
574
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
575
      expect(json_response['error']).to eq('email is missing')
576 577
    end

578
    it "creates email" do
579 580 581 582 583
      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
584

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

588
      expect(response).to have_http_status(400)
589
    end
590 591
  end

R
Robert Schilling 已提交
592
  describe 'GET /user/:id/emails' do
593 594 595
    before { admin }

    context 'when unauthenticated' do
596
      it 'returns authentication error' do
597
        get api("/users/#{user.id}/emails")
Z
Z.J. van de Weg 已提交
598
        expect(response).to have_http_status(401)
599 600 601 602
      end
    end

    context 'when authenticated' do
603
      it 'returns 404 for non-existing user' do
604
        get api('/users/999999/emails', admin)
Z
Z.J. van de Weg 已提交
605
        expect(response).to have_http_status(404)
606 607 608
        expect(json_response['message']).to eq('404 User Not Found')
      end

609
      it 'returns array of emails' do
610 611
        user.emails << email
        user.save
612

613
        get api("/users/#{user.id}/emails", admin)
614

Z
Z.J. van de Weg 已提交
615
        expect(response).to have_http_status(200)
616
        expect(response).to include_pagination_headers
617 618 619
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
620

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

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

R
Robert Schilling 已提交
629
  describe 'DELETE /user/:id/emails/:email_id' do
630 631 632
    before { admin }

    context 'when unauthenticated' do
633
      it 'returns authentication error' do
634
        delete api("/users/#{user.id}/emails/42")
Z
Z.J. van de Weg 已提交
635
        expect(response).to have_http_status(401)
636 637 638 639
      end
    end

    context 'when authenticated' do
640
      it 'deletes existing email' do
641 642
        user.emails << email
        user.save
643

644 645
        expect do
          delete api("/users/#{user.id}/emails/#{email.id}", admin)
646 647

          expect(response).to have_http_status(204)
648 649 650
        end.to change { user.emails.count }.by(-1)
      end

651
      it 'returns 404 error if user not found' do
652 653 654
        user.emails << email
        user.save
        delete api("/users/999999/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
655
        expect(response).to have_http_status(404)
656 657 658
        expect(json_response['message']).to eq('404 User Not Found')
      end

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

665
      it "returns a 404 for invalid ID" do
666 667
        delete api("/users/ASDF/emails/bar", admin)

668
        expect(response).to have_http_status(404)
669
      end
670 671 672
    end
  end

673
  describe "DELETE /users/:id" do
674
    let!(:namespace) { user.namespace }
675 676
    before { admin }

677
    it "deletes user" do
678
      delete api("/users/#{user.id}", admin)
679 680

      expect(response).to have_http_status(204)
681
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
682
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
683 684
    end

685
    it "does not delete for unauthenticated user" do
686
      delete api("/users/#{user.id}")
Z
Z.J. van de Weg 已提交
687
      expect(response).to have_http_status(401)
688 689
    end

690
    it "is not available for non admin users" do
691
      delete api("/users/#{user.id}", user)
Z
Z.J. van de Weg 已提交
692
      expect(response).to have_http_status(403)
693 694
    end

695
    it "returns 404 for non-existing user" do
696
      delete api("/users/999999", admin)
Z
Z.J. van de Weg 已提交
697
      expect(response).to have_http_status(404)
698
      expect(json_response['message']).to eq('404 User Not Found')
699
    end
700

701
    it "returns a 404 for invalid ID" do
702 703
      delete api("/users/ASDF", admin)

704
      expect(response).to have_http_status(404)
705
    end
706 707
  end

N
Nihad Abbasov 已提交
708
  describe "GET /user" do
709
    let(:personal_access_token) { create(:personal_access_token, user: user).token }
710 711 712 713

    context 'with regular user' do
      context 'with personal access token' do
        it 'returns 403 without private token when sudo is defined' do
714
          get api("/user?private_token=#{personal_access_token}&sudo=123")
715 716 717 718 719 720 721

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

      context 'with private token' do
        it 'returns 403 without private token when sudo defined' do
722
          get api("/user?private_token=#{user.private_token}&sudo=123")
723 724 725 726 727 728 729 730 731 732

          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')
733
        expect(json_response['id']).to eq(user.id)
734
      end
N
Nihad Abbasov 已提交
735
    end
736

737
    context 'with admin' do
738
      let(:admin_personal_access_token) { create(:personal_access_token, user: admin).token }
739 740 741

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

          expect(response).to have_http_status(403)
        end

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

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

      context 'with private token' do
757 758
        it 'returns sudoed user with private token when sudo defined' do
          get api("/user?private_token=#{admin.private_token}&sudo=#{user.id}")
759 760 761

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/login')
762
          expect(json_response['id']).to eq(user.id)
763 764
        end

765 766
        it 'returns initial current user without private token when sudo not defined' do
          get api("/user?private_token=#{admin.private_token}")
767 768 769

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
770
          expect(json_response['id']).to eq(admin.id)
771 772 773 774 775 776 777 778 779 780
        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
781
    end
N
Nihad Abbasov 已提交
782
  end
783 784 785

  describe "GET /user/keys" do
    context "when unauthenticated" do
786
      it "returns authentication error" do
787
        get api("/user/keys")
Z
Z.J. van de Weg 已提交
788
        expect(response).to have_http_status(401)
789 790
      end
    end
N
Nihad Abbasov 已提交
791

792
    context "when authenticated" do
793
      it "returns array of ssh keys" do
794 795
        user.keys << key
        user.save
796

797
        get api("/user/keys", user)
798

Z
Z.J. van de Weg 已提交
799
        expect(response).to have_http_status(200)
800
        expect(response).to include_pagination_headers
801 802
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
803 804 805 806
      end
    end
  end

R
Robert Schilling 已提交
807
  describe "GET /user/keys/:key_id" do
808
    it "returns single key" do
809 810 811
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
Z
Z.J. van de Weg 已提交
812
      expect(response).to have_http_status(200)
813
      expect(json_response["title"]).to eq(key.title)
814
    end
N
Nihad Abbasov 已提交
815

816
    it "returns 404 Not Found within invalid ID" do
817
      get api("/user/keys/42", user)
818

Z
Z.J. van de Weg 已提交
819
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
820
      expect(json_response['message']).to eq('404 Key Not Found')
821 822
    end

823
    it "returns 404 error if admin accesses user's ssh key" do
824 825 826 827
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
828
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
829
      expect(json_response['message']).to eq('404 Key Not Found')
830
    end
831

832
    it "returns 404 for invalid ID" do
833
      get api("/users/keys/ASDF", admin)
834

835
      expect(response).to have_http_status(404)
836
    end
837
  end
N
Nihad Abbasov 已提交
838

839
  describe "POST /user/keys" do
840
    it "creates ssh key" do
841
      key_attrs = attributes_for :key
842
      expect do
843
        post api("/user/keys", user), key_attrs
844
      end.to change{ user.keys.count }.by(1)
Z
Z.J. van de Weg 已提交
845
      expect(response).to have_http_status(201)
846 847
    end

848
    it "returns a 401 error if unauthorized" do
849
      post api("/user/keys"), title: 'some title', key: 'some key'
Z
Z.J. van de Weg 已提交
850
      expect(response).to have_http_status(401)
851 852
    end

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

Z
Z.J. van de Weg 已提交
856
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
857
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
858 859
    end

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

Z
Z.J. van de Weg 已提交
863
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
864
      expect(json_response['error']).to eq('title is missing')
865 866
    end

867
    it "does not create ssh key without title" do
868
      post api("/user/keys", user), key: "somekey"
Z
Z.J. van de Weg 已提交
869
      expect(response).to have_http_status(400)
870 871 872
    end
  end

R
Robert Schilling 已提交
873
  describe "DELETE /user/keys/:key_id" do
874
    it "deletes existed key" do
875 876
      user.keys << key
      user.save
877

878
      expect do
879
        delete api("/user/keys/#{key.id}", user)
880 881

        expect(response).to have_http_status(204)
882
      end.to change{user.keys.count}.by(-1)
883
    end
N
Nihad Abbasov 已提交
884

R
Robert Schilling 已提交
885
    it "returns 404 if key ID not found" do
886
      delete api("/user/keys/42", user)
R
Robert Schilling 已提交
887 888 889

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

892
    it "returns 401 error if unauthorized" do
893 894 895
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
Z
Z.J. van de Weg 已提交
896
      expect(response).to have_http_status(401)
897
    end
898

899
    it "returns a 404 for invalid ID" do
900 901
      delete api("/users/keys/ASDF", admin)

902
      expect(response).to have_http_status(404)
903
    end
904
  end
905

906 907
  describe "GET /user/emails" do
    context "when unauthenticated" do
908
      it "returns authentication error" do
909
        get api("/user/emails")
Z
Z.J. van de Weg 已提交
910
        expect(response).to have_http_status(401)
911 912 913 914
      end
    end

    context "when authenticated" do
915
      it "returns array of emails" do
916 917
        user.emails << email
        user.save
918

919
        get api("/user/emails", user)
920

Z
Z.J. van de Weg 已提交
921
        expect(response).to have_http_status(200)
922
        expect(response).to include_pagination_headers
923 924 925 926 927 928
        expect(json_response).to be_an Array
        expect(json_response.first["email"]).to eq(email.email)
      end
    end
  end

R
Robert Schilling 已提交
929
  describe "GET /user/emails/:email_id" do
930
    it "returns single email" do
931 932 933
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
Z
Z.J. van de Weg 已提交
934
      expect(response).to have_http_status(200)
935 936 937
      expect(json_response["email"]).to eq(email.email)
    end

938
    it "returns 404 Not Found within invalid ID" do
939
      get api("/user/emails/42", user)
Z
Z.J. van de Weg 已提交
940
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
941
      expect(json_response['message']).to eq('404 Email Not Found')
942 943
    end

944
    it "returns 404 error if admin accesses user's email" do
945 946 947 948
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
949
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
950
      expect(json_response['message']).to eq('404 Email Not Found')
951
    end
952

953
    it "returns 404 for invalid ID" do
954
      get api("/users/emails/ASDF", admin)
955

956
      expect(response).to have_http_status(404)
957
    end
958 959 960
  end

  describe "POST /user/emails" do
961
    it "creates email" do
962 963 964 965
      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 已提交
966
      expect(response).to have_http_status(201)
967 968
    end

969
    it "returns a 401 error if unauthorized" do
970
      post api("/user/emails"), email: 'some email'
Z
Z.J. van de Weg 已提交
971
      expect(response).to have_http_status(401)
972 973
    end

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

Z
Z.J. van de Weg 已提交
977
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
978
      expect(json_response['error']).to eq('email is missing')
979 980 981
    end
  end

R
Robert Schilling 已提交
982
  describe "DELETE /user/emails/:email_id" do
983
    it "deletes existed email" do
984 985
      user.emails << email
      user.save
986

987 988
      expect do
        delete api("/user/emails/#{email.id}", user)
989 990

        expect(response).to have_http_status(204)
991 992 993
      end.to change{user.emails.count}.by(-1)
    end

R
Robert Schilling 已提交
994
    it "returns 404 if email ID not found" do
995
      delete api("/user/emails/42", user)
R
Robert Schilling 已提交
996 997 998

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

1001
    it "returns 401 error if unauthorized" do
1002 1003 1004
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
Z
Z.J. van de Weg 已提交
1005
      expect(response).to have_http_status(401)
1006
    end
1007

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

R
Robert Schilling 已提交
1011
      expect(response).to have_http_status(400)
1012
    end
1013 1014
  end

1015
  describe 'POST /users/:id/block' do
1016
    before { admin }
1017
    it 'blocks existing user' do
1018 1019
      post api("/users/#{user.id}/block", admin)
      expect(response).to have_http_status(201)
1020 1021 1022
      expect(user.reload.state).to eq('blocked')
    end

1023
    it 'does not re-block ldap blocked users' do
1024
      post api("/users/#{ldap_blocked_user.id}/block", admin)
Z
Z.J. van de Weg 已提交
1025
      expect(response).to have_http_status(403)
1026 1027 1028
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

1029
    it 'does not be available for non admin users' do
1030
      post api("/users/#{user.id}/block", user)
Z
Z.J. van de Weg 已提交
1031
      expect(response).to have_http_status(403)
1032 1033 1034
      expect(user.reload.state).to eq('active')
    end

1035
    it 'returns a 404 error if user id not found' do
1036
      post api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
1037
      expect(response).to have_http_status(404)
1038 1039 1040 1041
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

1042
  describe 'POST /users/:id/unblock' do
1043
    let(:blocked_user)  { create(:user, state: 'blocked') }
1044
    before { admin }
1045

1046
    it 'unblocks existing user' do
1047 1048
      post api("/users/#{user.id}/unblock", admin)
      expect(response).to have_http_status(201)
1049 1050 1051
      expect(user.reload.state).to eq('active')
    end

1052
    it 'unblocks a blocked user' do
1053 1054
      post api("/users/#{blocked_user.id}/unblock", admin)
      expect(response).to have_http_status(201)
1055 1056 1057
      expect(blocked_user.reload.state).to eq('active')
    end

1058
    it 'does not unblock ldap blocked users' do
1059
      post api("/users/#{ldap_blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1060
      expect(response).to have_http_status(403)
1061
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
1062 1063
    end

1064
    it 'does not be available for non admin users' do
1065
      post api("/users/#{user.id}/unblock", user)
Z
Z.J. van de Weg 已提交
1066
      expect(response).to have_http_status(403)
1067 1068 1069
      expect(user.reload.state).to eq('active')
    end

1070
    it 'returns a 404 error if user id not found' do
1071
      post api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
1072
      expect(response).to have_http_status(404)
1073 1074
      expect(json_response['message']).to eq('404 User Not Found')
    end
1075

1076
    it "returns a 404 for invalid ID" do
1077
      post api("/users/ASDF/block", admin)
1078

1079
      expect(response).to have_http_status(404)
1080
    end
1081
  end
1082

1083
  describe 'GET /users/:id/events' do
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    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 已提交
1095 1096 1097
        other_user = create(:user)

        get api("/users/#{user.id}/events", other_user)
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

        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
      context 'joined event' do
        it 'returns the "joined" event' do
          get api("/users/#{user.id}/events", user)

1109 1110 1111 1112
          expect(response).to have_http_status(200)
          expect(response).to include_pagination_headers
          expect(json_response).to be_an Array

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

R
Rémy Coutable 已提交
1115 1116 1117 1118
          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!')
1119

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

R
Rémy Coutable 已提交
1122 1123 1124
          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)
1125 1126
        end
      end
A
Airat Shigapov 已提交
1127

1128
      context 'when there are multiple events from different projects' do
1129 1130
        let(:second_note) { create(:note_on_issue, project: create(:empty_project)) }
        let(:third_note) { create(:note_on_issue, project: project) }
A
Airat Shigapov 已提交
1131 1132

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

1135 1136 1137
          [second_note, third_note].each do |note|
            EventCreateService.new.leave_note(note, user)
          end
A
Airat Shigapov 已提交
1138 1139
        end

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

1143 1144
          comment_events = json_response.select { |e| e['action_name'] == 'commented on' }

1145 1146 1147
          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 已提交
1148 1149
        end
      end
1150 1151 1152 1153 1154 1155 1156 1157 1158
    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
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

  describe 'GET /users/:user_id/personal_access_tokens' do
    let!(:active_personal_access_token) { create(:personal_access_token, user: user) }
    let!(:revoked_personal_access_token) { create(:revoked_personal_access_token, user: user) }
    let!(:expired_personal_access_token) { create(:expired_personal_access_token, user: user) }

    it 'returns a 404 error if user not found' do
      get api("/users/#{not_existing_user_id}/personal_access_tokens", admin)

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

    it 'returns a 403 error when authenticated as normal user' do
      get api("/users/#{not_existing_user_id}/personal_access_tokens", user)

      expect(response).to have_http_status(403)
      expect(json_response['message']).to eq('403 Forbidden')
    end

    it 'returns an array of personal access tokens' do
      get api("/users/#{user.id}/personal_access_tokens", admin)

      expect(response).to have_http_status(200)
      expect(json_response).to be_an Array
      expect(json_response.size).to eq(3)
      expect(json_response.detect do |personal_access_token|
        personal_access_token['id'] == active_personal_access_token.id
      end['token']).to eq(active_personal_access_token.token)
    end

    it 'returns an array of active personal access tokens if active is set to true' do
      get api("/users/#{user.id}/personal_access_tokens?state=active", admin)

      expect(response).to have_http_status(200)
      expect(json_response).to be_an Array
      expect(json_response).to all(include('active' => true))
    end

    it 'returns an array of inactive personal access tokens if active is set to false' do
      get api("/users/#{user.id}/personal_access_tokens?state=inactive", admin)

      expect(response).to have_http_status(200)
      expect(json_response).to be_an Array
      expect(json_response).to all(include('active' => false))
    end
  end

  describe 'POST /users/:user_id/personal_access_tokens' do
    let(:name) { 'my new pat' }
    let(:expires_at) { '2016-12-28' }
    let(:scopes) { ['api', 'read_user'] }

    it 'returns validation error if personal access token miss some attributes' do
      post api("/users/#{user.id}/personal_access_tokens", admin)

      expect(response).to have_http_status(400)
      expect(json_response['error']).to eq('name is missing')
    end

    it 'returns a 404 error if user not found' do
      post api("/users/#{not_existing_user_id}/personal_access_tokens", admin),
        name: name,
        expires_at: expires_at

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

    it 'returns a 403 error when authenticated as normal user' do
      post api("/users/#{user.id}/personal_access_tokens", user),
        name: name,
        expires_at: expires_at

      expect(response).to have_http_status(403)
      expect(json_response['message']).to eq('403 Forbidden')
    end

    it 'creates a personal access token' do
      post api("/users/#{user.id}/personal_access_tokens", admin),
        name: name,
        expires_at: expires_at,
        scopes: scopes

      expect(response).to have_http_status(201)

      personal_access_token_id = json_response['id']

      expect(json_response['name']).to eq(name)
      expect(json_response['scopes']).to eq(scopes)
      expect(json_response['expires_at']).to eq(expires_at)
      expect(json_response['id']).to be_present
      expect(json_response['created_at']).to be_present
      expect(json_response['active']).to eq(false)
      expect(json_response['revoked']).to eq(false)
      expect(json_response['token']).to be_present
      expect(PersonalAccessToken.find(personal_access_token_id)).not_to eq(nil)
    end
  end

  describe 'DELETE /users/:id/personal_access_tokens/:personal_access_token_id' do
    let!(:personal_access_token) { create(:personal_access_token, user: user, revoked: false) }

    it 'returns a 404 error if user not found' do
      delete api("/users/#{not_existing_user_id}/personal_access_tokens/1", admin)

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

    it 'returns a 404 error if personal access token not found' do
      delete api("/users/#{user.id}/personal_access_tokens/42", admin)

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

    it 'returns a 403 error when authenticated as normal user' do
      delete api("/users/#{user.id}/personal_access_tokens/#{personal_access_token.id}", user)

      expect(response).to have_http_status(403)
      expect(json_response['message']).to eq('403 Forbidden')
    end

    it 'revokes a personal access token' do
      delete api("/users/#{user.id}/personal_access_tokens/#{personal_access_token.id}", admin)

      expect(response).to have_http_status(200)
      expect(personal_access_token.revoked).to eq(false)
      expect(personal_access_token.reload.revoked).to eq(true)
      expect(json_response['revoked']).to eq(true)
      expect(json_response['token']).to be_present
    end
  end
N
Nihad Abbasov 已提交
1293
end