2 require "old_relation_controller"
 
   4 class OldRelationControllerTest < ActionController::TestCase
 
   8   # test all routes which lead to this controller
 
  11       { :path => "/api/0.6/relation/1/history", :method => :get },
 
  12       { :controller => "old_relation", :action => "history", :id => "1" }
 
  15       { :path => "/api/0.6/relation/1/2", :method => :get },
 
  16       { :controller => "old_relation", :action => "version", :id => "1", :version => "2" }
 
  19       { :path => "/api/0.6/relation/1/2/redact", :method => :post },
 
  20       { :controller => "old_relation", :action => "redact", :id => "1", :version => "2" }
 
  24   # -------------------------------------
 
  25   # Test reading old relations.
 
  26   # -------------------------------------
 
  28     # check that a visible relations is returned properly
 
  29     get :history, :id => relations(:visible_relation).relation_id
 
  30     assert_response :success
 
  32     # check chat a non-existent relations is not returned
 
  33     get :history, :id => 0
 
  34     assert_response :not_found
 
  38   # test the redaction of an old version of a relation, while not being
 
  40   def test_redact_relation_unauthorised
 
  41     do_redact_relation(relations(:relation_with_versions_v3),
 
  43     assert_response :unauthorized, "should need to be authenticated to redact."
 
  47   # test the redaction of an old version of a relation, while being
 
  48   # authorised as a normal user.
 
  49   def test_redact_relation_normal_user
 
  50     basic_authorization(create(:user).email, "test")
 
  52     do_redact_relation(relations(:relation_with_versions_v3),
 
  54     assert_response :forbidden, "should need to be moderator to redact."
 
  58   # test that, even as moderator, the current version of a relation
 
  60   def test_redact_relation_current_version
 
  61     basic_authorization(create(:moderator_user).email, "test")
 
  63     do_redact_relation(relations(:relation_with_versions_v4),
 
  65     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
 
  69   # test that redacted relations aren't visible, regardless of
 
  70   # authorisation except as moderator...
 
  71   def test_version_redacted
 
  72     relation = relations(:relation_with_redacted_versions_v2)
 
  74     get :version, :id => relation.relation_id, :version => relation.version
 
  75     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
 
  77     # not even to a logged-in user
 
  78     basic_authorization(users(:public_user).email, "test")
 
  79     get :version, :id => relation.relation_id, :version => relation.version
 
  80     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
 
  84   # test that redacted nodes aren't visible in the history
 
  85   def test_history_redacted
 
  86     relation = relations(:relation_with_redacted_versions_v2)
 
  88     get :history, :id => relation.relation_id
 
  89     assert_response :success, "Redaction shouldn't have stopped history working."
 
  90     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."
 
  92     # not even to a logged-in user
 
  93     basic_authorization(users(:public_user).email, "test")
 
  94     get :version, :id => relation.relation_id, :version => relation.version
 
  95     get :history, :id => relation.relation_id
 
  96     assert_response :success, "Redaction shouldn't have stopped history working."
 
  97     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."
 
 101   # test the redaction of an old version of a relation, while being
 
 102   # authorised as a moderator.
 
 103   def test_redact_relation_moderator
 
 104     relation = relations(:relation_with_versions_v3)
 
 105     basic_authorization(create(:moderator_user).email, "test")
 
 107     do_redact_relation(relation, create(:redaction))
 
 108     assert_response :success, "should be OK to redact old version as moderator."
 
 110     # check moderator can still see the redacted data, when passing
 
 111     # the appropriate flag
 
 112     get :version, :id => relation.relation_id, :version => relation.version
 
 113     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
 
 114     get :version, :id => relation.relation_id, :version => relation.version, :show_redactions => "true"
 
 115     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
 
 117     # and when accessed via history
 
 118     get :history, :id => relation.relation_id
 
 119     assert_response :success, "Redaction shouldn't have stopped history working."
 
 120     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."
 
 121     get :history, :id => relation.relation_id, :show_redactions => "true"
 
 122     assert_response :success, "Redaction shouldn't have stopped history working."
 
 123     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."
 
 126   # testing that if the moderator drops auth, he can't see the
 
 127   # redacted stuff any more.
 
 128   def test_redact_relation_is_redacted
 
 129     relation = relations(:relation_with_versions_v3)
 
 130     basic_authorization(create(:moderator_user).email, "test")
 
 132     do_redact_relation(relation, create(:redaction))
 
 133     assert_response :success, "should be OK to redact old version as moderator."
 
 135     # re-auth as non-moderator
 
 136     basic_authorization(users(:public_user).email, "test")
 
 138     # check can't see the redacted data
 
 139     get :version, :id => relation.relation_id, :version => relation.version
 
 140     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
 
 142     # and when accessed via history
 
 143     get :history, :id => relation.relation_id
 
 144     assert_response :success, "Redaction shouldn't have stopped history working."
 
 145     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."
 
 149   # test the unredaction of an old version of a relation, while not being
 
 151   def test_unredact_relation_unauthorised
 
 152     relation = relations(:relation_with_redacted_versions_v3)
 
 154     post :redact, :id => relation.relation_id, :version => relation.version
 
 155     assert_response :unauthorized, "should need to be authenticated to unredact."
 
 159   # test the unredaction of an old version of a relation, while being
 
 160   # authorised as a normal user.
 
 161   def test_unredact_relation_normal_user
 
 162     relation = relations(:relation_with_redacted_versions_v3)
 
 163     basic_authorization(users(:public_user).email, "test")
 
 165     post :redact, :id => relation.relation_id, :version => relation.version
 
 166     assert_response :forbidden, "should need to be moderator to unredact."
 
 170   # test the unredaction of an old version of a relation, while being
 
 171   # authorised as a moderator.
 
 172   def test_unredact_relation_moderator
 
 173     relation = relations(:relation_with_redacted_versions_v3)
 
 174     basic_authorization(users(:moderator_user).email, "test")
 
 176     post :redact, :id => relation.relation_id, :version => relation.version
 
 177     assert_response :success, "should be OK to unredact old version as moderator."
 
 179     # check moderator can still see the redacted data, without passing
 
 180     # the appropriate flag
 
 181     get :version, :id => relation.relation_id, :version => relation.version
 
 182     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
 
 184     # and when accessed via history
 
 185     get :history, :id => relation.relation_id
 
 186     assert_response :success, "Redaction shouldn't have stopped history working."
 
 187     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."
 
 189     basic_authorization(users(:normal_user).email, "test")
 
 191     # check normal user can now see the redacted data
 
 192     get :version, :id => relation.relation_id, :version => relation.version
 
 193     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
 
 195     # and when accessed via history
 
 196     get :history, :id => relation.relation_id
 
 197     assert_response :success, "Redaction shouldn't have stopped history working."
 
 198     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."
 
 204   # check that the current version of a relation is equivalent to the
 
 205   # version which we're getting from the versions call.
 
 206   def check_current_version(relation_id)
 
 207     # get the current version
 
 208     current_relation = with_controller(RelationController.new) do
 
 209       get :read, :id => relation_id
 
 210       assert_response :success, "can't get current relation #{relation_id}"
 
 211       Relation.from_xml(@response.body)
 
 213     assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
 
 215     # get the "old" version of the relation from the version method
 
 216     get :version, :id => relation_id, :version => current_relation.version
 
 217     assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
 
 218     old_relation = Relation.from_xml(@response.body)
 
 220     # check that the relations are identical
 
 221     assert_relations_are_equal current_relation, old_relation
 
 225   # look at all the versions of the relation in the history and get each version from
 
 226   # the versions call. check that they're the same.
 
 227   def check_history_equals_versions(relation_id)
 
 228     get :history, :id => relation_id
 
 229     assert_response :success, "can't get relation #{relation_id} from API"
 
 230     history_doc = XML::Parser.string(@response.body).parse
 
 231     assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
 
 233     history_doc.find("//osm/relation").each do |relation_doc|
 
 234       history_relation = Relation.from_xml_node(relation_doc)
 
 235       assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
 
 237       get :version, :id => relation_id, :version => history_relation.version
 
 238       assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
 
 239       version_relation = Relation.from_xml(@response.body)
 
 240       assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
 
 242       assert_relations_are_equal history_relation, version_relation
 
 246   def do_redact_relation(relation, redaction)
 
 247     get :version, :id => relation.relation_id, :version => relation.version
 
 248     assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
 
 251     post :redact, :id => relation.relation_id, :version => relation.version, :redaction => redaction.id