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

J
Jeroen van Baarsen 已提交
3
describe API::API, api: true  do
4 5
  include ApiHelpers

6 7 8
  let(:user)  { create(:user) }
  let(:admin) { create(:admin) }
  let(:key)   { create(:key, user: user) }
9
  let(:email)   { create(:email, user: user) }
10
  let(:omniauth_user) { create(:omniauth_user) }
11 12
  let(:ldap_user) { create(:omniauth_user, provider: 'ldapmain') }
  let(:ldap_blocked_user) { create(:omniauth_user, provider: 'ldapmain', state: 'ldap_blocked') }
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
      it "returns one user" do
52
        get api("/users?username=#{omniauth_user.username}", user)
Z
Z.J. van de Weg 已提交
53
        expect(response).to have_http_status(200)
54 55 56
        expect(json_response).to be_an Array
        expect(json_response.first['username']).to eq(omniauth_user.username)
      end
N
Nihad Abbasov 已提交
57
    end
58 59

    context "when admin" do
60
      it "returns an array of users" do
61
        get api("/users", admin)
Z
Z.J. van de Weg 已提交
62
        expect(response).to have_http_status(200)
63 64
        expect(json_response).to be_an Array
        expect(json_response.first.keys).to include 'email'
65
        expect(json_response.first.keys).to include 'organization'
66 67
        expect(json_response.first.keys).to include 'identities'
        expect(json_response.first.keys).to include 'can_create_project'
S
Stan Hu 已提交
68
        expect(json_response.first.keys).to include 'two_factor_enabled'
69 70
        expect(json_response.first.keys).to include 'last_sign_in_at'
        expect(json_response.first.keys).to include 'confirmed_at'
71 72
      end
    end
N
Nihad Abbasov 已提交
73 74 75
  end

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

82
    it "returns a 401 if unauthenticated" do
83
      get api("/users/9998")
Z
Z.J. van de Weg 已提交
84
      expect(response).to have_http_status(401)
85
    end
V
Valeriy Sizov 已提交
86

87
    it "returns a 404 error if user id not found" do
88
      get api("/users/9999", user)
Z
Z.J. van de Weg 已提交
89
      expect(response).to have_http_status(404)
90
      expect(json_response['message']).to eq('404 Not found')
V
Valeriy Sizov 已提交
91
    end
92

93
    it "returns a 404 if invalid ID" do
94
      get api("/users/1ASDF", user)
Z
Z.J. van de Weg 已提交
95
      expect(response).to have_http_status(404)
96
    end
97 98 99 100
  end

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

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

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

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

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

137
    it "returns 201 Created on success" do
138
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
Z
Z.J. van de Weg 已提交
139
      expect(response).to have_http_status(201)
140 141
    end

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

      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

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

      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

162
    it "does not create user with invalid email" do
J
jubianchi 已提交
163
      post api('/users', admin),
164 165 166
        email: 'invalid email',
        password: 'password',
        name: 'test'
Z
Z.J. van de Weg 已提交
167
      expect(response).to have_http_status(400)
168 169
    end

170
    it 'returns 400 error if name not given' do
171
      post api('/users', admin), attributes_for(:user).except(:name)
Z
Z.J. van de Weg 已提交
172
      expect(response).to have_http_status(400)
J
jubianchi 已提交
173 174
    end

175
    it 'returns 400 error if password not given' do
176
      post api('/users', admin), attributes_for(:user).except(:password)
Z
Z.J. van de Weg 已提交
177
      expect(response).to have_http_status(400)
178 179
    end

180
    it 'returns 400 error if email not given' do
181
      post api('/users', admin), attributes_for(:user).except(:email)
Z
Z.J. van de Weg 已提交
182
      expect(response).to have_http_status(400)
183 184
    end

185
    it 'returns 400 error if username not given' do
186
      post api('/users', admin), attributes_for(:user).except(:username)
Z
Z.J. van de Weg 已提交
187
      expect(response).to have_http_status(400)
J
jubianchi 已提交
188 189
    end

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

209
    it "is not available for non admin users" do
210
      post api("/users", user), attributes_for(:user)
Z
Z.J. van de Weg 已提交
211
      expect(response).to have_http_status(403)
V
Valeriy Sizov 已提交
212
    end
213

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

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

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

M
Marin Jankovski 已提交
249
  describe "GET /users/sign_up" do
250
    it "redirects to sign in page" do
251
      get "/users/sign_up"
Z
Z.J. van de Weg 已提交
252
      expect(response).to have_http_status(302)
253
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
254 255 256
    end
  end

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

260 261
    before { admin }

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

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

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

277
    it 'updates user with his own email' do
J
jubianchi 已提交
278
      put api("/users/#{user.id}", admin), email: user.email
Z
Z.J. van de Weg 已提交
279
      expect(response).to have_http_status(200)
280 281
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
J
jubianchi 已提交
282 283
    end

284
    it 'updates user with his own username' do
J
jubianchi 已提交
285
      put api("/users/#{user.id}", admin), username: user.username
Z
Z.J. van de Weg 已提交
286
      expect(response).to have_http_status(200)
287 288
      expect(json_response['username']).to eq(user.username)
      expect(user.reload.username).to eq(user.username)
J
jubianchi 已提交
289 290
    end

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

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

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

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

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

326
    it "does not allow invalid update" do
327
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
Z
Z.J. van de Weg 已提交
328
      expect(response).to have_http_status(400)
329
      expect(user.reload.email).not_to eq('invalid email')
330 331
    end

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

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

343
    it "raises error for invalid ID" do
344 345 346
      expect{put api("/users/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end

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

    context "with existing user" do
367
      before do
368 369
        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 已提交
370
        @user = User.all.last
371
      end
372

373
      it 'returns 409 conflict error if email address exists' do
J
jubianchi 已提交
374
        put api("/users/#{@user.id}", admin), email: 'test@example.com'
Z
Z.J. van de Weg 已提交
375
        expect(response).to have_http_status(409)
376
        expect(@user.reload.email).to eq(@user.email)
J
jubianchi 已提交
377 378
      end

379
      it 'returns 409 conflict error if username taken' do
J
jubianchi 已提交
380 381
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
Z
Z.J. van de Weg 已提交
382
        expect(response).to have_http_status(409)
383
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
384
      end
385
    end
386 387
  end

A
Angus MacArthur 已提交
388 389 390
  describe "POST /users/:id/keys" do
    before { admin }

391
    it "does not create invalid ssh key" do
A
Angus MacArthur 已提交
392
      post api("/users/#{user.id}/keys", admin), { title: "invalid key" }
Z
Z.J. van de Weg 已提交
393
      expect(response).to have_http_status(400)
394
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
J
jubianchi 已提交
395 396
    end

397
    it 'does not create key without title' do
J
jubianchi 已提交
398
      post api("/users/#{user.id}/keys", admin), key: 'some key'
Z
Z.J. van de Weg 已提交
399
      expect(response).to have_http_status(400)
400
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
A
Angus MacArthur 已提交
401 402
    end

403
    it "creates ssh key" do
A
Angus MacArthur 已提交
404
      key_attrs = attributes_for :key
405
      expect do
A
Angus MacArthur 已提交
406
        post api("/users/#{user.id}/keys", admin), key_attrs
407
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
408
    end
409

C
Connor Shea 已提交
410 411 412
    it "returns 400 for invalid ID" do
      post api("/users/999999/keys", admin)
      expect(response).to have_http_status(400)
413
    end
A
Angus MacArthur 已提交
414 415
  end

416 417 418 419
  describe 'GET /user/:uid/keys' do
    before { admin }

    context 'when unauthenticated' do
420
      it 'returns authentication error' do
421
        get api("/users/#{user.id}/keys")
Z
Z.J. van de Weg 已提交
422
        expect(response).to have_http_status(401)
423 424 425 426
      end
    end

    context 'when authenticated' do
427
      it 'returns 404 for non-existing user' do
428
        get api('/users/999999/keys', admin)
Z
Z.J. van de Weg 已提交
429
        expect(response).to have_http_status(404)
430
        expect(json_response['message']).to eq('404 User Not Found')
431 432
      end

433
      it 'returns array of ssh keys' do
434 435 436
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
Z
Z.J. van de Weg 已提交
437
        expect(response).to have_http_status(200)
438 439
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
440 441 442 443 444 445 446 447
      end
    end
  end

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

    context 'when unauthenticated' do
448
      it 'returns authentication error' do
449
        delete api("/users/#{user.id}/keys/42")
Z
Z.J. van de Weg 已提交
450
        expect(response).to have_http_status(401)
451 452 453 454
      end
    end

    context 'when authenticated' do
455
      it 'deletes existing key' do
456 457
        user.keys << key
        user.save
458
        expect do
459
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
460
        end.to change { user.keys.count }.by(-1)
Z
Z.J. van de Weg 已提交
461
        expect(response).to have_http_status(200)
462 463
      end

464
      it 'returns 404 error if user not found' do
465 466 467
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
468
        expect(response).to have_http_status(404)
469
        expect(json_response['message']).to eq('404 User Not Found')
470 471
      end

472
      it 'returns 404 error if key not foud' do
473
        delete api("/users/#{user.id}/keys/42", admin)
Z
Z.J. van de Weg 已提交
474
        expect(response).to have_http_status(404)
475
        expect(json_response['message']).to eq('404 Key Not Found')
476 477 478 479
      end
    end
  end

480 481 482
  describe "POST /users/:id/emails" do
    before { admin }

483
    it "does not create invalid email" do
D
Douwe Maan 已提交
484
      post api("/users/#{user.id}/emails", admin), {}
Z
Z.J. van de Weg 已提交
485
      expect(response).to have_http_status(400)
486 487 488
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end

489
    it "creates email" do
490 491 492 493 494
      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
495

496
    it "raises error for invalid ID" do
C
Connor Shea 已提交
497 498
      post api("/users/999999/emails", admin)
      expect(response).to have_http_status(400)
499
    end
500 501 502 503 504 505
  end

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

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

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

519
      it 'returns array of emails' do
520 521 522
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
Z
Z.J. van de Weg 已提交
523
        expect(response).to have_http_status(200)
524 525 526
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
527

528
      it "raises error for invalid ID" do
K
Kamil Trzcinski 已提交
529
        put api("/users/ASDF/emails", admin)
Z
Z.J. van de Weg 已提交
530
        expect(response).to have_http_status(405)
531
      end
532 533 534 535 536 537 538
    end
  end

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

    context 'when unauthenticated' do
539
      it 'returns authentication error' do
540
        delete api("/users/#{user.id}/emails/42")
Z
Z.J. van de Weg 已提交
541
        expect(response).to have_http_status(401)
542 543 544 545
      end
    end

    context 'when authenticated' do
546
      it 'deletes existing email' do
547 548 549 550 551
        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 已提交
552
        expect(response).to have_http_status(200)
553 554
      end

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

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

569
      it "raises error for invalid ID" do
570 571
        expect{delete api("/users/ASDF/emails/bar", admin) }.to raise_error(ActionController::RoutingError)
      end
572 573 574
    end
  end

575
  describe "DELETE /users/:id" do
576
    let!(:namespace) { user.namespace }
577 578
    before { admin }

579
    it "deletes user" do
580
      delete api("/users/#{user.id}", admin)
Z
Z.J. van de Weg 已提交
581
      expect(response).to have_http_status(200)
582
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
583
      expect { Namespace.find(namespace.id) }.to raise_error ActiveRecord::RecordNotFound
584
      expect(json_response['email']).to eq(user.email)
585 586
    end

587
    it "does not delete for unauthenticated user" do
588
      delete api("/users/#{user.id}")
Z
Z.J. van de Weg 已提交
589
      expect(response).to have_http_status(401)
590 591
    end

592
    it "is not available for non admin users" do
593
      delete api("/users/#{user.id}", user)
Z
Z.J. van de Weg 已提交
594
      expect(response).to have_http_status(403)
595 596
    end

597
    it "returns 404 for non-existing user" do
598
      delete api("/users/999999", admin)
Z
Z.J. van de Weg 已提交
599
      expect(response).to have_http_status(404)
600
      expect(json_response['message']).to eq('404 User Not Found')
601
    end
602

603
    it "raises error for invalid ID" do
604 605
      expect{delete api("/users/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end
606 607
  end

N
Nihad Abbasov 已提交
608
  describe "GET /user" do
609
    it "returns current user" do
R
Robert Speicher 已提交
610
      get api("/user", user)
Z
Z.J. van de Weg 已提交
611
      expect(response).to have_http_status(200)
612 613 614 615 616
      expect(json_response['email']).to eq(user.email)
      expect(json_response['is_admin']).to eq(user.is_admin?)
      expect(json_response['can_create_project']).to eq(user.can_create_project?)
      expect(json_response['can_create_group']).to eq(user.can_create_group?)
      expect(json_response['projects_limit']).to eq(user.projects_limit)
617
      expect(json_response['private_token']).to be_blank
N
Nihad Abbasov 已提交
618
    end
619

620
    it "returns 401 error if user is unauthenticated" do
621
      get api("/user")
Z
Z.J. van de Weg 已提交
622
      expect(response).to have_http_status(401)
623
    end
N
Nihad Abbasov 已提交
624
  end
625 626 627

  describe "GET /user/keys" do
    context "when unauthenticated" do
628
      it "returns authentication error" do
629
        get api("/user/keys")
Z
Z.J. van de Weg 已提交
630
        expect(response).to have_http_status(401)
631 632
      end
    end
N
Nihad Abbasov 已提交
633

634
    context "when authenticated" do
635
      it "returns array of ssh keys" do
636 637 638
        user.keys << key
        user.save
        get api("/user/keys", user)
Z
Z.J. van de Weg 已提交
639
        expect(response).to have_http_status(200)
640 641
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
642 643 644 645 646
      end
    end
  end

  describe "GET /user/keys/:id" do
647
    it "returns single key" do
648 649 650
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
Z
Z.J. van de Weg 已提交
651
      expect(response).to have_http_status(200)
652
      expect(json_response["title"]).to eq(key.title)
653
    end
N
Nihad Abbasov 已提交
654

655
    it "returns 404 Not Found within invalid ID" do
656
      get api("/user/keys/42", user)
Z
Z.J. van de Weg 已提交
657
      expect(response).to have_http_status(404)
658
      expect(json_response['message']).to eq('404 Not found')
659 660
    end

661
    it "returns 404 error if admin accesses user's ssh key" do
662 663 664 665
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
Z
Z.J. van de Weg 已提交
666
      expect(response).to have_http_status(404)
667
      expect(json_response['message']).to eq('404 Not found')
668
    end
669

670
    it "returns 404 for invalid ID" do
671
      get api("/users/keys/ASDF", admin)
Z
Z.J. van de Weg 已提交
672
      expect(response).to have_http_status(404)
673
    end
674
  end
N
Nihad Abbasov 已提交
675

676
  describe "POST /user/keys" do
677
    it "creates ssh key" do
678
      key_attrs = attributes_for :key
679
      expect do
680
        post api("/user/keys", user), key_attrs
681
      end.to change{ user.keys.count }.by(1)
Z
Z.J. van de Weg 已提交
682
      expect(response).to have_http_status(201)
683 684
    end

685
    it "returns a 401 error if unauthorized" do
686
      post api("/user/keys"), title: 'some title', key: 'some key'
Z
Z.J. van de Weg 已提交
687
      expect(response).to have_http_status(401)
688 689
    end

690
    it "does not create ssh key without key" do
691
      post api("/user/keys", user), title: 'title'
Z
Z.J. van de Weg 已提交
692
      expect(response).to have_http_status(400)
693
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
J
jubianchi 已提交
694 695
    end

696
    it 'does not create ssh key without title' do
J
jubianchi 已提交
697
      post api('/user/keys', user), key: 'some key'
Z
Z.J. van de Weg 已提交
698
      expect(response).to have_http_status(400)
699
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
700 701
    end

702
    it "does not create ssh key without title" do
703
      post api("/user/keys", user), key: "somekey"
Z
Z.J. van de Weg 已提交
704
      expect(response).to have_http_status(400)
705 706 707 708
    end
  end

  describe "DELETE /user/keys/:id" do
709
    it "deletes existed key" do
710 711
      user.keys << key
      user.save
712
      expect do
713
        delete api("/user/keys/#{key.id}", user)
714
      end.to change{user.keys.count}.by(-1)
Z
Z.J. van de Weg 已提交
715
      expect(response).to have_http_status(200)
716
    end
N
Nihad Abbasov 已提交
717

718
    it "returns success if key ID not found" do
719
      delete api("/user/keys/42", user)
Z
Z.J. van de Weg 已提交
720
      expect(response).to have_http_status(200)
721 722
    end

723
    it "returns 401 error if unauthorized" do
724 725 726
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
Z
Z.J. van de Weg 已提交
727
      expect(response).to have_http_status(401)
728
    end
729

730
    it "raises error for invalid ID" do
731 732
      expect{delete api("/users/keys/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end
733
  end
734

735 736
  describe "GET /user/emails" do
    context "when unauthenticated" do
737
      it "returns authentication error" do
738
        get api("/user/emails")
Z
Z.J. van de Weg 已提交
739
        expect(response).to have_http_status(401)
740 741 742 743
      end
    end

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

  describe "GET /user/emails/:id" do
756
    it "returns single email" do
757 758 759
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
Z
Z.J. van de Weg 已提交
760
      expect(response).to have_http_status(200)
761 762 763
      expect(json_response["email"]).to eq(email.email)
    end

764
    it "returns 404 Not Found within invalid ID" do
765
      get api("/user/emails/42", user)
Z
Z.J. van de Weg 已提交
766
      expect(response).to have_http_status(404)
767 768 769
      expect(json_response['message']).to eq('404 Not found')
    end

770
    it "returns 404 error if admin accesses user's email" do
771 772 773 774
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
Z
Z.J. van de Weg 已提交
775
      expect(response).to have_http_status(404)
776 777
      expect(json_response['message']).to eq('404 Not found')
    end
778

779
    it "returns 404 for invalid ID" do
780
      get api("/users/emails/ASDF", admin)
Z
Z.J. van de Weg 已提交
781
      expect(response).to have_http_status(404)
782
    end
783 784 785
  end

  describe "POST /user/emails" do
786
    it "creates email" do
787 788 789 790
      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 已提交
791
      expect(response).to have_http_status(201)
792 793
    end

794
    it "returns a 401 error if unauthorized" do
795
      post api("/user/emails"), email: 'some email'
Z
Z.J. van de Weg 已提交
796
      expect(response).to have_http_status(401)
797 798
    end

799
    it "does not create email with invalid email" do
800
      post api("/user/emails", user), {}
Z
Z.J. van de Weg 已提交
801
      expect(response).to have_http_status(400)
802 803 804 805 806
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end
  end

  describe "DELETE /user/emails/:id" do
807
    it "deletes existed email" do
808 809 810 811 812
      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 已提交
813
      expect(response).to have_http_status(200)
814 815
    end

816
    it "returns success if email ID not found" do
817
      delete api("/user/emails/42", user)
Z
Z.J. van de Weg 已提交
818
      expect(response).to have_http_status(200)
819 820
    end

821
    it "returns 401 error if unauthorized" do
822 823 824
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
Z
Z.J. van de Weg 已提交
825
      expect(response).to have_http_status(401)
826
    end
827

828
    it "raises error for invalid ID" do
829 830
      expect{delete api("/users/emails/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end
831 832
  end

833 834
  describe 'PUT /user/:id/block' do
    before { admin }
835
    it 'blocks existing user' do
836
      put api("/users/#{user.id}/block", admin)
Z
Z.J. van de Weg 已提交
837
      expect(response).to have_http_status(200)
838 839 840
      expect(user.reload.state).to eq('blocked')
    end

841
    it 'does not re-block ldap blocked users' do
842
      put api("/users/#{ldap_blocked_user.id}/block", admin)
Z
Z.J. van de Weg 已提交
843
      expect(response).to have_http_status(403)
844 845 846
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
    end

847
    it 'does not be available for non admin users' do
848
      put api("/users/#{user.id}/block", user)
Z
Z.J. van de Weg 已提交
849
      expect(response).to have_http_status(403)
850 851 852
      expect(user.reload.state).to eq('active')
    end

853
    it 'returns a 404 error if user id not found' do
854
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
855
      expect(response).to have_http_status(404)
856 857 858 859 860
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

  describe 'PUT /user/:id/unblock' do
861
    let(:blocked_user)  { create(:user, state: 'blocked') }
862
    before { admin }
863

864
    it 'unblocks existing user' do
865
      put api("/users/#{user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
866
      expect(response).to have_http_status(200)
867 868 869
      expect(user.reload.state).to eq('active')
    end

870
    it 'unblocks a blocked user' do
871
      put api("/users/#{blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
872
      expect(response).to have_http_status(200)
873 874 875
      expect(blocked_user.reload.state).to eq('active')
    end

876
    it 'does not unblock ldap blocked users' do
877
      put api("/users/#{ldap_blocked_user.id}/unblock", admin)
Z
Z.J. van de Weg 已提交
878
      expect(response).to have_http_status(403)
879
      expect(ldap_blocked_user.reload.state).to eq('ldap_blocked')
880 881
    end

882
    it 'does not be available for non admin users' do
883
      put api("/users/#{user.id}/unblock", user)
Z
Z.J. van de Weg 已提交
884
      expect(response).to have_http_status(403)
885 886 887
      expect(user.reload.state).to eq('active')
    end

888
    it 'returns a 404 error if user id not found' do
889
      put api('/users/9999/block', admin)
Z
Z.J. van de Weg 已提交
890
      expect(response).to have_http_status(404)
891 892
      expect(json_response['message']).to eq('404 User Not Found')
    end
893

894
    it "raises error for invalid ID" do
895 896
      expect{put api("/users/ASDF/block", admin) }.to raise_error(ActionController::RoutingError)
    end
897
  end
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

  describe 'GET /user/:id/events' do
    let(:user) { create(:user) }
    let(:lambda_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
        get api("/users/#{user.id}/events", lambda_user)

        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)

          first_event = json_response.first

          expect(first_event['action_name']).to eq('commented on')
          expect(first_event['project_id'].to_i).to eq(project.id)
          expect(first_event['author_username']).to eq(user.username)
          expect(first_event['note']['id']).to eq(note.id)
          expect(first_event['note']['body']).to eq('What an awesome day!')

          last_event = json_response.last

          expect(last_event['action_name']).to eq('joined')
          expect(last_event['project_id'].to_i).to eq(project.id)
          expect(last_event['author_username']).to eq(user.username)
          expect(last_event['author']['name']).to eq(user.name)
        end
      end
    end

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

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