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