Commit b047359d authored by Patrick Bajao's avatar Patrick Bajao

Refactor specs to use one-liner expectation

parent 0e33f16b
...@@ -5,9 +5,11 @@ require 'spec_helper' ...@@ -5,9 +5,11 @@ require 'spec_helper'
describe Gitlab::AuthorizedKeys do describe Gitlab::AuthorizedKeys do
let(:logger) { double('logger').as_null_object } let(:logger) { double('logger').as_null_object }
subject { described_class.new(logger) } subject(:authorized_keys) { described_class.new(logger) }
describe '#accessible?' do describe '#accessible?' do
subject { authorized_keys.accessible? }
context 'authorized_keys file exists' do context 'authorized_keys file exists' do
before do before do
create_authorized_keys_fixture create_authorized_keys_fixture
...@@ -18,9 +20,7 @@ describe Gitlab::AuthorizedKeys do ...@@ -18,9 +20,7 @@ describe Gitlab::AuthorizedKeys do
end end
context 'can open file' do context 'can open file' do
it 'returns true' do it { is_expected.to be_truthy }
expect(subject.accessible?).to eq(true)
end
end end
context 'cannot open file' do context 'cannot open file' do
...@@ -28,21 +28,23 @@ describe Gitlab::AuthorizedKeys do ...@@ -28,21 +28,23 @@ describe Gitlab::AuthorizedKeys do
allow(File).to receive(:open).and_raise(Errno::EACCES) allow(File).to receive(:open).and_raise(Errno::EACCES)
end end
it 'returns false' do it { is_expected.to be_falsey }
expect(subject.accessible?).to eq(false)
end
end end
end end
context 'authorized_keys file does not exist' do context 'authorized_keys file does not exist' do
it 'returns false' do it { is_expected.to be_falsey }
expect(subject.accessible?).to eq(false)
end
end end
end end
describe '#add_key' do describe '#add_key' do
let(:id) { 'key-741' }
subject { authorized_keys.add_key(id, key) }
context 'authorized_keys file exists' do context 'authorized_keys file exists' do
let(:key) { 'ssh-rsa AAAAB3NzaDAxx2E trailing garbage' }
before do before do
create_authorized_keys_fixture create_authorized_keys_fixture
end end
...@@ -55,19 +57,20 @@ describe Gitlab::AuthorizedKeys do ...@@ -55,19 +57,20 @@ describe Gitlab::AuthorizedKeys do
auth_line = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E" auth_line = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E"
expect(logger).to receive(:info).with('Adding key (key-741): ssh-rsa AAAAB3NzaDAxx2E') expect(logger).to receive(:info).with('Adding key (key-741): ssh-rsa AAAAB3NzaDAxx2E')
expect(subject.add_key('key-741', 'ssh-rsa AAAAB3NzaDAxx2E trailing garbage')) expect(subject).to be_truthy
.to be_truthy
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{auth_line}\n") expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{auth_line}\n")
end end
end end
context 'authorized_keys file does not exist' do context 'authorized_keys file does not exist' do
let(:key) { 'ssh-rsa AAAAB3NzaDAxx2E' }
before do before do
delete_authorized_keys_file delete_authorized_keys_file
end end
it 'creates the file' do it 'creates the file' do
expect(subject.add_key('key-741', 'ssh-rsa AAAAB3NzaDAxx2E')).to be_truthy expect(subject).to be_truthy
expect(File.exist?(tmp_authorized_keys_path)).to be_truthy expect(File.exist?(tmp_authorized_keys_path)).to be_truthy
end end
end end
...@@ -81,6 +84,8 @@ describe Gitlab::AuthorizedKeys do ...@@ -81,6 +84,8 @@ describe Gitlab::AuthorizedKeys do
] ]
end end
subject { authorized_keys.batch_add_keys(keys) }
context 'authorized_keys file exists' do context 'authorized_keys file exists' do
before do before do
create_authorized_keys_fixture create_authorized_keys_fixture
...@@ -96,7 +101,7 @@ describe Gitlab::AuthorizedKeys do ...@@ -96,7 +101,7 @@ describe Gitlab::AuthorizedKeys do
expect(logger).to receive(:info).with('Adding key (key-12): ssh-dsa ASDFASGADG') expect(logger).to receive(:info).with('Adding key (key-12): ssh-dsa ASDFASGADG')
expect(logger).to receive(:info).with('Adding key (key-123): ssh-rsa GFDGDFSGSDFG') expect(logger).to receive(:info).with('Adding key (key-123): ssh-rsa GFDGDFSGSDFG')
expect(subject.batch_add_keys(keys)).to be_truthy expect(subject).to be_truthy
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{auth_line1}\n#{auth_line2}\n") expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{auth_line1}\n#{auth_line2}\n")
end end
...@@ -104,7 +109,7 @@ describe Gitlab::AuthorizedKeys do ...@@ -104,7 +109,7 @@ describe Gitlab::AuthorizedKeys do
let(:keys) { [double(shell_id: 'key-123', key: "ssh-rsa A\tSDFA\nSGADG")] } let(:keys) { [double(shell_id: 'key-123', key: "ssh-rsa A\tSDFA\nSGADG")] }
it "doesn't add keys" do it "doesn't add keys" do
expect(subject.batch_add_keys(keys)).to be_falsey expect(subject).to be_falsey
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n") expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n")
end end
end end
...@@ -116,16 +121,28 @@ describe Gitlab::AuthorizedKeys do ...@@ -116,16 +121,28 @@ describe Gitlab::AuthorizedKeys do
end end
it 'creates the file' do it 'creates the file' do
expect(subject.batch_add_keys(keys)).to be_truthy expect(subject).to be_truthy
expect(File.exist?(tmp_authorized_keys_path)).to be_truthy expect(File.exist?(tmp_authorized_keys_path)).to be_truthy
end end
end end
end end
describe '#rm_key' do describe '#rm_key' do
let(:key) { 'key-741' }
subject { authorized_keys.rm_key(key) }
context 'authorized_keys file exists' do context 'authorized_keys file exists' do
let(:other_line) { "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E" }
let(:delete_line) { "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E" }
before do before do
create_authorized_keys_fixture create_authorized_keys_fixture
File.open(tmp_authorized_keys_path, 'a') do |auth_file|
auth_file.puts delete_line
auth_file.puts other_line
end
end end
after do after do
...@@ -133,16 +150,10 @@ describe Gitlab::AuthorizedKeys do ...@@ -133,16 +150,10 @@ describe Gitlab::AuthorizedKeys do
end end
it "removes the right line" do it "removes the right line" do
other_line = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E"
delete_line = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E"
erased_line = delete_line.gsub(/./, '#') erased_line = delete_line.gsub(/./, '#')
File.open(tmp_authorized_keys_path, 'a') do |auth_file|
auth_file.puts delete_line
auth_file.puts other_line
end
expect(logger).to receive(:info).with('Removing key (key-741)') expect(logger).to receive(:info).with('Removing key (key-741)')
expect(subject.rm_key('key-741')).to be_truthy expect(subject).to be_truthy
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{erased_line}\n#{other_line}\n") expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{erased_line}\n#{other_line}\n")
end end
end end
...@@ -152,13 +163,13 @@ describe Gitlab::AuthorizedKeys do ...@@ -152,13 +163,13 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file delete_authorized_keys_file
end end
it 'returns false' do it { is_expected.to be_falsey }
expect(subject.rm_key('key-741')).to be_falsey
end
end end
end end
describe '#clear' do describe '#clear' do
subject { authorized_keys.clear }
context 'authorized_keys file exists' do context 'authorized_keys file exists' do
before do before do
create_authorized_keys_fixture create_authorized_keys_fixture
...@@ -168,9 +179,7 @@ describe Gitlab::AuthorizedKeys do ...@@ -168,9 +179,7 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file delete_authorized_keys_file
end end
it "returns true" do it { is_expected.to be_truthy }
expect(subject.clear).to be_truthy
end
end end
context 'authorized_keys file does not exist' do context 'authorized_keys file does not exist' do
...@@ -178,13 +187,13 @@ describe Gitlab::AuthorizedKeys do ...@@ -178,13 +187,13 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file delete_authorized_keys_file
end end
it "still returns true" do it { is_expected.to be_truthy }
expect(subject.clear).to be_truthy
end
end end
end end
describe '#list_key_ids' do describe '#list_key_ids' do
subject { authorized_keys.list_key_ids }
context 'authorized_keys file exists' do context 'authorized_keys file exists' do
before do before do
create_authorized_keys_fixture( create_authorized_keys_fixture(
...@@ -197,9 +206,7 @@ describe Gitlab::AuthorizedKeys do ...@@ -197,9 +206,7 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file delete_authorized_keys_file
end end
it 'returns array of key IDs' do it { is_expected.to eq([1, 2, 3, 9000]) }
expect(subject.list_key_ids).to eq([1, 2, 3, 9000])
end
end end
context 'authorized_keys file does not exist' do context 'authorized_keys file does not exist' do
...@@ -207,9 +214,7 @@ describe Gitlab::AuthorizedKeys do ...@@ -207,9 +214,7 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file delete_authorized_keys_file
end end
it 'returns an empty array' do it { is_expected.to be_empty }
expect(subject.list_key_ids).to be_empty
end
end end
end end
......
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
require 'spec_helper' require 'spec_helper'
describe SystemCheck::App::AuthorizedKeysPermissionCheck do describe SystemCheck::App::AuthorizedKeysPermissionCheck do
subject { described_class.new } subject(:system_check) { described_class.new }
describe '#skip?' do describe '#skip?' do
subject { system_check.skip? }
context 'authorized keys enabled' do context 'authorized keys enabled' do
it 'returns false' do it { is_expected.to eq(false) }
expect(subject.skip?).to eq(false)
end
end end
context 'authorized keys not enabled' do context 'authorized keys not enabled' do
...@@ -17,34 +17,29 @@ describe SystemCheck::App::AuthorizedKeysPermissionCheck do ...@@ -17,34 +17,29 @@ describe SystemCheck::App::AuthorizedKeysPermissionCheck do
stub_application_setting(authorized_keys_enabled: false) stub_application_setting(authorized_keys_enabled: false)
end end
it 'returns true' do it { is_expected.to eq(true) }
expect(subject.skip?).to eq(true)
end
end end
end end
describe '#check?' do describe '#check?' do
let(:authorized_keys) { double } subject { system_check.check? }
before do before do
allow(Gitlab::AuthorizedKeys).to receive(:new).and_return(authorized_keys) expect_next_instance_of(Gitlab::AuthorizedKeys) do |instance|
allow(authorized_keys).to receive(:accessible?).and_return(accessible?) allow(instance).to receive(:accessible?) { accessible? }
end
end end
context 'authorized keys is accessible' do context 'authorized keys is accessible' do
let(:accessible?) { true } let(:accessible?) { true }
it 'returns true' do it { is_expected.to eq(true) }
expect(subject.check?).to eq(true)
end
end end
context 'authorized keys is not accessible' do context 'authorized keys is not accessible' do
let(:accessible?) { false } let(:accessible?) { false }
it 'returns false' do it { is_expected.to eq(false) }
expect(subject.check?).to eq(false)
end
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