Commit 34a86120 authored by Semyon Pupkov's avatar Semyon Pupkov

Use setter for key instead AR callback

ref: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6763
parent d5cd0d67
......@@ -6,7 +6,7 @@ class Key < ActiveRecord::Base
belongs_to :user
before_validation :strip_white_space, :generate_fingerprint
before_validation :generate_fingerprint
validates :title, presence: true, length: { within: 0..255 }
validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ }
......@@ -21,8 +21,9 @@ class Key < ActiveRecord::Base
after_destroy :remove_from_shell
after_destroy :post_destroy_hook
def strip_white_space
self.key = key.strip unless key.blank?
def key=(value)
value.strip! unless value.blank?
write_attribute(:key, value)
end
def publishable_key
......
---
title: Use setter for key instead AR callback
merge_request: 7488
author: Semyon Pupkov
......@@ -82,4 +82,14 @@ describe Key, models: true do
@key.destroy
end
end
describe '#key=' do
let(:valid_key) do
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0= dummy@gitlab.com"
end
it 'strips white spaces' do
expect(described_class.new(key: " #{valid_key} ").key).to eq(valid_key)
end
end
end
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