summaryrefslogtreecommitdiff
path: root/qa/qa/resource/ssh_key.rb
blob: 317d70ef2c344a29980f5845bb2ba141007d663b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

module QA
  module Resource
    class SSHKey < Base
      extend Forwardable

      attr_reader :title
      attr_accessor :expires_at

      attribute :id

      def_delegators :key, :private_key, :public_key, :md5_fingerprint

      def initialize
        self.title = Time.now.to_f
      end

      def key
        @key ||= Runtime::Key::RSA.new
      end

      def fabricate!
        Page::Main::Menu.perform(&:click_settings_link)
        Page::Profile::Menu.perform(&:click_ssh_keys)

        Page::Profile::SSHKeys.perform do |profile_page|
          profile_page.add_key(public_key, title)
        end
      end

      def fabricate_via_api!
        api_post
      end

      def title=(title)
        @title = "E2E test key: #{title}"
      end

      def api_delete
        QA::Runtime::Logger.debug("Deleting SSH key with title '#{title}' and fingerprint '#{md5_fingerprint}'")

        super
      end

      def api_get_path
        "/user/keys/#{id}"
      end

      def api_post_path
        '/user/keys'
      end

      def api_post_body
        {
          title: title,
          key: public_key,
          expires_at: expires_at
        }
      end

      def api_delete_path
        "/user/keys/#{id}"
      end

      def replicated?
        api_client = Runtime::API::Client.new(:geo_secondary)

        QA::Runtime::Logger.debug('Checking for SSH key replication')

        Support::Retrier.retry_until(max_duration: QA::EE::Runtime::Geo.max_db_replication_time, sleep_interval: 3) do
          response = get Runtime::API::Request.new(api_client, api_get_path).url

          response.code == QA::Support::Api::HTTP_STATUS_OK &&
            parse_body(response)[:title].include?(title)
        end
      end
    end
  end
end