frikandel 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
- require "spec_helper"
1
+ require "rails_helper"
2
2
  require "support/application_controller"
3
3
 
4
+
4
5
  class SessionInvalidError < StandardError; end
5
6
 
6
7
  class CustomizedOnInvalidSessionController < ApplicationController
@@ -12,8 +13,8 @@ class CustomizedOnInvalidSessionController < ApplicationController
12
13
  alias my_on_invalid_session on_invalid_session
13
14
  end
14
15
 
15
- describe CustomizedOnInvalidSessionController do
16
16
 
17
+ RSpec.describe CustomizedOnInvalidSessionController do
17
18
  it "uses the overwritten on_invalid_cookie function" do
18
19
  get :home
19
20
  request.session[:max_ttl] = 1.minute.ago
@@ -1,14 +1,22 @@
1
- require "spec_helper"
1
+ require "rails_helper"
2
2
  require "support/application_controller"
3
3
 
4
4
 
5
5
  class LimitSessionLifetimeController < ApplicationController
6
6
  include Frikandel::LimitSessionLifetime
7
7
 
8
- before_filter :flash_alert_and_redirect_home, only: [:redirect_home]
8
+ if respond_to?(:before_action)
9
+ before_action :flash_alert_and_redirect_home, only: [:redirect_home]
10
+ else
11
+ before_filter :flash_alert_and_redirect_home, only: [:redirect_home]
12
+ end
9
13
 
10
14
  def home
11
- render text: "ttl test"
15
+ if Rails::VERSION::MAJOR >= 5
16
+ render plain: "ttl test"
17
+ else
18
+ render text: "ttl test"
19
+ end
12
20
  end
13
21
 
14
22
  def redirect_home
@@ -24,7 +32,7 @@ protected
24
32
  end
25
33
 
26
34
 
27
- describe LimitSessionLifetimeController do
35
+ RSpec.describe LimitSessionLifetimeController do
28
36
  context "requests" do
29
37
  it "writes ttl and max_ttl to session" do
30
38
  expect(session[:ttl]).to be_nil
@@ -45,8 +53,8 @@ describe LimitSessionLifetimeController do
45
53
  expect(session[:ttl]).to be_a(Time)
46
54
  expect(session[:max_ttl]).to be_a(Time)
47
55
 
48
- flash.should_not be_empty
49
- flash[:alert].should eql("alert test")
56
+ expect(flash).not_to be_empty
57
+ expect(flash[:alert]).to eql("alert test")
50
58
  end
51
59
 
52
60
  it "holds the session for at least .1 seconds" do
@@ -57,8 +65,8 @@ describe LimitSessionLifetimeController do
57
65
 
58
66
  get :home
59
67
 
60
- session[:user_id].should be_present
61
- session[:user_id].should eq 1337
68
+ expect(session[:user_id]).to be_present
69
+ expect(session[:user_id]).to eq 1337
62
70
  end
63
71
 
64
72
  it "destroys the session after SESSION_TTL" do
@@ -69,7 +77,7 @@ describe LimitSessionLifetimeController do
69
77
 
70
78
  get :home
71
79
 
72
- session[:user_id].should be_blank
80
+ expect(session[:user_id]).to be_blank
73
81
  end
74
82
 
75
83
  it "destroys the session after SESSION_MAX_TTL" do
@@ -80,7 +88,7 @@ describe LimitSessionLifetimeController do
80
88
 
81
89
  get :home
82
90
 
83
- session[:user_id].should be_blank
91
+ expect(session[:user_id]).to be_blank
84
92
  end
85
93
 
86
94
  it "is configurable" do
@@ -92,7 +100,7 @@ describe LimitSessionLifetimeController do
92
100
 
93
101
  get :home
94
102
 
95
- session[:user_id].should be_blank
103
+ expect(session[:user_id]).to be_blank
96
104
  end
97
105
 
98
106
 
@@ -103,17 +111,17 @@ describe LimitSessionLifetimeController do
103
111
  session.delete(:ttl)
104
112
  session[:max_ttl] = "SomeMaxTTL"
105
113
 
106
- controller.should_receive(:reset_session).and_call_original
107
- controller.should_receive(:persist_session_timestamp).and_call_original
114
+ expect(controller).to receive(:reset_session).and_call_original
115
+ expect(controller).to receive(:persist_session_timestamp).and_call_original
108
116
  get :home
109
117
 
110
- session[:user_id].should be_blank
111
- session[:ip_address].should be_blank
112
- session[:ttl].should be_present
113
- session[:ttl].should be_a(Time)
114
- session[:max_ttl].should be_present
115
- session[:max_ttl].should_not eql("SomeMaxTTL")
116
- session[:max_ttl].should be_a(Time)
118
+ expect(session[:user_id]).to be_blank
119
+ expect(session[:ip_address]).to be_blank
120
+ expect(session[:ttl]).to be_present
121
+ expect(session[:ttl]).to be_a(Time)
122
+ expect(session[:max_ttl]).to be_present
123
+ expect(session[:max_ttl]).not_to eql("SomeMaxTTL")
124
+ expect(session[:max_ttl]).to be_a(Time)
117
125
  end
118
126
 
119
127
  it "allows the request to be rendered as normal" do
@@ -122,7 +130,7 @@ describe LimitSessionLifetimeController do
122
130
 
123
131
  get :home
124
132
 
125
- response.body.should eql("ttl test")
133
+ expect(response.body).to eql("ttl test")
126
134
  end
127
135
  end
128
136
 
@@ -134,17 +142,17 @@ describe LimitSessionLifetimeController do
134
142
  session[:ttl] = "SomeTTL"
135
143
  session.delete(:max_ttl)
136
144
 
137
- controller.should_receive(:reset_session).and_call_original
138
- controller.should_receive(:persist_session_timestamp).and_call_original
145
+ expect(controller).to receive(:reset_session).and_call_original
146
+ expect(controller).to receive(:persist_session_timestamp).and_call_original
139
147
  get :home
140
148
 
141
- session[:user_id].should be_blank
142
- session[:ip_address].should be_blank
143
- session[:ttl].should be_present
144
- session[:ttl].should_not eql("SomeTTL")
145
- session[:ttl].should be_a(Time)
146
- session[:max_ttl].should be_present
147
- session[:max_ttl].should be_a(Time)
149
+ expect(session[:user_id]).to be_blank
150
+ expect(session[:ip_address]).to be_blank
151
+ expect(session[:ttl]).to be_present
152
+ expect(session[:ttl]).not_to eql("SomeTTL")
153
+ expect(session[:ttl]).to be_a(Time)
154
+ expect(session[:max_ttl]).to be_present
155
+ expect(session[:max_ttl]).to be_a(Time)
148
156
  end
149
157
 
150
158
  it "allows the request to be rendered as normal" do
@@ -153,7 +161,7 @@ describe LimitSessionLifetimeController do
153
161
 
154
162
  get :home
155
163
 
156
- response.body.should eql("ttl test")
164
+ expect(response.body).to eql("ttl test")
157
165
  end
158
166
  end
159
167
 
@@ -165,16 +173,16 @@ describe LimitSessionLifetimeController do
165
173
  session.delete(:ttl)
166
174
  session.delete(:max_ttl)
167
175
 
168
- controller.should_receive(:reset_session).and_call_original
169
- controller.should_receive(:persist_session_timestamp).and_call_original
176
+ expect(controller).to receive(:reset_session).and_call_original
177
+ expect(controller).to receive(:persist_session_timestamp).and_call_original
170
178
  get :home
171
179
 
172
- session[:user_id].should be_blank
173
- session[:ip_address].should be_blank
174
- session[:ttl].should be_present
175
- session[:ttl].should be_a(Time)
176
- session[:max_ttl].should be_present
177
- session[:max_ttl].should be_a(Time)
180
+ expect(session[:user_id]).to be_blank
181
+ expect(session[:ip_address]).to be_blank
182
+ expect(session[:ttl]).to be_present
183
+ expect(session[:ttl]).to be_a(Time)
184
+ expect(session[:max_ttl]).to be_present
185
+ expect(session[:max_ttl]).to be_a(Time)
178
186
  end
179
187
 
180
188
  it "allows the request to be rendered as normal" do
@@ -183,7 +191,7 @@ describe LimitSessionLifetimeController do
183
191
 
184
192
  get :home
185
193
 
186
- response.body.should eql("ttl test")
194
+ expect(response.body).to eql("ttl test")
187
195
  end
188
196
  end
189
197
  end
@@ -194,10 +202,10 @@ describe LimitSessionLifetimeController do
194
202
  session[:ttl] = "SomeTTL"
195
203
  session[:max_ttl] = "SomeMaxTTL"
196
204
 
197
- controller.should_receive(:reached_ttl?).and_return(true)
198
- controller.stub(:reached_max_ttl?).and_return(false)
205
+ expect(controller).to receive(:reached_ttl?).and_return(true)
206
+ allow(controller).to receive(:reached_max_ttl?).and_return(false)
199
207
 
200
- controller.should_receive(:on_invalid_session)
208
+ expect(controller).to receive(:on_invalid_session)
201
209
 
202
210
  controller.send(:validate_session_timestamp)
203
211
  end
@@ -206,10 +214,10 @@ describe LimitSessionLifetimeController do
206
214
  session[:ttl] = "SomeTTL"
207
215
  session[:max_ttl] = "SomeMaxTTL"
208
216
 
209
- controller.stub(:reached_ttl?).and_return(false)
210
- controller.should_receive(:reached_max_ttl?).and_return(true)
217
+ allow(controller).to receive(:reached_ttl?).and_return(false)
218
+ expect(controller).to receive(:reached_max_ttl?).and_return(true)
211
219
 
212
- controller.should_receive(:on_invalid_session)
220
+ expect(controller).to receive(:on_invalid_session)
213
221
 
214
222
  controller.send(:validate_session_timestamp)
215
223
  end
@@ -218,10 +226,10 @@ describe LimitSessionLifetimeController do
218
226
  session[:ttl] = "SomeTTL"
219
227
  session[:max_ttl] = "SomeMaxTTL"
220
228
 
221
- controller.stub(:reached_ttl?).and_return(true)
222
- controller.stub(:reached_max_ttl?).and_return(true)
229
+ allow(controller).to receive(:reached_ttl?).and_return(true)
230
+ allow(controller).to receive(:reached_max_ttl?).and_return(true)
223
231
 
224
- controller.should_receive(:on_invalid_session)
232
+ expect(controller).to receive(:on_invalid_session)
225
233
 
226
234
  controller.send(:validate_session_timestamp)
227
235
  end
@@ -230,7 +238,7 @@ describe LimitSessionLifetimeController do
230
238
  session.delete(:ttl)
231
239
  session[:max_ttl] = "SomeMaxTTL"
232
240
 
233
- controller.should_receive(:reset_session)
241
+ expect(controller).to receive(:reset_session)
234
242
 
235
243
  controller.send(:validate_session_timestamp)
236
244
  end
@@ -239,7 +247,7 @@ describe LimitSessionLifetimeController do
239
247
  session[:ttl] = "SomeTTL"
240
248
  session.delete(:max_ttl)
241
249
 
242
- controller.should_receive(:persist_session_timestamp)
250
+ expect(controller).to receive(:persist_session_timestamp)
243
251
 
244
252
  controller.send(:validate_session_timestamp)
245
253
  end
@@ -248,7 +256,7 @@ describe LimitSessionLifetimeController do
248
256
  session.delete(:ttl)
249
257
  session.delete(:max_ttl)
250
258
 
251
- controller.should_receive(:persist_session_timestamp)
259
+ expect(controller).to receive(:persist_session_timestamp)
252
260
 
253
261
  controller.send(:validate_session_timestamp)
254
262
  end
@@ -257,10 +265,10 @@ describe LimitSessionLifetimeController do
257
265
  session[:ttl] = "SomeTTL"
258
266
  session[:max_ttl] = "SomeMaxTTL"
259
267
 
260
- controller.stub(:reached_ttl?).and_return(false)
261
- controller.stub(:reached_max_ttl?).and_return(false)
268
+ allow(controller).to receive(:reached_ttl?).and_return(false)
269
+ allow(controller).to receive(:reached_max_ttl?).and_return(false)
262
270
 
263
- controller.should_receive(:persist_session_timestamp)
271
+ expect(controller).to receive(:persist_session_timestamp)
264
272
 
265
273
  controller.send(:validate_session_timestamp)
266
274
  end
@@ -270,29 +278,29 @@ describe LimitSessionLifetimeController do
270
278
  context ".reached_ttl?" do
271
279
  it "returns true if persisted ttl is less than configured ttl seconds ago" do
272
280
  current_time = Time.now
273
- Time.stub(:now).and_return(current_time)
281
+ allow(Time).to receive(:now).and_return(current_time)
274
282
 
275
283
  session[:ttl] = current_time.ago(Frikandel::Configuration.ttl + 1)
276
284
 
277
- controller.send(:reached_ttl?).should be_true
285
+ expect(controller.send(:reached_ttl?)).to be_truthy
278
286
  end
279
287
 
280
288
  it "returns false if persisted ttl is equal to configured ttl seconds ago" do
281
289
  current_time = Time.now
282
- Time.stub(:now).and_return(current_time)
290
+ allow(Time).to receive(:now).and_return(current_time)
283
291
 
284
292
  session[:ttl] = current_time.ago(Frikandel::Configuration.ttl)
285
293
 
286
- controller.send(:reached_ttl?).should be_false
294
+ expect(controller.send(:reached_ttl?)).to be_falsey
287
295
  end
288
296
 
289
297
  it "returns false if persisted ttl is greater than configured ttl seconds ago" do
290
298
  current_time = Time.now
291
- Time.stub(:now).and_return(current_time)
299
+ allow(Time).to receive(:now).and_return(current_time)
292
300
 
293
301
  session[:ttl] = current_time.ago(Frikandel::Configuration.ttl - 1)
294
302
 
295
- controller.send(:reached_ttl?).should be_false
303
+ expect(controller.send(:reached_ttl?)).to be_falsey
296
304
  end
297
305
  end
298
306
 
@@ -300,29 +308,29 @@ describe LimitSessionLifetimeController do
300
308
  context ".reached_max_ttl?" do
301
309
  it "returns true if persisted max_ttl is less than current time" do
302
310
  current_time = Time.now
303
- Time.stub(:now).and_return(current_time)
311
+ allow(Time).to receive(:now).and_return(current_time)
304
312
 
305
313
  session[:max_ttl] = current_time.ago(1)
306
314
 
307
- controller.send(:reached_max_ttl?).should be_true
315
+ expect(controller.send(:reached_max_ttl?)).to be_truthy
308
316
  end
309
317
 
310
318
  it "returns false if persisted max_ttl is equal to current time" do
311
319
  current_time = Time.now
312
- Time.stub(:now).and_return(current_time)
320
+ allow(Time).to receive(:now).and_return(current_time)
313
321
 
314
322
  session[:max_ttl] = current_time
315
323
 
316
- controller.send(:reached_max_ttl?).should be_false
324
+ expect(controller.send(:reached_max_ttl?)).to be_falsey
317
325
  end
318
326
 
319
327
  it "returns false if persisted max_ttl is greater than current time" do
320
328
  current_time = Time.now
321
- Time.stub(:now).and_return(current_time)
329
+ allow(Time).to receive(:now).and_return(current_time)
322
330
 
323
331
  session[:max_ttl] = current_time.since(1)
324
332
 
325
- controller.send(:reached_max_ttl?).should be_false
333
+ expect(controller.send(:reached_max_ttl?)).to be_falsey
326
334
  end
327
335
  end
328
336
 
@@ -330,7 +338,7 @@ describe LimitSessionLifetimeController do
330
338
  context ".persist_session_timestamp" do
331
339
  it "sets ttl to current time" do
332
340
  current_time = Time.now
333
- Time.stub(:now).and_return(current_time)
341
+ allow(Time).to receive(:now).and_return(current_time)
334
342
 
335
343
  expect {
336
344
  controller.send(:persist_session_timestamp)
@@ -342,7 +350,7 @@ describe LimitSessionLifetimeController do
342
350
  it "sets max_ttl to configured max_ttl seconds in future if it's blank" do
343
351
  current_time = Time.now
344
352
  max_ttl_time = current_time.since(Frikandel::Configuration.max_ttl)
345
- Time.stub(:now).and_return(current_time)
353
+ allow(Time).to receive(:now).and_return(current_time)
346
354
 
347
355
  expect {
348
356
  controller.send(:persist_session_timestamp)
@@ -365,7 +373,7 @@ describe LimitSessionLifetimeController do
365
373
 
366
374
  context ".reset_session" do
367
375
  it "calls persist_session_timestamp" do
368
- controller.should_receive(:persist_session_timestamp).and_call_original
376
+ expect(controller).to receive(:persist_session_timestamp).and_call_original
369
377
  controller.send(:reset_session)
370
378
  end
371
379
  end
@@ -11,6 +11,8 @@ Bundler.require(*Rails.groups)
11
11
  require "frikandel"
12
12
 
13
13
  module Dummy
14
+ RAILS_GEM_VERSION = Gem::Version.new(Rails::VERSION::STRING).freeze
15
+
14
16
  class Application < Rails::Application
15
17
  # Settings in config/environments/* take precedence over those specified here.
16
18
  # Application configuration should go into files in config/initializers
@@ -13,8 +13,13 @@ Dummy::Application.configure do
13
13
  config.eager_load = false
14
14
 
15
15
  # Configure static asset server for tests with Cache-Control for performance.
16
- config.serve_static_assets = true
17
- config.static_cache_control = "public, max-age=3600"
16
+ if Dummy::RAILS_GEM_VERSION < Gem::Version.new('5.0.0')
17
+ config.serve_static_assets = true
18
+ config.static_cache_control = "public, max-age=3600"
19
+ else
20
+ config.public_file_server.enabled = true
21
+ config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
22
+ end
18
23
 
19
24
  # Show full error reports and disable caching.
20
25
  config.consider_all_requests_local = true
@@ -1,43 +1,43 @@
1
- require "spec_helper"
1
+ require "rails_helper"
2
2
 
3
- describe Frikandel::Configuration do
3
+ RSpec.describe Frikandel::Configuration do
4
4
 
5
5
  it "is a singleton" do
6
- Frikandel::Configuration.should respond_to :instance
7
- Frikandel::Configuration.instance.should be_a Frikandel::Configuration
8
- Frikandel::Configuration.instance.should be_equal Frikandel::Configuration.instance
6
+ expect(Frikandel::Configuration).to respond_to :instance
7
+ expect(Frikandel::Configuration.instance).to be_a Frikandel::Configuration
8
+ expect(Frikandel::Configuration.instance).to be_equal Frikandel::Configuration.instance
9
9
  end
10
10
 
11
11
  it "delegates max_ttl and max_ttl= to the singleton instance" do
12
- Frikandel::Configuration.instance.should_receive(:max_ttl).and_return(:some_max_ttl)
13
- Frikandel::Configuration.instance.should_receive(:max_ttl=).with(:some_value).and_return(:some_max_ttl=)
12
+ expect(Frikandel::Configuration.instance).to receive(:max_ttl).and_return(:some_max_ttl)
13
+ expect(Frikandel::Configuration.instance).to receive(:max_ttl=).with(:some_value).and_return(:some_max_ttl=)
14
14
 
15
- Frikandel::Configuration.max_ttl.should eq :some_max_ttl
16
- Frikandel::Configuration.send(:max_ttl=, :some_value).should eq :some_max_ttl=
15
+ expect(Frikandel::Configuration.max_ttl).to eq :some_max_ttl
16
+ expect(Frikandel::Configuration.send(:max_ttl=, :some_value)).to eq :some_max_ttl=
17
17
  end
18
18
 
19
19
  it "delegates ttl and ttl= to the singleton instance" do
20
- Frikandel::Configuration.instance.should_receive(:ttl).and_return(:some_ttl)
21
- Frikandel::Configuration.instance.should_receive(:ttl=).with(:some_value).and_return(:some_ttl=)
20
+ expect(Frikandel::Configuration.instance).to receive(:ttl).and_return(:some_ttl)
21
+ expect(Frikandel::Configuration.instance).to receive(:ttl=).with(:some_value).and_return(:some_ttl=)
22
22
 
23
- Frikandel::Configuration.ttl.should eq :some_ttl
24
- Frikandel::Configuration.send(:ttl=, :some_value).should eq :some_ttl=
23
+ expect(Frikandel::Configuration.ttl).to eq :some_ttl
24
+ expect(Frikandel::Configuration.send(:ttl=, :some_value)).to eq :some_ttl=
25
25
  end
26
26
 
27
27
  it "has 24 hours as default-max_ttl" do
28
- Frikandel::Configuration.max_ttl.should eq 24.hours
28
+ expect(Frikandel::Configuration.max_ttl).to eq 24.hours
29
29
  end
30
30
 
31
31
  it "has 2 hours as default-ttl" do
32
- Frikandel::Configuration.ttl.should eq 2.hours
32
+ expect(Frikandel::Configuration.ttl).to eq 2.hours
33
33
  end
34
34
 
35
35
  it "ttls can be set" do
36
36
  Frikandel::Configuration.max_ttl = 50.hours
37
37
  Frikandel::Configuration.ttl = 5.hours
38
38
 
39
- Frikandel::Configuration.max_ttl.should eq 50.hours
40
- Frikandel::Configuration.ttl.should eq 5.hours
39
+ expect(Frikandel::Configuration.max_ttl).to eq 50.hours
40
+ expect(Frikandel::Configuration.ttl).to eq 5.hours
41
41
  end
42
42
 
43
43
  end