Commit c878b3d0 authored by Sanad Liaquat's avatar Sanad Liaquat

Merge branch 'qa-shl-cache-pats' into 'master'

Create and use PersonalAccessTokenCache

See merge request gitlab-org/gitlab!64492
parents 961497de 7ecb563e
......@@ -87,6 +87,7 @@ module QA
autoload :CiVariable, 'qa/resource/ci_variable'
autoload :Runner, 'qa/resource/runner'
autoload :PersonalAccessToken, 'qa/resource/personal_access_token'
autoload :PersonalAccessTokenCache, 'qa/resource/personal_access_token_cache'
autoload :ProjectAccessToken, 'qa/resource/project_access_token'
autoload :User, 'qa/resource/user'
autoload :ProjectMilestone, 'qa/resource/project_milestone'
......
......@@ -11,21 +11,25 @@ module QA
# This *could* be different than the api_client.user or the api_user provided by the QA::Resource::ApiFabricator module
attr_writer :user
attribute :token do
Page::Profile::PersonalAccessTokens.perform(&:created_access_token)
end
attribute :token
# Only Admins can create PAT via the API.
# If Runtime::Env.admin_personal_access_token is provided, fabricate via the API,
# else, fabricate via the browser.
def fabricate_via_api!
if Runtime::Env.admin_personal_access_token && !@user.nil?
self.api_client = Runtime::API::Client.as_admin
@token = QA::Resource::PersonalAccessTokenCache.get_token_for_username(user.username)
return if @token
super
else
fabricate!
end
resource = if Runtime::Env.admin_personal_access_token && !@user.nil?
self.api_client = Runtime::API::Client.as_admin
super
else
fabricate!
end
QA::Resource::PersonalAccessTokenCache.set_token_for_username(user.username, self.token)
resource
end
# When a user is not provided, use default user
......@@ -66,6 +70,8 @@ module QA
# Expire in 2 days just in case the token is created just before midnight
token_page.fill_expiry_date(Time.now.utc.to_date + 2)
token_page.click_create_token_button
self.token = Page::Profile::PersonalAccessTokens.perform(&:created_access_token)
end
end
end
......
# frozen_string_literal: true
module QA
module Resource
class PersonalAccessTokenCache
@personal_access_tokens = {}
def self.get_token_for_username(username)
@personal_access_tokens[username]
end
def self.set_token_for_username(username, token)
@personal_access_tokens[username] = token
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment