1 module AuthenticatedTestHelper
2 # Sets the current user in the session from the user fixtures.
4 @request.session[:user] = user ? users(user).id : nil
8 @request.env['Content-Type'] = type
12 @request.env["HTTP_ACCEPT"] = accept
15 def authorize_as(user)
17 @request.env["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{users(user).login}:test")}"
18 accept 'application/xml'
19 content_type 'application/xml'
21 @request.env["HTTP_AUTHORIZATION"] = nil
27 # http://project.ioni.st/post/217#post-217
29 # def test_new_publication
30 # assert_difference(Publication, :count) do
31 # post :create, :publication => {...}
36 def assert_difference(object, method = nil, difference = 1)
37 initial_value = object.send(method)
39 assert_equal initial_value + difference, object.send(method), "#{object}##{method}"
42 def assert_no_difference(object, method, &block)
43 assert_difference object, method, 0, &block
46 # Assert the block redirects to the login
48 # assert_requires_login(:bob) { |c| c.get :edit, :id => 1 }
50 def assert_requires_login(login = nil)
51 yield HttpLoginProxy.new(self, login)
54 def assert_http_authentication_required(login = nil)
55 yield XmlLoginProxy.new(self, login)
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)
68 attr_reader :controller
70 def initialize(controller, login)
71 @controller = controller
77 raise NotImplementedError
81 raise NotImplementedError
84 def method_missing(method, *args)
87 @controller.send(method, *args)
92 class HttpLoginProxy < BaseLoginProxy
95 @controller.login_as @login if @login
99 @controller.assert_redirected_to :controller => 'account', :action => 'login'
103 class XmlLoginProxy < BaseLoginProxy
106 @controller.accept 'application/xml'
107 @controller.authorize_as @login if @login
111 @controller.assert_response 401