Commit 7b9b3c5a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Fix part of api specs for rubocop

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent cf259cdb
...@@ -4,20 +4,20 @@ describe API::API, api: true do ...@@ -4,20 +4,20 @@ describe API::API, api: true do
include ApiHelpers include ApiHelpers
let!(:user) { create(:user) } let!(:user) { create(:user) }
let!(:application) { Doorkeeper::Application.create!(:name => "MyApp", :redirect_uri => "https://app.com", :owner => user) } let!(:application) { Doorkeeper::Application.create!(name: "MyApp", redirect_uri: "https://app.com", owner: user) }
let!(:token) { Doorkeeper::AccessToken.create! :application_id => application.id, :resource_owner_id => user.id } let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id }
describe "when unauthenticated" do describe "when unauthenticated" do
it "returns authentication success" do it "returns authentication success" do
get api("/user"), :access_token => token.token get api("/user"), access_token: token.token
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
end end
describe "when token invalid" do describe "when token invalid" do
it "returns authentication error" do it "returns authentication error" do
get api("/user"), :access_token => "123a" get api("/user"), access_token: "123a"
expect(response.status).to eq(401) expect(response.status).to eq(401)
end end
end end
......
...@@ -81,7 +81,7 @@ describe API::API, api: true do ...@@ -81,7 +81,7 @@ describe API::API, api: true do
end end
it 'should return the correct order when sorted by id' do it 'should return the correct order when sorted by id' do
get api('/projects', user), { order_by: 'id', sort: 'desc'} get api('/projects', user), { order_by: 'id', sort: 'desc' }
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.first['id']).to eq(project3.id) expect(json_response.first['id']).to eq(project3.id)
...@@ -90,7 +90,7 @@ describe API::API, api: true do ...@@ -90,7 +90,7 @@ describe API::API, api: true do
it 'returns projects in the correct order when ci_enabled_first parameter is passed' do it 'returns projects in the correct order when ci_enabled_first parameter is passed' do
[project, project2, project3].each{ |project| project.build_missing_services } [project, project2, project3].each{ |project| project.build_missing_services }
project2.gitlab_ci_service.update(active: true, token: "token", project_url: "url") project2.gitlab_ci_service.update(active: true, token: "token", project_url: "url")
get api('/projects', user), { ci_enabled_first: 'true'} get api('/projects', user), { ci_enabled_first: 'true' }
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.first['id']).to eq(project2.id) expect(json_response.first['id']).to eq(project2.id)
...@@ -123,13 +123,13 @@ describe API::API, api: true do ...@@ -123,13 +123,13 @@ describe API::API, api: true do
expect(json_response).to be_an Array expect(json_response).to be_an Array
project_name = project.name project_name = project.name
expect(json_response.detect { expect(json_response.detect do |project|
|project| project['name'] == project_name project['name'] == project_name
}['name']).to eq(project_name) end['name']).to eq(project_name)
expect(json_response.detect { expect(json_response.detect do |project|
|project| project['owner']['username'] == user.username project['owner']['username'] == user.username
}['owner']['username']).to eq(user.username) end['owner']['username']).to eq(user.username)
end end
end end
end end
...@@ -138,9 +138,9 @@ describe API::API, api: true do ...@@ -138,9 +138,9 @@ describe API::API, api: true do
context 'maximum number of projects reached' do context 'maximum number of projects reached' do
it 'should not create new project and respond with 403' do it 'should not create new project and respond with 403' do
allow_any_instance_of(User).to receive(:projects_limit_left).and_return(0) allow_any_instance_of(User).to receive(:projects_limit_left).and_return(0)
expect { expect do
post api('/projects', user2), name: 'foo' post api('/projects', user2), name: 'foo'
}.to change {Project.count}.by(0) end.to change {Project.count}.by(0)
expect(response.status).to eq(403) expect(response.status).to eq(403)
end end
end end
...@@ -474,9 +474,9 @@ describe API::API, api: true do ...@@ -474,9 +474,9 @@ describe API::API, api: true do
before { snippet } before { snippet }
it 'should delete existing project snippet' do it 'should delete existing project snippet' do
expect { expect do
delete api("/projects/#{project.id}/snippets/#{snippet.id}", user) delete api("/projects/#{project.id}/snippets/#{snippet.id}", user)
}.to change { Snippet.count }.by(-1) end.to change { Snippet.count }.by(-1)
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
...@@ -548,9 +548,9 @@ describe API::API, api: true do ...@@ -548,9 +548,9 @@ describe API::API, api: true do
it 'should create new ssh key' do it 'should create new ssh key' do
key_attrs = attributes_for :key key_attrs = attributes_for :key
expect { expect do
post api("/projects/#{project.id}/keys", user), key_attrs post api("/projects/#{project.id}/keys", user), key_attrs
}.to change{ project.deploy_keys.count }.by(1) end.to change{ project.deploy_keys.count }.by(1)
end end
end end
...@@ -558,9 +558,9 @@ describe API::API, api: true do ...@@ -558,9 +558,9 @@ describe API::API, api: true do
before { deploy_key } before { deploy_key }
it 'should delete existing key' do it 'should delete existing key' do
expect { expect do
delete api("/projects/#{project.id}/keys/#{deploy_key.id}", user) delete api("/projects/#{project.id}/keys/#{deploy_key.id}", user)
}.to change{ project.deploy_keys.count }.by(-1) end.to change{ project.deploy_keys.count }.by(-1)
end end
it 'should return 404 Not Found with invalid ID' do it 'should return 404 Not Found with invalid ID' do
......
...@@ -36,9 +36,9 @@ describe API::API, api: true do ...@@ -36,9 +36,9 @@ describe API::API, api: true do
describe "POST /hooks" do describe "POST /hooks" do
it "should create new hook" do it "should create new hook" do
expect { expect do
post api("/hooks", admin), url: 'http://example.com' post api("/hooks", admin), url: 'http://example.com'
}.to change { SystemHook.count }.by(1) end.to change { SystemHook.count }.by(1)
end end
it "should respond with 400 if url not given" do it "should respond with 400 if url not given" do
...@@ -47,9 +47,9 @@ describe API::API, api: true do ...@@ -47,9 +47,9 @@ describe API::API, api: true do
end end
it "should not create new hook without url" do it "should not create new hook without url" do
expect { expect do
post api("/hooks", admin) post api("/hooks", admin)
}.to_not change { SystemHook.count } end.to_not change { SystemHook.count }
end end
end end
...@@ -68,9 +68,9 @@ describe API::API, api: true do ...@@ -68,9 +68,9 @@ describe API::API, api: true do
describe "DELETE /hooks/:id" do describe "DELETE /hooks/:id" do
it "should delete a hook" do it "should delete a hook" do
expect { expect do
delete api("/hooks/#{hook.id}", admin) delete api("/hooks/#{hook.id}", admin)
}.to change { SystemHook.count }.by(-1) end.to change { SystemHook.count }.by(-1)
end end
it "should return success if hook id not found" do it "should return success if hook id not found" do
......
...@@ -21,9 +21,9 @@ describe API::API, api: true do ...@@ -21,9 +21,9 @@ describe API::API, api: true do
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
username = user.username username = user.username
expect(json_response.detect { expect(json_response.detect do |user|
|user| user['username'] == username user['username'] == username
}['username']).to eq(username) end['username']).to eq(username)
end end
end end
...@@ -62,9 +62,9 @@ describe API::API, api: true do ...@@ -62,9 +62,9 @@ describe API::API, api: true do
before{ admin } before{ admin }
it "should create user" do it "should create user" do
expect { expect do
post api("/users", admin), attributes_for(:user, projects_limit: 3) post api("/users", admin), attributes_for(:user, projects_limit: 3)
}.to change { User.count }.by(1) end.to change { User.count }.by(1)
end end
it "should create user with correct attributes" do it "should create user with correct attributes" do
...@@ -103,9 +103,9 @@ describe API::API, api: true do ...@@ -103,9 +103,9 @@ describe API::API, api: true do
it "should not create user with invalid email" do it "should not create user with invalid email" do
post api('/users', admin), post api('/users', admin),
email: 'invalid email', email: 'invalid email',
password: 'password', password: 'password',
name: 'test' name: 'test'
expect(response.status).to eq(400) expect(response.status).to eq(400)
end end
...@@ -131,21 +131,21 @@ describe API::API, api: true do ...@@ -131,21 +131,21 @@ describe API::API, api: true do
it 'should return 400 error if user does not validate' do it 'should return 400 error if user does not validate' do
post api('/users', admin), post api('/users', admin),
password: 'pass', password: 'pass',
email: 'test@example.com', email: 'test@example.com',
username: 'test!', username: 'test!',
name: 'test', name: 'test',
bio: 'g' * 256, bio: 'g' * 256,
projects_limit: -1 projects_limit: -1
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(json_response['message']['password']). expect(json_response['message']['password']).
to eq(['is too short (minimum is 8 characters)']) to eq(['is too short (minimum is 8 characters)'])
expect(json_response['message']['bio']). expect(json_response['message']['bio']).
to eq(['is too long (maximum is 255 characters)']) to eq(['is too long (maximum is 255 characters)'])
expect(json_response['message']['projects_limit']). expect(json_response['message']['projects_limit']).
to eq(['must be greater than or equal to 0']) to eq(['must be greater than or equal to 0'])
expect(json_response['message']['username']). expect(json_response['message']['username']).
to eq([Gitlab::Regex.send(:namespace_regex_message)]) to eq([Gitlab::Regex.send(:namespace_regex_message)])
end end
it "shouldn't available for non admin users" do it "shouldn't available for non admin users" do
...@@ -156,20 +156,20 @@ describe API::API, api: true do ...@@ -156,20 +156,20 @@ describe API::API, api: true do
context 'with existing user' do context 'with existing user' do
before do before do
post api('/users', admin), post api('/users', admin),
email: 'test@example.com', email: 'test@example.com',
password: 'password', password: 'password',
username: 'test', username: 'test',
name: 'foo' name: 'foo'
end end
it 'should return 409 conflict error if user with same email exists' do it 'should return 409 conflict error if user with same email exists' do
expect { expect do
post api('/users', admin), post api('/users', admin),
name: 'foo', name: 'foo',
email: 'test@example.com', email: 'test@example.com',
password: 'password', password: 'password',
username: 'foo' username: 'foo'
}.to change { User.count }.by(0) end.to change { User.count }.by(0)
expect(response.status).to eq(409) expect(response.status).to eq(409)
expect(json_response['message']).to eq('Email has already been taken') expect(json_response['message']).to eq('Email has already been taken')
end end
...@@ -177,10 +177,10 @@ describe API::API, api: true do ...@@ -177,10 +177,10 @@ describe API::API, api: true do
it 'should return 409 conflict error if same username exists' do it 'should return 409 conflict error if same username exists' do
expect do expect do
post api('/users', admin), post api('/users', admin),
name: 'foo', name: 'foo',
email: 'foo@example.com', email: 'foo@example.com',
password: 'password', password: 'password',
username: 'test' username: 'test'
end.to change { User.count }.by(0) end.to change { User.count }.by(0)
expect(response.status).to eq(409) expect(response.status).to eq(409)
expect(json_response['message']).to eq('Username has already been taken') expect(json_response['message']).to eq('Username has already been taken')
...@@ -203,7 +203,7 @@ describe API::API, api: true do ...@@ -203,7 +203,7 @@ describe API::API, api: true do
before { admin } before { admin }
it "should update user with new bio" do it "should update user with new bio" do
put api("/users/#{user.id}", admin), {bio: 'new test bio'} put api("/users/#{user.id}", admin), { bio: 'new test bio' }
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response['bio']).to eq('new test bio') expect(json_response['bio']).to eq('new test bio')
expect(user.reload.bio).to eq('new test bio') expect(user.reload.bio).to eq('new test bio')
...@@ -224,14 +224,14 @@ describe API::API, api: true do ...@@ -224,14 +224,14 @@ describe API::API, api: true do
end end
it "should update admin status" do it "should update admin status" do
put api("/users/#{user.id}", admin), {admin: true} put api("/users/#{user.id}", admin), { admin: true }
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response['is_admin']).to eq(true) expect(json_response['is_admin']).to eq(true)
expect(user.reload.admin).to eq(true) expect(user.reload.admin).to eq(true)
end end
it "should not update admin status" do it "should not update admin status" do
put api("/users/#{admin_user.id}", admin), {can_create_group: false} put api("/users/#{admin_user.id}", admin), { can_create_group: false }
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response['is_admin']).to eq(true) expect(json_response['is_admin']).to eq(true)
expect(admin_user.reload.admin).to eq(true) expect(admin_user.reload.admin).to eq(true)
...@@ -239,7 +239,7 @@ describe API::API, api: true do ...@@ -239,7 +239,7 @@ describe API::API, api: true do
end end
it "should not allow invalid update" do it "should not allow invalid update" do
put api("/users/#{user.id}", admin), {email: 'invalid email'} put api("/users/#{user.id}", admin), { email: 'invalid email' }
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(user.reload.email).not_to eq('invalid email') expect(user.reload.email).not_to eq('invalid email')
end end
...@@ -250,36 +250,36 @@ describe API::API, api: true do ...@@ -250,36 +250,36 @@ describe API::API, api: true do
end end
it "should return 404 for non-existing user" do it "should return 404 for non-existing user" do
put api("/users/999999", admin), {bio: 'update should fail'} put api("/users/999999", admin), { bio: 'update should fail' }
expect(response.status).to eq(404) expect(response.status).to eq(404)
expect(json_response['message']).to eq('404 Not found') expect(json_response['message']).to eq('404 Not found')
end end
it 'should return 400 error if user does not validate' do it 'should return 400 error if user does not validate' do
put api("/users/#{user.id}", admin), put api("/users/#{user.id}", admin),
password: 'pass', password: 'pass',
email: 'test@example.com', email: 'test@example.com',
username: 'test!', username: 'test!',
name: 'test', name: 'test',
bio: 'g' * 256, bio: 'g' * 256,
projects_limit: -1 projects_limit: -1
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(json_response['message']['password']). expect(json_response['message']['password']).
to eq(['is too short (minimum is 8 characters)']) to eq(['is too short (minimum is 8 characters)'])
expect(json_response['message']['bio']). expect(json_response['message']['bio']).
to eq(['is too long (maximum is 255 characters)']) to eq(['is too long (maximum is 255 characters)'])
expect(json_response['message']['projects_limit']). expect(json_response['message']['projects_limit']).
to eq(['must be greater than or equal to 0']) to eq(['must be greater than or equal to 0'])
expect(json_response['message']['username']). expect(json_response['message']['username']).
to eq([Gitlab::Regex.send(:namespace_regex_message)]) to eq([Gitlab::Regex.send(:namespace_regex_message)])
end end
context "with existing user" do context "with existing user" do
before { before do
post api("/users", admin), { email: 'test@example.com', password: 'password', username: 'test', name: 'test' } 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' } post api("/users", admin), { email: 'foo@bar.com', password: 'password', username: 'john', name: 'john' }
@user = User.all.last @user = User.all.last
} end
it 'should return 409 conflict error if email address exists' do it 'should return 409 conflict error if email address exists' do
put api("/users/#{@user.id}", admin), email: 'test@example.com' put api("/users/#{@user.id}", admin), email: 'test@example.com'
...@@ -313,9 +313,9 @@ describe API::API, api: true do ...@@ -313,9 +313,9 @@ describe API::API, api: true do
it "should create ssh key" do it "should create ssh key" do
key_attrs = attributes_for :key key_attrs = attributes_for :key
expect { expect do
post api("/users/#{user.id}/keys", admin), key_attrs post api("/users/#{user.id}/keys", admin), key_attrs
}.to change{ user.keys.count }.by(1) end.to change{ user.keys.count }.by(1)
end end
end end
...@@ -361,9 +361,9 @@ describe API::API, api: true do ...@@ -361,9 +361,9 @@ describe API::API, api: true do
it 'should delete existing key' do it 'should delete existing key' do
user.keys << key user.keys << key
user.save user.save
expect { expect do
delete api("/users/#{user.id}/keys/#{key.id}", admin) delete api("/users/#{user.id}/keys/#{key.id}", admin)
}.to change { user.keys.count }.by(-1) end.to change { user.keys.count }.by(-1)
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
...@@ -475,9 +475,9 @@ describe API::API, api: true do ...@@ -475,9 +475,9 @@ describe API::API, api: true do
describe "POST /user/keys" do describe "POST /user/keys" do
it "should create ssh key" do it "should create ssh key" do
key_attrs = attributes_for :key key_attrs = attributes_for :key
expect { expect do
post api("/user/keys", user), key_attrs post api("/user/keys", user), key_attrs
}.to change{ user.keys.count }.by(1) end.to change{ user.keys.count }.by(1)
expect(response.status).to eq(201) expect(response.status).to eq(201)
end end
...@@ -508,9 +508,9 @@ describe API::API, api: true do ...@@ -508,9 +508,9 @@ describe API::API, api: true do
it "should delete existed key" do it "should delete existed key" do
user.keys << key user.keys << key
user.save user.save
expect { expect do
delete api("/user/keys/#{key.id}", user) delete api("/user/keys/#{key.id}", user)
}.to change{user.keys.count}.by(-1) end.to change{user.keys.count}.by(-1)
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment