]> git.openstreetmap.org Git - rails.git/blob - test/controllers/redactions_controller_test.rb
Merge remote-tracking branch 'upstream/pull/2994'
[rails.git] / test / controllers / redactions_controller_test.rb
1 require "test_helper"
2
3 class RedactionsControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/redactions", :method => :get },
9       { :controller => "redactions", :action => "index" }
10     )
11     assert_routing(
12       { :path => "/redactions/new", :method => :get },
13       { :controller => "redactions", :action => "new" }
14     )
15     assert_routing(
16       { :path => "/redactions", :method => :post },
17       { :controller => "redactions", :action => "create" }
18     )
19     assert_routing(
20       { :path => "/redactions/1", :method => :get },
21       { :controller => "redactions", :action => "show", :id => "1" }
22     )
23     assert_routing(
24       { :path => "/redactions/1/edit", :method => :get },
25       { :controller => "redactions", :action => "edit", :id => "1" }
26     )
27     assert_routing(
28       { :path => "/redactions/1", :method => :put },
29       { :controller => "redactions", :action => "update", :id => "1" }
30     )
31     assert_routing(
32       { :path => "/redactions/1", :method => :delete },
33       { :controller => "redactions", :action => "destroy", :id => "1" }
34     )
35   end
36
37   def test_index
38     create(:redaction)
39
40     get redactions_path
41     assert_response :success
42     assert_template :index
43     assert_select "ul#redaction_list", 1 do
44       assert_select "li", Redaction.count
45     end
46   end
47
48   def test_new
49     get new_redaction_path
50     assert_response :redirect
51     assert_redirected_to login_path(:referer => new_redaction_path)
52   end
53
54   def test_new_moderator
55     session_for(create(:moderator_user))
56
57     get new_redaction_path
58     assert_response :success
59     assert_template :new
60   end
61
62   def test_new_non_moderator
63     session_for(create(:user))
64
65     get new_redaction_path
66     assert_response :redirect
67     assert_redirected_to :controller => "errors", :action => "forbidden"
68   end
69
70   def test_create_moderator
71     session_for(create(:moderator_user))
72
73     post redactions_path(:redaction => { :title => "Foo", :description => "Description here." })
74     assert_response :redirect
75     assert_redirected_to(redaction_path(Redaction.find_by(:title => "Foo")))
76   end
77
78   def test_create_moderator_invalid
79     session_for(create(:moderator_user))
80
81     post redactions_path(:redaction => { :title => "Foo", :description => "" })
82     assert_response :success
83     assert_template :new
84   end
85
86   def test_create_non_moderator
87     session_for(create(:user))
88
89     post redactions_path(:redaction => { :title => "Foo", :description => "Description here." })
90     assert_response :redirect
91     assert_redirected_to :controller => "errors", :action => "forbidden"
92   end
93
94   def test_destroy_moderator_empty
95     session_for(create(:moderator_user))
96
97     # create an empty redaction
98     redaction = create(:redaction)
99
100     delete redaction_path(:id => redaction)
101     assert_response :redirect
102     assert_redirected_to(redactions_path)
103   end
104
105   def test_destroy_moderator_non_empty
106     session_for(create(:moderator_user))
107
108     # create elements in the redaction
109     redaction = create(:redaction)
110     create(:old_node, :redaction => redaction)
111
112     delete redaction_path(:id => redaction)
113     assert_response :redirect
114     assert_redirected_to(redaction_path(redaction))
115     assert_match(/^Redaction is not empty/, flash[:error])
116   end
117
118   def test_delete_non_moderator
119     session_for(create(:user))
120
121     delete redaction_path(:id => create(:redaction))
122     assert_response :redirect
123     assert_redirected_to :controller => "errors", :action => "forbidden"
124   end
125
126   def test_edit
127     redaction = create(:redaction)
128
129     get edit_redaction_path(:id => redaction)
130     assert_response :redirect
131     assert_redirected_to login_path(:referer => edit_redaction_path(redaction))
132   end
133
134   def test_edit_moderator
135     session_for(create(:moderator_user))
136
137     get edit_redaction_path(:id => create(:redaction))
138     assert_response :success
139   end
140
141   def test_edit_non_moderator
142     session_for(create(:user))
143
144     get edit_redaction_path(:id => create(:redaction))
145     assert_response :redirect
146     assert_redirected_to :controller => "errors", :action => "forbidden"
147   end
148
149   def test_update_moderator
150     session_for(create(:moderator_user))
151
152     redaction = create(:redaction)
153
154     put redaction_path(:id => redaction, :redaction => { :title => "Foo", :description => "Description here." })
155     assert_response :redirect
156     assert_redirected_to(redaction_path(redaction))
157   end
158
159   def test_update_moderator_invalid
160     session_for(create(:moderator_user))
161
162     redaction = create(:redaction)
163
164     put redaction_path(:id => redaction, :redaction => { :title => "Foo", :description => "" })
165     assert_response :success
166     assert_template :edit
167   end
168
169   def test_updated_non_moderator
170     session_for(create(:user))
171
172     redaction = create(:redaction)
173
174     put redaction_path(:id => redaction, :redaction => { :title => "Foo", :description => "Description here." })
175     assert_response :redirect
176     assert_redirected_to :controller => "errors", :action => "forbidden"
177   end
178 end