2019-07-25 01:24:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-06 11:00:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe 'Admin > Users > Impersonation Tokens', :js do
|
2022-06-15 11:09:20 -04:00
|
|
|
include Spec::Support::Helpers::ModalHelpers
|
|
|
|
|
2017-01-06 11:00:46 -05:00
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
let!(:user) { create(:user) }
|
|
|
|
|
2017-02-27 13:56:54 -05:00
|
|
|
def active_impersonation_tokens
|
2017-03-01 11:59:03 -05:00
|
|
|
find(".table.active-tokens")
|
2017-01-06 11:00:46 -05:00
|
|
|
end
|
|
|
|
|
2017-07-14 05:36:47 -04:00
|
|
|
def no_personal_access_tokens_message
|
|
|
|
find(".settings-message")
|
2017-01-06 11:00:46 -05:00
|
|
|
end
|
|
|
|
|
2018-11-08 10:03:56 -05:00
|
|
|
def created_impersonation_token
|
|
|
|
find("#created-personal-access-token").value
|
|
|
|
end
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-06-21 19:44:10 -04:00
|
|
|
sign_in(admin)
|
2020-11-23 10:09:37 -05:00
|
|
|
gitlab_enable_admin_mode_sign_in(admin)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-01-06 11:00:46 -05:00
|
|
|
|
|
|
|
describe "token creation" do
|
|
|
|
it "allows creation of a token" do
|
2017-03-23 09:08:39 -04:00
|
|
|
name = 'Hello World'
|
2017-01-06 11:00:46 -05:00
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
visit admin_user_impersonation_tokens_path(user_id: user.username)
|
2021-06-17 08:10:02 -04:00
|
|
|
fill_in "Token name", with: name
|
2017-01-06 11:00:46 -05:00
|
|
|
|
|
|
|
# Set date to 1st of next month
|
2021-06-17 08:10:02 -04:00
|
|
|
find_field("Expiration date").click
|
2017-02-09 10:21:09 -05:00
|
|
|
find(".pika-next").click
|
2017-01-06 11:00:46 -05:00
|
|
|
click_on "1"
|
|
|
|
|
|
|
|
# Scopes
|
2022-04-14 14:08:29 -04:00
|
|
|
check "read_api"
|
2017-01-06 11:00:46 -05:00
|
|
|
check "read_user"
|
|
|
|
|
2017-07-28 06:07:46 -04:00
|
|
|
click_on "Create impersonation token"
|
|
|
|
|
2017-02-27 13:56:54 -05:00
|
|
|
expect(active_impersonation_tokens).to have_text(name)
|
2021-09-15 14:11:29 -04:00
|
|
|
expect(active_impersonation_tokens).to have_text('in')
|
2022-04-14 14:08:29 -04:00
|
|
|
expect(active_impersonation_tokens).to have_text('read_api')
|
2017-02-27 13:56:54 -05:00
|
|
|
expect(active_impersonation_tokens).to have_text('read_user')
|
2017-07-28 06:07:46 -04:00
|
|
|
expect(PersonalAccessTokensFinder.new(impersonation: true).execute.count).to equal(1)
|
2018-11-08 10:03:56 -05:00
|
|
|
expect(created_impersonation_token).not_to be_empty
|
2017-02-27 13:56:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'active tokens' do
|
2017-03-01 11:59:03 -05:00
|
|
|
let!(:impersonation_token) { create(:personal_access_token, :impersonation, user: user) }
|
2017-02-27 13:56:54 -05:00
|
|
|
let!(:personal_access_token) { create(:personal_access_token, user: user) }
|
|
|
|
|
|
|
|
it 'only shows impersonation tokens' do
|
|
|
|
visit admin_user_impersonation_tokens_path(user_id: user.username)
|
|
|
|
|
|
|
|
expect(active_impersonation_tokens).to have_text(impersonation_token.name)
|
|
|
|
expect(active_impersonation_tokens).not_to have_text(personal_access_token.name)
|
2021-09-15 14:11:29 -04:00
|
|
|
expect(active_impersonation_tokens).to have_text('in')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows absolute times' do
|
|
|
|
admin.update!(time_display_relative: false)
|
|
|
|
visit admin_user_impersonation_tokens_path(user_id: user.username)
|
|
|
|
|
2021-09-26 17:10:02 -04:00
|
|
|
expect(active_impersonation_tokens).to have_text(personal_access_token.expires_at.strftime('%b %-d'))
|
2017-01-06 11:00:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "inactive tokens" do
|
2017-03-01 11:59:03 -05:00
|
|
|
let!(:impersonation_token) { create(:personal_access_token, :impersonation, user: user) }
|
2017-02-23 12:47:06 -05:00
|
|
|
|
|
|
|
it "allows revocation of an active impersonation token" do
|
|
|
|
visit admin_user_impersonation_tokens_path(user_id: user.username)
|
2017-01-06 11:00:46 -05:00
|
|
|
|
2022-06-15 11:09:20 -04:00
|
|
|
accept_gl_confirm(button_text: 'Revoke') { click_on "Revoke" }
|
2017-01-06 11:00:46 -05:00
|
|
|
|
2017-07-14 05:36:47 -04:00
|
|
|
expect(page).to have_selector(".settings-message")
|
2020-05-15 05:07:59 -04:00
|
|
|
expect(no_personal_access_tokens_message).to have_text("This user has no active impersonation tokens.")
|
2017-01-06 11:00:46 -05:00
|
|
|
end
|
|
|
|
|
2017-07-14 08:22:17 -04:00
|
|
|
it "removes expired tokens from 'active' section" do
|
2021-04-04 14:09:19 -04:00
|
|
|
impersonation_token.update!(expires_at: 5.days.ago)
|
2017-02-23 12:47:06 -05:00
|
|
|
|
|
|
|
visit admin_user_impersonation_tokens_path(user_id: user.username)
|
2017-01-06 11:00:46 -05:00
|
|
|
|
2017-07-14 05:36:47 -04:00
|
|
|
expect(page).to have_selector(".settings-message")
|
2020-05-15 05:07:59 -04:00
|
|
|
expect(no_personal_access_tokens_message).to have_text("This user has no active impersonation tokens.")
|
2017-01-06 11:00:46 -05:00
|
|
|
end
|
|
|
|
end
|
2021-08-03 17:09:39 -04:00
|
|
|
|
|
|
|
describe "impersonation disabled state" do
|
|
|
|
before do
|
|
|
|
stub_config_setting(impersonation_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not show impersonation tokens tab" do
|
|
|
|
visit admin_user_path(user)
|
|
|
|
|
|
|
|
expect(page).not_to have_content("Impersonation Tokens")
|
|
|
|
end
|
|
|
|
end
|
2017-01-06 11:00:46 -05:00
|
|
|
end
|