Guest User

Untitled

a guest
Mar 1st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. def test_hate_method_should_record_vote
  2. initial_vote_count = Vote.count
  3. post :hate, { :id => 2 }, { :user => 7 }
  4. assert_equal initial_vote_count + 1, Vote.count
  5. end
  6.  
  7.  
  8. # before the method hate runs it must be authenticated with login_required from lib/authenticated_system.rb
  9.  
  10. def login_required
  11. username, passwd = get_auth_data
  12. # I think the get_auth_data might be creating the error
  13. self.current_user ||= User.authenticate(username, passwd) || :false if username && passwd
  14. return true if logged_in? && authorized?
  15. respond_to do |accepts|
  16. accepts.html do
  17. session[:return_to] = request.request_uri
  18. redirect_to :controller => 'account', :action => 'login'
  19. end
  20. accepts.xml do
  21. headers["Status"] = "Unauthorized"
  22. headers["WWW-Authenticate"] = %(Basic realm="Web Password")
  23. render :text => "Could't authenticate you", :status => '401 Unauthorized'
  24. end
  25. end
  26. false
  27. end
  28.  
  29.  
  30. def get_auth_data
  31. user, pass = nil, nil
  32. # extract authorisation credentials
  33. if request.env.has_key? 'X-HTTP_AUTHORIZATION'
  34. # try to get it where mod_rewrite might have put it
  35. authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split
  36. elsif request.env.has_key? 'HTTP_AUTHORIZATION'
  37. # this is the regular location
  38. authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
  39. end
  40.  
  41. # at the moment we only support basic authentication
  42. if authdata && authdata[0] == 'Basic'
  43. user, pass = Base64.decode64(authdata[1]).split(':')[0..1]
  44. end
  45. return [user, pass]
  46. end
Add Comment
Please, Sign In to add comment