]> git.openstreetmap.org Git - rails.git/blob - test/controllers/acls_controller_test.rb
Merge pull request #7373 from kmpoppe/note-moderation-zone-message
[rails.git] / test / controllers / acls_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class AclsControllerTest < ActionDispatch::IntegrationTest
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/acls", :method => :get },
11       { :controller => "acls", :action => "index" }
12     )
13     assert_routing(
14       { :path => "/acls/new", :method => :get },
15       { :controller => "acls", :action => "new" }
16     )
17     assert_routing(
18       { :path => "/acls", :method => :post },
19       { :controller => "acls", :action => "create" }
20     )
21     assert_routing(
22       { :path => "/acls/1/edit", :method => :get },
23       { :controller => "acls", :action => "edit", :id => "1" }
24     )
25     assert_routing(
26       { :path => "/acls/1", :method => :put },
27       { :controller => "acls", :action => "update", :id => "1" }
28     )
29     assert_routing(
30       { :path => "/acls/1", :method => :delete },
31       { :controller => "acls", :action => "destroy", :id => "1" }
32     )
33   end
34
35   def test_index
36     get acls_path
37     assert_redirected_to login_path(:referer => acls_path)
38   end
39
40   def test_index_non_administrator
41     session_for(create(:user))
42
43     get acls_path
44     assert_redirected_to :controller => "errors", :action => "forbidden"
45   end
46
47   def test_index_administrator
48     session_for(create(:administrator_user))
49
50     get acls_path
51     assert_response :success
52     assert_template :index
53   end
54
55   def test_index_invalid_paged
56     session_for(create(:administrator_user))
57
58     %w[-1 fred].each do |id|
59       get acls_path(:before => id)
60       assert_redirected_to :controller => "errors", :action => "bad_request"
61
62       get acls_path(:after => id)
63       assert_redirected_to :controller => "errors", :action => "bad_request"
64     end
65   end
66
67   def test_new
68     get new_acl_path
69     assert_redirected_to login_path(:referer => new_acl_path)
70   end
71
72   def test_new_non_administrator
73     session_for(create(:user))
74
75     get new_acl_path
76     assert_redirected_to :controller => "errors", :action => "forbidden"
77   end
78
79   def test_new_administrator
80     session_for(create(:administrator_user))
81
82     get new_acl_path
83     assert_response :success
84     assert_template :new
85   end
86
87   def test_create
88     assert_no_difference "Acl.count" do
89       post acls_path(:acl => { :address => "192.0.2.0/24", :k => "no_account_creation" })
90     end
91     assert_response :forbidden
92   end
93
94   def test_create_non_administrator
95     session_for(create(:user))
96
97     assert_no_difference "Acl.count" do
98       post acls_path(:acl => { :address => "192.0.2.0/24", :k => "no_account_creation" })
99     end
100     assert_redirected_to :controller => "errors", :action => "forbidden"
101   end
102
103   def test_create_administrator
104     session_for(create(:administrator_user))
105
106     assert_difference "Acl.count", 1 do
107       post acls_path(:acl => { :address => "192.0.2.0/24", :k => "no_account_creation" })
108     end
109     assert_redirected_to acls_path
110   end
111
112   def test_edit
113     acl = create(:acl)
114
115     get edit_acl_path(acl)
116     assert_redirected_to login_path(:referer => edit_acl_path(acl))
117   end
118
119   def test_edit_non_administrator
120     session_for(create(:user))
121
122     get edit_acl_path(create(:acl))
123     assert_redirected_to :controller => "errors", :action => "forbidden"
124   end
125
126   def test_edit_administrator
127     session_for(create(:administrator_user))
128
129     get edit_acl_path(create(:acl))
130     assert_response :success
131     assert_template :edit
132   end
133
134   def test_update
135     acl = create(:acl, :k => "no_account_creation")
136
137     put acl_path(acl, :acl => { :k => "no_note_comment" })
138     assert_response :forbidden
139     assert_equal "no_account_creation", acl.reload.k
140   end
141
142   def test_update_non_administrator
143     session_for(create(:user))
144     acl = create(:acl, :k => "no_account_creation")
145
146     put acl_path(acl, :acl => { :k => "no_note_comment" })
147     assert_redirected_to :controller => "errors", :action => "forbidden"
148     assert_equal "no_account_creation", acl.reload.k
149   end
150
151   def test_update_administrator
152     session_for(create(:administrator_user))
153     acl = create(:acl, :k => "no_account_creation")
154
155     put acl_path(acl, :acl => { :k => "no_note_comment" })
156     assert_redirected_to acls_path
157     assert_equal "no_note_comment", acl.reload.k
158   end
159
160   def test_destroy
161     acl = create(:acl)
162
163     assert_no_difference "Acl.count" do
164       delete acl_path(acl)
165     end
166     assert_response :forbidden
167   end
168
169   def test_destroy_non_administrator
170     session_for(create(:user))
171     acl = create(:acl)
172
173     assert_no_difference "Acl.count" do
174       delete acl_path(acl)
175     end
176     assert_redirected_to :controller => "errors", :action => "forbidden"
177   end
178
179   def test_destroy_administrator
180     session_for(create(:administrator_user))
181     acl = create(:acl)
182
183     assert_difference "Acl.count", -1 do
184       delete acl_path(acl)
185     end
186     assert_redirected_to acls_path
187   end
188 end