]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Fix typos in assertion messages.
[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", relations(:visible_relation).relation_id, "browse/feature"
50   end
51
52   def test_read_relation_history
53     browse_check "relation_history", relations(:visible_relation).relation_id, "browse/history"
54   end
55
56   def test_read_way
57     browse_check "way", ways(:visible_way).way_id, "browse/feature"
58   end
59
60   def test_read_way_history
61     browse_check "way_history", ways(:visible_way).way_id, "browse/history"
62   end
63
64   def test_read_node
65     browse_check "node", nodes(:visible_node).node_id, "browse/feature"
66   end
67
68   def test_read_node_history
69     browse_check "node_history", nodes(:visible_node).node_id, "browse/history"
70   end
71
72   def test_read_changeset
73     browse_check "changeset", changesets(:normal_user_first_change).id, "browse/changeset"
74     browse_check "changeset", changesets(:public_user_first_change).id, "browse/changeset"
75   end
76
77   def test_read_changeset_hidden_comments
78     create_list(:changeset_comment, 3)
79     create(:changeset_comment, :visible => false)
80
81     browse_check "changeset", changesets(:normal_user_closed_change).id, "browse/changeset"
82     assert_select "div.changeset-comments ul li", :count => 3
83
84     session[:user] = create(:moderator_user).id
85
86     browse_check "changeset", changesets(:normal_user_closed_change).id, "browse/changeset"
87     assert_select "div.changeset-comments ul li", :count => 4
88   end
89
90   def test_read_note
91     open_note = create(:note_with_comments)
92
93     browse_check "note", open_note.id, "browse/note"
94   end
95
96   def test_read_hidden_note
97     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
98
99     get :note, :id => hidden_note_with_comment.id
100     assert_response :not_found
101     assert_template "browse/not_found"
102     assert_template :layout => "map"
103
104     xhr :get, :note, :id => hidden_note_with_comment.id
105     assert_response :not_found
106     assert_template "browse/not_found"
107     assert_template :layout => "xhr"
108
109     session[:user] = create(:moderator_user).id
110
111     browse_check "note", hidden_note_with_comment.id, "browse/note"
112   end
113
114   def test_read_note_hidden_comments
115     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
116       create(:note_comment, :note => note, :visible => false)
117     end
118
119     browse_check "note", note_with_hidden_comment.id, "browse/note"
120     assert_select "div.note-comments ul li", :count => 1
121
122     session[:user] = create(:moderator_user).id
123
124     browse_check "note", note_with_hidden_comment.id, "browse/note"
125     assert_select "div.note-comments ul li", :count => 2
126   end
127
128   ##
129   #  Methods to check redaction.
130   #
131   # note that these are presently highly reliant on the structure of the
132   # page for the selection tests, which doesn't work out particularly
133   # well if that structure changes. so... if you change the page layout
134   # then please make it more easily (and robustly) testable!
135   ##
136   def test_redacted_node
137     get :node, :id => current_nodes(:redacted_node).id
138     assert_response :success
139     assert_template "feature"
140
141     # check that we don't show lat/lon for a redacted node.
142     assert_select ".browse-section", 1
143     assert_select ".browse-section.browse-node", 1
144     assert_select ".browse-section.browse-node .latitude", 0
145     assert_select ".browse-section.browse-node .longitude", 0
146   end
147
148   def test_redacted_node_history
149     get :node_history, :id => nodes(:redacted_node_redacted_version).node_id
150     assert_response :success
151     assert_template "browse/history"
152
153     # there are 2 revisions of the redacted node, but only one
154     # should be showing details here.
155     assert_select ".browse-section", 2
156     assert_select ".browse-section.browse-redacted", 1
157     assert_select ".browse-section.browse-node", 1
158     assert_select ".browse-section.browse-node .latitude", 0
159     assert_select ".browse-section.browse-node .longitude", 0
160   end
161
162   def test_redacted_way_history
163     get :way_history, :id => ways(:way_with_redacted_versions_v1).way_id
164     assert_response :success
165     assert_template "browse/history"
166
167     # there are 4 revisions of the redacted way, but only 2
168     # should be showing details here.
169     assert_select ".browse-section", 4
170     assert_select ".browse-section.browse-redacted", 2
171     assert_select ".browse-section.browse-way", 2
172   end
173
174   def test_redacted_relation_history
175     get :relation_history, :id => relations(:relation_with_redacted_versions_v1).relation_id
176     assert_response :success
177     assert_template "browse/history"
178
179     # there are 4 revisions of the redacted relation, but only 2
180     # should be showing details here.
181     assert_select ".browse-section", 4
182     assert_select ".browse-section.browse-redacted", 2
183     assert_select ".browse-section.browse-relation", 2
184   end
185
186   private
187
188   # This is a convenience method for most of the above checks
189   # First we check that when we don't have an id, it will correctly return a 404
190   # then we check that we get the correct 404 when a non-existant id is passed
191   # then we check that it will get a successful response, when we do pass an id
192   def browse_check(type, id, template)
193     assert_raise ActionController::UrlGenerationError do
194       get type
195     end
196
197     assert_raise ActionController::UrlGenerationError do
198       get type, :id => -10 # we won't have an id that's negative
199     end
200
201     get type, :id => 0
202     assert_response :not_found
203     assert_template "browse/not_found"
204     assert_template :layout => "map"
205
206     xhr :get, type, :id => 0
207     assert_response :not_found
208     assert_template "browse/not_found"
209     assert_template :layout => "xhr"
210
211     get type, :id => id
212     assert_response :success
213     assert_template template
214     assert_template :layout => "map"
215
216     xhr :get, type, :id => id
217     assert_response :success
218     assert_template template
219     assert_template :layout => "xhr"
220   end
221 end