1 # frozen_string_literal: true
 
   6   class OldRelationsControllerTest < ActionDispatch::IntegrationTest
 
   8     # test all routes which lead to this controller
 
  11         { :path => "/api/0.6/relation/1/history", :method => :get },
 
  12         { :controller => "api/old_relations", :action => "index", :relation_id => "1" }
 
  15         { :path => "/api/0.6/relation/1/history.json", :method => :get },
 
  16         { :controller => "api/old_relations", :action => "index", :relation_id => "1", :format => "json" }
 
  19         { :path => "/api/0.6/relation/1/2", :method => :get },
 
  20         { :controller => "api/old_relations", :action => "show", :relation_id => "1", :version => "2" }
 
  23         { :path => "/api/0.6/relation/1/2.json", :method => :get },
 
  24         { :controller => "api/old_relations", :action => "show", :relation_id => "1", :version => "2", :format => "json" }
 
  29     # check that a visible relations is returned properly
 
  31       relation = create(:relation, :with_history, :version => 2)
 
  33       get api_relation_versions_path(relation)
 
  35       assert_response :success
 
  36       assert_dom "osm:root", 1 do
 
  37         assert_dom "> relation", 2 do |dom_relations|
 
  38           assert_dom dom_relations[0], "> @id", relation.id.to_s
 
  39           assert_dom dom_relations[0], "> @version", "1"
 
  41           assert_dom dom_relations[1], "> @id", relation.id.to_s
 
  42           assert_dom dom_relations[1], "> @version", "2"
 
  48     # check that a non-existent relations is not returned
 
  49     def test_index_invalid
 
  50       get api_relation_versions_path(0)
 
  51       assert_response :not_found
 
  55     # test that redacted relations aren't visible in the history
 
  56     def test_index_redacted_unauthorised
 
  57       relation = create(:relation, :with_history, :version => 2)
 
  58       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
 
  60       get api_relation_versions_path(relation)
 
  62       assert_response :success, "Redaction shouldn't have stopped history working."
 
  63       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
 
  64                  "redacted relation #{relation.id} version 1 shouldn't be present in the history."
 
  66       get api_relation_versions_path(relation, :show_redactions => "true")
 
  68       assert_response :success, "Redaction shouldn't have stopped history working."
 
  69       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
 
  70                  "redacted relation #{relation.id} version 1 shouldn't be present in the history when passing flag."
 
  73     def test_index_redacted_normal_user
 
  74       relation = create(:relation, :with_history, :version => 2)
 
  75       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
 
  77       get api_relation_versions_path(relation), :headers => bearer_authorization_header
 
  79       assert_response :success, "Redaction shouldn't have stopped history working."
 
  80       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
 
  81                  "redacted relation #{relation.id} version 1 shouldn't be present in the history, even when logged in."
 
  83       get api_relation_versions_path(relation, :show_redactions => "true"), :headers => bearer_authorization_header
 
  85       assert_response :success, "Redaction shouldn't have stopped history working."
 
  86       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
 
  87                  "redacted relation #{relation.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
 
  90     def test_index_redacted_moderator
 
  91       relation = create(:relation, :with_history, :version => 2)
 
  92       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
 
  93       auth_header = bearer_authorization_header create(:moderator_user)
 
  95       get api_relation_versions_path(relation), :headers => auth_header
 
  97       assert_response :success, "Redaction shouldn't have stopped history working."
 
  98       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
 
  99                  "relation #{relation.id} version 1 should not be present in the history for moderators when not passing flag."
 
 101       get api_relation_versions_path(relation, :show_redactions => "true"), :headers => auth_header
 
 103       assert_response :success, "Redaction shouldn't have stopped history working."
 
 104       assert_dom "osm relation[id='#{relation.id}'][version='1']", 1,
 
 105                  "relation #{relation.id} version 1 should still be present in the history for moderators when passing flag."
 
 109       relation = create(:relation, :with_history, :version => 2)
 
 110       create(:old_relation_tag, :old_relation => relation.old_relations[0], :k => "k1", :v => "v1")
 
 111       create(:old_relation_tag, :old_relation => relation.old_relations[1], :k => "k2", :v => "v2")
 
 113       get api_relation_version_path(relation, 1)
 
 115       assert_response :success
 
 116       assert_dom "osm:root", 1 do
 
 117         assert_dom "> relation", 1 do
 
 118           assert_dom "> @id", relation.id.to_s
 
 119           assert_dom "> @version", "1"
 
 120           assert_dom "> tag", 1 do
 
 121             assert_dom "> @k", "k1"
 
 122             assert_dom "> @v", "v1"
 
 127       get api_relation_version_path(relation, 2)
 
 129       assert_response :success
 
 130       assert_dom "osm:root", 1 do
 
 131         assert_dom "> relation", 1 do
 
 132           assert_dom "> @id", relation.id.to_s
 
 133           assert_dom "> @version", "2"
 
 134           assert_dom "> tag", 1 do
 
 135             assert_dom "> @k", "k2"
 
 136             assert_dom "> @v", "v2"
 
 143     # test that redacted relations aren't visible, regardless of
 
 144     # authorisation except as moderator...
 
 145     def test_show_redacted_unauthorised
 
 146       relation = create(:relation, :with_history, :version => 2)
 
 147       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
 
 149       get api_relation_version_path(relation, 1)
 
 151       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
 
 153       get api_relation_version_path(relation, 1, :show_redactions => "true")
 
 155       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API when passing flag."
 
 158     def test_show_redacted_normal_user
 
 159       relation = create(:relation, :with_history, :version => 2)
 
 160       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
 
 162       get api_relation_version_path(relation, 1), :headers => bearer_authorization_header
 
 164       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
 
 166       get api_relation_version_path(relation, 1, :show_redactions => "true"), :headers => bearer_authorization_header
 
 168       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in and passing flag."
 
 171     def test_show_redacted_moderator
 
 172       relation = create(:relation, :with_history, :version => 2)
 
 173       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
 
 174       auth_header = bearer_authorization_header create(:moderator_user)
 
 176       get api_relation_version_path(relation, 1), :headers => auth_header
 
 178       assert_response :forbidden, "Redacted relation should be gone for moderator, when flag not passed."
 
 180       get api_relation_version_path(relation, 1, :show_redactions => "true"), :headers => auth_header
 
 182       assert_response :success, "Redacted relation should not be gone for moderator, when flag passed."