debian-mirror-gitlab/spec/requests/projects/settings/access_tokens_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.6 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
2022-03-02 08:16:31 +05:30
require 'spec_helper'
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
RSpec.describe Projects::Settings::AccessTokensController, feature_category: :authentication_and_authorization do
2020-05-24 23:13:21 +05:30
let_it_be(:user) { create(:user) }
2021-04-29 21:17:54 +05:30
let_it_be(:group) { create(:group) }
2022-03-02 08:16:31 +05:30
let_it_be(:resource) { create(:project, group: group) }
2023-01-13 00:05:48 +05:30
let_it_be(:access_token_user) { create(:user, :project_bot) }
2020-05-24 23:13:21 +05:30
before_all do
2022-03-02 08:16:31 +05:30
resource.add_maintainer(user)
2023-01-13 00:05:48 +05:30
resource.add_maintainer(access_token_user)
2020-05-24 23:13:21 +05:30
end
before do
sign_in(user)
end
2021-01-03 14:25:43 +05:30
shared_examples 'feature unavailable' do
context 'user is not a maintainer' do
2020-05-24 23:13:21 +05:30
before do
2022-03-02 08:16:31 +05:30
resource.add_developer(user)
2020-05-24 23:13:21 +05:30
end
2022-03-02 08:16:31 +05:30
it { expect(subject).to have_gitlab_http_status(:not_found) }
2020-05-24 23:13:21 +05:30
end
end
2022-03-02 08:16:31 +05:30
describe 'GET /:namespace/:project/-/settings/access_tokens' do
2023-01-13 00:05:48 +05:30
let(:get_access_tokens) do
2022-03-02 08:16:31 +05:30
get project_settings_access_tokens_path(resource)
response
end
2020-05-24 23:13:21 +05:30
2023-01-13 00:05:48 +05:30
let(:get_access_tokens_json) do
get project_settings_access_tokens_path(resource), params: { format: :json }
response
end
subject(:get_access_tokens_with_page) do
get project_settings_access_tokens_path(resource), params: { page: 1 }
response
end
2021-01-03 14:25:43 +05:30
it_behaves_like 'feature unavailable'
2022-03-02 08:16:31 +05:30
it_behaves_like 'GET resource access tokens available'
2023-01-13 00:05:48 +05:30
it_behaves_like 'GET access tokens are paginated and ordered'
2020-05-24 23:13:21 +05:30
end
2022-03-02 08:16:31 +05:30
describe 'POST /:namespace/:project/-/settings/access_tokens' do
2021-01-03 14:25:43 +05:30
let(:access_token_params) { { name: 'Nerd bot', scopes: ["api"], expires_at: Date.today + 1.month } }
2020-05-24 23:13:21 +05:30
2022-03-02 08:16:31 +05:30
subject do
post project_settings_access_tokens_path(resource), params: { resource_access_token: access_token_params }
response
end
2020-05-24 23:13:21 +05:30
2021-01-03 14:25:43 +05:30
it_behaves_like 'feature unavailable'
2022-03-02 08:16:31 +05:30
it_behaves_like 'POST resource access tokens available'
2021-04-29 21:17:54 +05:30
context 'when project access token creation is disabled' do
before do
group.namespace_settings.update_column(:resource_access_token_creation_allowed, false)
end
2022-03-02 08:16:31 +05:30
it { expect(subject).to have_gitlab_http_status(:not_found) }
2021-04-29 21:17:54 +05:30
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-09-30 23:02:18 +05:30
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-03-02 08:16:31 +05:30
subject { post project_settings_access_tokens_path(resource), params: { resource_access_token: access_token_params } }
2021-09-30 23:02:18 +05:30
2022-03-02 08:16:31 +05:30
it_behaves_like 'POST resource access tokens available'
2021-09-30 23:02:18 +05:30
end
2020-05-24 23:13:21 +05:30
end
2022-03-02 08:16:31 +05:30
describe 'PUT /:namespace/:project/-/settings/access_tokens/:id', :sidekiq_inline do
2023-01-13 00:05:48 +05:30
let(:resource_access_token) { create(:personal_access_token, user: access_token_user) }
2020-05-24 23:13:21 +05:30
2022-03-02 08:16:31 +05:30
subject do
put revoke_project_settings_access_token_path(resource, resource_access_token)
response
end
2020-05-24 23:13:21 +05:30
2021-01-03 14:25:43 +05:30
it_behaves_like 'feature unavailable'
2022-03-02 08:16:31 +05:30
it_behaves_like 'PUT resource access tokens available'
2020-05-24 23:13:21 +05:30
end
2022-11-25 23:54:43 +05:30
describe '#index' do
2023-01-13 00:05:48 +05:30
let_it_be(:resource_access_tokens) { create_list(:personal_access_token, 3, user: access_token_user) }
2022-11-25 23:54:43 +05:30
before do
get project_settings_access_tokens_path(resource)
end
it 'includes details of the active project access tokens' do
2023-01-13 00:05:48 +05:30
active_access_tokens =
2022-11-25 23:54:43 +05:30
::ProjectAccessTokenSerializer.new.represent(resource_access_tokens.reverse, project: resource)
2023-01-13 00:05:48 +05:30
expect(assigns(:active_access_tokens).to_json).to eq(active_access_tokens.to_json)
2022-11-25 23:54:43 +05:30
end
end
2020-05-24 23:13:21 +05:30
end