Skip to content

Commit

Permalink
Test for keys model and controller
Browse files Browse the repository at this point in the history
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
sonalkr132 committed Aug 2, 2015
1 parent 7e2f0bf commit 644abef
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ group :test do
gem 'launchy'
gem 'rubocop', require: false
gem 'haml-lint', require: false
gem 'shoulda-matchers'
end

# To use ActiveModel has_secure_password
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ GEM
sass-rails (~> 5.0.0.beta1)
sprockets (>= 2.3.0)
shellany (0.0.1)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
sketchily (4.0.1)
nokogiri
rails (>= 3.1)
Expand Down Expand Up @@ -417,6 +419,7 @@ DEPENDENCIES
rugged (~> 0.21)
sass-rails
sass-rails-source-maps
shoulda-matchers
sketchily
sqlite3
test-unit
Expand Down
5 changes: 0 additions & 5 deletions app/models/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class Key < ActiveRecord::Base
after_create :add_to_shell
after_destroy :remove_from_shell

# projects that has this key
def projects
user.authorized_projects
end

def shell_id
"key-#{id}"
end
Expand Down
1 change: 0 additions & 1 deletion lib/gg/key_fingerprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def fingerprint

cmd_output, cmd_status = popen(cmd, '/tmp')
end
puts cmd_status, cmd_output
return nil unless cmd_status.zero?

# 16 hex bytes separated by ':', optionally starting with "MD5:"
Expand Down
42 changes: 41 additions & 1 deletion spec/controllers/keys_controller_spec.rb
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
12 changes: 10 additions & 2 deletions spec/factories/keys.rb
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
96 changes: 94 additions & 2 deletions spec/models/key_spec.rb
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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'shoulda/matchers'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Expand Down

0 comments on commit 644abef

Please sign in to comment.