forked from cloudfoundry-attic/bosh-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_credentials_provider_spec.rb
82 lines (72 loc) · 2.68 KB
/
git_credentials_provider_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module Bosh::Workspace
describe GitCredentialsProvider do
let!(:credentials_provider) { GitCredentialsProvider.new(file) }
let(:file) { "credentials_file" }
let(:file_exist) { true }
let(:valid) { true }
let(:url_protocols) { [] }
let(:url) { 'http://foo.com/bar.git' }
let(:user) { nil }
let(:result) { nil }
let(:allowed_types) { [:plaintext] }
let(:credentials) do
instance_double "Bosh::Workspace::Credentials",
:valid? => valid, url_protocols: url_protocols
end
subject { credentials_provider.callback.call url, user, allowed_types }
before do
allow(Credentials).to receive(:new).and_return(credentials)
allow(File).to receive(:exist?).and_return(file_exist)
allow(credentials).to receive(:find_by_url).with(url).and_return(result)
end
describe '#callback' do
context "with sshkey" do
let(:user) { 'git' }
let(:result) { { private_key: 'barkey' } }
let(:allowed_types) { [:ssh_key] }
it 'returns Rugged sshkey credentials' do
expect(Rugged::Credentials::SshKey).to receive(:new) do |args|
expect(args[:username]).to eq user
expect(IO.read(args[:privatekey])).to eq('barkey')
end; subject
end
end
context "with username/password" do
let(:result) { { username: user, password: 'barpw' } }
let(:allowed_types) { [:plaintext] }
it 'returns Rugged user password credentials' do
expect(Rugged::Credentials::UserPassword).to receive(:new) do |args|
expect(args[:username]).to eq user
expect(args[:password]).to eq 'barpw'
end; subject
end
end
context "without credentials file" do
let(:file_exist) { false }
it 'raises an error' do
expect{ subject }.to raise_error /credentials file does not exist/i
end
end
context "with invalid credentials file" do
let(:valid) { false }
before { expect(credentials).to receive(:errors) { ['foo error'] } }
it 'raises an error' do
expect{ subject }.to raise_error /is not valid/i
end
end
context "without credentials for given url" do
let(:result) { nil }
it 'raises an error' do
expect{ subject }.to raise_error /no credentials found/i
end
end
context "without protocol support" do
let(:url_protocols) { {"https://foo.com" => :https } }
before { expect(Rugged).to receive(:features).and_return([]) }
it 'raises an error' do
expect{ subject }.to raise_error /requires https support/i
end
end
end
end
end