Commit bf06a073 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'setter-for-key' into 'master'

Use setter for key instead AR callback

## What does this MR do?

- [x] [Changelog entry](https://docs.gitlab.com/ce/development/changelog.html) added
- n/a [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md)
- n/a API support added
- Tests
  - [x] Added for this feature/bug
  - [x] All builds are passing
- [x] Conform by the [merge request performance guides](http://docs.gitlab.com/ce/development/merge_request_performance_guidelines.html)
- [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides)
- [x] Branch has no merge conflicts with `master` (if it does - rebase it please)
- [ ] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits)

## What are the relevant issue numbers?
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6763

See merge request !7488
parents 1263708d 2a951840
...@@ -6,7 +6,7 @@ class Key < ActiveRecord::Base ...@@ -6,7 +6,7 @@ class Key < ActiveRecord::Base
belongs_to :user belongs_to :user
before_validation :strip_white_space, :generate_fingerprint before_validation :generate_fingerprint
validates :title, presence: true, length: { within: 0..255 } validates :title, presence: true, length: { within: 0..255 }
validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ } validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ }
...@@ -21,8 +21,9 @@ class Key < ActiveRecord::Base ...@@ -21,8 +21,9 @@ class Key < ActiveRecord::Base
after_destroy :remove_from_shell after_destroy :remove_from_shell
after_destroy :post_destroy_hook after_destroy :post_destroy_hook
def strip_white_space def key=(value)
self.key = key.strip unless key.blank? value.strip! unless value.blank?
write_attribute(:key, value)
end end
def publishable_key def publishable_key
......
---
title: Use setter for key instead AR callback
merge_request: 7488
author: Semyon Pupkov
...@@ -71,15 +71,25 @@ describe Key, models: true do ...@@ -71,15 +71,25 @@ describe Key, models: true do
context 'callbacks' do context 'callbacks' do
it 'adds new key to authorized_file' do it 'adds new key to authorized_file' do
@key = build(:personal_key, id: 7) key = build(:personal_key, id: 7)
expect(GitlabShellWorker).to receive(:perform_async).with(:add_key, @key.shell_id, @key.key) expect(GitlabShellWorker).to receive(:perform_async).with(:add_key, key.shell_id, key.key)
@key.save key.save!
end end
it 'removes key from authorized_file' do it 'removes key from authorized_file' do
@key = create(:personal_key) key = create(:personal_key)
expect(GitlabShellWorker).to receive(:perform_async).with(:remove_key, @key.shell_id, @key.key) expect(GitlabShellWorker).to receive(:perform_async).with(:remove_key, key.shell_id, key.key)
@key.destroy 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 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