diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-29 06:09:39 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-29 06:09:39 +0300 |
commit | 8574de998fbefb1d846baddc80c88e9b2dc56c72 (patch) | |
tree | 09ca42fc3e2ee33077499a873cdcf6bec4dbfc5b /qa | |
parent | ac6a319712d345ab54d4eb5be1a0a4116823dd1e (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'qa')
-rw-r--r-- | qa/Rakefile | 6 | ||||
-rw-r--r-- | qa/qa/page/component/select2.rb | 6 | ||||
-rw-r--r-- | qa/qa/tools/delete_test_ssh_keys.rb | 61 |
3 files changed, 72 insertions, 1 deletions
diff --git a/qa/Rakefile b/qa/Rakefile index 7ba8a6d68ba..9a960a60e56 100644 --- a/qa/Rakefile +++ b/qa/Rakefile @@ -1,6 +1,7 @@ require_relative 'qa/tools/revoke_all_personal_access_tokens' require_relative 'qa/tools/delete_subgroups' require_relative 'qa/tools/generate_perf_testdata' +require_relative 'qa/tools/delete_test_ssh_keys' desc "Revokes all personal access tokens" task :revoke_personal_access_tokens do @@ -39,3 +40,8 @@ end desc "Generate data and run load tests" task generate_data_and_run_load_test: [:generate_perf_testdata, :run_artillery_load_tests] + +desc "Deletes test ssh keys for a provided user" +task :delete_test_ssh_keys do + QA::Tools::DeleteTestSSHKeys.new.run +end diff --git a/qa/qa/page/component/select2.rb b/qa/qa/page/component/select2.rb index 7e3308c0c8f..b8beb64b6bd 100644 --- a/qa/qa/page/component/select2.rb +++ b/qa/qa/page/component/select2.rb @@ -18,10 +18,14 @@ module QA end end - def search_and_select(item_text) + def search_item(item_text) find('.select2-input').set(item_text) wait_for_search_to_complete + end + + def search_and_select(item_text) + search_item(item_text) select_item(item_text) end diff --git a/qa/qa/tools/delete_test_ssh_keys.rb b/qa/qa/tools/delete_test_ssh_keys.rb new file mode 100644 index 00000000000..f502542b529 --- /dev/null +++ b/qa/qa/tools/delete_test_ssh_keys.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require_relative '../../qa' + +# This script deletes all test ssh keys (with titles including 'key for ssh tests' or 'key for audit event test') of a user specified by ENV['GITLAB_USERNAME'] +# Required environment variables: GITLAB_QA_ACCESS_TOKEN, GITLAB_ADDRESS and GITLAB_USERNAME + +module QA + module Tools + class DeleteTestSSHKeys + include Support::Api + + def initialize + raise ArgumentError, "Please provide GITLAB_ADDRESS" unless ENV['GITLAB_ADDRESS'] + raise ArgumentError, "Please provide GITLAB_QA_ACCESS_TOKEN" unless ENV['GITLAB_QA_ACCESS_TOKEN'] + raise ArgumentError, "Please provide GITLAB_USERNAME" unless ENV['GITLAB_USERNAME'] + + @api_client = Runtime::API::Client.new(ENV['GITLAB_ADDRESS'], personal_access_token: ENV['GITLAB_QA_ACCESS_TOKEN']) + @username = ENV['GITLAB_USERNAME'] + end + + def run + STDOUT.puts 'Running...' + + user_id = fetch_user_id + test_ssh_key_ids = fetch_test_ssh_key_ids(user_id) + STDOUT.puts "Number of test ssh keys to be deleted: #{test_ssh_key_ids.length}" + + delete_ssh_keys(user_id, test_ssh_key_ids) unless test_ssh_key_ids.empty? + STDOUT.puts "\nDone" + end + + private + + def fetch_user_id + get_user_response = get Runtime::API::Request.new(@api_client, "/users?username=#{@username}").url + user = JSON.parse(get_user_response.body).first + raise "Unexpected user found. Expected #{@username}, found #{user['username']}" unless user['username'] == @username + + user["id"] + end + + def delete_ssh_keys(user_id, ssh_key_ids) + STDOUT.puts "Deleting #{ssh_key_ids.length} ssh keys..." + ssh_key_ids.each do |key_id| + delete_response = delete Runtime::API::Request.new(@api_client, "/users/#{user_id}/keys/#{key_id}").url + dot_or_f = delete_response.code == 204 ? "\e[32m.\e[0m" : "\e[31mF\e[0m" + print dot_or_f + end + end + + def fetch_test_ssh_key_ids(user_id) + get_keys_response = get Runtime::API::Request.new(@api_client, "/users/#{user_id}/keys").url + JSON.parse(get_keys_response.body) + .select { |key| (key["title"].include?('key for ssh tests') || key["title"].include?('key for audit event test')) } + .map { |key| key['id'] } + .uniq + end + end + end +end |