Refactor specs to use one-liner expectation

This commit is contained in:
Patrick Bajao 2019-08-29 12:21:59 +08:00
parent 0e33f16b5f
commit b047359de5
2 changed files with 54 additions and 54 deletions

View File

@ -5,9 +5,11 @@ require 'spec_helper'
describe Gitlab::AuthorizedKeys do
let(:logger) { double('logger').as_null_object }
subject { described_class.new(logger) }
subject(:authorized_keys) { described_class.new(logger) }
describe '#accessible?' do
subject { authorized_keys.accessible? }
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture
@ -18,9 +20,7 @@ describe Gitlab::AuthorizedKeys do
end
context 'can open file' do
it 'returns true' do
expect(subject.accessible?).to eq(true)
end
it { is_expected.to be_truthy }
end
context 'cannot open file' do
@ -28,21 +28,23 @@ describe Gitlab::AuthorizedKeys do
allow(File).to receive(:open).and_raise(Errno::EACCES)
end
it 'returns false' do
expect(subject.accessible?).to eq(false)
end
it { is_expected.to be_falsey }
end
end
context 'authorized_keys file does not exist' do
it 'returns false' do
expect(subject.accessible?).to eq(false)
end
it { is_expected.to be_falsey }
end
end
describe '#add_key' do
let(:id) { 'key-741' }
subject { authorized_keys.add_key(id, key) }
context 'authorized_keys file exists' do
let(:key) { 'ssh-rsa AAAAB3NzaDAxx2E trailing garbage' }
before do
create_authorized_keys_fixture
end
@ -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"
expect(logger).to receive(:info).with('Adding key (key-741): ssh-rsa AAAAB3NzaDAxx2E')
expect(subject.add_key('key-741', 'ssh-rsa AAAAB3NzaDAxx2E trailing garbage'))
.to be_truthy
expect(subject).to be_truthy
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{auth_line}\n")
end
end
context 'authorized_keys file does not exist' do
let(:key) { 'ssh-rsa AAAAB3NzaDAxx2E' }
before do
delete_authorized_keys_file
end
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
end
end
@ -81,6 +84,8 @@ describe Gitlab::AuthorizedKeys do
]
end
subject { authorized_keys.batch_add_keys(keys) }
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture
@ -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-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")
end
@ -104,7 +109,7 @@ describe Gitlab::AuthorizedKeys do
let(:keys) { [double(shell_id: 'key-123', key: "ssh-rsa A\tSDFA\nSGADG")] }
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")
end
end
@ -116,16 +121,28 @@ describe Gitlab::AuthorizedKeys do
end
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
end
end
end
describe '#rm_key' do
let(:key) { 'key-741' }
subject { authorized_keys.rm_key(key) }
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
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
after do
@ -133,16 +150,10 @@ describe Gitlab::AuthorizedKeys do
end
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(/./, '#')
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(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")
end
end
@ -152,13 +163,13 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file
end
it 'returns false' do
expect(subject.rm_key('key-741')).to be_falsey
end
it { is_expected.to be_falsey }
end
end
describe '#clear' do
subject { authorized_keys.clear }
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture
@ -168,9 +179,7 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file
end
it "returns true" do
expect(subject.clear).to be_truthy
end
it { is_expected.to be_truthy }
end
context 'authorized_keys file does not exist' do
@ -178,13 +187,13 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file
end
it "still returns true" do
expect(subject.clear).to be_truthy
end
it { is_expected.to be_truthy }
end
end
describe '#list_key_ids' do
subject { authorized_keys.list_key_ids }
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture(
@ -197,9 +206,7 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file
end
it 'returns array of key IDs' do
expect(subject.list_key_ids).to eq([1, 2, 3, 9000])
end
it { is_expected.to eq([1, 2, 3, 9000]) }
end
context 'authorized_keys file does not exist' do
@ -207,9 +214,7 @@ describe Gitlab::AuthorizedKeys do
delete_authorized_keys_file
end
it 'returns an empty array' do
expect(subject.list_key_ids).to be_empty
end
it { is_expected.to be_empty }
end
end

View File

@ -3,13 +3,13 @@
require 'spec_helper'
describe SystemCheck::App::AuthorizedKeysPermissionCheck do
subject { described_class.new }
subject(:system_check) { described_class.new }
describe '#skip?' do
subject { system_check.skip? }
context 'authorized keys enabled' do
it 'returns false' do
expect(subject.skip?).to eq(false)
end
it { is_expected.to eq(false) }
end
context 'authorized keys not enabled' do
@ -17,34 +17,29 @@ describe SystemCheck::App::AuthorizedKeysPermissionCheck do
stub_application_setting(authorized_keys_enabled: false)
end
it 'returns true' do
expect(subject.skip?).to eq(true)
end
it { is_expected.to eq(true) }
end
end
describe '#check?' do
let(:authorized_keys) { double }
subject { system_check.check? }
before do
allow(Gitlab::AuthorizedKeys).to receive(:new).and_return(authorized_keys)
allow(authorized_keys).to receive(:accessible?).and_return(accessible?)
expect_next_instance_of(Gitlab::AuthorizedKeys) do |instance|
allow(instance).to receive(:accessible?) { accessible? }
end
end
context 'authorized keys is accessible' do
let(:accessible?) { true }
it 'returns true' do
expect(subject.check?).to eq(true)
end
it { is_expected.to eq(true) }
end
context 'authorized keys is not accessible' do
let(:accessible?) { false }
it 'returns false' do
expect(subject.check?).to eq(false)
end
it { is_expected.to eq(false) }
end
end
end