diff options
Diffstat (limited to 'qa/qa/runtime/api/client.rb')
-rw-r--r-- | qa/qa/runtime/api/client.rb | 73 |
1 files changed, 44 insertions, 29 deletions
diff --git a/qa/qa/runtime/api/client.rb b/qa/qa/runtime/api/client.rb index 8a5e22fbc37..b5b572890c1 100644 --- a/qa/qa/runtime/api/client.rb +++ b/qa/qa/runtime/api/client.rb @@ -16,17 +16,21 @@ module QA enable_ip_limits if ip_limits end + # Personal access token + # + # It is possible to set the environment variable GITLAB_QA_ACCESS_TOKEN + # to use a specific access token rather than create one from the UI + # unless a specific user has been passed + # + # @return [String] def personal_access_token - @personal_access_token ||= begin - # you can set the environment variable GITLAB_QA_ACCESS_TOKEN - # to use a specific access token rather than create one from the UI - # unless a specific user has been passed - @user.nil? ? Runtime::Env.personal_access_token ||= create_personal_access_token : create_personal_access_token - end + @personal_access_token ||= if user.nil? + Runtime::Env.personal_access_token ||= create_personal_access_token + else + create_personal_access_token + end - if @user&.admin? - Runtime::Env.admin_personal_access_token = @personal_access_token - end + Runtime::Env.admin_personal_access_token = @personal_access_token if user&.admin? # rubocop:disable Cop/UserAdmin @personal_access_token end @@ -82,27 +86,38 @@ module QA Page::Main::Menu.perform(&:sign_out) end + # Create PAT + # + # Use api if admin personal access token is present and skip any UI actions otherwise perform creation via UI + # + # @return [String] def create_personal_access_token - signed_in_initially = Page::Main::Menu.perform(&:signed_in?) - - Page::Main::Menu.perform(&:sign_out) if @is_new_session && signed_in_initially - - token = Resource::PersonalAccessToken.fabricate! do |pat| - pat.user = user - end.token - - # If this is a new session, that tests that follow could fail if they - # try to sign in without starting a new session. - # Also, if the browser wasn't already signed in, leaving it - # signed in could cause tests to fail when they try to sign - # in again. For example, that would happen if a test has a - # before(:context) block that fabricates via the API, and - # it's the first test to run so it creates an access token - # - # Sign out so the tests can successfully sign in - Page::Main::Menu.perform(&:sign_out) if @is_new_session || !signed_in_initially - - token + if Runtime::Env.admin_personal_access_token + Resource::PersonalAccessToken.fabricate_via_api! do |pat| + pat.user = user + end.token + else + signed_in_initially = Page::Main::Menu.perform(&:signed_in?) + + Page::Main::Menu.perform(&:sign_out) if @is_new_session && signed_in_initially + + token = Resource::PersonalAccessToken.fabricate! do |pat| + pat.user = user + end.token + + # If this is a new session, that tests that follow could fail if they + # try to sign in without starting a new session. + # Also, if the browser wasn't already signed in, leaving it + # signed in could cause tests to fail when they try to sign + # in again. For example, that would happen if a test has a + # before(:context) block that fabricates via the API, and + # it's the first test to run so it creates an access token + # + # Sign out so the tests can successfully sign in + Page::Main::Menu.perform(&:sign_out) if @is_new_session || !signed_in_initially + + token + end end end end |