[1] | 1 | module AuthenticatedTestHelper |
---|
| 2 | # Sets the current user in the session from the user fixtures. |
---|
| 3 | def login_as(user) |
---|
| 4 | @request.session[:user] = user ? users(user).id : nil |
---|
| 5 | end |
---|
| 6 | |
---|
| 7 | def content_type(type) |
---|
| 8 | @request.env['Content-Type'] = type |
---|
| 9 | end |
---|
| 10 | |
---|
| 11 | def accept(accept) |
---|
| 12 | @request.env["HTTP_ACCEPT"] = accept |
---|
| 13 | end |
---|
| 14 | |
---|
| 15 | def authorize_as(user) |
---|
| 16 | if user |
---|
| 17 | @request.env["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{users(user).login}:test")}" |
---|
| 18 | accept 'application/xml' |
---|
| 19 | content_type 'application/xml' |
---|
| 20 | else |
---|
| 21 | @request.env["HTTP_AUTHORIZATION"] = nil |
---|
| 22 | accept nil |
---|
| 23 | content_type nil |
---|
| 24 | end |
---|
| 25 | end |
---|
| 26 | |
---|
| 27 | # http://project.ioni.st/post/217#post-217 |
---|
| 28 | # |
---|
| 29 | # def test_new_publication |
---|
| 30 | # assert_difference(Publication, :count) do |
---|
| 31 | # post :create, :publication => {...} |
---|
| 32 | # # ... |
---|
| 33 | # end |
---|
| 34 | # end |
---|
| 35 | # |
---|
| 36 | def assert_difference(object, method = nil, difference = 1) |
---|
| 37 | initial_value = object.send(method) |
---|
| 38 | yield |
---|
| 39 | assert_equal initial_value + difference, object.send(method), "#{object}##{method}" |
---|
| 40 | end |
---|
| 41 | |
---|
| 42 | def assert_no_difference(object, method, &block) |
---|
| 43 | assert_difference object, method, 0, &block |
---|
| 44 | end |
---|
| 45 | |
---|
| 46 | # Assert the block redirects to the login |
---|
| 47 | # |
---|
| 48 | # assert_requires_login(:bob) { |c| c.get :edit, :id => 1 } |
---|
| 49 | # |
---|
| 50 | def assert_requires_login(login = nil) |
---|
| 51 | yield HttpLoginProxy.new(self, login) |
---|
| 52 | end |
---|
| 53 | |
---|
| 54 | def assert_http_authentication_required(login = nil) |
---|
| 55 | yield XmlLoginProxy.new(self, login) |
---|
| 56 | end |
---|
| 57 | |
---|
| 58 | def reset!(*instance_vars) |
---|
| 59 | instance_vars = [:controller, :request, :response] unless instance_vars.any? |
---|
| 60 | instance_vars.collect! { |v| "@#{v}".to_sym } |
---|
| 61 | instance_vars.each do |var| |
---|
| 62 | instance_variable_set(var, instance_variable_get(var).class.new) |
---|
| 63 | end |
---|
| 64 | end |
---|
| 65 | end |
---|
| 66 | |
---|
| 67 | class BaseLoginProxy |
---|
| 68 | attr_reader :controller |
---|
| 69 | attr_reader :options |
---|
| 70 | def initialize(controller, login) |
---|
| 71 | @controller = controller |
---|
| 72 | @login = login |
---|
| 73 | end |
---|
| 74 | |
---|
| 75 | private |
---|
| 76 | def authenticated |
---|
| 77 | raise NotImplementedError |
---|
| 78 | end |
---|
| 79 | |
---|
| 80 | def check |
---|
| 81 | raise NotImplementedError |
---|
| 82 | end |
---|
| 83 | |
---|
| 84 | def method_missing(method, *args) |
---|
| 85 | @controller.reset! |
---|
| 86 | authenticate |
---|
| 87 | @controller.send(method, *args) |
---|
| 88 | check |
---|
| 89 | end |
---|
| 90 | end |
---|
| 91 | |
---|
| 92 | class HttpLoginProxy < BaseLoginProxy |
---|
| 93 | protected |
---|
| 94 | def authenticate |
---|
| 95 | @controller.login_as @login if @login |
---|
| 96 | end |
---|
| 97 | |
---|
| 98 | def check |
---|
| 99 | @controller.assert_redirected_to :controller => 'account', :action => 'login' |
---|
| 100 | end |
---|
| 101 | end |
---|
| 102 | |
---|
| 103 | class XmlLoginProxy < BaseLoginProxy |
---|
| 104 | protected |
---|
| 105 | def authenticate |
---|
| 106 | @controller.accept 'application/xml' |
---|
| 107 | @controller.authorize_as @login if @login |
---|
| 108 | end |
---|
| 109 | |
---|
| 110 | def check |
---|
| 111 | @controller.assert_response 401 |
---|
| 112 | end |
---|
| 113 | end |
---|