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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      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
276
    end
V
Valeriy Sizov 已提交
277 278
  end

M
Marin Jankovski 已提交
279
  describe "GET /users/sign_up" do
280
    it "redirects to sign in page" do
281
      get "/users/sign_up"
Z
Z.J. van de Weg 已提交
282
      expect(response).to have_http_status(302)
283
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
284 285 286
    end
  end

287
  describe "PUT /users/:id" do
288 289
    let!(:admin_user) { create(:admin) }

290 291
    before { admin }

292
    it "updates user with new bio" do
293
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
Z
Z.J. van de Weg 已提交
294
      expect(response).to have_http_status(200)
295 296
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
297 298
    end

299 300
    it "updates user with organization" do
      put api("/users/#{user.id}", admin), { organization: 'GitLab' }
301

302 303 304 305 306
      expect(response).to have_http_status(200)
      expect(json_response['organization']).to eq('GitLab')
      expect(user.reload.organization).to eq('GitLab')
    end

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

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

321
    it "updates user's existing identity" do
322
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
Z
Z.J. van de Weg 已提交
323
      expect(response).to have_http_status(200)
324 325 326
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

327
    it 'updates user with new identity' do
R
Robert Schilling 已提交
328
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: 'john'
Z
Z.J. van de Weg 已提交
329
      expect(response).to have_http_status(200)
R
Robert Schilling 已提交
330
      expect(user.reload.identities.first.extern_uid).to eq('john')
331 332 333
      expect(user.reload.identities.first.provider).to eq('github')
    end

334
    it "updates admin status" do
335
      put api("/users/#{user.id}", admin), { admin: true }
Z
Z.J. van de Weg 已提交
336
      expect(response).to have_http_status(200)
337 338
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
339 340
    end

341
    it "updates external status" do
342 343 344 345 346 347
      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

348
    it "does not update admin status" do
349
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
Z
Z.J. van de Weg 已提交
350
      expect(response).to have_http_status(200)
351 352 353
      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)
354 355
    end

356
    it "does not allow invalid update" do
357
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
Z
Z.J. van de Weg 已提交
358
      expect(response).to have_http_status(400)
359
      expect(user.reload.email).not_to eq('invalid email')
360 361
    end

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

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

373
    it "returns a 404 if invalid ID" do
374 375
      put api("/users/ASDF", admin)

376
      expect(response).to have_http_status(404)
377 378
    end

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

R
Robert Schilling 已提交
398 399 400 401 402 403 404 405 406 407 408 409
    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

410
    context "with existing user" do
411
      before do
412 413
        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 已提交
414
        @user = User.all.last
415
      end
416

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

423
      it 'returns 409 conflict error if username taken' do
J
jubianchi 已提交
424 425
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
Z
Z.J. van de Weg 已提交
426
        expect(response).to have_http_status(409)
427
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
428
      end
429
    end
430 431
  end

A
Angus MacArthur 已提交
432 433 434
  describe "POST /users/:id/keys" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
438
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
439
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
440 441
    end

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

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

449
    it "creates ssh key" do
A
Angus MacArthur 已提交
450
      key_attrs = attributes_for :key
451
      expect do
A
Angus MacArthur 已提交
452
        post api("/users/#{user.id}/keys", admin), key_attrs
453
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
454
    end
455

456
    it "returns 400 for invalid ID" do
C
Connor Shea 已提交
457
      post api("/users/999999/keys", admin)
458
      expect(response).to have_http_status(400)
459
    end
A
Angus MacArthur 已提交
460 461
  end

R
Robert Schilling 已提交
462
  describe 'GET /user/:id/keys' do
463 464 465
    before { admin }

    context 'when unauthenticated' do
466
      it 'returns authentication error' do
467
        get api("/users/#{user.id}/keys")
Z
Z.J. van de Weg 已提交
468
        expect(response).to have_http_status(401)
469 470 471 472
      end
    end

    context 'when authenticated' do
473
      it 'returns 404 for non-existing user' do
474
        get api('/users/999999/keys', admin)
Z
Z.J. van de Weg 已提交
475
        expect(response).to have_http_status(404)
476
        expect(json_response['message']).to eq('404 User Not Found')
477 478
      end

479
      it 'returns array of ssh keys' do
480 481 482
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
Z
Z.J. van de Weg 已提交
483
        expect(response).to have_http_status(200)
484 485
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
486 487 488 489
      end
    end
  end

R
Robert Schilling 已提交
490
  describe 'DELETE /user/:id/keys/:key_id' do
491 492 493
    before { admin }

    context 'when unauthenticated' do
494
      it 'returns authentication error' do
495
        delete api("/users/#{user.id}/keys/42")
Z
Z.J. van de Weg 已提交
496
        expect(response).to have_http_status(401)
497 498 499 500
      end
    end

    context 'when authenticated' do
501
      it 'deletes existing key' do
502 503
        user.keys << key
        user.save
504
        expect do
505
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
506
        end.to change { user.keys.count }.by(-1)
Z
Z.J. van de Weg 已提交
507
        expect(response).to have_http_status(200)
508 509
      end

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

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

526 527 528
  describe "POST /users/:id/emails" do
    before { admin }

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

Z
Z.J. van de Weg 已提交
532
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
533
      expect(json_response['error']).to eq('email is missing')
534 535
    end

536
    it "creates email" do
537 538 539 540 541
      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
542

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

546
      expect(response).to have_http_status(400)
547
    end
548 549
  end

R
Robert Schilling 已提交
550
  describe 'GET /user/:id/emails' do
551 552 553
    before { admin }

    context 'when unauthenticated' do
554
      it 'returns authentication error' do
555
        get api("/users/#{user.id}/emails")
Z
Z.J. van de Weg 已提交
556
        expect(response).to have_http_status(401)
557 558 559 560
      end
    end

    context 'when authenticated' do
561
      it 'returns 404 for non-existing user' do
562
        get api('/users/999999/emails', admin)
Z
Z.J. van de Weg 已提交
563
        expect(response).to have_http_status(404)
564 565 566
        expect(json_response['message']).to eq('404 User Not Found')
      end

567
      it 'returns array of emails' do
568 569 570
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
Z
Z.J. van de Weg 已提交
571
        expect(response).to have_http_status(200)
572 573 574
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
575

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

579
        expect(response).to have_http_status(404)
580
      end
581 582 583
    end
  end

R
Robert Schilling 已提交
584
  describe 'DELETE /user/:id/emails/:email_id' do
585 586 587
    before { admin }

    context 'when unauthenticated' do
588
      it 'returns authentication error' do
589
        delete api("/users/#{user.id}/emails/42")
Z
Z.J. van de Weg 已提交
590
        expect(response).to have_http_status(401)
591 592 593 594
      end
    end

    context 'when authenticated' do
595
      it 'deletes existing email' do
596 597 598 599 600
        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 已提交
601
        expect(response).to have_http_status(200)
602 603
      end

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

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

618
      it "returns a 404 for invalid ID" do
619 620
        delete api("/users/ASDF/emails/bar", admin)

621
        expect(response).to have_http_status(404)
622
      end
623 624 625
    end
  end

626
  describe "DELETE /users/:id" do
627
    let!(:namespace) { user.namespace }
628 629
    before { admin }

630
    it "deletes user" do
631
      delete api("/users/#{user.id}", admin)
Z
Z.J. van de Weg 已提交
632
      expect(response).to have_http_status(200)
633
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
634
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
635
      expect(json_response['email']).to eq(user.email)
636 637
    end

638
    it "does not delete for unauthenticated user" do
639
      delete api("/users/#{user.id}")
Z
Z.J. van de Weg 已提交
640
      expect(response).to have_http_status(401)
641 642
    end

643
    it "is not available for non admin users" do
644
      delete api("/users/#{user.id}", user)
Z
Z.J. van de Weg 已提交
645
      expect(response).to have_http_status(403)
646 647
    end

648
    it "returns 404 for non-existing user" do
649
      delete api("/users/999999", admin)
Z
Z.J. van de Weg 已提交
650
      expect(response).to have_http_status(404)
651
      expect(json_response['message']).to eq('404 User Not Found')
652
    end
653

654
    it "returns a 404 for invalid ID" do
655 656
      delete api("/users/ASDF", admin)

657
      expect(response).to have_http_status(404)
658
    end
659 660
  end

N
Nihad Abbasov 已提交
661
  describe "GET /user" do
662
    let(:personal_access_token) { create(:personal_access_token, user: user).token }
663 664 665 666

    context 'with regular user' do
      context 'with personal access token' do
        it 'returns 403 without private token when sudo is defined' do
667
          get api("/user?private_token=#{personal_access_token}&sudo=123")
668 669 670 671 672 673 674

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

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

          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')
686
        expect(json_response['id']).to eq(user.id)
687
      end
N
Nihad Abbasov 已提交
688
    end
689

690
    context 'with admin' do
691
      let(:admin_personal_access_token) { create(:personal_access_token, user: admin).token }
692 693 694

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

          expect(response).to have_http_status(403)
        end

700 701
        it 'returns initial current user without private token when sudo not defined' do
          get api("/user?private_token=#{admin_personal_access_token}")
702 703 704

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
705
          expect(json_response['id']).to eq(admin.id)
706 707 708 709
        end
      end

      context 'with private token' do
710 711
        it 'returns sudoed user with private token when sudo defined' do
          get api("/user?private_token=#{admin.private_token}&sudo=#{user.id}")
712 713 714

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

718 719
        it 'returns initial current user without private token when sudo not defined' do
          get api("/user?private_token=#{admin.private_token}")
720 721 722

          expect(response).to have_http_status(200)
          expect(response).to match_response_schema('user/public')
723
          expect(json_response['id']).to eq(admin.id)
724 725 726 727 728 729 730 731 732 733
        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
734
    end
N
Nihad Abbasov 已提交
735
  end
736 737 738

  describe "GET /user/keys" do
    context "when unauthenticated" do
739
      it "returns authentication error" do
740
        get api("/user/keys")
Z
Z.J. van de Weg 已提交
741
        expect(response).to have_http_status(401)
742 743
      end
    end
N
Nihad Abbasov 已提交
744

745
    context "when authenticated" do
746
      it "returns array of ssh keys" do
747 748 749
        user.keys << key
        user.save
        get api("/user/keys", user)
Z
Z.J. van de Weg 已提交
750
        expect(response).to have_http_status(200)
751 752
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
753 754 755 756
      end
    end
  end

R
Robert Schilling 已提交
757
  describe "GET /user/keys/:key_id" do
758
    it "returns single key" do
759 760 761
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
Z
Z.J. van de Weg 已提交
762
      expect(response).to have_http_status(200)
763
      expect(json_response["title"]).to eq(key.title)
764
    end
N
Nihad Abbasov 已提交
765

766
    it "returns 404 Not Found within invalid ID" do
767
      get api("/user/keys/42", user)
768

Z
Z.J. van de Weg 已提交
769
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
770
      expect(json_response['message']).to eq('404 Key Not Found')
771 772
    end

773
    it "returns 404 error if admin accesses user's ssh key" do
774 775 776 777
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
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
    end
781

782
    it "returns 404 for invalid ID" do
783
      get api("/users/keys/ASDF", admin)
784

785
      expect(response).to have_http_status(404)
786
    end
787
  end
N
Nihad Abbasov 已提交
788

789
  describe "POST /user/keys" do
790
    it "creates ssh key" do
791
      key_attrs = attributes_for :key
792
      expect do
793
        post api("/user/keys", user), key_attrs
794
      end.to change{ user.keys.count }.by(1)
Z
Z.J. van de Weg 已提交
795
      expect(response).to have_http_status(201)
796 797
    end

798
    it "returns a 401 error if unauthorized" do
799
      post api("/user/keys"), title: 'some title', key: 'some key'
Z
Z.J. van de Weg 已提交
800
      expect(response).to have_http_status(401)
801 802
    end

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

Z
Z.J. van de Weg 已提交
806
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
807
      expect(json_response['error']).to eq('key is missing')
J
jubianchi 已提交
808 809
    end

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

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

817
    it "does not create ssh key without title" do
818
      post api("/user/keys", user), key: "somekey"
Z
Z.J. van de Weg 已提交
819
      expect(response).to have_http_status(400)
820 821 822
    end
  end

R
Robert Schilling 已提交
823
  describe "DELETE /user/keys/:key_id" do
824
    it "deletes existed key" do
825 826
      user.keys << key
      user.save
827
      expect do
828
        delete api("/user/keys/#{key.id}", user)
829
      end.to change{user.keys.count}.by(-1)
Z
Z.J. van de Weg 已提交
830
      expect(response).to have_http_status(200)
831
    end
N
Nihad Abbasov 已提交
832

R
Robert Schilling 已提交
833
    it "returns 404 if key ID not found" do
834
      delete api("/user/keys/42", user)
R
Robert Schilling 已提交
835 836 837

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

840
    it "returns 401 error if unauthorized" do
841 842 843
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
Z
Z.J. van de Weg 已提交
844
      expect(response).to have_http_status(401)
845
    end
846

847
    it "returns a 404 for invalid ID" do
848 849
      delete api("/users/keys/ASDF", admin)

850
      expect(response).to have_http_status(404)
851
    end
852
  end
853

854 855
  describe "GET /user/emails" do
    context "when unauthenticated" do
856
      it "returns authentication error" do
857
        get api("/user/emails")
Z
Z.J. van de Weg 已提交
858
        expect(response).to have_http_status(401)
859 860 861 862
      end
    end

    context "when authenticated" do
863
      it "returns array of emails" do
864 865 866
        user.emails << email
        user.save
        get api("/user/emails", user)
Z
Z.J. van de Weg 已提交
867
        expect(response).to have_http_status(200)
868 869 870 871 872 873
        expect(json_response).to be_an Array
        expect(json_response.first["email"]).to eq(email.email)
      end
    end
  end

R
Robert Schilling 已提交
874
  describe "GET /user/emails/:email_id" do
875
    it "returns single email" do
876 877 878
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
Z
Z.J. van de Weg 已提交
879
      expect(response).to have_http_status(200)
880 881 882
      expect(json_response["email"]).to eq(email.email)
    end

883
    it "returns 404 Not Found within invalid ID" do
884
      get api("/user/emails/42", user)
Z
Z.J. van de Weg 已提交
885
      expect(response).to have_http_status(404)
R
Robert Schilling 已提交
886
      expect(json_response['message']).to eq('404 Email Not Found')
887 888
    end

889
    it "returns 404 error if admin accesses user's email" do
890 891 892 893
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
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
    end
897

898
    it "returns 404 for invalid ID" do
899
      get api("/users/emails/ASDF", admin)
900

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

  describe "POST /user/emails" do
906
    it "creates email" do
907 908 909 910
      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 已提交
911
      expect(response).to have_http_status(201)
912 913
    end

914
    it "returns a 401 error if unauthorized" do
915
      post api("/user/emails"), email: 'some email'
Z
Z.J. van de Weg 已提交
916
      expect(response).to have_http_status(401)
917 918
    end

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

Z
Z.J. van de Weg 已提交
922
      expect(response).to have_http_status(400)
R
Robert Schilling 已提交
923
      expect(json_response['error']).to eq('email is missing')
924 925 926
    end
  end

R
Robert Schilling 已提交
927
  describe "DELETE /user/emails/:email_id" do
928
    it "deletes existed email" do
929 930 931 932 933
      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 已提交
934
      expect(response).to have_http_status(200)
935 936
    end

R
Robert Schilling 已提交
937
    it "returns 404 if email ID not found" do
938
      delete api("/user/emails/42", user)
R
Robert Schilling 已提交
939 940 941

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

944
    it "returns 401 error if unauthorized" do
945 946 947
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
Z
Z.J. van de Weg 已提交
948
      expect(response).to have_http_status(401)
949
    end
950

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

R
Robert Schilling 已提交
954
      expect(response).to have_http_status(400)
955
    end
956 957
  end

958
  describe 'PUT /users/:id/block' do
959
    before { admin }
960
    it 'blocks existing user' do
961
      put api("/users/#{user.id}/block", admin)
Z
Z.J. van de Weg 已提交
962
      expect(response).to have_http_status(200)
963 964 965
      expect(user.reload.state).to eq('blocked')
    end

966
    it 'does not re-block ldap blocked users' do
967
      put api("/users/#{ldap_blocked_user.id}/block", admin)
Z
Z.J. van de Weg 已提交
968
      expect(response).to have_http_status(403)
969 970 971
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

972
    it 'does not be available for non admin users' do
973
      put api("/users/#{user.id}/block", user)
Z
Z.J. van de Weg 已提交
974
      expect(response).to have_http_status(403)
975 976 977
      expect(user.reload.state).to eq('active')
    end

978
    it 'returns a 404 error if user id not found' do
979
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
980
      expect(response).to have_http_status(404)
981 982 983 984
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

985
  describe 'PUT /users/:id/unblock' do
986
    let(:blocked_user)  { create(:user, state: 'blocked') }
987
    before { admin }
988

989
    it 'unblocks existing user' do
990
      put api("/users/#{user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
991
      expect(response).to have_http_status(200)
992 993 994
      expect(user.reload.state).to eq('active')
    end

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

1001
    it 'does not unblock ldap blocked users' do
1002
      put api("/users/#{ldap_blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
1003
      expect(response).to have_http_status(403)
1004
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
1005 1006
    end

1007
    it 'does not be available for non admin users' do
1008
      put api("/users/#{user.id}/unblock", user)
Z
Z.J. van de Weg 已提交
1009
      expect(response).to have_http_status(403)
1010 1011 1012
      expect(user.reload.state).to eq('active')
    end

1013
    it 'returns a 404 error if user id not found' do
1014
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
1015
      expect(response).to have_http_status(404)
1016 1017
      expect(json_response['message']).to eq('404 User Not Found')
    end
1018

1019
    it "returns a 404 for invalid ID" do
1020 1021
      put api("/users/ASDF/block", admin)

1022
      expect(response).to have_http_status(404)
1023
    end
1024
  end
1025

1026
  describe 'GET /users/:id/events' do
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    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 已提交
1038 1039 1040
        other_user = create(:user)

        get api("/users/#{user.id}/events", other_user)
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

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

R
Rémy Coutable 已提交
1058 1059 1060 1061
          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!')
1062

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

R
Rémy Coutable 已提交
1065 1066 1067
          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)
1068 1069
        end
      end
A
Airat Shigapov 已提交
1070

1071
      context 'when there are multiple events from different projects' do
1072 1073
        let(:second_note) { create(:note_on_issue, project: create(:empty_project)) }
        let(:third_note) { create(:note_on_issue, project: project) }
A
Airat Shigapov 已提交
1074 1075

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

1078 1079 1080
          [second_note, third_note].each do |note|
            EventCreateService.new.leave_note(note, user)
          end
A
Airat Shigapov 已提交
1081 1082
        end

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

1086 1087
          comment_events = json_response.select { |e| e['action_name'] == 'commented on' }

1088 1089 1090
          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 已提交
1091 1092
        end
      end
1093 1094 1095 1096 1097 1098 1099 1100 1101
    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 已提交
1102
end