Skip to content

Commit a42963a

Browse files
committed
Setup basic spec state and extract common behavior
This is in preparation for the new caching changes. Since the default test cache is the [`NullStore`](http://api.rubyonrails.org/classes/ActiveSupport/Cache/NullStore.html) we need to ensure we test the cache behavior. Since we don't know what side-effects enabling the cache for the entire test suite will result in, we enable it only for these specs. Since it's likely that we will want to test caching in other parts of the code later, we make put this common setup into a support module. We choose the in-memory cache as these are tests and we want them to be fast. The in-memory cache requires no external dependencies and is one of the fastest cache options. We know all forms of the cache state will still require munging of the headers. So since we're extracting common behavior we moved the header munging logic in to a shared example set.
1 parent 131a0d0 commit a42963a

File tree

2 files changed

+79
-39
lines changed

2 files changed

+79
-39
lines changed

spec/kracken/controllers/token_authenticatable_spec.rb

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "support/base_controller_double"
2+
require "support/using_cache"
23

34
module Kracken
45
class TokenAuthController < BaseControllerDouble
@@ -19,43 +20,16 @@ def authenticate_or_request_with_http_token(realm = nil)
1920
end
2021

2122
RSpec.describe Controllers::TokenAuthenticatable do
22-
describe "authenticating via a token" do
23-
context "on a cache hit" do
24-
it "munges the request headers to support parameterized tokens"
25-
it "leaves the request header unchange when with no parameterized token"
26-
it "uses the exising cache to bypass the authentication process"
27-
it "returns the auth info"
28-
it "exposes the auth info via the `current_` helpers"
29-
it "lazy loads the current user"
30-
end
23+
describe "authenticating via a token", :using_cache do
24+
shared_examples "the authorization request headers" do |token_helper|
25+
let(:expected_token) { public_send token_helper }
3126

32-
context "on a cache miss with an invalid token" do
33-
it "munges the request headers to support parameterized tokens"
34-
it "leaves the request header unchange when with no parameterized token"
35-
it "follows the token authentication process"
36-
it "returns nil"
37-
it "doesn't cache invalid tokens"
38-
end
39-
40-
context "on a cache miss with a valid token" do
41-
before do
42-
allow(Authenticator).to receive(:user_with_token)
43-
end
44-
45-
it "follows the token authentication process"
46-
it "returns the auth info"
47-
it "exposes the auth info via the `current_` helpers"
48-
it "sets the auth info as the cache value"
49-
it "sets the cache expiration to one minute by default"
50-
it "sets the cache expiration to the environment setting `KRACKEN_TOKEN_TTL` when available"
51-
it "eager loads the current user"
52-
53-
it "munges the request headers to support parameterized tokens" do
27+
specify "are munged to include a provided parameterized token" do
5428
controller = TokenAuthController.new
5529
controller.request.env = {
5630
'HTTP_AUTHORIZATION' => 'Token token="header token"'
5731
}
58-
controller.params = { token: "param token" }
32+
controller.params = { token: expected_token }
5933

6034
expect {
6135
controller.authenticate_user_with_token!
@@ -64,27 +38,79 @@ def authenticate_or_request_with_http_token(realm = nil)
6438
}.from(
6539
'HTTP_AUTHORIZATION' => 'Token token="header token"'
6640
).to(
67-
'HTTP_AUTHORIZATION' => 'Token token="param token"'
41+
'HTTP_AUTHORIZATION' => "Token token=\"#{expected_token}\""
6842
)
6943
end
7044

71-
it "leaves the request header unchange when with no parameterized token" do
45+
specify "are not modified when no parameterized token provided" do
7246
controller = TokenAuthController.new
7347
controller.request.env = {
74-
'HTTP_AUTHORIZATION' => 'Token token="any token"'
48+
'HTTP_AUTHORIZATION' => "Token token=\"#{expected_token}\""
7549
}
7650

7751
expect {
7852
controller.authenticate_user_with_token!
7953
}.not_to change { controller.request.env }.from(
80-
'HTTP_AUTHORIZATION' => 'Token token="any token"'
54+
'HTTP_AUTHORIZATION' => "Token token=\"#{expected_token}\""
8155
)
8256
end
57+
end
8358

84-
it "authenticates the current user via the token" do
85-
a_user = instance_double(User)
86-
allow(Authenticator).to receive(:user_with_token).with("any token")
59+
context "on a cache hit" do
60+
let(:cached_token) { "any token" }
61+
let(:cache_key) { "auth/token/any token" }
62+
63+
before do
64+
Rails.cache.write(cache_key, "auth info")
65+
end
66+
67+
include_examples "the authorization request headers", :cached_token
68+
69+
it "uses the exising cache to bypass the authentication process"
70+
it "returns the auth info"
71+
it "exposes the auth info via the `current_` helpers"
72+
it "lazy loads the current user"
73+
end
74+
75+
context "on a cache miss with an invalid token" do
76+
let(:invalid_token) { "any token" }
77+
78+
before do
79+
allow(Authenticator).to receive(:user_with_token).with(invalid_token)
80+
.and_return(nil)
81+
end
82+
83+
include_examples "the authorization request headers", :invalid_token
84+
85+
it "follows the token authentication process"
86+
it "returns nil"
87+
it "doesn't cache invalid tokens"
88+
end
89+
90+
context "on a cache miss with a valid token" do
91+
let(:a_user) {
92+
instance_double(User, id: user_id, team_ids: some_team_ids)
93+
}
94+
let(:some_team_ids) { [:some, :team, :ids] }
95+
let(:user_id) { :any_id }
96+
let(:valid_token) { "any token" }
97+
98+
before do
99+
allow(Authenticator).to receive(:user_with_token).with(valid_token)
87100
.and_return(a_user)
101+
end
102+
103+
include_examples "the authorization request headers", :valid_token
104+
105+
it "follows the token authentication process"
106+
it "returns the auth info"
107+
it "exposes the auth info via the `current_` helpers"
108+
it "sets the auth info as the cache value"
109+
it "sets the cache expiration to one minute by default"
110+
it "sets the cache expiration to the environment setting `KRACKEN_TOKEN_TTL` when available"
111+
it "eager loads the current user"
112+
113+
it "authenticates the current user via the token" do
88114
controller = TokenAuthController.new
89115
controller.request.env = {
90116
'HTTP_AUTHORIZATION' => 'Token token="any token"'

spec/support/using_cache.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
RSpec.shared_context "using Rails cache", :using_cache do
2+
before(:context) do
3+
@org_cache = Rails.cache
4+
Rails.cache = ActiveSupport::Cache.lookup_store(:memory_store)
5+
end
6+
7+
after(:context) do
8+
Rails.cache = @org_cache
9+
end
10+
11+
before do
12+
Rails.cache.clear
13+
end
14+
end

0 commit comments

Comments
 (0)