summaryrefslogtreecommitdiff
path: root/spec/views/profiles/keys/_key.html.haml_spec.rb
blob: 09053a29fe0105d44c6f8894f785c1b81bb50a28 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'profiles/keys/_key.html.haml', feature_category: :authentication_and_authorization do
  let_it_be(:user) { create(:user) }

  before do
    allow(view).to receive(:key).and_return(key)
    allow(view).to receive(:is_admin).and_return(false)
  end

  context 'when the key partial is used' do
    let_it_be(:key) do
      create(:personal_key,
             user: user,
             last_used_at: 7.days.ago,
             expires_at: 2.days.from_now)
    end

    it 'displays the correct values', :aggregate_failures do
      render

      expect(rendered).to have_text(key.title)
      expect(rendered).to have_css('[data-testid="key-icon"]')
      expect(rendered).to have_text(key.fingerprint)
      expect(rendered).to have_text(l(key.last_used_at, format: "%b %d, %Y"))
      expect(rendered).to have_text(l(key.created_at, format: "%b %d, %Y"))
      expect(rendered).to have_text(key.expires_at.to_date)
      expect(rendered).to have_button('Remove')
    end

    context 'displays the usage type' do
      where(:usage_type, :usage_type_text, :displayed_buttons, :hidden_buttons, :revoke_ssh_signatures_ff) do
        [
          [:auth, 'Authentication', ['Remove'], ['Revoke'], true],
          [:auth_and_signing, 'Authentication & Signing', %w[Remove Revoke], [], true],
          [:signing, 'Signing', %w[Remove Revoke], [], true],
          [:auth, 'Authentication', ['Remove'], ['Revoke'], false],
          [:auth_and_signing, 'Authentication & Signing', %w[Remove], ['Revoke'], false],
          [:signing, 'Signing', %w[Remove], ['Revoke'], false]
        ]
      end

      with_them do
        let(:key) { create(:key, user: user, usage_type: usage_type) }

        it 'renders usage type text' do
          render

          expect(rendered).to have_text(usage_type_text)
        end

        it 'renders remove/revoke buttons', :aggregate_failures do
          stub_feature_flags(revoke_ssh_signatures: revoke_ssh_signatures_ff)

          render

          displayed_buttons.each do |button|
            expect(rendered).to have_text(button)
          end

          hidden_buttons.each do |button|
            expect(rendered).not_to have_text(button)
          end
        end
      end
    end

    context 'when the key has not been used' do
      let_it_be(:key) do
        create(:personal_key,
               user: user,
               last_used_at: nil)
      end

      it 'renders "Never" for last used' do
        render

        expect(rendered).to have_text('Last used: Never')
      end
    end

    context 'when the key does not have an expiration date' do
      let_it_be(:key) do
        create(:personal_key,
               user: user,
               expires_at: nil)
      end

      it 'renders "Never" for expires' do
        render

        expect(rendered).to have_text('Expires: Never')
      end
    end

    context 'when the key has expired' do
      let_it_be(:key) { create(:personal_key, :expired, user: user) }

      it 'renders "Expired:" as the expiration date label' do
        render

        expect(rendered).to have_text('Expired:')
      end
    end

    context 'when the key is not deletable' do
      # Turns out key.can_delete? is only false for LDAP keys
      # but LDAP keys don't exist outside EE
      before do
        allow(key).to receive(:can_delete?).and_return(false)
      end

      it 'does not render the partial' do
        render

        expect(response).not_to have_text('Remove')
        expect(response).not_to have_text('Revoke')
      end
    end

    context 'icon tooltip' do
      using RSpec::Parameterized::TableSyntax

      where(:valid, :expiry, :result) do
        false | 2.days.from_now | 'Key type is forbidden. Must be DSA, ECDSA, ED25519, ECDSA_SK, or ED25519_SK'
        true  | 2.days.from_now | ''
      end

      with_them do
        let_it_be(:key) do
          create(:personal_key, user: user)
        end

        it 'renders the correct icon', :aggregate_failures do
          unless valid
            stub_application_setting(rsa_key_restriction: ApplicationSetting::FORBIDDEN_KEY_VALUE)
          end

          key.expires_at = expiry

          render

          if result.empty?
            expect(rendered).to have_css('[data-testid="key-icon"]')
          else
            expect(rendered).to have_css('[data-testid="warning-solid-icon"]')
            expect(rendered).to have_selector("span.has-tooltip[title='#{result}']")
          end
        end
      end
    end
  end
end