]> git.openstreetmap.org Git - rails.git/blob - test/controllers/redactions_controller_test.rb
Simplify remaining link_to ... user_path in views
[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_show
49     redaction = create(:redaction, :title => "tested-redaction")
50
51     get redaction_path(redaction)
52     assert_response :success
53     assert_dom "h1", :text => /tested-redaction/
54     assert_dom "a[href='#{user_path redaction.user}']", :text => redaction.user.display_name
55   end
56
57   def test_new
58     get new_redaction_path
59     assert_redirected_to login_path(:referer => new_redaction_path)
60   end
61
62   def test_new_moderator
63     session_for(create(:moderator_user))
64
65     get new_redaction_path
66     assert_response :success
67     assert_template :new
68   end
69
70   def test_new_non_moderator
71     session_for(create(:user))
72
73     get new_redaction_path
74     assert_redirected_to :controller => "errors", :action => "forbidden"
75   end
76
77   def test_create_moderator
78     session_for(create(:moderator_user))
79
80     post redactions_path(:redaction => { :title => "Foo", :description => "Description here." })
81     assert_redirected_to(redaction_path(Redaction.find_by(:title => "Foo")))
82   end
83
84   def test_create_moderator_invalid
85     session_for(create(:moderator_user))
86
87     post redactions_path(:redaction => { :title => "Foo", :description => "" })
88     assert_response :success
89     assert_template :new
90   end
91
92   def test_create_non_moderator
93     session_for(create(:user))
94
95     post redactions_path(:redaction => { :title => "Foo", :description => "Description here." })
96     assert_redirected_to :controller => "errors", :action => "forbidden"
97   end
98
99   def test_destroy_moderator_empty
100     session_for(create(:moderator_user))
101
102     # create an empty redaction
103     redaction = create(:redaction)
104
105     delete redaction_path(:id => redaction)
106     assert_redirected_to(redactions_path)
107   end
108
109   def test_destroy_moderator_non_empty
110     session_for(create(:moderator_user))
111
112     # create elements in the redaction
113     redaction = create(:redaction)
114     create(:old_node, :redaction => redaction)
115
116     delete redaction_path(:id => redaction)
117     assert_redirected_to(redaction_path(redaction))
118     assert_match(/^Redaction is not empty/, flash[:error])
119   end
120
121   def test_delete_non_moderator
122     session_for(create(:user))
123
124     delete redaction_path(:id => create(:redaction))
125     assert_redirected_to :controller => "errors", :action => "forbidden"
126   end
127
128   def test_edit
129     redaction = create(:redaction)
130
131     get edit_redaction_path(:id => redaction)
132     assert_redirected_to login_path(:referer => edit_redaction_path(redaction))
133   end
134
135   def test_edit_moderator
136     session_for(create(:moderator_user))
137
138     get edit_redaction_path(:id => create(:redaction))
139     assert_response :success
140   end
141
142   def test_edit_non_moderator
143     session_for(create(:user))
144
145     get edit_redaction_path(:id => create(:redaction))
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_redirected_to(redaction_path(redaction))
156   end
157
158   def test_update_moderator_invalid
159     session_for(create(:moderator_user))
160
161     redaction = create(:redaction)
162
163     put redaction_path(:id => redaction, :redaction => { :title => "Foo", :description => "" })
164     assert_response :success
165     assert_template :edit
166   end
167
168   def test_updated_non_moderator
169     session_for(create(:user))
170
171     redaction = create(:redaction)
172
173     put redaction_path(:id => redaction, :redaction => { :title => "Foo", :description => "Description here." })
174     assert_redirected_to :controller => "errors", :action => "forbidden"
175   end
176 end