2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-04 17:07:49 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Applications, :api do
|
2017-01-04 17:07:49 -05:00
|
|
|
let(:admin_user) { create(:user, admin: true) }
|
|
|
|
let(:user) { create(:user, admin: false) }
|
2018-10-11 11:25:52 -04:00
|
|
|
let!(:application) { create(:application, name: 'another_application', redirect_uri: 'http://other_application.url', scopes: '') }
|
2017-01-04 17:07:49 -05:00
|
|
|
|
|
|
|
describe 'POST /applications' do
|
|
|
|
context 'authenticated and authorized user' do
|
|
|
|
it 'creates and returns an OAuth application' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications', admin_user), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: '' }
|
2017-01-04 17:07:49 -05:00
|
|
|
end.to change { Doorkeeper::Application.count }.by 1
|
|
|
|
|
|
|
|
application = Doorkeeper::Application.find_by(name: 'application_name', redirect_uri: 'http://application.url')
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-01-04 17:07:49 -05:00
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['application_id']).to eq application.uid
|
|
|
|
expect(json_response['secret']).to eq application.secret
|
|
|
|
expect(json_response['callback_url']).to eq application.redirect_uri
|
2020-01-22 13:08:47 -05:00
|
|
|
expect(json_response['confidential']).to eq application.confidential
|
2017-01-04 17:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow creating an application with the wrong redirect_uri format' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications', admin_user), params: { name: 'application_name', redirect_uri: 'http://', scopes: '' }
|
2017-01-04 17:07:49 -05:00
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2017-01-04 17:07:49 -05:00
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['message']['redirect_uri'][0]).to eq('must be an absolute URI.')
|
|
|
|
end
|
|
|
|
|
2018-11-28 17:53:48 -05:00
|
|
|
it 'does not allow creating an application with a forbidden URI format' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications', admin_user), params: { name: 'application_name', redirect_uri: 'javascript://alert()', scopes: '' }
|
2018-11-28 17:53:48 -05:00
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-11-28 17:53:48 -05:00
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['message']['redirect_uri'][0]).to eq('is forbidden by the server.')
|
|
|
|
end
|
|
|
|
|
2017-01-04 17:07:49 -05:00
|
|
|
it 'does not allow creating an application without a name' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications', admin_user), params: { redirect_uri: 'http://application.url', scopes: '' }
|
2017-01-04 17:07:49 -05:00
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2017-01-04 17:07:49 -05:00
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['error']).to eq('name is missing')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow creating an application without a redirect_uri' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications', admin_user), params: { name: 'application_name', scopes: '' }
|
2017-01-04 17:07:49 -05:00
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2017-01-04 17:07:49 -05:00
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['error']).to eq('redirect_uri is missing')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow creating an application without scopes' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications', admin_user), params: { name: 'application_name', redirect_uri: 'http://application.url' }
|
2017-01-04 17:07:49 -05:00
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2017-01-04 17:07:49 -05:00
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['error']).to eq('scopes is missing')
|
|
|
|
end
|
2020-01-22 13:08:47 -05:00
|
|
|
|
2020-06-29 17:09:07 -04:00
|
|
|
it 'defaults to creating an application with confidential' do
|
2020-01-22 13:08:47 -05:00
|
|
|
expect do
|
|
|
|
post api('/applications', admin_user), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: '', confidential: nil }
|
2020-06-29 17:09:07 -04:00
|
|
|
end.to change { Doorkeeper::Application.count }.by(1)
|
2020-01-22 13:08:47 -05:00
|
|
|
|
2020-06-29 17:09:07 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-01-22 13:08:47 -05:00
|
|
|
expect(json_response).to be_a Hash
|
2020-06-29 17:09:07 -04:00
|
|
|
expect(json_response['callback_url']).to eq('http://application.url')
|
|
|
|
expect(json_response['confidential']).to be true
|
2020-01-22 13:08:47 -05:00
|
|
|
end
|
2017-01-04 17:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user without authorization' do
|
|
|
|
it 'does not create application' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications', user), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: '' }
|
2017-01-04 17:07:49 -05:00
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-01-04 17:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-authenticated user' do
|
|
|
|
it 'does not create application' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/applications'), params: { name: 'application_name', redirect_uri: 'http://application.url' }
|
2017-01-04 17:07:49 -05:00
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-01-04 17:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-11 06:54:15 -04:00
|
|
|
|
|
|
|
describe 'GET /applications' do
|
|
|
|
context 'authenticated and authorized user' do
|
|
|
|
it 'can list application' do
|
2018-10-11 08:12:25 -04:00
|
|
|
get api('/applications', admin_user)
|
2018-10-11 06:54:15 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-11 06:54:15 -04:00
|
|
|
expect(json_response).to be_a(Array)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-11 09:35:17 -04:00
|
|
|
context 'authorized user without authorization' do
|
|
|
|
it 'cannot list application' do
|
|
|
|
get api('/applications', user)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-10-11 09:35:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-11 06:54:15 -04:00
|
|
|
context 'non-authenticated user' do
|
|
|
|
it 'cannot list application' do
|
2018-10-11 20:28:40 -04:00
|
|
|
get api('/applications')
|
2018-10-11 06:54:15 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-10-11 06:54:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /applications/:id' do
|
|
|
|
context 'authenticated and authorized user' do
|
|
|
|
it 'can delete an application' do
|
2018-10-11 08:12:25 -04:00
|
|
|
expect do
|
|
|
|
delete api("/applications/#{application.id}", admin_user)
|
2018-10-11 09:35:17 -04:00
|
|
|
end.to change { Doorkeeper::Application.count }.by(-1)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2018-10-11 06:54:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-11 09:35:17 -04:00
|
|
|
context 'authorized user without authorization' do
|
2018-10-11 06:54:15 -04:00
|
|
|
it 'cannot delete an application' do
|
2018-10-11 08:12:25 -04:00
|
|
|
delete api("/applications/#{application.id}", user)
|
2018-10-11 06:54:15 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-10-11 09:35:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-authenticated user' do
|
|
|
|
it 'cannot delete an application' do
|
|
|
|
delete api("/applications/#{application.id}")
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-10-11 06:54:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-04 17:07:49 -05:00
|
|
|
end
|