Commit 53f8e517 authored by Robert Speicher's avatar Robert Speicher

Merge branch '327328-ssh-key-error-refactor' into 'master'

Refactor expired SSH key error messages

See merge request gitlab-org/gitlab!64519
parents 7d3777a3 bd507838
......@@ -8,14 +8,7 @@ module EE
def ssh_key_expiration_tooltip(key)
return super unless ::Key.expiration_enforced? && key.expired?
# The key returns a validation error for an invalid keys which is displayed in git related operations.
# However, on the UI we don't want to show this message,
# so we strip it out and check for any other errors
return s_('Profiles|Invalid key.') unless key.errors.full_messages.reject do |m|
m.eql?('Key has expired and the instance administrator has enforced expiration')
end.empty?
s_('Profiles|Expired key is not valid.')
key.only_expired_and_enforced? ? s_('Profiles|Expired key is not valid.') : s_('Profiles|Invalid key.')
end
override :ssh_key_expires_field_description
......
......@@ -12,7 +12,17 @@ module EE
validate :expiration, if: -> { ::Key.expiration_enforced? }
def expiration
errors.add(:key, 'has expired and the instance administrator has enforced expiration') if expired?
errors.add(:key, :expired_and_enforced, message: 'has expired and the instance administrator has enforced expiration') if expired?
end
# Returns true if the key is:
# - Expired
# - Expiration is enforced
# - Not invalid for any other reason
def only_expired_and_enforced?
return false unless ::Key.expiration_enforced? && expired?
errors.map(&:type).reject { |t| t.eql?(:expired_and_enforced) }.empty?
end
end
......
......@@ -24,6 +24,33 @@ RSpec.describe Key do
end
end
describe 'only_expired_and_enforced?' do
using RSpec::Parameterized::TableSyntax
where(:expired, :enforced, :valid, :result) do
true | true | true | true
true | false | true | false
false | true | true | false
true | true | false | true
false | false | true | false
end
with_them do
before do
stub_licensed_features(enforce_ssh_key_expiration: true)
stub_ee_application_setting(enforce_ssh_key_expiration: enforced)
end
it 'checks if ssh key expiration is enforced' do
key = create(:personal_key)
key.update_attribute(:expires_at, 3.days.ago) if expired
key.update_attribute(:title, nil) unless valid
expect(key.only_expired_and_enforced?).to be(result)
end
end
end
describe '.expiration_enforced?' do
using RSpec::Parameterized::TableSyntax
......
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