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