Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / test / functional / .svn / text-base / login_system_test.rb.svn-base
blobb48f891543e3bdeb2ea9c422f8afbbd5c2c43a5a
1 require File.dirname(__FILE__) + '/../test_helper'
3 class LoginSystemTest < Test::Unit::TestCase
4   
5   fixtures :users
6   test_helper :routing, :login
7   
8   class StubController < ActionController::Base
9     def rescue_action(e)
10       raise e
11     end
12     
13     def method_missing(method, *args, &block)
14       if (args.size == 0) and not block_given?
15         render :text => 'just a test'
16       else
17         super
18       end
19     end
20   end
21   
22   class LoginRequiredController < StubController
23     include LoginSystem
24     
25     attr_writer :condition
26     
27     def condition?
28       @condition ||= false
29     end
30   end
31   
32   class NoLoginRequiredController < LoginRequiredController
33     no_login_required
34   end
35   
36   class OnlyAllowAccessToWhenController < LoginRequiredController
37     only_allow_access_to :edit, :new, :when => [:admin, :developer], :denied_url => { :action => :test }, :denied_message => 'Fun.'
38   end
39   
40   class OnlyAllowAccessToWhenDefaultsController < LoginRequiredController
41     only_allow_access_to :edit, :when => :admin
42   end
43   
44   class OnlyAllowAccessToIfController < LoginRequiredController
45     only_allow_access_to :edit, :if => :condition?
46   end
47   
48   def setup
49     @controller = LoginRequiredController.new
50     @request = ActionController::TestRequest.new
51     @response = ActionController::TestResponse.new
52     setup_custom_routes
53   end
54   
55   def teardown
56     teardown_custom_routes
57   end
58   
59   def test_authenticate__with_user_in_session
60     login_as(:existing)
61     get :index
62     assert_response :success
63   end
64   def test_authenticate__without_user_in_session
65     get :index
66     assert_redirected_to login_url
67   end
68   
69   # Class Methods
70   
71   def test_no_login_required
72     @controller = NoLoginRequiredController.new
73     get :index
74     assert_response :success
75   end
76   
77   def test_only_allow_access_to__when_user_in_role
78     @controller = OnlyAllowAccessToWhenController.new
79     login_as(:admin)
80     get :edit
81     assert_response :success
82   end
83   def test_only_allow_access_to__when_user_in_role_2
84     @controller = OnlyAllowAccessToWhenController.new
85     login_as(:developer)
86     get :new
87     assert_response :success
88   end
89   def test_only_allow_access_to__when_user_in_role_3
90     @controller = OnlyAllowAccessToWhenController.new
91     login_as(:admin)
92     get :another
93     assert_response :success
94   end
95   def test_only_allow_access_to__when_user_not_in_role
96     @controller = OnlyAllowAccessToWhenController.new
97     login_as(:non_admin)
98     get :edit
99     assert_redirected_to :action => :test
100     assert_equal 'Fun.', flash[:error]
101   end
102   def test_only_allow_access_to__when_user_not_in_role_2
103     @controller = OnlyAllowAccessToWhenController.new
104     login_as(:non_admin)
105     get :new
106     assert_redirected_to :action => :test
107     assert_equal 'Fun.', flash[:error]
108   end
109   def test_only_allow_access_to__when__user_not_in_role_3
110     @controller = OnlyAllowAccessToWhenController.new
111     login_as(:non_admin)
112     get :another
113     assert_response :success
114   end
115   def test_only_allow_access_to__when__user_not_in_role__defaults
116     @controller = OnlyAllowAccessToWhenDefaultsController.new
117     login_as(:non_admin)
118     get :edit
119     assert_redirected_to :action => :index
120     assert_equal 'Access denied.', flash[:error]
121   end
122   
123   def test_only_allow_access_to__if__condition_true
124     @controller = OnlyAllowAccessToIfController.new
125     @controller.condition = true
126     login_as(:existing)
127     get :edit
128     assert_response :success
129   end
130   def test_only_allow_access_to__if__condition_false
131     @controller = OnlyAllowAccessToIfController.new
132     @controller.condition = false
133     login_as(:existing)
134     get :edit
135     assert_response :redirect
136   end