]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_way_controller_test.rb
Copy the redaction code from nodes to ways
[rails.git] / test / functional / old_way_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_way_controller'
3
4 class OldWayControllerTest < 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 => "/api/0.6/way/1/history", :method => :get },
12       { :controller => "old_way", :action => "history", :id => "1" }
13     )
14     assert_routing(
15       { :path => "/api/0.6/way/1/2", :method => :get },
16       { :controller => "old_way", :action => "version", :id => "1", :version => "2" }
17     )
18   end
19
20   # -------------------------------------
21   # Test reading old ways.
22   # -------------------------------------
23
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
28   end
29   
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
34   end
35   
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
40   end
41   
42   ##
43   # check that we can retrieve versions of a way
44   def test_version
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)
48   end
49
50   ##
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)
57   end
58
59   ##
60   # test the redaction of an old version of a way, while not being
61   # authorised.
62   def test_redact_way_unauthorised
63     do_redact_way(ways(:way_with_versions),
64                    redactions(:example))
65     assert_response :unauthorized, "should need to be authenticated to redact."
66   end
67
68     ##
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")
73
74     do_redact_way(ways(:way_with_versions),
75                    redactions(:example))
76     assert_response :forbidden, "should need to be moderator to redact."
77   end
78
79   ##
80   # test that, even as moderator, the current version of a way
81   # can't be redacted.
82   def test_redact_way_current_version
83     basic_authorization(users(:moderator_user).email, "test")
84
85     do_redact_way(ways(:way_with_versions_v4),
86                    redactions(:example))
87     assert_response :forbidden, "shouldn't be OK to redact current version as moderator."
88   end    
89
90   ##
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)
95
96     get :version, :id => way.way_id, :version => way.version
97     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
98
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."
103   end
104
105   ##
106   # test that redacted nodes aren't visible in the history
107   def test_history_redacted
108     way = ways(:way_with_redacted_versions_v2)
109
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."
113
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."
120   end
121
122   ##
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)
127     basic_authorization(users(:moderator_user).email, "test")
128
129     do_redact_way(way, redactions(:example))
130     assert_response :success, "should be OK to redact old version as moderator."
131
132     # check moderator can still see the redacted data
133     get :version, :id => way.way_id, :version => way.version
134     assert_response :success, "After redaction, node should not be gone for moderator."
135     
136     # and when accessed via history
137     get :history, :id => way.way_id
138     assert_response :success, "Redaction shouldn't have stopped history working."
139     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."
140   end
141
142   # testing that if the moderator drops auth, he can't see the
143   # redacted stuff any more.
144   def test_redact_way_is_redacted
145     way = ways(:way_with_versions)
146     basic_authorization(users(:moderator_user).email, "test")
147
148     do_redact_way(way, redactions(:example))
149     assert_response :success, "should be OK to redact old version as moderator."
150
151     # re-auth as non-moderator
152     basic_authorization(users(:public_user).email, "test")
153
154     # check can't see the redacted data
155     get :version, :id => way.way_id, :version => way.version
156     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
157     
158     # and when accessed via history
159     get :version, :id => way.node_id, :version => way.version
160     get :history, :id => way.node_id
161     assert_response :success, "Redaction shouldn't have stopped history working."
162     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."
163   end
164
165   ##
166   # check that the current version of a way is equivalent to the
167   # version which we're getting from the versions call.
168   def check_current_version(way_id)
169     # get the current version
170     current_way = with_controller(WayController.new) do
171       get :read, :id => way_id
172       assert_response :success, "can't get current way #{way_id}"
173       Way.from_xml(@response.body)
174     end
175     assert_not_nil current_way, "getting way #{way_id} returned nil"
176
177     # get the "old" version of the way from the version method
178     get :version, :id => way_id, :version => current_way.version
179     assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
180     old_way = Way.from_xml(@response.body)
181
182     # check that the ways are identical
183     assert_ways_are_equal current_way, old_way
184   end
185
186   ##
187   # look at all the versions of the way in the history and get each version from
188   # the versions call. check that they're the same.
189   def check_history_equals_versions(way_id)
190     get :history, :id => way_id
191     assert_response :success, "can't get way #{way_id} from API"
192     history_doc = XML::Parser.string(@response.body).parse
193     assert_not_nil history_doc, "parsing way #{way_id} history failed"
194
195     history_doc.find("//osm/way").each do |way_doc|
196       history_way = Way.from_xml_node(way_doc)
197       assert_not_nil history_way, "parsing way #{way_id} version failed"
198
199       get :version, :id => way_id, :version => history_way.version
200       assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
201       version_way = Way.from_xml(@response.body)
202       assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
203       
204       assert_ways_are_equal history_way, version_way
205     end
206   end
207
208   def do_redact_way(way, redaction)
209     get :version, :id => way.way_id, :version => way.version
210     assert_response :success, "should be able to get version #{way.version} of node #{way.way_id}."
211     
212     # now redact it
213     post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id
214   end
215
216 end