2020-05-05 21:09:42 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
require 'spec_helper'
|
2020-05-05 21:09:42 +00:00
|
|
|
|
2020-06-03 18:08:28 +00:00
|
|
|
RSpec.describe Projects::Settings::AccessTokensController do
|
2020-05-05 21:09:42 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
2021-04-06 03:09:02 +00:00
|
|
|
let_it_be(:group) { create(:group) }
|
2022-01-17 12:17:11 +00:00
|
|
|
let_it_be(:resource) { create(:project, group: group) }
|
2020-10-16 12:09:33 +00:00
|
|
|
let_it_be(:bot_user) { create(:user, :project_bot) }
|
2020-05-05 21:09:42 +00:00
|
|
|
|
|
|
|
before_all do
|
2022-01-17 12:17:11 +00:00
|
|
|
resource.add_maintainer(user)
|
|
|
|
resource.add_maintainer(bot_user)
|
2020-05-05 21:09:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2020-09-30 18:09:52 +00:00
|
|
|
shared_examples 'feature unavailable' do
|
2020-10-16 12:09:33 +00:00
|
|
|
context 'user is not a maintainer' do
|
|
|
|
before do
|
2022-01-17 12:17:11 +00:00
|
|
|
resource.add_developer(user)
|
2020-10-16 12:09:33 +00:00
|
|
|
end
|
2020-05-05 21:09:42 +00:00
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
it { expect(subject).to have_gitlab_http_status(:not_found) }
|
2020-05-05 21:09:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
describe 'GET /:namespace/:project/-/settings/access_tokens' do
|
|
|
|
subject do
|
|
|
|
get project_settings_access_tokens_path(resource)
|
|
|
|
response
|
|
|
|
end
|
2020-05-05 21:09:42 +00:00
|
|
|
|
2020-09-30 18:09:52 +00:00
|
|
|
it_behaves_like 'feature unavailable'
|
2022-01-17 12:17:11 +00:00
|
|
|
it_behaves_like 'GET resource access tokens available'
|
2020-05-05 21:09:42 +00:00
|
|
|
end
|
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
describe 'POST /:namespace/:project/-/settings/access_tokens' do
|
2020-10-16 12:09:33 +00:00
|
|
|
let(:access_token_params) { { name: 'Nerd bot', scopes: ["api"], expires_at: Date.today + 1.month } }
|
2020-05-05 21:09:42 +00:00
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
subject do
|
|
|
|
post project_settings_access_tokens_path(resource), params: { resource_access_token: access_token_params }
|
|
|
|
response
|
|
|
|
end
|
2020-05-05 21:09:42 +00:00
|
|
|
|
2020-09-30 18:09:52 +00:00
|
|
|
it_behaves_like 'feature unavailable'
|
2022-01-17 12:17:11 +00:00
|
|
|
it_behaves_like 'POST resource access tokens available'
|
2021-04-06 03:09:02 +00:00
|
|
|
|
|
|
|
context 'when project access token creation is disabled' do
|
|
|
|
before do
|
|
|
|
group.namespace_settings.update_column(:resource_access_token_creation_allowed, false)
|
|
|
|
end
|
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
it { expect(subject).to have_gitlab_http_status(:not_found) }
|
2021-04-06 03:09:02 +00:00
|
|
|
|
|
|
|
it 'does not create the token' do
|
|
|
|
expect { subject }.not_to change { PersonalAccessToken.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add the project bot as a member' do
|
|
|
|
expect { subject }.not_to change { Member.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create the project bot user' do
|
|
|
|
expect { subject }.not_to change { User.count }
|
|
|
|
end
|
|
|
|
end
|
2021-06-29 12:08:48 +00:00
|
|
|
|
|
|
|
context 'with custom access level' do
|
|
|
|
let(:access_token_params) { { name: 'Nerd bot', scopes: ["api"], expires_at: Date.today + 1.month, access_level: 20 } }
|
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
subject { post project_settings_access_tokens_path(resource), params: { resource_access_token: access_token_params } }
|
2021-06-29 12:08:48 +00:00
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
it_behaves_like 'POST resource access tokens available'
|
2021-06-29 12:08:48 +00:00
|
|
|
end
|
2020-05-05 21:09:42 +00:00
|
|
|
end
|
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
describe 'PUT /:namespace/:project/-/settings/access_tokens/:id', :sidekiq_inline do
|
|
|
|
let(:resource_access_token) { create(:personal_access_token, user: bot_user) }
|
2020-05-05 21:09:42 +00:00
|
|
|
|
2022-01-17 12:17:11 +00:00
|
|
|
subject do
|
|
|
|
put revoke_project_settings_access_token_path(resource, resource_access_token)
|
|
|
|
response
|
|
|
|
end
|
2020-05-05 21:09:42 +00:00
|
|
|
|
2020-09-30 18:09:52 +00:00
|
|
|
it_behaves_like 'feature unavailable'
|
2022-01-17 12:17:11 +00:00
|
|
|
it_behaves_like 'PUT resource access tokens available'
|
2020-05-05 21:09:42 +00:00
|
|
|
end
|
|
|
|
end
|