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