-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shoulda-matcher for easier validation checking. Removed project method from keys model cause we are not using it. Stub callback methods of keys model to save hassel.
- Loading branch information
1 parent
7e2f0bf
commit 644abef
Showing
8 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,45 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe KeysController, type: :controller do | ||
describe KeysController, type: :controller do | ||
let(:user) { create(:user) } | ||
before { sign_in(user) } | ||
|
||
describe 'GET index' do | ||
it 'renders the index template' do | ||
get :index | ||
expect(response).to render_template('index') | ||
end | ||
end | ||
|
||
describe 'POST create' do | ||
before do | ||
allow_any_instance_of(Key).to receive(:add_to_shell).and_return(true) | ||
end | ||
it 'adds key and redirects to key path' do | ||
ssh_key = | ||
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx3ke+rnMT/ILY81K1un1CWf9ghcP' + | ||
'glIlV7pMV2H5AwyC/Dx5x+DyKmNmhBmvCYJ+1we8f0pPXLx2QpyAXw8s0s+sBL/gkiz' + | ||
'sqqwrUzK9Rlkj58kvNFl8gLQk3qqs8dR6bODP9LQqCGhMFErQtDQTvBq91jhWuIIunu' + | ||
'mK7T+0GWDMf7O9CNdr/aprYrUfuGLggOdz0oPja792V+ay1xWAHEOueKfGvOGFDbQlc' + | ||
'TT2uI9wYz9RGkLhDNOo4S74W59xMwMpf77XsoTYxcdrAT7WpTlzaj2usbbGBgcBKx5k' + | ||
'b0dPBOQ3rQadtZnLjN2dZAeapUO2MElyX0lxt1nrbIKC2 [email protected]' | ||
post :create, key: { title: 'test', key: ssh_key } | ||
expect(Key.last.title).to eq('test') | ||
expect(response).to redirect_to(keys_path) | ||
end | ||
end | ||
|
||
describe 'DELETE destroy' do | ||
before do | ||
allow_any_instance_of(Key).to receive(:add_to_shell).and_return(true) | ||
allow_any_instance_of(Key).to receive(:remove_from_shell).and_return(true) | ||
@request.env['HTTP_REFERER'] = 'http://test.host/keys' | ||
end | ||
it 'removes key and redirects to key path' do | ||
key = create(:key, user: user) | ||
delete :destroy, id: key.id | ||
expect(Key.all.count).to be(0) | ||
expect(response).to redirect_to(keys_path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
FactoryGirl.define do | ||
|
||
factory :key do | ||
key 'MyText' | ||
association :user | ||
sequence :key do |n| | ||
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx3ke+rnMT/ILY81K1un1CWf9ghcPgl' + | ||
'IlV7pMV2H5AwyC/Dx5x+DyKmNmhBmvCYJ+1we8f0pPXLx2QpyAXw8s0s+sBL/gkizsqqw' + | ||
'rUzK9Rlkj58kvNFl8gLQk3qqs8dR6bODP9LQqCGhMFErQtDQTvBq91jhWuIIunumK7T+0' + | ||
'GWDMf7O9CNdr/aprYrUfuGLggOdz0oPja792V+ay1xWAHEOueKfGvOGFDbQlcTT2uI9wY' + | ||
'z9RGkLhDNOo4S74W59xMwMpf77XsoTYxcdrAT7WpTlzaj2usbbGBgcBKx5kb0dPBOQ3rQ' + | ||
"adtZnLjN2dZAeapUO2MElyX0lxt1nrbIKC#{n} [email protected]" | ||
end | ||
title 'MyString' | ||
fingerprint 'MyString' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,97 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Key, type: :model do | ||
pending 'add some examples to (or delete) #{__FILE__}' | ||
describe Key do | ||
describe 'Associations' do | ||
it { is_expected.to belong_to(:user) } | ||
end | ||
|
||
describe 'Validation' do | ||
it { is_expected.to validate_presence_of(:title) } | ||
it { is_expected.to validate_presence_of(:key) } | ||
it { is_expected.to validate_length_of(:title) } | ||
it { is_expected.to validate_length_of(:key) } | ||
end | ||
|
||
describe 'Methods' do | ||
before do | ||
allow_any_instance_of(Key).to receive(:add_to_shell).and_return(true) | ||
end | ||
|
||
context 'shell_id' do | ||
let(:key) { create(:key) } | ||
|
||
it 'formats id properly' do | ||
expect(key.shell_id).to eq("key-#{key.id}") | ||
end | ||
end | ||
end | ||
|
||
describe 'validation of' do | ||
before do | ||
allow_any_instance_of(Key).to receive(:add_to_shell).and_return(true) | ||
end | ||
|
||
context 'uniqueness' do | ||
let(:user) { create(:user) } | ||
let(:dummy_key) { create(:key, user: user) } | ||
|
||
it 'accepts the key once' do | ||
expect(build(:key, user: user)).to be_valid | ||
end | ||
|
||
it 'does not accept the exact same key twice' do | ||
expect(build(:key, key: dummy_key.key, user: user)).not_to be_valid | ||
end | ||
|
||
it 'does not accept a duplicate key with a different comment' do | ||
duplicate = build(:key, key: dummy_key.key, user: user) | ||
duplicate.key << ' extra comment' | ||
expect(duplicate).not_to be_valid | ||
end | ||
end | ||
|
||
context 'fingerprintable key' do | ||
it 'accepts the fingerprintable key' do | ||
expect(build(:key)).to be_valid | ||
end | ||
|
||
it 'rejects an unfingerprintable key that contains a space' do | ||
key = build(:key) | ||
|
||
# Not always the middle, but close enough | ||
key.key = key.key[0..100] + ' ' + key.key[101..-1] | ||
|
||
expect(key).not_to be_valid | ||
end | ||
|
||
it 'rejects the unfingerprintable key (not a key)' do | ||
expect(build(:key, key: 'ssh-rsa an-invalid-key==')).not_to be_valid | ||
end | ||
|
||
it 'rejects the multiple line key' do | ||
key = build(:key) | ||
key.key.gsub!(' ', '\n') | ||
expect(key).not_to be_valid | ||
end | ||
end | ||
end | ||
|
||
describe 'callbacks' do | ||
before do | ||
allow(Gg::Shell).to receive(:remove_key).and_return(true) | ||
allow(Gg::Shell).to receive(:add_key).and_return(true) | ||
end | ||
|
||
it 'should add new key to authorized_file' do | ||
key = build(:key, id: 7) | ||
expect(Gg::Shell).to receive(:add_key).with(key.shell_id, key.key) | ||
key.save | ||
end | ||
|
||
it 'should remove key from authorized_file' do | ||
key = create(:key) | ||
expect(Gg::Shell).to receive(:remove_key).with(key.shell_id, key.key) | ||
key.destroy | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters