Skip to content

Commit 53badd6

Browse files
committed
Enable branch coverage and add more tests to get 100%
1 parent 86c69d1 commit 53badd6

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

.simplecov

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
if ENV['COVERAGE']
22
SimpleCov.start do
3+
enable_coverage :branch # available in ruby >= 2.5, already required by actionpack 6
34
add_filter '/spec/'
45
end
56
end

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Redis Session Store authors
1616
- Olek Poplavsky
1717
- Tim Lossen
1818
- Todd Bealmear
19+
- Vasily Fedoseyev
1920
- Aleksey Dashkevych
2021
- Olle Jonsson
2122
- Nicolas Rodriguez

lib/redis-session-store.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# the MemCacheStore code, simply dropping in Redis instead.
55
class RedisSessionStore < ActionDispatch::Session::AbstractSecureStore
66
VERSION = '0.11.5'.freeze
7+
# :nocov:
78
# Rails 3.1 and beyond defines the constant elsewhere
89
unless defined?(ENV_SESSION_OPTIONS_KEY)
910
ENV_SESSION_OPTIONS_KEY = if Rack.release.split('.').first.to_i > 1
@@ -12,6 +13,7 @@ class RedisSessionStore < ActionDispatch::Session::AbstractSecureStore
1213
Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY
1314
end
1415
end
16+
# :nocov:
1517

1618
USE_INDIFFERENT_ACCESS = defined?(ActiveSupport).freeze
1719
# ==== Options

spec/redis_session_store_spec.rb

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,21 +252,54 @@
252252

253253
context 'when session id does not exist in redis' do
254254
it 'returns false' do
255-
expect(redis).to receive(:exists).with('foo').and_return(false)
255+
expect(redis).to receive(:exists?).with('foo').and_return(false)
256256
expect(store.send(:session_exists?, :env)).to eq(false)
257257
end
258258
end
259259

260260
context 'when session id exists in redis' do
261261
it 'returns true' do
262-
expect(redis).to receive(:exists).with('foo').and_return(true)
262+
expect(redis).to receive(:exists?).with('foo').and_return(true)
263263
expect(store.send(:session_exists?, :env)).to eq(true)
264264
end
265265
end
266266

267267
context 'when redis is down' do
268-
it 'returns true (fallback to old behavior)' do
268+
before do
269269
allow(store).to receive(:redis).and_raise(Redis::CannotConnectError)
270+
end
271+
272+
it 'returns true (fallback to old behavior)' do
273+
expect(store.send(:session_exists?, :env)).to eq(true)
274+
end
275+
276+
context 'when :on_redis_down re-raises' do
277+
before { store.on_redis_down = ->(e, *) { raise e } }
278+
279+
it 'explodes' do
280+
expect do
281+
store.send(:session_exists?, :env)
282+
end.to raise_error(Redis::CannotConnectError)
283+
end
284+
end
285+
end
286+
287+
context 'when redis does suport exists?' do
288+
it 'calls it' do
289+
expect(redis).to receive(:exists?).and_return(false)
290+
expect(redis).not_to receive(:exists)
291+
store.send(:session_exists?, :env)
292+
end
293+
end
294+
295+
context 'when redis does not support #exists?' do
296+
before do
297+
allow(redis).to receive(:respond_to?).with(:exists?).and_return(false)
298+
allow(redis).to receive(:exists?).and_raise('shouhld not be called')
299+
end
300+
301+
it 'uses old method exist' do
302+
expect(redis).to receive(:exists).with('foo').and_return(true)
270303
expect(store.send(:session_exists?, :env)).to eq(true)
271304
end
272305
end
@@ -345,6 +378,15 @@
345378
store.send(:destroy, env)
346379
end
347380

381+
context 'when no cookie hash in request' do
382+
let(:env) { {} }
383+
384+
it 'does nothing' do
385+
allow(store).to receive(:redis).and_return(double('redis'))
386+
store.send(:destroy, env)
387+
end
388+
end
389+
348390
context 'when redis is down' do
349391
before do
350392
allow(store).to receive(:redis).and_raise(Redis::CannotConnectError)
@@ -546,9 +588,10 @@ def self.dump(_value)
546588
end
547589

548590
describe 'setting the session' do
591+
let(:env) { { 'rack.session.options' => {} } }
592+
let(:sid) { 1234 }
593+
549594
it 'allows changing the session' do
550-
env = { 'rack.session.options' => {} }
551-
sid = 1234
552595
allow(store).to receive(:redis).and_return(Redis.new)
553596
data1 = { 'foo' => 'bar' }
554597
store.send(:set_session, env, sid, data1)
@@ -560,7 +603,6 @@ def self.dump(_value)
560603

561604
it 'allows changing the session when the session has an expiry' do
562605
env = { 'rack.session.options' => { expire_after: 60 } }
563-
sid = 1234
564606
allow(store).to receive(:redis).and_return(Redis.new)
565607
data1 = { 'foo' => 'bar' }
566608
store.send(:set_session, env, sid, data1)
@@ -569,5 +611,33 @@ def self.dump(_value)
569611
_, session = store.send(:get_session, env, sid)
570612
expect(session).to eq(data2)
571613
end
614+
615+
context 'when redis is down' do
616+
let(:redis_exception_type) { Redis::CannotConnectError }
617+
618+
before { allow(store).to receive(:redis).and_raise(redis_exception_type) }
619+
620+
it 'returns false' do
621+
expect(store.send(:set_session, env, sid, { 'foo' => 'bar' })).to eq false
622+
end
623+
624+
it 'calls on_redis_down' do
625+
store.on_redis_down = double('on_redis_down')
626+
expect(store.on_redis_down).to receive(:call).with(
627+
be_kind_of(redis_exception_type), env, sid
628+
)
629+
store.send(:set_session, env, sid, { 'foo' => 'bar' })
630+
end
631+
632+
context 'when :on_redis_down re-raises' do
633+
before { store.on_redis_down = ->(e, *) { raise e } }
634+
635+
it 'explodes' do
636+
expect do
637+
store.send(:set_session, env, sid, { 'foo' => 'bar' })
638+
end.to raise_error(Redis::CannotConnectError)
639+
end
640+
end
641+
end
572642
end
573643
end

0 commit comments

Comments
 (0)