Skip to content

Commit fac373f

Browse files
committedFeb 13, 2012
Bring Ticket Stores under test
Write tests for Ticket Store classes Fixes #30 Update a lot of our gems
1 parent 0c7bde5 commit fac373f

18 files changed

+435
-87
lines changed
 

‎.simplecov

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SimpleCov.start do
22
add_filter "/spec/.*_spec\.rb"
33
add_filter "/spec/.*/shared_examples.*"
4+
add_filter "/spec/.*/.*helper(s?).rb"
45
end
56

67
# vim: filetype=ruby

‎Gemfile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
source "http://rubygems.org"
22

33
group :development do
4-
gem "json", "~> 1.6.1"
5-
gem "rspec", "~> 2.7.0"
4+
gem "json"
5+
gem "rspec"
66
gem "bundler", ">= 1.0"
7-
gem "jeweler", "~> 1.6.2"
7+
gem "jeweler"
88
gem "actionpack"
9+
gem "activerecord"
910
gem "rake"
1011
gem "simplecov", :require => false
1112
gem "guard"
1213
gem "guard-rspec"
14+
gem "sqlite3", :require => false
15+
gem "database_cleaner"
1316
end
1417

1518
gem "activesupport", :require => "active_support"

‎Gemfile.lock

+20-13
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,55 @@ GEM
44
actionpack (2.3.11)
55
activesupport (= 2.3.11)
66
rack (~> 1.1.0)
7+
activerecord (2.3.11)
8+
activesupport (= 2.3.11)
79
activesupport (2.3.11)
10+
database_cleaner (0.7.1)
811
diff-lcs (1.1.3)
912
ffi (1.0.11)
1013
git (1.2.5)
1114
guard (1.0.0)
1215
ffi (>= 0.5.0)
1316
thor (~> 0.14.6)
14-
guard-rspec (0.5.11)
15-
guard (>= 0.8.4)
17+
guard-rspec (0.6.0)
18+
guard (>= 0.10.0)
1619
jeweler (1.6.4)
1720
bundler (~> 1.0)
1821
git (>= 1.2.5)
1922
rake
20-
json (1.6.1)
23+
json (1.6.5)
2124
multi_json (1.0.4)
2225
rack (1.1.2)
2326
rake (0.9.2.2)
24-
rspec (2.7.0)
25-
rspec-core (~> 2.7.0)
26-
rspec-expectations (~> 2.7.0)
27-
rspec-mocks (~> 2.7.0)
28-
rspec-core (2.7.1)
29-
rspec-expectations (2.7.0)
27+
rspec (2.8.0)
28+
rspec-core (~> 2.8.0)
29+
rspec-expectations (~> 2.8.0)
30+
rspec-mocks (~> 2.8.0)
31+
rspec-core (2.8.0)
32+
rspec-expectations (2.8.0)
3033
diff-lcs (~> 1.1.2)
31-
rspec-mocks (2.7.0)
34+
rspec-mocks (2.8.0)
3235
simplecov (0.5.4)
3336
multi_json (~> 1.0.3)
3437
simplecov-html (~> 0.5.3)
3538
simplecov-html (0.5.3)
39+
sqlite3 (1.3.5)
3640
thor (0.14.6)
3741

3842
PLATFORMS
3943
ruby
4044

4145
DEPENDENCIES
4246
actionpack
47+
activerecord
4348
activesupport
4449
bundler (>= 1.0)
50+
database_cleaner
4551
guard
4652
guard-rspec
47-
jeweler (~> 1.6.2)
48-
json (~> 1.6.1)
53+
jeweler
54+
json
4955
rake
50-
rspec (~> 2.7.0)
56+
rspec
5157
simplecov
58+
sqlite3

‎Guardfile

+2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ guard 'rspec', :version => 2, :cli => '-c -f doc' do
55
watch(%r{^spec/.+_spec\.rb$})
66
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
77
watch('spec/spec_helper.rb') { "spec" }
8+
watch(%r{^spec/support/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
810
end
911

‎lib/casclient.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def set_real_logger(real_logger)
5656
# Log using the appropriate method if we have a logger
5757
# if we dont' have a logger, gracefully ignore.
5858
def method_missing(name, *args)
59-
if @real_logger && @real_logger.respond_to?(name)
59+
if !@real_logger.nil? && @real_logger.respond_to?(name)
6060
@real_logger.send(name, *args)
6161
end
6262
end

‎lib/casclient/tickets/storage.rb

+29-20
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@ module Storage
44
class AbstractTicketStore
55

66
attr_accessor :log
7-
@log = CASClient::LoggerWrapper.new
7+
def log
8+
@log ||= CASClient::LoggerWrapper.new
9+
end
810

9-
def process_single_sign_out(si)
11+
def process_single_sign_out(st)
1012

11-
session_id, session = get_session_for_service_ticket(si)
13+
session_id, session = get_session_for_service_ticket(st)
1214
if session
1315
session.destroy
14-
log.debug("Destroyed #{session.inspect} for session #{session_id.inspect} corresponding to service ticket #{si.inspect}.")
16+
log.debug("Destroyed #{session.inspect} for session #{session_id.inspect} corresponding to service ticket #{st.inspect}.")
1517
else
1618
log.debug("Data for session #{session_id.inspect} was not found. It may have already been cleared by a local CAS logout request.")
1719
end
1820

1921
if session_id
2022
log.info("Single-sign-out for service ticket #{session_id.inspect} completed successfuly.")
2123
else
22-
log.debug("No session id found for CAS ticket #{si}")
24+
log.debug("No session id found for CAS ticket #{st}")
2325
end
2426
end
2527

2628
def get_session_for_service_ticket(st)
27-
session_id = read_service_session_lookup(si)
28-
if session_id
29-
session = ActiveRecord::SessionStore::Session.find_by_session_id(session_id)
29+
session_id = read_service_session_lookup(st)
30+
unless session_id.nil?
31+
# This feels a bit hackish, but there isn't really a better way to go about it that I am aware of yet
32+
session = ActiveRecord::SessionStore.session_class.find_by_session_id(session_id)
3033
else
31-
log.warn("Couldn't destroy session with SessionIndex #{si} because no corresponding session id could be looked up.")
34+
log.warn("Couldn't destroy session service ticket #{st} because no corresponding session id could be found.")
3235
end
3336
[session_id, session]
3437
end
@@ -53,6 +56,12 @@ def retrieve_pgt(pgt_iou)
5356
def read_service_session_lookup(st)
5457
raise 'Implement this in a subclass!'
5558
end
59+
60+
def session_id_from_controller(controller)
61+
session_id = controller.request.session_options[:id] || controller.session.session_id
62+
raise CASClient::CASException, "Failed to extract session_id from controller" if session_id.nil?
63+
session_id
64+
end
5665
end
5766

5867
# A Ticket Store that keeps it's tickets in a directory on the local filesystem.
@@ -83,10 +92,10 @@ def initialize(config={})
8392
# Rails session id.
8493
# Returns the filename of the lookup file created.
8594
def store_service_session_lookup(st, controller)
86-
raise CASException, "No service_ticket specified." unless st
87-
raise CASException, "No controller specified." unless controller
95+
raise CASException, "No service_ticket specified." if st.nil?
96+
raise CASException, "No controller specified." if controller.nil?
8897

89-
sid = controller.request.session_options[:id] || controller.session.session_id
98+
sid = session_id_from_controller(controller)
9099

91100
st = st.ticket if st.kind_of? ServiceTicket
92101
f = File.new(filename_of_service_session_lookup(st), 'w')
@@ -100,11 +109,11 @@ def store_service_session_lookup(st, controller)
100109
# cas_sess.<session ticket> file created in a prior call to
101110
# #store_service_session_lookup.
102111
def read_service_session_lookup(st)
103-
raise CASException, "No service_ticket specified." unless st
112+
raise CASException, "No service_ticket specified." if st.nil?
104113

105114
st = st.ticket if st.kind_of? ServiceTicket
106115
ssl_filename = filename_of_service_session_lookup(st)
107-
return File.exists?(ssl_filename) && IO.read(ssl_filename)
116+
return IO.read(ssl_filename) if File.exists?(ssl_filename)
108117
end
109118

110119
# Removes a stored relationship between a ServiceTicket and a local
@@ -113,14 +122,17 @@ def read_service_session_lookup(st)
113122
#
114123
# See #store_service_session_lookup.
115124
def cleanup_service_session_lookup(st)
116-
raise CASException, "No service_ticket specified." unless st
125+
raise CASException, "No service_ticket specified." if st.nil?
117126

118127
st = st.ticket if st.kind_of? ServiceTicket
119128
ssl_filename = filename_of_service_session_lookup(st)
120129
File.delete(ssl_filename) if File.exists?(ssl_filename)
121130
end
122131

123132
def save_pgt_iou(pgt_iou, pgt)
133+
raise CASException, "Invalid pgt_iou" if pgt_iou.nil?
134+
raise CASException, "Invalid pgt" if pgt.nil?
135+
124136
# TODO: pstore contents should probably be encrypted...
125137
pstore = open_pstore
126138

@@ -135,17 +147,14 @@ def retrieve_pgt(pgt_iou)
135147
pstore = open_pstore
136148

137149
pgt = nil
150+
# TODO: need to periodically clean the storage, otherwise it will just keep growing
138151
pstore.transaction do
139152
pgt = pstore[pgt_iou]
153+
pstore.delete pgt_iou
140154
end
141155

142156
raise CASException, "Invalid pgt_iou specified. Perhaps this pgt has already been retrieved?" unless pgt
143157

144-
# TODO: need to periodically clean the storage, otherwise it will just keep growing
145-
pstore.transaction do
146-
pstore.delete pgt_iou
147-
end
148-
149158
pgt
150159
end
151160

‎lib/casclient/tickets/storage/active_record_ticket_store.rb

+11-6
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,40 @@ def initialize(config={})
1919
end
2020

2121
def store_service_session_lookup(st, controller)
22-
#get the session from the rack env using ActiveRecord::SessionStore::SESSION_RECORD_KEY = 'rack.session.record'
22+
raise CASException, "No service_ticket specified." unless st
23+
raise CASException, "No controller specified." unless controller
2324

2425
st = st.ticket if st.kind_of? ServiceTicket
2526
session = controller.session
2627
session[:service_ticket] = st
2728
end
2829

29-
def get_session_for_service_ticket(st)
30+
def read_service_session_lookup(st)
31+
raise CASException, "No service_ticket specified." unless st
3032
st = st.ticket if st.kind_of? ServiceTicket
3133
session = ActiveRecord::SessionStore::Session.find_by_service_ticket(st)
32-
session_id = session ? session.session_id : nil
33-
[session_id, session]
34+
session ? session.session_id : nil
3435
end
3536

3637
def cleanup_service_session_lookup(st)
3738
#no cleanup needed for this ticket store
39+
#we still raise the exception for API compliance
40+
raise CASException, "No service_ticket specified." unless st
3841
end
3942

4043
def save_pgt_iou(pgt_iou, pgt)
44+
raise CASClient::CASException.new("Invalid pgt_iou") if pgt_iou.nil?
45+
raise CASClient::CASException.new("Invalid pgt") if pgt.nil?
4146
pgtiou = CasPgtiou.create(:pgt_iou => pgt_iou, :pgt_id => pgt)
4247
end
4348

4449
def retrieve_pgt(pgt_iou)
4550
raise CASException, "No pgt_iou specified. Cannot retrieve the pgt." unless pgt_iou
4651

4752
pgtiou = CasPgtiou.find_by_pgt_iou(pgt_iou)
48-
pgt = pgtiou.pgt_id
4953

50-
raise CASException, "Invalid pgt_iou specified. Perhaps this pgt has already been retrieved?" unless pgt
54+
raise CASException, "Invalid pgt_iou specified. Perhaps this pgt has already been retrieved?" unless pgtiou
55+
pgt = pgtiou.pgt_id
5156

5257
pgtiou.destroy
5358

‎spec/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.sqlite3

0 commit comments

Comments
 (0)
Please sign in to comment.