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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      expect(response).to have_http_status(201)
    end

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

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

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

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

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

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

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

193
    it "does not create user with invalid email" do
J
jubianchi 已提交
194
      post api('/users', admin),
195 196 197
        email: 'invalid email',
        password: 'password',
        name: 'test'
Z
Z.J. van de Weg 已提交
198
      expect(response).to have_http_status(400)
199 200
    end

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

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

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

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

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

240
    it "is not available for non admin users" do
241
      post api("/users", user), attributes_for(:user)
Z
Z.J. van de Weg 已提交
242
      expect(response).to have_http_status(403)
V
Valeriy Sizov 已提交
243
    end
244

J
jubianchi 已提交
245 246 247
    context 'with existing user' do
      before do
        post api('/users', admin),
248 249 250 251
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
J
jubianchi 已提交
252
      end
253

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

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

      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
285
    end
V
Valeriy Sizov 已提交
286 287
  end

M
Marin Jankovski 已提交
288
  describe "GET /users/sign_up" do
289
    it "redirects to sign in page" do
290
      get "/users/sign_up"
Z
Z.J. van de Weg 已提交
291
      expect(response).to have_http_status(302)
292
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
293 294 295
    end
  end

296
  describe "PUT /users/:id" do
297 298
    let!(:admin_user) { create(:admin) }

299 300
    before { admin }

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

308 309
    it "updates user with organization" do
      put api("/users/#{user.id}", admin), { organization: 'GitLab' }
310

311 312 313 314 315
      expect(response).to have_http_status(200)
      expect(json_response['organization']).to eq('GitLab')
      expect(user.reload.organization).to eq('GitLab')
    end

316
    it 'updates user with his own email' do
J
jubianchi 已提交
317
      put api("/users/#{user.id}", admin), email: user.email
Z
Z.J. van de Weg 已提交
318
      expect(response).to have_http_status(200)
319 320
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
J
jubianchi 已提交
321 322
    end

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

330
    it "updates user's existing identity" do
331
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
Z
Z.J. van de Weg 已提交
332
      expect(response).to have_http_status(200)
333 334 335
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

336
    it 'updates user with new identity' do
R
Robert Schilling 已提交
337
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: 'john'
Z
Z.J. van de Weg 已提交
338
      expect(response).to have_http_status(200)
R
Robert Schilling 已提交
339
      expect(user.reload.identities.first.extern_uid).to eq('john')
340 341 342
      expect(user.reload.identities.first.provider).to eq('github')
    end

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

350
    it "updates external status" do
351 352 353 354 355 356
      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

357
    it "does not update admin status" do
358
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
Z
Z.J. van de Weg 已提交
359
      expect(response).to have_http_status(200)
360 361 362
      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)
363 364
    end

365
    it "does not allow invalid update" do
366
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
Z
Z.J. van de Weg 已提交
367
      expect(response).to have_http_status(400)
368
      expect(user.reload.email).not_to eq('invalid email')
369 370
    end

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

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

382
    it "returns a 404 if invalid ID" do
383 384
      put api("/users/ASDF", admin)

385
      expect(response).to have_http_status(404)
386 387
    end

388
    it 'returns 400 error if user does not validate' do
J
jubianchi 已提交
389
      put api("/users/#{user.id}", admin),
390 391 392 393 394 395
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
Z
Z.J. van de Weg 已提交
396
      expect(response).to have_http_status(400)
397
      expect(json_response['message']['password']).
398
        to eq(['is too short (minimum is 8 characters)'])
399
      expect(json_response['message']['bio']).
400
        to eq(['is too long (maximum is 255 characters)'])
401
      expect(json_response['message']['projects_limit']).
402
        to eq(['must be greater than or equal to 0'])
403
      expect(json_response['message']['username']).
R
Robert Speicher 已提交
404
        to eq([Gitlab::Regex.namespace_regex_message])
405
    end
406

R
Robert Schilling 已提交
407 408 409 410 411 412 413 414 415 416 417 418
    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

419
    context "with existing user" do
420
      before do
421 422
        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 已提交
423
        @user = User.all.last
424
      end
425

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

432
      it 'returns 409 conflict error if username taken' do
J
jubianchi 已提交
433 434
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
Z
Z.J. van de Weg 已提交
435
        expect(response).to have_http_status(409)
436
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
437
      end
438
    end
439 440
  end

A
Angus MacArthur 已提交
441 442 443
  describe "POST /users/:id/keys" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
447
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
448
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
449 450
    end

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

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

458
    it "creates ssh key" do
A
Angus MacArthur 已提交
459
      key_attrs = attributes_for :key
460
      expect do
A
Angus MacArthur 已提交
461
        post api("/users/#{user.id}/keys", admin), key_attrs
462
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
463
    end
464

465
    it "returns 400 for invalid ID" do
C
Connor Shea 已提交
466
      post api("/users/999999/keys", admin)
467
      expect(response).to have_http_status(400)
468
    end
A
Angus MacArthur 已提交
469 470
  end

R
Robert Schilling 已提交
471
  describe 'GET /user/:id/keys' do
472 473 474
    before { admin }

    context 'when unauthenticated' do
475
      it 'returns authentication error' do
476
        get api("/users/#{user.id}/keys")
Z
Z.J. van de Weg 已提交
477
        expect(response).to have_http_status(401)
478 479 480 481
      end
    end

    context 'when authenticated' do
482
      it 'returns 404 for non-existing user' do
483
        get api('/users/999999/keys', admin)
Z
Z.J. van de Weg 已提交
484
        expect(response).to have_http_status(404)
485
        expect(json_response['message']).to eq('404 User Not Found')
486 487
      end

488
      it 'returns array of ssh keys' do
489 490 491
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
Z
Z.J. van de Weg 已提交
492
        expect(response).to have_http_status(200)
493 494
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
495 496 497 498
      end
    end
  end

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

    context 'when unauthenticated' do
503
      it 'returns authentication error' do
504
        delete api("/users/#{user.id}/keys/42")
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 'deletes existing key' do
511 512
        user.keys << key
        user.save
513
        expect do
514
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
515
        end.to change { user.keys.count }.by(-1)
Z
Z.J. van de Weg 已提交
516
        expect(response).to have_http_status(200)
517 518
      end

519
      it 'returns 404 error if user not found' do
520 521 522
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
523
        expect(response).to have_http_status(404)
524
        expect(json_response['message']).to eq('404 User Not Found')
525 526
      end

527
      it 'returns 404 error if key not foud' do
528
        delete api("/users/#{user.id}/keys/42", admin)
Z
Z.J. van de Weg 已提交
529
        expect(response).to have_http_status(404)
530
        expect(json_response['message']).to eq('404 Key Not Found')
531 532 533 534
      end
    end
  end

535 536 537
  describe "POST /users/:id/emails" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
541
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
542
      expect(json_response['error']).to eq('email is missing')
543 544
    end

545
    it "creates email" do
546 547 548 549 550
      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
551

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

555
      expect(response).to have_http_status(400)
556
    end
557 558
  end

R
Robert Schilling 已提交
559
  describe 'GET /user/:id/emails' do
560 561 562
    before { admin }

    context 'when unauthenticated' do
563
      it 'returns authentication error' do
564
        get api("/users/#{user.id}/emails")
Z
Z.J. van de Weg 已提交
565
        expect(response).to have_http_status(401)
566 567 568 569
      end
    end

    context 'when authenticated' do
570
      it 'returns 404 for non-existing user' do
571
        get api('/users/999999/emails', admin)
Z
Z.J. van de Weg 已提交
572
        expect(response).to have_http_status(404)
573 574 575
        expect(json_response['message']).to eq('404 User Not Found')
      end

576
      it 'returns array of emails' do
577 578 579
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
Z
Z.J. van de Weg 已提交
580
        expect(response).to have_http_status(200)
581 582 583
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
584

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

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

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

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

    context 'when authenticated' do
604
      it 'deletes existing email' do
605 606 607 608 609
        user.emails << email
        user.save
        expect do
          delete api("/users/#{user.id}/emails/#{email.id}", admin)
        end.to change { user.emails.count }.by(-1)
Z
Z.J. van de Weg 已提交
610
        expect(response).to have_http_status(200)
611 612
      end

613
      it 'returns 404 error if user not found' do
614 615 616
        user.emails << email
        user.save
        delete api("/users/999999/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
617
        expect(response).to have_http_status(404)
618 619 620
        expect(json_response['message']).to eq('404 User Not Found')
      end

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

627
      it "returns a 404 for invalid ID" do
628 629
        delete api("/users/ASDF/emails/bar", admin)

630
        expect(response).to have_http_status(404)
631
      end
632 633 634
    end
  end

635
  describe "DELETE /users/:id" do
636
    let!(:namespace) { user.namespace }
637 638
    before { admin }

639
    it "deletes user" do
640
      delete api("/users/#{user.id}", admin)
Z
Z.J. van de Weg 已提交
641
      expect(response).to have_http_status(200)
642
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
643
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
644
      expect(json_response['email']).to eq(user.email)
645 646
    end

647
    it "does not delete for unauthenticated user" do
648
      delete api("/users/#{user.id}")
Z
Z.J. van de Weg 已提交
649
      expect(response).to have_http_status(401)
650 651
    end

652
    it "is not available for non admin users" do
653
      delete api("/users/#{user.id}", user)
Z
Z.J. van de Weg 已提交
654
      expect(response).to have_http_status(403)
655 656
    end

657
    it "returns 404 for non-existing user" do
658
      delete api("/users/999999", admin)
Z
Z.J. van de Weg 已提交
659
      expect(response).to have_http_status(404)
660
      expect(json_response['message']).to eq('404 User Not Found')
661
    end
662

663
    it "returns a 404 for invalid ID" do
664 665
      delete api("/users/ASDF", admin)

666
      expect(response).to have_http_status(404)
667
    end
668 669
  end

N
Nihad Abbasov 已提交
670
  describe "GET /user" do
671
    let(:personal_access_token) { create(:personal_access_token, user: user).token }
672 673 674 675

    context 'with regular user' do
      context 'with personal access token' do
        it 'returns 403 without private token when sudo is defined' do
676
          get api("/user?private_token=#{personal_access_token}&sudo=123")
677 678 679 680 681 682 683

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

      context 'with private token' do
        it 'returns 403 without private token when sudo defined' do
684
          get api("/user?private_token=#{user.private_token}&sudo=123")
685 686 687 688 689 690 691 692 693 694

          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')
695
        expect(json_response['id']).to eq(user.id)
696
      end
N
Nihad Abbasov 已提交
697
    end
698

699
    context 'with admin' do
700
      let(:admin_personal_access_token) { create(:personal_access_token, user: admin).token }
701 702 703

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

          expect(response).to have_http_status(403)
        end

709 710
        it 'returns initial current user without private token when sudo not defined' do
          get api("/user?private_token=#{admin_personal_access_token}")
711 712 713

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
714
          expect(json_response['id']).to eq(admin.id)
715 716 717 718
        end
      end

      context 'with private token' do
719 720
        it 'returns sudoed user with private token when sudo defined' do
          get api("/user?private_token=#{admin.private_token}&sudo=#{user.id}")
721 722 723

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/login')
724
          expect(json_response['id']).to eq(user.id)
725 726
        end

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

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
732
          expect(json_response['id']).to eq(admin.id)
733 734 735 736 737 738 739 740 741 742
        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
743
    end
N
Nihad Abbasov 已提交
744
  end
745 746 747

  describe "GET /user/keys" do
    context "when unauthenticated" do
748
      it "returns authentication error" do
749
        get api("/user/keys")
Z
Z.J. van de Weg 已提交
750
        expect(response).to have_http_status(401)
751 752
      end
    end
N
Nihad Abbasov 已提交
753

754
    context "when authenticated" do
755
      it "returns array of ssh keys" do
756 757 758
        user.keys << key
        user.save
        get api("/user/keys", user)
Z
Z.J. van de Weg 已提交
759
        expect(response).to have_http_status(200)
760 761
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
762 763 764 765
      end
    end
  end

R
Robert Schilling 已提交
766
  describe "GET /user/keys/:key_id" do
767
    it "returns single key" do
768 769 770
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
Z
Z.J. van de Weg 已提交
771
      expect(response).to have_http_status(200)
772
      expect(json_response["title"]).to eq(key.title)
773
    end
N
Nihad Abbasov 已提交
774

775
    it "returns 404 Not Found within invalid ID" do
776
      get api("/user/keys/42", user)
777

Z
Z.J. van de Weg 已提交
778
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
779
      expect(json_response['message']).to eq('404 Key Not Found')
780 781
    end

782
    it "returns 404 error if admin accesses user's ssh key" do
783 784 785 786
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
787
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
788
      expect(json_response['message']).to eq('404 Key Not Found')
789
    end
790

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

794
      expect(response).to have_http_status(404)
795
    end
796
  end
N
Nihad Abbasov 已提交
797

798
  describe "POST /user/keys" do
799
    it "creates ssh key" do
800
      key_attrs = attributes_for :key
801
      expect do
802
        post api("/user/keys", user), key_attrs
803
      end.to change{ user.keys.count }.by(1)
Z
Z.J. van de Weg 已提交
804
      expect(response).to have_http_status(201)
805 806
    end

807
    it "returns a 401 error if unauthorized" do
808
      post api("/user/keys"), title: 'some title', key: 'some key'
Z
Z.J. van de Weg 已提交
809
      expect(response).to have_http_status(401)
810 811
    end

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

Z
Z.J. van de Weg 已提交
815
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
816
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
817 818
    end

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

Z
Z.J. van de Weg 已提交
822
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
823
      expect(json_response['error']).to eq('title is missing')
824 825
    end

826
    it "does not create ssh key without title" do
827
      post api("/user/keys", user), key: "somekey"
Z
Z.J. van de Weg 已提交
828
      expect(response).to have_http_status(400)
829 830 831
    end
  end

R
Robert Schilling 已提交
832
  describe "DELETE /user/keys/:key_id" do
833
    it "deletes existed key" do
834 835
      user.keys << key
      user.save
836
      expect do
837
        delete api("/user/keys/#{key.id}", user)
838
      end.to change{user.keys.count}.by(-1)
Z
Z.J. van de Weg 已提交
839
      expect(response).to have_http_status(200)
840
    end
N
Nihad Abbasov 已提交
841

R
Robert Schilling 已提交
842
    it "returns 404 if key ID not found" do
843
      delete api("/user/keys/42", user)
R
Robert Schilling 已提交
844 845 846

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

849
    it "returns 401 error if unauthorized" do
850 851 852
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
Z
Z.J. van de Weg 已提交
853
      expect(response).to have_http_status(401)
854
    end
855

856
    it "returns a 404 for invalid ID" do
857 858
      delete api("/users/keys/ASDF", admin)

859
      expect(response).to have_http_status(404)
860
    end
861
  end
862

863 864
  describe "GET /user/emails" do
    context "when unauthenticated" do
865
      it "returns authentication error" do
866
        get api("/user/emails")
Z
Z.J. van de Weg 已提交
867
        expect(response).to have_http_status(401)
868 869 870 871
      end
    end

    context "when authenticated" do
872
      it "returns array of emails" do
873 874 875
        user.emails << email
        user.save
        get api("/user/emails", user)
Z
Z.J. van de Weg 已提交
876
        expect(response).to have_http_status(200)
877 878 879 880 881 882
        expect(json_response).to be_an Array
        expect(json_response.first["email"]).to eq(email.email)
      end
    end
  end

R
Robert Schilling 已提交
883
  describe "GET /user/emails/:email_id" do
884
    it "returns single email" do
885 886 887
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
Z
Z.J. van de Weg 已提交
888
      expect(response).to have_http_status(200)
889 890 891
      expect(json_response["email"]).to eq(email.email)
    end

892
    it "returns 404 Not Found within invalid ID" do
893
      get api("/user/emails/42", user)
Z
Z.J. van de Weg 已提交
894
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
895
      expect(json_response['message']).to eq('404 Email Not Found')
896 897
    end

898
    it "returns 404 error if admin accesses user's email" do
899 900 901 902
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
903
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
904
      expect(json_response['message']).to eq('404 Email Not Found')
905
    end
906

907
    it "returns 404 for invalid ID" do
908
      get api("/users/emails/ASDF", admin)
909

910
      expect(response).to have_http_status(404)
911
    end
912 913 914
  end

  describe "POST /user/emails" do
915
    it "creates email" do
916 917 918 919
      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 已提交
920
      expect(response).to have_http_status(201)
921 922
    end

923
    it "returns a 401 error if unauthorized" do
924
      post api("/user/emails"), email: 'some email'
Z
Z.J. van de Weg 已提交
925
      expect(response).to have_http_status(401)
926 927
    end

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

Z
Z.J. van de Weg 已提交
931
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
932
      expect(json_response['error']).to eq('email is missing')
933 934 935
    end
  end

R
Robert Schilling 已提交
936
  describe "DELETE /user/emails/:email_id" do
937
    it "deletes existed email" do
938 939 940 941 942
      user.emails << email
      user.save
      expect do
        delete api("/user/emails/#{email.id}", user)
      end.to change{user.emails.count}.by(-1)
Z
Z.J. van de Weg 已提交
943
      expect(response).to have_http_status(200)
944 945
    end

R
Robert Schilling 已提交
946
    it "returns 404 if email ID not found" do
947
      delete api("/user/emails/42", user)
R
Robert Schilling 已提交
948 949 950

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

953
    it "returns 401 error if unauthorized" do
954 955 956
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
Z
Z.J. van de Weg 已提交
957
      expect(response).to have_http_status(401)
958
    end
959

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

R
Robert Schilling 已提交
963
      expect(response).to have_http_status(400)
964
    end
965 966
  end

967
  describe 'PUT /users/:id/block' do
968
    before { admin }
969
    it 'blocks existing user' do
970
      put api("/users/#{user.id}/block", admin)
Z
Z.J. van de Weg 已提交
971
      expect(response).to have_http_status(200)
972 973 974
      expect(user.reload.state).to eq('blocked')
    end

975
    it 'does not re-block ldap blocked users' do
976
      put api("/users/#{ldap_blocked_user.id}/block", admin)
Z
Z.J. van de Weg 已提交
977
      expect(response).to have_http_status(403)
978 979 980
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

981
    it 'does not be available for non admin users' do
982
      put api("/users/#{user.id}/block", user)
Z
Z.J. van de Weg 已提交
983
      expect(response).to have_http_status(403)
984 985 986
      expect(user.reload.state).to eq('active')
    end

987
    it 'returns a 404 error if user id not found' do
988
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
989
      expect(response).to have_http_status(404)
990 991 992 993
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

994
  describe 'PUT /users/:id/unblock' do
995
    let(:blocked_user)  { create(:user, state: 'blocked') }
996
    before { admin }
997

998
    it 'unblocks existing user' do
999
      put api("/users/#{user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1000
      expect(response).to have_http_status(200)
1001 1002 1003
      expect(user.reload.state).to eq('active')
    end

1004
    it 'unblocks a blocked user' do
1005
      put api("/users/#{blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1006
      expect(response).to have_http_status(200)
1007 1008 1009
      expect(blocked_user.reload.state).to eq('active')
    end

1010
    it 'does not unblock ldap blocked users' do
1011
      put api("/users/#{ldap_blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1012
      expect(response).to have_http_status(403)
1013
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
1014 1015
    end

1016
    it 'does not be available for non admin users' do
1017
      put api("/users/#{user.id}/unblock", user)
Z
Z.J. van de Weg 已提交
1018
      expect(response).to have_http_status(403)
1019 1020 1021
      expect(user.reload.state).to eq('active')
    end

1022
    it 'returns a 404 error if user id not found' do
1023
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
1024
      expect(response).to have_http_status(404)
1025 1026
      expect(json_response['message']).to eq('404 User Not Found')
    end
1027

1028
    it "returns a 404 for invalid ID" do
1029 1030
      put api("/users/ASDF/block", admin)

1031
      expect(response).to have_http_status(404)
1032
    end
1033
  end
1034

1035
  describe 'GET /users/:id/events' do
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    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 已提交
1047 1048 1049
        other_user = create(:user)

        get api("/users/#{user.id}/events", other_user)
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

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

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

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

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

R
Rémy Coutable 已提交
1067 1068 1069 1070
          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!')
1071

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

R
Rémy Coutable 已提交
1074 1075 1076
          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)
1077 1078
        end
      end
A
Airat Shigapov 已提交
1079

1080
      context 'when there are multiple events from different projects' do
1081 1082
        let(:second_note) { create(:note_on_issue, project: create(:empty_project)) }
        let(:third_note) { create(:note_on_issue, project: project) }
A
Airat Shigapov 已提交
1083 1084

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

1087 1088 1089
          [second_note, third_note].each do |note|
            EventCreateService.new.leave_note(note, user)
          end
A
Airat Shigapov 已提交
1090 1091
        end

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

1095 1096
          comment_events = json_response.select { |e| e['action_name'] == 'commented on' }

1097 1098 1099
          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 已提交
1100 1101
        end
      end
1102 1103 1104 1105 1106 1107 1108 1109 1110
    end

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

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