-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_public_key_spec.rb
173 lines (146 loc) · 4.41 KB
/
rsa_public_key_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Keyless::RsaPublicKey do
# Use this method to swallow any raised exceptions. Just wrap any code you
# want with this method. This is useful in situations were you test some
# behaviour in the middle of a raising call.
#
# @yield
def ignore_exceptions
yield
rescue StandardError
nil
end
let(:instance) { described_class.instance }
before do
instance.url = nil
instance.caching = nil
instance.expiration = nil
end
describe 'singleton' do
it 'is a singleton implementation' do
expect(described_class.instance).to be(instance)
end
end
describe '#configure' do
it 'yields the singleton instance' do
expect do |block|
instance.configure(&block)
end.to yield_with_args(instance)
end
end
describe '#url' do
it 'delivers the default value' do
expect(instance.url).to be_nil
end
it 'can be reconfigured' do
instance.url = '/tmp/file'
expect(instance.url).to be_eql('/tmp/file')
end
end
describe '#caching' do
it 'delivers the default value' do
expect(instance.caching).to be(false)
end
it 'can be reconfigured' do
instance.caching = true
expect(instance.caching).to be(true)
end
end
describe '#expiration' do
it 'delivers the default value' do
expect(instance.expiration).to be_eql(1.hour)
end
it 'can be reconfigured' do
instance.expiration = 2.minutes
expect(instance.expiration).to be_eql(2.minutes)
end
end
describe '#cache?' do
it 'detects the configuration (true)' do
instance.caching = true
expect(instance.cache?).to be(true)
end
it 'detects the configuration (false)' do
instance.caching = false
expect(instance.cache?).to be(false)
end
end
describe '#remote?' do
it 'detects remote URLs (http)' do
instance.url = 'http://public.key'
expect(instance.remote?).to be(true)
end
it 'detects remote URLs (https)' do
instance.url = 'https://public.key'
expect(instance.remote?).to be(true)
end
it 'detects local URLs' do
instance.url = '/tmp/public.key'
expect(instance.remote?).to be(false)
end
end
describe '#fetch_encoded_key' do
context 'without URL' do
it 'raises an error' do
instance.url = nil
expect { instance.fetch_encoded_key }.to \
raise_error(ArgumentError, /No URL.*configured/)
end
end
context 'with remote URL' do
it 'performs a HTTP GET request to get the public key', :vcr do
instance.url = 'http://localhost:1337/rsa1.pub'
expect(instance.fetch_encoded_key).to \
be_eql(file_fixture('rsa1.pub').read)
end
it 'raises a FetchError when not successful', :vcr do
instance.url = 'https://httpstat.us/502'
expect { instance.fetch_encoded_key }.to \
raise_error(Keyless::RsaPublicKey::FetchError,
/502 Bad Gateway/)
end
end
context 'with local URL' do
it 'performs a local file read to get the public key' do
fixture = file_fixture('rsa1.pub')
instance.url = fixture.path
expect(instance.fetch_encoded_key).to be_eql(fixture.read)
end
end
end
describe '#fetch' do
let(:configure_pub_old) { instance.url = file_fixture('rsa1.pub').path }
let(:configure_pub_new) { instance.url = file_fixture('rsa2.pub').path }
context 'with cache' do
before do
instance.caching = true
instance.cache.clear
end
it 'cache the key' do
configure_pub_old
expect { configure_pub_new }.not_to change { instance.fetch.to_s }
end
it 'caches nothing on issues', :vcr do
instance.url = 'https://httpstat.us/502'
expect { ignore_exceptions { instance.fetch } }.not_to \
change { instance.cache.fetch('encoded_key') }.from(nil)
end
end
context 'without cache' do
it 'does not cache the key' do
instance.caching = false
configure_pub_old
expect { configure_pub_new }.to change { instance.fetch.to_s }
end
end
end
describe '.fetch' do
it 'just shortcuts the instance method' do
Keyless.configure do |conf|
conf.rsa_public_key_url = file_fixture('rsa1.pub').path
end
expect(described_class.fetch).to be_a(OpenSSL::PKey::RSA)
end
end
end