]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
7bd119885f04beb9c972a33ebe90803c09d1c412
[rails.git] / test / controllers / browse_controller_test.rb
1 require "test_helper"
2 require "browse_controller"
3
4 class BrowseControllerTest < 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 => "/node/1", :method => :get },
12       { :controller => "browse", :action => "node", :id => "1" }
13     )
14     assert_routing(
15       { :path => "/node/1/history", :method => :get },
16       { :controller => "browse", :action => "node_history", :id => "1" }
17     )
18     assert_routing(
19       { :path => "/way/1", :method => :get },
20       { :controller => "browse", :action => "way", :id => "1" }
21     )
22     assert_routing(
23       { :path => "/way/1/history", :method => :get },
24       { :controller => "browse", :action => "way_history", :id => "1" }
25     )
26     assert_routing(
27       { :path => "/relation/1", :method => :get },
28       { :controller => "browse", :action => "relation", :id => "1" }
29     )
30     assert_routing(
31       { :path => "/relation/1/history", :method => :get },
32       { :controller => "browse", :action => "relation_history", :id => "1" }
33     )
34     assert_routing(
35       { :path => "/changeset/1", :method => :get },
36       { :controller => "browse", :action => "changeset", :id => "1" }
37     )
38     assert_routing(
39       { :path => "/note/1", :method => :get },
40       { :controller => "browse", :action => "note", :id => "1" }
41     )
42     assert_routing(
43       { :path => "/note/new", :method => :get },
44       { :controller => "browse", :action => "new_note" }
45     )
46   end
47
48   def test_read_relation
49     browse_check "relation", create(:relation).id, "browse/feature"
50   end
51
52   def test_read_relation_history
53     browse_check "relation_history", create(:relation, :with_history).id, "browse/history"
54   end
55
56   def test_read_way
57     browse_check "way", create(:way).id, "browse/feature"
58   end
59
60   def test_read_way_history
61     browse_check "way_history", create(:way, :with_history).id, "browse/history"
62   end
63
64   def test_read_node
65     browse_check "node", create(:node).id, "browse/feature"
66   end
67
68   def test_read_node_history
69     browse_check "node_history", create(:node, :with_history).id, "browse/history"
70   end
71
72   def test_read_changeset
73     private_changeset = create(:changeset, :user => create(:user, :data_public => false))
74     changeset = create(:changeset)
75     browse_check "changeset", private_changeset.id, "browse/changeset"
76     browse_check "changeset", changeset.id, "browse/changeset"
77   end
78
79   def test_read_changeset_hidden_comments
80     changeset = create(:changeset)
81     create_list(:changeset_comment, 3, :changeset => changeset)
82     create(:changeset_comment, :visible => false, :changeset => changeset)
83
84     browse_check "changeset", changeset.id, "browse/changeset"
85     assert_select "div.changeset-comments ul li", :count => 3
86
87     session[:user] = create(:moderator_user).id
88
89     browse_check "changeset", changeset.id, "browse/changeset"
90     assert_select "div.changeset-comments ul li", :count => 4
91   end
92
93   def test_read_note
94     open_note = create(:note_with_comments)
95
96     browse_check "note", open_note.id, "browse/note"
97   end
98
99   def test_read_hidden_note
100     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
101
102     get :note, :id => hidden_note_with_comment.id
103     assert_response :not_found
104     assert_template "browse/not_found"
105     assert_template :layout => "map"
106
107     xhr :get, :note, :id => hidden_note_with_comment.id
108     assert_response :not_found
109     assert_template "browse/not_found"
110     assert_template :layout => "xhr"
111
112     session[:user] = create(:moderator_user).id
113
114     browse_check "note", hidden_note_with_comment.id, "browse/note"
115   end
116
117   def test_read_note_hidden_comments
118     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
119       create(:note_comment, :note => note, :visible => false)
120     end
121
122     browse_check "note", note_with_hidden_comment.id, "browse/note"
123     assert_select "div.note-comments ul li", :count => 1
124
125     session[:user] = create(:moderator_user).id
126
127     browse_check "note", note_with_hidden_comment.id, "browse/note"
128     assert_select "div.note-comments ul li", :count => 2
129   end
130
131   ##
132   #  Methods to check redaction.
133   #
134   # note that these are presently highly reliant on the structure of the
135   # page for the selection tests, which doesn't work out particularly
136   # well if that structure changes. so... if you change the page layout
137   # then please make it more easily (and robustly) testable!
138   ##
139   def test_redacted_node
140     node = create(:node, :with_history, :deleted, :version => 2)
141     node_v1 = node.old_nodes.find_by(:version => 1)
142     node_v1.redact!(create(:redaction))
143
144     get :node, :id => node.id
145     assert_response :success
146     assert_template "feature"
147
148     # check that we don't show lat/lon for a redacted node.
149     assert_select ".browse-section", 1
150     assert_select ".browse-section.browse-node", 1
151     assert_select ".browse-section.browse-node .latitude", 0
152     assert_select ".browse-section.browse-node .longitude", 0
153   end
154
155   def test_redacted_node_history
156     node = create(:node, :with_history, :deleted, :version => 2)
157     node_v1 = node.old_nodes.find_by(:version => 1)
158     node_v1.redact!(create(:redaction))
159
160     get :node_history, :id => node.id
161     assert_response :success
162     assert_template "browse/history"
163
164     # there are 2 revisions of the redacted node, but only one
165     # should be showing details here.
166     assert_select ".browse-section", 2
167     assert_select ".browse-section.browse-redacted", 1
168     assert_select ".browse-section.browse-node", 1
169     assert_select ".browse-section.browse-node .latitude", 0
170     assert_select ".browse-section.browse-node .longitude", 0
171   end
172
173   def test_redacted_way_history
174     way = create(:way, :with_history, :version => 4)
175     way_v1 = way.old_ways.find_by(:version => 1)
176     way_v1.redact!(create(:redaction))
177     way_v3 = way.old_ways.find_by(:version => 3)
178     way_v3.redact!(create(:redaction))
179
180     get :way_history, :id => way.id
181     assert_response :success
182     assert_template "browse/history"
183
184     # there are 4 revisions of the redacted way, but only 2
185     # should be showing details here.
186     assert_select ".browse-section", 4
187     assert_select ".browse-section.browse-redacted", 2
188     assert_select ".browse-section.browse-way", 2
189   end
190
191   def test_redacted_relation_history
192     relation = create(:relation, :with_history, :version => 4)
193     relation_v1 = relation.old_relations.find_by(:version => 1)
194     relation_v1.redact!(create(:redaction))
195     relation_v3 = relation.old_relations.find_by(:version => 3)
196     relation_v3.redact!(create(:redaction))
197
198     get :relation_history, :id => relation.id
199     assert_response :success
200     assert_template "browse/history"
201
202     # there are 4 revisions of the redacted relation, but only 2
203     # should be showing details here.
204     assert_select ".browse-section", 4
205     assert_select ".browse-section.browse-redacted", 2
206     assert_select ".browse-section.browse-relation", 2
207   end
208
209   private
210
211   # This is a convenience method for most of the above checks
212   # First we check that when we don't have an id, it will correctly return a 404
213   # then we check that we get the correct 404 when a non-existant id is passed
214   # then we check that it will get a successful response, when we do pass an id
215   def browse_check(type, id, template)
216     assert_raise ActionController::UrlGenerationError do
217       get type
218     end
219
220     assert_raise ActionController::UrlGenerationError do
221       get type, :id => -10 # we won't have an id that's negative
222     end
223
224     get type, :id => 0
225     assert_response :not_found
226     assert_template "browse/not_found"
227     assert_template :layout => "map"
228
229     xhr :get, type, :id => 0
230     assert_response :not_found
231     assert_template "browse/not_found"
232     assert_template :layout => "xhr"
233
234     get type, :id => id
235     assert_response :success
236     assert_template template
237     assert_template :layout => "map"
238
239     xhr :get, type, :id => id
240     assert_response :success
241     assert_template template
242     assert_template :layout => "xhr"
243   end
244 end