]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_way_controller_test.rb
84f0e04b0bb503c9c605741982faca2175383a76
[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_v3),
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_v3),
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 :bad_request, "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_v3)
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, 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."
138     
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."
146   end
147
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")
153
154     do_redact_way(way, redactions(:example))
155     assert_response :success, "should be OK to redact old version as moderator."
156
157     # re-auth as non-moderator
158     basic_authorization(users(:public_user).email, "test")
159
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."
163     
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."
168   end
169
170   ##
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)
179     end
180     assert_not_nil current_way, "getting way #{way_id} returned nil"
181
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)
186
187     # check that the ways are identical
188     assert_ways_are_equal current_way, old_way
189   end
190
191   ##
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"
199
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"
203
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}"
208       
209       assert_ways_are_equal history_way, version_way
210     end
211   end
212
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}."
216     
217     # now redact it
218     post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id
219   end
220
221 end