users_spec.rb 26.3 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) }
N
Nihad Abbasov 已提交
11 12

  describe "GET /users" do
13 14 15
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/users")
16
        expect(response.status).to eq(401)
17
      end
N
Nihad Abbasov 已提交
18 19
    end

20
    context "when authenticated" do
N
Nihad Abbasov 已提交
21
      it "should return an array of users" do
R
Robert Speicher 已提交
22
        get api("/users", user)
23 24
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
M
Marin Jankovski 已提交
25
        username = user.username
26 27 28
        expect(json_response.detect do |user|
          user['username'] == username
        end['username']).to eq(username)
N
Nihad Abbasov 已提交
29
      end
30 31 32 33 34 35 36

      it "should return one user" do
        get api("/users?username=#{omniauth_user.username}", user)
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['username']).to eq(omniauth_user.username)
      end
N
Nihad Abbasov 已提交
37
    end
38 39 40 41

    context "when admin" do
      it "should return an array of users" do
        get api("/users", admin)
42 43 44 45 46
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first.keys).to include 'email'
        expect(json_response.first.keys).to include 'identities'
        expect(json_response.first.keys).to include 'can_create_project'
S
Stan Hu 已提交
47
        expect(json_response.first.keys).to include 'two_factor_enabled'
48 49
      end
    end
N
Nihad Abbasov 已提交
50 51 52 53
  end

  describe "GET /users/:id" do
    it "should return a user by id" do
R
Robert Speicher 已提交
54
      get api("/users/#{user.id}", user)
55 56
      expect(response.status).to eq(200)
      expect(json_response['username']).to eq(user.username)
N
Nihad Abbasov 已提交
57 58
    end

59 60
    it "should return a 401 if unauthenticated" do
      get api("/users/9998")
61
      expect(response.status).to eq(401)
62
    end
V
Valeriy Sizov 已提交
63

64 65
    it "should return a 404 error if user id not found" do
      get api("/users/9999", user)
66 67
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
V
Valeriy Sizov 已提交
68
    end
69 70 71 72 73

    it "should return a 404 if invalid ID" do
      get api("/users/1ASDF", user)
      expect(response.status).to eq(404)
    end
74 75 76 77
  end

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

    it "should create user" do
80
      expect do
81
        post api("/users", admin), attributes_for(:user, projects_limit: 3)
82
      end.to change { User.count }.by(1)
V
Valeriy Sizov 已提交
83 84
    end

85 86
    it "should create user with correct attributes" do
      post api('/users', admin), attributes_for(:user, admin: true, can_create_group: true)
87
      expect(response.status).to eq(201)
88 89
      user_id = json_response['id']
      new_user = User.find(user_id)
90 91 92
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(true)
      expect(new_user.can_create_group).to eq(true)
93 94
    end

95 96
    it "should create non-admin user" do
      post api('/users', admin), attributes_for(:user, admin: false, can_create_group: false)
97
      expect(response.status).to eq(201)
98 99
      user_id = json_response['id']
      new_user = User.find(user_id)
100 101 102
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
      expect(new_user.can_create_group).to eq(false)
103 104 105 106
    end

    it "should create non-admin users by default" do
      post api('/users', admin), attributes_for(:user)
107
      expect(response.status).to eq(201)
108 109
      user_id = json_response['id']
      new_user = User.find(user_id)
110 111
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
112 113
    end

114 115
    it "should return 201 Created on success" do
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
116
      expect(response.status).to eq(201)
117 118 119
    end

    it "should not create user with invalid email" do
J
jubianchi 已提交
120
      post api('/users', admin),
121 122 123
        email: 'invalid email',
        password: 'password',
        name: 'test'
124
      expect(response.status).to eq(400)
125 126
    end

J
jubianchi 已提交
127
    it 'should return 400 error if name not given' do
128
      post api('/users', admin), attributes_for(:user).except(:name)
129
      expect(response.status).to eq(400)
J
jubianchi 已提交
130 131 132
    end

    it 'should return 400 error if password not given' do
133
      post api('/users', admin), attributes_for(:user).except(:password)
134
      expect(response.status).to eq(400)
135 136
    end

137 138 139 140 141 142 143
    it 'should return 400 error if email not given' do
      post api('/users', admin), attributes_for(:user).except(:email)
      expect(response.status).to eq(400)
    end

    it 'should return 400 error if username not given' do
      post api('/users', admin), attributes_for(:user).except(:username)
144
      expect(response.status).to eq(400)
J
jubianchi 已提交
145 146 147 148
    end

    it 'should return 400 error if user does not validate' do
      post api('/users', admin),
149 150 151 152 153 154
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
155 156
      expect(response.status).to eq(400)
      expect(json_response['message']['password']).
157
        to eq(['is too short (minimum is 8 characters)'])
158
      expect(json_response['message']['bio']).
159
        to eq(['is too long (maximum is 255 characters)'])
160
      expect(json_response['message']['projects_limit']).
161
        to eq(['must be greater than or equal to 0'])
162
      expect(json_response['message']['username']).
R
Robert Speicher 已提交
163
        to eq([Gitlab::Regex.namespace_regex_message])
164 165
    end

V
Valeriy Sizov 已提交
166
    it "shouldn't available for non admin users" do
167
      post api("/users", user), attributes_for(:user)
168
      expect(response.status).to eq(403)
V
Valeriy Sizov 已提交
169
    end
170

J
jubianchi 已提交
171 172 173
    context 'with existing user' do
      before do
        post api('/users', admin),
174 175 176 177
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
J
jubianchi 已提交
178
      end
179

J
jubianchi 已提交
180
      it 'should return 409 conflict error if user with same email exists' do
181
        expect do
J
jubianchi 已提交
182
          post api('/users', admin),
183 184 185 186 187
            name: 'foo',
            email: 'test@example.com',
            password: 'password',
            username: 'foo'
        end.to change { User.count }.by(0)
188 189
        expect(response.status).to eq(409)
        expect(json_response['message']).to eq('Email has already been taken')
190 191
      end

J
jubianchi 已提交
192 193 194
      it 'should return 409 conflict error if same username exists' do
        expect do
          post api('/users', admin),
195 196 197 198
            name: 'foo',
            email: 'foo@example.com',
            password: 'password',
            username: 'test'
J
jubianchi 已提交
199
        end.to change { User.count }.by(0)
200 201
        expect(response.status).to eq(409)
        expect(json_response['message']).to eq('Username has already been taken')
202 203
      end
    end
V
Valeriy Sizov 已提交
204 205
  end

M
Marin Jankovski 已提交
206
  describe "GET /users/sign_up" do
207

208 209
    it "should redirect to sign in page" do
      get "/users/sign_up"
210 211
      expect(response.status).to eq(302)
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
212 213 214
    end
  end

215
  describe "PUT /users/:id" do
216 217
    let!(:admin_user) { create(:admin) }

218 219
    before { admin }

220
    it "should update user with new bio" do
221
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
222 223 224
      expect(response.status).to eq(200)
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
225 226
    end

J
jubianchi 已提交
227 228
    it 'should update user with his own email' do
      put api("/users/#{user.id}", admin), email: user.email
229 230 231
      expect(response.status).to eq(200)
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
J
jubianchi 已提交
232 233 234 235
    end

    it 'should update user with his own username' do
      put api("/users/#{user.id}", admin), username: user.username
236 237 238
      expect(response.status).to eq(200)
      expect(json_response['username']).to eq(user.username)
      expect(user.reload.username).to eq(user.username)
J
jubianchi 已提交
239 240
    end

241 242 243 244 245 246 247 248 249 250 251 252 253
    it "should update user's existing identity" do
      put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
      expect(response.status).to eq(200)
      expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
    end

    it 'should update user with new identity' do
      put api("/users/#{user.id}", admin), provider: 'github', extern_uid: '67890'
      expect(response.status).to eq(200)
      expect(user.reload.identities.first.extern_uid).to eq('67890')
      expect(user.reload.identities.first.provider).to eq('github')
    end

254
    it "should update admin status" do
255
      put api("/users/#{user.id}", admin), { admin: true }
256 257 258
      expect(response.status).to eq(200)
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
259 260 261
    end

    it "should not update admin status" do
262
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
263 264 265 266
      expect(response.status).to eq(200)
      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)
267 268
    end

269
    it "should not allow invalid update" do
270
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
271 272
      expect(response.status).to eq(400)
      expect(user.reload.email).not_to eq('invalid email')
273 274 275 276
    end

    it "shouldn't available for non admin users" do
      put api("/users/#{user.id}", user), attributes_for(:user)
277
      expect(response.status).to eq(403)
278 279 280
    end

    it "should return 404 for non-existing user" do
281
      put api("/users/999999", admin), { bio: 'update should fail' }
282 283
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
J
jubianchi 已提交
284 285
    end

286 287 288 289
    it "should raise error for invalid ID" do
      expect{put api("/users/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end

J
jubianchi 已提交
290 291
    it 'should return 400 error if user does not validate' do
      put api("/users/#{user.id}", admin),
292 293 294 295 296 297
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
298 299
      expect(response.status).to eq(400)
      expect(json_response['message']['password']).
300
        to eq(['is too short (minimum is 8 characters)'])
301
      expect(json_response['message']['bio']).
302
        to eq(['is too long (maximum is 255 characters)'])
303
      expect(json_response['message']['projects_limit']).
304
        to eq(['must be greater than or equal to 0'])
305
      expect(json_response['message']['username']).
R
Robert Speicher 已提交
306
        to eq([Gitlab::Regex.namespace_regex_message])
307
    end
308 309

    context "with existing user" do
310
      before do
311 312
        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 已提交
313
        @user = User.all.last
314
      end
315

J
jubianchi 已提交
316 317
      it 'should return 409 conflict error if email address exists' do
        put api("/users/#{@user.id}", admin), email: 'test@example.com'
318 319
        expect(response.status).to eq(409)
        expect(@user.reload.email).to eq(@user.email)
J
jubianchi 已提交
320 321 322 323 324
      end

      it 'should return 409 conflict error if username taken' do
        @user_id = User.all.last.id
        put api("/users/#{@user.id}", admin), username: 'test'
325 326
        expect(response.status).to eq(409)
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
327
      end
328
    end
329 330
  end

A
Angus MacArthur 已提交
331 332 333 334 335
  describe "POST /users/:id/keys" do
    before { admin }

    it "should not create invalid ssh key" do
      post api("/users/#{user.id}/keys", admin), { title: "invalid key" }
336 337
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
J
jubianchi 已提交
338 339 340 341
    end

    it 'should not create key without title' do
      post api("/users/#{user.id}/keys", admin), key: 'some key'
342 343
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
A
Angus MacArthur 已提交
344 345 346 347
    end

    it "should create ssh key" do
      key_attrs = attributes_for :key
348
      expect do
A
Angus MacArthur 已提交
349
        post api("/users/#{user.id}/keys", admin), key_attrs
350
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
351
    end
352

K
Kamil Trzcinski 已提交
353 354 355
    it "should return 405 for invalid ID" do
      post api("/users/ASDF/keys", admin)
      expect(response.status).to eq(405)
356
    end
A
Angus MacArthur 已提交
357 358
  end

359 360 361 362 363 364
  describe 'GET /user/:uid/keys' do
    before { admin }

    context 'when unauthenticated' do
      it 'should return authentication error' do
        get api("/users/#{user.id}/keys")
365
        expect(response.status).to eq(401)
366 367 368 369 370 371
      end
    end

    context 'when authenticated' do
      it 'should return 404 for non-existing user' do
        get api('/users/999999/keys', admin)
372 373
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 User Not Found')
374 375 376 377 378 379
      end

      it 'should return array of ssh keys' do
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
380 381 382
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
383
      end
384

K
Kamil Trzcinski 已提交
385
      it "should return 405 for invalid ID" do
386
        get api("/users/ASDF/keys", admin)
K
Kamil Trzcinski 已提交
387
        expect(response.status).to eq(405)
388
      end
389 390 391 392 393 394 395 396 397
    end
  end

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

    context 'when unauthenticated' do
      it 'should return authentication error' do
        delete api("/users/#{user.id}/keys/42")
398
        expect(response.status).to eq(401)
399 400 401 402 403 404 405
      end
    end

    context 'when authenticated' do
      it 'should delete existing key' do
        user.keys << key
        user.save
406
        expect do
407
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
408
        end.to change { user.keys.count }.by(-1)
409
        expect(response.status).to eq(200)
410 411 412 413 414 415
      end

      it 'should return 404 error if user not found' do
        user.keys << key
        user.save
        delete api("/users/999999/keys/#{key.id}", admin)
416 417
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 User Not Found')
418 419 420 421
      end

      it 'should return 404 error if key not foud' do
        delete api("/users/#{user.id}/keys/42", admin)
422 423
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 Key Not Found')
424 425 426 427
      end
    end
  end

428 429 430 431
  describe "POST /users/:id/emails" do
    before { admin }

    it "should not create invalid email" do
D
Douwe Maan 已提交
432
      post api("/users/#{user.id}/emails", admin), {}
433 434 435 436 437 438 439 440 441 442
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end

    it "should create email" do
      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
443 444

    it "should raise error for invalid ID" do
K
Kamil Trzcinski 已提交
445 446
      post api("/users/ASDF/emails", admin)
      expect(response.status).to eq(405)
447
    end
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  end

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

    context 'when unauthenticated' do
      it 'should return authentication error' do
        get api("/users/#{user.id}/emails")
        expect(response.status).to eq(401)
      end
    end

    context 'when authenticated' do
      it 'should return 404 for non-existing user' do
        get api('/users/999999/emails', admin)
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 User Not Found')
      end

      it 'should return array of emails' do
        user.emails << email
        user.save
        get api("/users/#{user.id}/emails", admin)
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['email']).to eq(email.email)
      end
475 476

      it "should raise error for invalid ID" do
K
Kamil Trzcinski 已提交
477 478
        put api("/users/ASDF/emails", admin)
        expect(response.status).to eq(405)
479
      end
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    end
  end

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

    context 'when unauthenticated' do
      it 'should return authentication error' do
        delete api("/users/#{user.id}/emails/42")
        expect(response.status).to eq(401)
      end
    end

    context 'when authenticated' do
      it 'should delete existing email' do
        user.emails << email
        user.save
        expect do
          delete api("/users/#{user.id}/emails/#{email.id}", admin)
        end.to change { user.emails.count }.by(-1)
        expect(response.status).to eq(200)
      end

      it 'should return 404 error if user not found' do
        user.emails << email
        user.save
        delete api("/users/999999/emails/#{email.id}", admin)
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 User Not Found')
      end

      it 'should return 404 error if email not foud' do
        delete api("/users/#{user.id}/emails/42", admin)
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 Email Not Found')
      end
516 517 518 519

      it "should raise error for invalid ID" do
        expect{delete api("/users/ASDF/emails/bar", admin) }.to raise_error(ActionController::RoutingError)
      end
520 521 522
    end
  end

523 524 525 526 527
  describe "DELETE /users/:id" do
    before { admin }

    it "should delete user" do
      delete api("/users/#{user.id}", admin)
528
      expect(response.status).to eq(200)
529
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
530
      expect(json_response['email']).to eq(user.email)
531 532
    end

533 534
    it "should not delete for unauthenticated user" do
      delete api("/users/#{user.id}")
535
      expect(response.status).to eq(401)
536 537
    end

538 539
    it "shouldn't available for non admin users" do
      delete api("/users/#{user.id}", user)
540
      expect(response.status).to eq(403)
541 542 543 544
    end

    it "should return 404 for non-existing user" do
      delete api("/users/999999", admin)
545 546
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 User Not Found')
547
    end
548 549 550 551

    it "should raise error for invalid ID" do
      expect{delete api("/users/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end
552 553
  end

N
Nihad Abbasov 已提交
554 555
  describe "GET /user" do
    it "should return current user" do
R
Robert Speicher 已提交
556
      get api("/user", user)
557 558 559 560 561 562
      expect(response.status).to eq(200)
      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)
N
Nihad Abbasov 已提交
563
    end
564 565 566

    it "should return 401 error if user is unauthenticated" do
      get api("/user")
567
      expect(response.status).to eq(401)
568
    end
N
Nihad Abbasov 已提交
569
  end
570 571 572 573 574

  describe "GET /user/keys" do
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/user/keys")
575
        expect(response.status).to eq(401)
576 577
      end
    end
N
Nihad Abbasov 已提交
578

579 580 581 582 583
    context "when authenticated" do
      it "should return array of ssh keys" do
        user.keys << key
        user.save
        get api("/user/keys", user)
584 585 586
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
587 588 589 590 591
      end
    end
  end

  describe "GET /user/keys/:id" do
J
Johannes Schleifenbaum 已提交
592
    it "should return single key" do
593 594 595
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
596 597
      expect(response.status).to eq(200)
      expect(json_response["title"]).to eq(key.title)
598
    end
N
Nihad Abbasov 已提交
599

600 601
    it "should return 404 Not Found within invalid ID" do
      get api("/user/keys/42", user)
602 603
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
604 605
    end

606 607 608 609 610
    it "should return 404 error if admin accesses user's ssh key" do
      user.keys << key
      user.save
      admin
      get api("/user/keys/#{key.id}", admin)
611 612
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
613
    end
614 615 616 617 618

    it "should return 404 for invalid ID" do
      get api("/users/keys/ASDF", admin)
      expect(response.status).to eq(404)
    end
619
  end
N
Nihad Abbasov 已提交
620

621
  describe "POST /user/keys" do
622
    it "should create ssh key" do
623
      key_attrs = attributes_for :key
624
      expect do
625
        post api("/user/keys", user), key_attrs
626
      end.to change{ user.keys.count }.by(1)
627
      expect(response.status).to eq(201)
628 629 630 631
    end

    it "should return a 401 error if unauthorized" do
      post api("/user/keys"), title: 'some title', key: 'some key'
632
      expect(response.status).to eq(401)
633 634 635 636
    end

    it "should not create ssh key without key" do
      post api("/user/keys", user), title: 'title'
637 638
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
J
jubianchi 已提交
639 640 641 642
    end

    it 'should not create ssh key without title' do
      post api('/user/keys', user), key: 'some key'
643 644
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
645 646 647 648
    end

    it "should not create ssh key without title" do
      post api("/user/keys", user), key: "somekey"
649
      expect(response.status).to eq(400)
650 651 652 653 654 655 656
    end
  end

  describe "DELETE /user/keys/:id" do
    it "should delete existed key" do
      user.keys << key
      user.save
657
      expect do
658
        delete api("/user/keys/#{key.id}", user)
659
      end.to change{user.keys.count}.by(-1)
660
      expect(response.status).to eq(200)
661
    end
N
Nihad Abbasov 已提交
662

K
Kevin Lyda 已提交
663
    it "should return success if key ID not found" do
664
      delete api("/user/keys/42", user)
665
      expect(response.status).to eq(200)
666 667 668 669 670 671
    end

    it "should return 401 error if unauthorized" do
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
672
      expect(response.status).to eq(401)
673
    end
674 675 676 677

    it "should raise error for invalid ID" do
      expect{delete api("/users/keys/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end
678
  end
679

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  describe "GET /user/emails" do
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/user/emails")
        expect(response.status).to eq(401)
      end
    end

    context "when authenticated" do
      it "should return array of emails" do
        user.emails << email
        user.save
        get api("/user/emails", user)
        expect(response.status).to eq(200)
        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
    it "should return single email" do
      user.emails << email
      user.save
      get api("/user/emails/#{email.id}", user)
      expect(response.status).to eq(200)
      expect(json_response["email"]).to eq(email.email)
    end

    it "should return 404 Not Found within invalid ID" do
      get api("/user/emails/42", user)
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
    end

    it "should return 404 error if admin accesses user's email" do
      user.emails << email
      user.save
      admin
      get api("/user/emails/#{email.id}", admin)
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
    end
723 724 725 726 727

    it "should return 404 for invalid ID" do
      get api("/users/emails/ASDF", admin)
      expect(response.status).to eq(404)
    end
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
  end

  describe "POST /user/emails" do
    it "should create email" do
      email_attrs = attributes_for :email
      expect do
        post api("/user/emails", user), email_attrs
      end.to change{ user.emails.count }.by(1)
      expect(response.status).to eq(201)
    end

    it "should return a 401 error if unauthorized" do
      post api("/user/emails"), email: 'some email'
      expect(response.status).to eq(401)
    end

    it "should not create email with invalid email" do
      post api("/user/emails", user), {}
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "email" not given')
    end
  end

  describe "DELETE /user/emails/:id" do
    it "should delete existed email" do
      user.emails << email
      user.save
      expect do
        delete api("/user/emails/#{email.id}", user)
      end.to change{user.emails.count}.by(-1)
      expect(response.status).to eq(200)
    end

    it "should return success if email ID not found" do
      delete api("/user/emails/42", user)
      expect(response.status).to eq(200)
    end

    it "should return 401 error if unauthorized" do
      user.emails << email
      user.save
      delete api("/user/emails/#{email.id}")
      expect(response.status).to eq(401)
    end
772 773 774 775

    it "should raise error for invalid ID" do
      expect{delete api("/users/emails/ASDF", admin) }.to raise_error(ActionController::RoutingError)
    end
776 777
  end

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
  describe 'PUT /user/:id/block' do
    before { admin }
    it 'should block existing user' do
      put api("/users/#{user.id}/block", admin)
      expect(response.status).to eq(200)
      expect(user.reload.state).to eq('blocked')
    end

    it 'should not be available for non admin users' do
      put api("/users/#{user.id}/block", user)
      expect(response.status).to eq(403)
      expect(user.reload.state).to eq('active')
    end

    it 'should return a 404 error if user id not found' do
      put api('/users/9999/block', admin)
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 User Not Found')
    end
  end

  describe 'PUT /user/:id/unblock' do
    before { admin }
    it 'should unblock existing user' do
      put api("/users/#{user.id}/unblock", admin)
      expect(response.status).to eq(200)
      expect(user.reload.state).to eq('active')
    end

    it 'should unblock a blocked user' do
      put api("/users/#{user.id}/block", admin)
      expect(response.status).to eq(200)
      expect(user.reload.state).to eq('blocked')
      put api("/users/#{user.id}/unblock", admin)
      expect(response.status).to eq(200)
      expect(user.reload.state).to eq('active')
    end

    it 'should not be available for non admin users' do
      put api("/users/#{user.id}/unblock", user)
      expect(response.status).to eq(403)
      expect(user.reload.state).to eq('active')
    end

    it 'should return a 404 error if user id not found' do
      put api('/users/9999/block', admin)
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 User Not Found')
    end
827 828 829 830

    it "should raise error for invalid ID" do
      expect{put api("/users/ASDF/block", admin) }.to raise_error(ActionController::RoutingError)
    end
831
  end
N
Nihad Abbasov 已提交
832
end