1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_way_controller'
4 class OldWayControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/way/1/history", :method => :get },
12 { :controller => "old_way", :action => "history", :id => "1" }
15 { :path => "/api/0.6/way/1/2", :method => :get },
16 { :controller => "old_way", :action => "version", :id => "1", :version => "2" }
20 # -------------------------------------
21 # Test reading old ways.
22 # -------------------------------------
24 def test_history_visible
25 # check that a visible way is returned properly
26 get :history, :id => ways(:visible_way).way_id
27 assert_response :success
30 def test_history_invisible
31 # check that an invisible way's history is returned properly
32 get :history, :id => ways(:invisible_way).way_id
33 assert_response :success
36 def test_history_invalid
37 # check chat a non-existent way is not returned
38 get :history, :id => 0
39 assert_response :not_found
43 # check that we can retrieve versions of a way
45 check_current_version(current_ways(:visible_way).id)
46 check_current_version(current_ways(:used_way).id)
47 check_current_version(current_ways(:way_with_versions).id)
51 # check that returned history is the same as getting all
52 # versions of a way from the api.
53 def test_history_equals_versions
54 check_history_equals_versions(current_ways(:visible_way).id)
55 check_history_equals_versions(current_ways(:used_way).id)
56 check_history_equals_versions(current_ways(:way_with_versions).id)
60 # test the redaction of an old version of a way, while not being
62 def test_redact_way_unauthorised
63 do_redact_way(ways(:way_with_versions_v3),
65 assert_response :unauthorized, "should need to be authenticated to redact."
69 # test the redaction of an old version of a way, while being
70 # authorised as a normal user.
71 def test_redact_way_normal_user
72 basic_authorization(users(:public_user).email, "test")
74 do_redact_way(ways(:way_with_versions_v3),
76 assert_response :forbidden, "should need to be moderator to redact."
80 # test that, even as moderator, the current version of a way
82 def test_redact_way_current_version
83 basic_authorization(users(:moderator_user).email, "test")
85 do_redact_way(ways(:way_with_versions_v4),
87 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
91 # test that redacted ways aren't visible, regardless of
92 # authorisation except as moderator...
93 def test_version_redacted
94 way = ways(:way_with_redacted_versions_v2)
96 get :version, :id => way.way_id, :version => way.version
97 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
99 # not even to a logged-in user
100 basic_authorization(users(:public_user).email, "test")
101 get :version, :id => way.way_id, :version => way.version
102 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
106 # test that redacted nodes aren't visible in the history
107 def test_history_redacted
108 way = ways(:way_with_redacted_versions_v2)
110 get :history, :id => way.way_id
111 assert_response :success, "Redaction shouldn't have stopped history working."
112 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history."
114 # not even to a logged-in user
115 basic_authorization(users(:public_user).email, "test")
116 get :version, :id => way.way_id, :version => way.version
117 get :history, :id => way.way_id
118 assert_response :success, "Redaction shouldn't have stopped history working."
119 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 0, "redacted node #{way.way_id} version #{way.version} shouldn't be present in the history, even when logged in."
123 # test the redaction of an old version of a way, while being
124 # authorised as a moderator.
125 def test_redact_way_moderator
126 way = ways(:way_with_versions_v3)
127 basic_authorization(users(:moderator_user).email, "test")
129 do_redact_way(way, redactions(:example))
130 assert_response :success, "should be OK to redact old version as moderator."
132 # check moderator can still see the redacted data, when passing
133 # the appropriate flag
134 get :version, :id => way.way_id, :version => way.version
135 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
136 get :version, :id => way.way_id, :version => way.version, :show_redactions => 'true'
137 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
139 # and when accessed via history
140 get :history, :id => way.way_id
141 assert_response :success, "Redaction shouldn't have stopped history working."
142 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 0, "way #{way.way_id} version #{way.version} should not be present in the history for moderators when not passing flag."
143 get :history, :id => way.way_id, :show_redactions => 'true'
144 assert_response :success, "Redaction shouldn't have stopped history working."
145 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators when passing flag."
148 # testing that if the moderator drops auth, he can't see the
149 # redacted stuff any more.
150 def test_redact_way_is_redacted
151 way = ways(:way_with_versions_v3)
152 basic_authorization(users(:moderator_user).email, "test")
154 do_redact_way(way, redactions(:example))
155 assert_response :success, "should be OK to redact old version as moderator."
157 # re-auth as non-moderator
158 basic_authorization(users(:public_user).email, "test")
160 # check can't see the redacted data
161 get :version, :id => way.way_id, :version => way.version
162 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
164 # and when accessed via history
165 get :history, :id => way.way_id
166 assert_response :success, "Redaction shouldn't have stopped history working."
167 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history."
171 # check that the current version of a way is equivalent to the
172 # version which we're getting from the versions call.
173 def check_current_version(way_id)
174 # get the current version
175 current_way = with_controller(WayController.new) do
176 get :read, :id => way_id
177 assert_response :success, "can't get current way #{way_id}"
178 Way.from_xml(@response.body)
180 assert_not_nil current_way, "getting way #{way_id} returned nil"
182 # get the "old" version of the way from the version method
183 get :version, :id => way_id, :version => current_way.version
184 assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
185 old_way = Way.from_xml(@response.body)
187 # check that the ways are identical
188 assert_ways_are_equal current_way, old_way
192 # look at all the versions of the way in the history and get each version from
193 # the versions call. check that they're the same.
194 def check_history_equals_versions(way_id)
195 get :history, :id => way_id
196 assert_response :success, "can't get way #{way_id} from API"
197 history_doc = XML::Parser.string(@response.body).parse
198 assert_not_nil history_doc, "parsing way #{way_id} history failed"
200 history_doc.find("//osm/way").each do |way_doc|
201 history_way = Way.from_xml_node(way_doc)
202 assert_not_nil history_way, "parsing way #{way_id} version failed"
204 get :version, :id => way_id, :version => history_way.version
205 assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
206 version_way = Way.from_xml(@response.body)
207 assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
209 assert_ways_are_equal history_way, version_way
213 def do_redact_way(way, redaction)
214 get :version, :id => way.way_id, :version => way.version
215 assert_response :success, "should be able to get version #{way.version} of node #{way.way_id}."
218 post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id