users_spec.rb 16.9 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) }
N
Nihad Abbasov 已提交
9 10

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

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

    context "when admin" do
      it "should return an array of users" do
        get api("/users", admin)
33 34 35 36 37
        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 已提交
38
        expect(json_response.first.keys).to include 'two_factor_enabled'
39 40
      end
    end
N
Nihad Abbasov 已提交
41 42 43 44
  end

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

50 51
    it "should return a 401 if unauthenticated" do
      get api("/users/9998")
52
      expect(response.status).to eq(401)
53
    end
V
Valeriy Sizov 已提交
54

55 56
    it "should return a 404 error if user id not found" do
      get api("/users/9999", user)
57 58
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
V
Valeriy Sizov 已提交
59
    end
60 61 62 63
  end

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

    it "should create user" do
66
      expect do
67
        post api("/users", admin), attributes_for(:user, projects_limit: 3)
68
      end.to change { User.count }.by(1)
V
Valeriy Sizov 已提交
69 70
    end

71 72
    it "should create user with correct attributes" do
      post api('/users', admin), attributes_for(:user, admin: true, can_create_group: true)
73
      expect(response.status).to eq(201)
74 75
      user_id = json_response['id']
      new_user = User.find(user_id)
76 77 78
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(true)
      expect(new_user.can_create_group).to eq(true)
79 80
    end

81 82
    it "should create non-admin user" do
      post api('/users', admin), attributes_for(:user, admin: false, can_create_group: false)
83
      expect(response.status).to eq(201)
84 85
      user_id = json_response['id']
      new_user = User.find(user_id)
86 87 88
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
      expect(new_user.can_create_group).to eq(false)
89 90 91 92
    end

    it "should create non-admin users by default" do
      post api('/users', admin), attributes_for(:user)
93
      expect(response.status).to eq(201)
94 95
      user_id = json_response['id']
      new_user = User.find(user_id)
96 97
      expect(new_user).not_to eq(nil)
      expect(new_user.admin).to eq(false)
98 99
    end

100 101
    it "should return 201 Created on success" do
      post api("/users", admin), attributes_for(:user, projects_limit: 3)
102
      expect(response.status).to eq(201)
103 104 105
    end

    it "should not create user with invalid email" do
J
jubianchi 已提交
106
      post api('/users', admin),
107 108 109
        email: 'invalid email',
        password: 'password',
        name: 'test'
110
      expect(response.status).to eq(400)
111 112
    end

J
jubianchi 已提交
113
    it 'should return 400 error if name not given' do
114
      post api('/users', admin), attributes_for(:user).except(:name)
115
      expect(response.status).to eq(400)
J
jubianchi 已提交
116 117 118
    end

    it 'should return 400 error if password not given' do
119
      post api('/users', admin), attributes_for(:user).except(:password)
120
      expect(response.status).to eq(400)
121 122
    end

123 124 125 126 127 128 129
    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)
130
      expect(response.status).to eq(400)
J
jubianchi 已提交
131 132 133 134
    end

    it 'should return 400 error if user does not validate' do
      post api('/users', admin),
135 136 137 138 139 140
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
141 142
      expect(response.status).to eq(400)
      expect(json_response['message']['password']).
143
        to eq(['is too short (minimum is 8 characters)'])
144
      expect(json_response['message']['bio']).
145
        to eq(['is too long (maximum is 255 characters)'])
146
      expect(json_response['message']['projects_limit']).
147
        to eq(['must be greater than or equal to 0'])
148
      expect(json_response['message']['username']).
149
        to eq([Gitlab::Regex.send(:namespace_regex_message)])
150 151
    end

V
Valeriy Sizov 已提交
152
    it "shouldn't available for non admin users" do
153
      post api("/users", user), attributes_for(:user)
154
      expect(response.status).to eq(403)
V
Valeriy Sizov 已提交
155
    end
156

J
jubianchi 已提交
157 158 159
    context 'with existing user' do
      before do
        post api('/users', admin),
160 161 162 163
          email: 'test@example.com',
          password: 'password',
          username: 'test',
          name: 'foo'
J
jubianchi 已提交
164
      end
165

J
jubianchi 已提交
166
      it 'should return 409 conflict error if user with same email exists' do
167
        expect do
J
jubianchi 已提交
168
          post api('/users', admin),
169 170 171 172 173
            name: 'foo',
            email: 'test@example.com',
            password: 'password',
            username: 'foo'
        end.to change { User.count }.by(0)
174 175
        expect(response.status).to eq(409)
        expect(json_response['message']).to eq('Email has already been taken')
176 177
      end

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

M
Marin Jankovski 已提交
192
  describe "GET /users/sign_up" do
193

194 195
    it "should redirect to sign in page" do
      get "/users/sign_up"
196 197
      expect(response.status).to eq(302)
      expect(response).to redirect_to(new_user_session_path)
M
Marin Jankovski 已提交
198 199 200
    end
  end

201
  describe "PUT /users/:id" do
202 203
    let!(:admin_user) { create(:admin) }

204 205
    before { admin }

206
    it "should update user with new bio" do
207
      put api("/users/#{user.id}", admin), { bio: 'new test bio' }
208 209 210
      expect(response.status).to eq(200)
      expect(json_response['bio']).to eq('new test bio')
      expect(user.reload.bio).to eq('new test bio')
211 212
    end

J
jubianchi 已提交
213 214
    it 'should update user with his own email' do
      put api("/users/#{user.id}", admin), email: user.email
215 216 217
      expect(response.status).to eq(200)
      expect(json_response['email']).to eq(user.email)
      expect(user.reload.email).to eq(user.email)
J
jubianchi 已提交
218 219 220 221
    end

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

227
    it "should update admin status" do
228
      put api("/users/#{user.id}", admin), { admin: true }
229 230 231
      expect(response.status).to eq(200)
      expect(json_response['is_admin']).to eq(true)
      expect(user.reload.admin).to eq(true)
232 233 234
    end

    it "should not update admin status" do
235
      put api("/users/#{admin_user.id}", admin), { can_create_group: false }
236 237 238 239
      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)
240 241
    end

242
    it "should not allow invalid update" do
243
      put api("/users/#{user.id}", admin), { email: 'invalid email' }
244 245
      expect(response.status).to eq(400)
      expect(user.reload.email).not_to eq('invalid email')
246 247 248 249
    end

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

    it "should return 404 for non-existing user" do
254
      put api("/users/999999", admin), { bio: 'update should fail' }
255 256
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
J
jubianchi 已提交
257 258 259 260
    end

    it 'should return 400 error if user does not validate' do
      put api("/users/#{user.id}", admin),
261 262 263 264 265 266
        password: 'pass',
        email: 'test@example.com',
        username: 'test!',
        name: 'test',
        bio: 'g' * 256,
        projects_limit: -1
267 268
      expect(response.status).to eq(400)
      expect(json_response['message']['password']).
269
        to eq(['is too short (minimum is 8 characters)'])
270
      expect(json_response['message']['bio']).
271
        to eq(['is too long (maximum is 255 characters)'])
272
      expect(json_response['message']['projects_limit']).
273
        to eq(['must be greater than or equal to 0'])
274
      expect(json_response['message']['username']).
275
        to eq([Gitlab::Regex.send(:namespace_regex_message)])
276
    end
277 278

    context "with existing user" do
279
      before do
280 281
        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 已提交
282
        @user = User.all.last
283
      end
284

J
jubianchi 已提交
285 286
      it 'should return 409 conflict error if email address exists' do
        put api("/users/#{@user.id}", admin), email: 'test@example.com'
287 288
        expect(response.status).to eq(409)
        expect(@user.reload.email).to eq(@user.email)
J
jubianchi 已提交
289 290 291 292 293
      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'
294 295
        expect(response.status).to eq(409)
        expect(@user.reload.username).to eq(@user.username)
J
jubianchi 已提交
296
      end
297
    end
298 299
  end

A
Angus MacArthur 已提交
300 301 302 303 304
  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" }
305 306
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
J
jubianchi 已提交
307 308 309 310
    end

    it 'should not create key without title' do
      post api("/users/#{user.id}/keys", admin), key: 'some key'
311 312
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
A
Angus MacArthur 已提交
313 314 315 316
    end

    it "should create ssh key" do
      key_attrs = attributes_for :key
317
      expect do
A
Angus MacArthur 已提交
318
        post api("/users/#{user.id}/keys", admin), key_attrs
319
      end.to change{ user.keys.count }.by(1)
A
Angus MacArthur 已提交
320 321 322
    end
  end

323 324 325 326 327 328
  describe 'GET /user/:uid/keys' do
    before { admin }

    context 'when unauthenticated' do
      it 'should return authentication error' do
        get api("/users/#{user.id}/keys")
329
        expect(response.status).to eq(401)
330 331 332 333 334 335
      end
    end

    context 'when authenticated' do
      it 'should return 404 for non-existing user' do
        get api('/users/999999/keys', admin)
336 337
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 User Not Found')
338 339 340 341 342 343
      end

      it 'should return array of ssh keys' do
        user.keys << key
        user.save
        get api("/users/#{user.id}/keys", admin)
344 345 346
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(key.title)
347 348 349 350 351 352 353 354 355 356
      end
    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")
357
        expect(response.status).to eq(401)
358 359 360 361 362 363 364
      end
    end

    context 'when authenticated' do
      it 'should delete existing key' do
        user.keys << key
        user.save
365
        expect do
366
          delete api("/users/#{user.id}/keys/#{key.id}", admin)
367
        end.to change { user.keys.count }.by(-1)
368
        expect(response.status).to eq(200)
369 370 371 372 373 374
      end

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

      it 'should return 404 error if key not foud' do
        delete api("/users/#{user.id}/keys/42", admin)
381 382
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 Key Not Found')
383 384 385 386
      end
    end
  end

387 388 389 390 391
  describe "DELETE /users/:id" do
    before { admin }

    it "should delete user" do
      delete api("/users/#{user.id}", admin)
392
      expect(response.status).to eq(200)
393
      expect { User.find(user.id) }.to raise_error ActiveRecord::RecordNotFound
394
      expect(json_response['email']).to eq(user.email)
395 396
    end

397 398
    it "should not delete for unauthenticated user" do
      delete api("/users/#{user.id}")
399
      expect(response.status).to eq(401)
400 401
    end

402 403
    it "shouldn't available for non admin users" do
      delete api("/users/#{user.id}", user)
404
      expect(response.status).to eq(403)
405 406 407 408
    end

    it "should return 404 for non-existing user" do
      delete api("/users/999999", admin)
409 410
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 User Not Found')
411 412 413
    end
  end

N
Nihad Abbasov 已提交
414 415
  describe "GET /user" do
    it "should return current user" do
R
Robert Speicher 已提交
416
      get api("/user", user)
417 418 419 420 421 422
      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 已提交
423
    end
424 425 426

    it "should return 401 error if user is unauthenticated" do
      get api("/user")
427
      expect(response.status).to eq(401)
428
    end
N
Nihad Abbasov 已提交
429
  end
430 431 432 433 434

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

439 440 441 442 443
    context "when authenticated" do
      it "should return array of ssh keys" do
        user.keys << key
        user.save
        get api("/user/keys", user)
444 445 446
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first["title"]).to eq(key.title)
447 448 449 450 451
      end
    end
  end

  describe "GET /user/keys/:id" do
J
Johannes Schleifenbaum 已提交
452
    it "should return single key" do
453 454 455
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
456 457
      expect(response.status).to eq(200)
      expect(json_response["title"]).to eq(key.title)
458
    end
N
Nihad Abbasov 已提交
459

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

466 467 468 469 470
    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)
471 472
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Not found')
473
    end
474
  end
N
Nihad Abbasov 已提交
475

476
  describe "POST /user/keys" do
477
    it "should create ssh key" do
478
      key_attrs = attributes_for :key
479
      expect do
480
        post api("/user/keys", user), key_attrs
481
      end.to change{ user.keys.count }.by(1)
482
      expect(response.status).to eq(201)
483 484 485 486
    end

    it "should return a 401 error if unauthorized" do
      post api("/user/keys"), title: 'some title', key: 'some key'
487
      expect(response.status).to eq(401)
488 489 490 491
    end

    it "should not create ssh key without key" do
      post api("/user/keys", user), title: 'title'
492 493
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "key" not given')
J
jubianchi 已提交
494 495 496 497
    end

    it 'should not create ssh key without title' do
      post api('/user/keys', user), key: 'some key'
498 499
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "title" not given')
500 501 502 503
    end

    it "should not create ssh key without title" do
      post api("/user/keys", user), key: "somekey"
504
      expect(response.status).to eq(400)
505 506 507 508 509 510 511
    end
  end

  describe "DELETE /user/keys/:id" do
    it "should delete existed key" do
      user.keys << key
      user.save
512
      expect do
513
        delete api("/user/keys/#{key.id}", user)
514
      end.to change{user.keys.count}.by(-1)
515
      expect(response.status).to eq(200)
516
    end
N
Nihad Abbasov 已提交
517

K
Kevin Lyda 已提交
518
    it "should return success if key ID not found" do
519
      delete api("/user/keys/42", user)
520
      expect(response.status).to eq(200)
521 522 523 524 525 526
    end

    it "should return 401 error if unauthorized" do
      user.keys << key
      user.save
      delete api("/user/keys/#{key.id}")
527
      expect(response.status).to eq(401)
528 529
    end
  end
N
Nihad Abbasov 已提交
530
end