]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_relation_controller_test.rb
fa20e2fc953942375241a8ddbff429989df34952
[rails.git] / test / functional / old_relation_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_relation_controller'
3
4 class OldRelationControllerTest < 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/relation/1/history", :method => :get },
12       { :controller => "old_relation", :action => "history", :id => "1" }
13     )
14     assert_routing(
15       { :path => "/api/0.6/relation/1/2", :method => :get },
16       { :controller => "old_relation", :action => "version", :id => "1", :version => "2" }
17     )
18   end
19
20   # -------------------------------------
21   # Test reading old relations.
22   # -------------------------------------
23   def test_history
24     # check that a visible relations is returned properly
25     get :history, :id => relations(:visible_relation).relation_id
26     assert_response :success
27
28     # check chat a non-existent relations is not returned
29     get :history, :id => 0
30     assert_response :not_found
31   end
32
33   ##
34   # test the redaction of an old version of a relation, while not being
35   # authorised.
36   def test_redact_relation_unauthorised
37     do_redact_relation(relations(:relation_with_versions_v3),
38                        redactions(:example))
39     assert_response :unauthorized, "should need to be authenticated to redact."
40   end
41
42     ##
43   # test the redaction of an old version of a relation, while being 
44   # authorised as a normal user.
45   def test_redact_relation_normal_user
46     basic_authorization(users(:public_user).email, "test")
47
48     do_redact_relation(relations(:relation_with_versions_v3),
49                        redactions(:example))
50     assert_response :forbidden, "should need to be moderator to redact."
51   end
52
53   ##
54   # test that, even as moderator, the current version of a relation
55   # can't be redacted.
56   def test_redact_relation_current_version
57     basic_authorization(users(:moderator_user).email, "test")
58
59     do_redact_relation(relations(:relation_with_versions_v4),
60                        redactions(:example))
61     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
62   end    
63
64   ##
65   # test that redacted relations aren't visible, regardless of 
66   # authorisation except as moderator...
67   def test_version_redacted
68     relation = relations(:relation_with_redacted_versions_v2)
69
70     get :version, :id => relation.relation_id, :version => relation.version
71     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
72
73     # not even to a logged-in user
74     basic_authorization(users(:public_user).email, "test")
75     get :version, :id => relation.relation_id, :version => relation.version
76     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
77   end
78
79   ##
80   # test that redacted nodes aren't visible in the history
81   def test_history_redacted
82     relation = relations(:relation_with_redacted_versions_v2)
83
84     get :history, :id => relation.relation_id
85     assert_response :success, "Redaction shouldn't have stopped history working."
86     assert_select "osm relation[id=#{relation.relation_id}][version=#{relation.version}]", 0, "redacted relation #{relation.relation_id} version #{relation.version} shouldn't be present in the history."
87
88     # not even to a logged-in user
89     basic_authorization(users(:public_user).email, "test")
90     get :version, :id => relation.relation_id, :version => relation.version
91     get :history, :id => relation.relation_id
92     assert_response :success, "Redaction shouldn't have stopped history working."
93     assert_select "osm relation[id=#{relation.relation_id}][version=#{relation.version}]", 0, "redacted node #{relation.relation_id} version #{relation.version} shouldn't be present in the history, even when logged in."
94   end
95
96   ##
97   # test the redaction of an old version of a relation, while being 
98   # authorised as a moderator.
99   def test_redact_relation_moderator
100     relation = relations(:relation_with_versions_v3)
101     basic_authorization(users(:moderator_user).email, "test")
102
103     do_redact_relation(relation, redactions(:example))
104     assert_response :success, "should be OK to redact old version as moderator."
105
106     # check moderator can still see the redacted data, when passing
107     # the appropriate flag
108     get :version, :id => relation.relation_id, :version => relation.version
109     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
110     get :version, :id => relation.relation_id, :version => relation.version, :show_redactions => 'true'
111     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
112     
113     # and when accessed via history
114     get :history, :id => relation.relation_id
115     assert_response :success, "Redaction shouldn't have stopped history working."
116     assert_select "osm relation[id=#{relation.relation_id}][version=#{relation.version}]", 0, "relation #{relation.relation_id} version #{relation.version} should not be present in the history for moderators when not passing flag."
117     get :history, :id => relation.relation_id, :show_redactions => 'true'
118     assert_response :success, "Redaction shouldn't have stopped history working."
119     assert_select "osm relation[id=#{relation.relation_id}][version=#{relation.version}]", 1, "relation #{relation.relation_id} version #{relation.version} should still be present in the history for moderators when passing flag."
120   end
121
122   # testing that if the moderator drops auth, he can't see the
123   # redacted stuff any more.
124   def test_redact_relation_is_redacted
125     relation = relations(:relation_with_versions_v3)
126     basic_authorization(users(:moderator_user).email, "test")
127
128     do_redact_relation(relation, redactions(:example))
129     assert_response :success, "should be OK to redact old version as moderator."
130
131     # re-auth as non-moderator
132     basic_authorization(users(:public_user).email, "test")
133
134     # check can't see the redacted data
135     get :version, :id => relation.relation_id, :version => relation.version
136     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
137     
138     # and when accessed via history
139     get :history, :id => relation.relation_id
140     assert_response :success, "Redaction shouldn't have stopped history working."
141     assert_select "osm relation[id=#{relation.relation_id}][version=#{relation.version}]", 0, "redacted relation #{relation.relation_id} version #{relation.version} shouldn't be present in the history."
142   end
143
144   ##
145   # check that the current version of a relation is equivalent to the
146   # version which we're getting from the versions call.
147   def check_current_version(relation_id)
148     # get the current version
149     current_relation = with_controller(RelationController.new) do
150       get :read, :id => relation_id
151       assert_response :success, "can't get current relation #{relation_id}"
152       Relation.from_xml(@response.body)
153     end
154     assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
155
156     # get the "old" version of the relation from the version method
157     get :version, :id => relation_id, :version => current_relation.version
158     assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
159     old_relation = Relation.from_xml(@response.body)
160
161     # check that the relations are identical
162     assert_relations_are_equal current_relation, old_relation
163   end
164
165   ##
166   # look at all the versions of the relation in the history and get each version from
167   # the versions call. check that they're the same.
168   def check_history_equals_versions(relation_id)
169     get :history, :id => relation_id
170     assert_response :success, "can't get relation #{relation_id} from API"
171     history_doc = XML::Parser.string(@response.body).parse
172     assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
173
174     history_doc.find("//osm/relation").each do |relation_doc|
175       history_relation = Relation.from_xml_node(relation_doc)
176       assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
177
178       get :version, :id => relation_id, :version => history_relation.version
179       assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
180       version_relation = Relation.from_xml(@response.body)
181       assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
182       
183       assert_relations_are_equal history_relation, version_relation
184     end
185   end
186
187   def do_redact_relation(relation, redaction)
188     get :version, :id => relation.relation_id, :version => relation.version
189     assert_response :success, "should be able to get version #{relation.version} of node #{relation.relation_id}."
190     
191     # now redact it
192     post :redact, :id => relation.relation_id, :version => relation.version, :redaction => redaction.id
193   end
194 end