2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe API::Applications, :api, feature_category: :authentication_and_authorization do
|
2023-04-23 21:23:45 +05:30
|
|
|
let_it_be(:admin) { create(:admin) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:scopes) { 'api' }
|
|
|
|
let_it_be(:path) { "/applications" }
|
2021-10-29 20:43:33 +05:30
|
|
|
let!(:application) { create(:application, name: 'another_application', owner: nil, redirect_uri: 'http://other_application.url', scopes: scopes) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
describe 'POST /applications' do
|
2023-04-23 21:23:45 +05:30
|
|
|
it_behaves_like 'POST request permissions for admin mode', { name: 'application_name', redirect_uri: 'http://application.url', scopes: 'api' }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'authenticated and authorized user' do
|
|
|
|
it 'creates and returns an OAuth application' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: scopes }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.to change { Doorkeeper::Application.count }.by 1
|
|
|
|
|
|
|
|
application = Doorkeeper::Application.find_by(name: 'application_name', redirect_uri: 'http://application.url')
|
|
|
|
|
|
|
|
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-03-13 15:44:24 +05:30
|
|
|
expect(json_response['confidential']).to eq application.confidential
|
2021-10-29 20:43:33 +05:30
|
|
|
expect(application.scopes.to_s).to eq('api')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow creating an application with the wrong redirect_uri format' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://', scopes: scopes }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(json_response).to be_a Hash
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(json_response['message']['redirect_uri'][0]).to eq('must be a valid URI.')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-29 20:51:05 +05:30
|
|
|
it 'does not allow creating an application with a forbidden URI format' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'javascript://alert()', scopes: scopes }
|
2018-11-29 20:51:05 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-11-29 20:51:05 +05:30
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['message']['redirect_uri'][0]).to eq('is forbidden by the server.')
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'does not allow creating an application without a name' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { redirect_uri: 'http://application.url', scopes: scopes }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', scopes: scopes }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['error']).to eq('redirect_uri is missing')
|
|
|
|
end
|
|
|
|
|
2021-10-29 20:43:33 +05:30
|
|
|
it 'does not allow creating an application without specifying `scopes`' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://application.url' }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(json_response).to be_a Hash
|
2021-10-29 20:43:33 +05:30
|
|
|
expect(json_response['error']).to eq('scopes is missing, scopes is empty')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow creating an application with blank `scopes`' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: '' }
|
2021-10-29 20:43:33 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['error']).to eq('scopes is empty')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow creating an application with invalid `scopes`' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: 'non_existent_scope' }
|
2021-10-29 20:43:33 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['message']['scopes'][0]).to eq('doesn\'t match configured on the server.')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'multiple scopes' do
|
|
|
|
it 'creates an application with multiple `scopes` when each scope specified is seperated by a space' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: 'api read_user' }
|
2021-10-29 20:43:33 +05:30
|
|
|
end.to change { Doorkeeper::Application.count }.by 1
|
|
|
|
|
|
|
|
application = Doorkeeper::Application.last
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(application.scopes.to_s).to eq('api read_user')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow creating an application with multiple `scopes` when one of the scopes is invalid' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: 'api non_existent_scope' }
|
2021-10-29 20:43:33 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['message']['scopes'][0]).to eq('doesn\'t match configured on the server.')
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it 'defaults to creating an application with confidential' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path, admin, admin_mode: true), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: scopes, confidential: nil }
|
2020-07-28 23:09:34 +05:30
|
|
|
end.to change { Doorkeeper::Application.count }.by(1)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(json_response).to be_a Hash
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(json_response['callback_url']).to eq('http://application.url')
|
|
|
|
expect(json_response['confidential']).to be true
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user without authorization' do
|
|
|
|
it 'does not create application' do
|
|
|
|
expect do
|
2021-10-29 20:43:33 +05:30
|
|
|
post api('/applications', user), params: { name: 'application_name', redirect_uri: 'http://application.url', scopes: scopes }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-authenticated user' do
|
|
|
|
it 'does not create application' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
post api(path), params: { name: 'application_name', redirect_uri: 'http://application.url' }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.not_to change { Doorkeeper::Application.count }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /applications' do
|
2023-04-23 21:23:45 +05:30
|
|
|
it_behaves_like 'GET request permissions for admin mode'
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
it 'can list application' do
|
|
|
|
get api(path, admin, admin_mode: true)
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(json_response).to be_a(Array)
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-authenticated user' do
|
|
|
|
it 'cannot list application' do
|
2023-04-23 21:23:45 +05:30
|
|
|
get api(path)
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /applications/:id' do
|
2023-04-23 21:23:45 +05:30
|
|
|
context 'user authorization' do
|
|
|
|
let!(:path) { "/applications/#{application.id}" }
|
|
|
|
|
|
|
|
it_behaves_like 'DELETE request permissions for admin mode'
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
context 'authenticated and authorized user' do
|
|
|
|
it 'can delete an application' do
|
|
|
|
expect do
|
2023-04-23 21:23:45 +05:30
|
|
|
delete api("#{path}/#{application.id}", admin, admin_mode: true)
|
2018-12-13 13:39:08 +05:30
|
|
|
end.to change { Doorkeeper::Application.count }.by(-1)
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
it 'cannot delete non-existing application' do
|
2023-04-23 21:23:45 +05:30
|
|
|
delete api("#{path}/#{non_existing_record_id}", admin, admin_mode: true)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-authenticated user' do
|
|
|
|
it 'cannot delete an application' do
|
2023-04-23 21:23:45 +05:30
|
|
|
delete api("#{path}/#{application.id}")
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|