Commit 86fab629de57001c9b80c1fb9ee9c83d3072395b
1 parent
ef2f9593
Exists in
master
and in
21 other branches
users: fix typo and test User.current
Showing
2 changed files
with
18 additions
and
10 deletions
Show diff stats
lib/authenticated_system.rb
| ... | ... | @@ -2,17 +2,21 @@ module AuthenticatedSystem |
| 2 | 2 | |
| 3 | 3 | protected |
| 4 | 4 | |
| 5 | - # See impl. from http://stackoverflow.com/a/2513456/670229 | |
| 6 | - def self.included? base | |
| 7 | - base.around_filter do | |
| 5 | + def self.included base | |
| 6 | + # See impl. from http://stackoverflow.com/a/2513456/670229 | |
| 7 | + base.around_filter do |&block| | |
| 8 | 8 | begin |
| 9 | 9 | User.current = current_user |
| 10 | - yield | |
| 10 | + block.call | |
| 11 | 11 | ensure |
| 12 | 12 | # to address the thread variable leak issues in Puma/Thin webserver |
| 13 | 13 | User.current = nil |
| 14 | 14 | end |
| 15 | 15 | end |
| 16 | + | |
| 17 | + # Inclusion hook to make #current_user and #logged_in? | |
| 18 | + # available as ActionView helper methods. | |
| 19 | + base.send :helper_method, :current_user, :logged_in? | |
| 16 | 20 | end |
| 17 | 21 | |
| 18 | 22 | # Returns true or false if the user is logged in. |
| ... | ... | @@ -134,12 +138,6 @@ module AuthenticatedSystem |
| 134 | 138 | end |
| 135 | 139 | end |
| 136 | 140 | |
| 137 | - # Inclusion hook to make #current_user and #logged_in? | |
| 138 | - # available as ActionView helper methods. | |
| 139 | - def self.included(base) | |
| 140 | - base.send :helper_method, :current_user, :logged_in? | |
| 141 | - end | |
| 142 | - | |
| 143 | 141 | # When called with before_filter :login_from_cookie will check for an :auth_token |
| 144 | 142 | # cookie and log the user back in if apropriate |
| 145 | 143 | def login_from_cookie | ... | ... |
test/functional/application_controller_test.rb
| ... | ... | @@ -191,6 +191,16 @@ class ApplicationControllerTest < ActionController::TestCase |
| 191 | 191 | assert_tag :tag => 'option', :attributes => { :value => 'it' }, :content => 'Italiano' |
| 192 | 192 | end |
| 193 | 193 | |
| 194 | + should 'set and unset the current user' do | |
| 195 | + testuser = create_user 'testuser' | |
| 196 | + login_as 'testuser' | |
| 197 | + User.expects(:current=).with do |user| | |
| 198 | + user == testuser | |
| 199 | + end.at_least_once | |
| 200 | + User.expects(:current=).with(nil).at_least_once | |
| 201 | + get :index | |
| 202 | + end | |
| 203 | + | |
| 194 | 204 | should 'display link to webmail if enabled for system' do |
| 195 | 205 | @controller.stubs(:get_layout).returns('application') |
| 196 | 206 | login_as('ze') | ... | ... |