5     class RedactionsControllerTest < ActionDispatch::IntegrationTest
 
   7       # test all routes which lead to this controller
 
  10           { :path => "/api/0.6/node/1/2/redaction", :method => :post },
 
  11           { :controller => "api/old_nodes/redactions", :action => "create", :node_id => "1", :version => "2" }
 
  14           { :path => "/api/0.6/node/1/2/redaction", :method => :delete },
 
  15           { :controller => "api/old_nodes/redactions", :action => "destroy", :node_id => "1", :version => "2" }
 
  19           { :controller => "api/old_nodes/redactions", :action => "create", :node_id => "1", :version => "2", :allow_delete => true },
 
  20           { :path => "/api/0.6/node/1/2/redact", :method => :post }
 
  25       # test that, even as moderator, the current version of a node
 
  27       def test_create_on_current_version
 
  28         node = create(:node, :with_history, :version => 2)
 
  29         old_node = node.old_nodes.find_by(:version => 2)
 
  30         redaction = create(:redaction)
 
  31         auth_header = bearer_authorization_header create(:moderator_user)
 
  33         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
 
  35         assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
 
  36         assert_nil old_node.reload.redaction
 
  39       def test_create_without_redaction_id
 
  40         node = create(:node, :with_history, :version => 2)
 
  41         old_node = node.old_nodes.find_by(:version => 1)
 
  42         auth_header = bearer_authorization_header create(:moderator_user)
 
  44         post api_node_version_redaction_path(*old_node.id), :headers => auth_header
 
  46         assert_response :bad_request, "should need redaction ID to redact."
 
  47         assert_nil old_node.reload.redaction
 
  51       # test the redaction of an old version of a node, while not being
 
  53       def test_create_by_unauthorised
 
  54         node = create(:node, :with_history, :version => 2)
 
  55         old_node = node.old_nodes.find_by(:version => 1)
 
  56         redaction = create(:redaction)
 
  58         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }
 
  60         assert_response :unauthorized, "should need to be authenticated to redact."
 
  61         assert_nil old_node.reload.redaction
 
  64       def test_create_by_normal_user_without_write_redactions_scope
 
  65         node = create(:node, :with_history, :version => 2)
 
  66         old_node = node.old_nodes.find_by(:version => 1)
 
  67         redaction = create(:redaction)
 
  68         auth_header = bearer_authorization_header create(:user), :scopes => %w[read_prefs write_api]
 
  70         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
 
  72         assert_response :forbidden, "should need to be moderator to redact."
 
  73         assert_nil old_node.reload.redaction
 
  76       def test_create_by_normal_user_with_write_redactions_scope
 
  77         node = create(:node, :with_history, :version => 2)
 
  78         old_node = node.old_nodes.find_by(:version => 1)
 
  79         redaction = create(:redaction)
 
  80         auth_header = bearer_authorization_header create(:user), :scopes => %w[write_redactions]
 
  82         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
 
  84         assert_response :forbidden, "should need to be moderator to redact."
 
  85         assert_nil old_node.reload.redaction
 
  88       def test_create_by_moderator_without_write_redactions_scope
 
  89         node = create(:node, :with_history, :version => 2)
 
  90         old_node = node.old_nodes.find_by(:version => 1)
 
  91         redaction = create(:redaction)
 
  92         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs write_api]
 
  94         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
 
  96         assert_response :forbidden, "should need to have write_redactions scope to redact."
 
  97         assert_nil old_node.reload.redaction
 
 100       def test_create_by_moderator_with_write_redactions_scope
 
 101         node = create(:node, :with_history, :version => 2)
 
 102         old_node = node.old_nodes.find_by(:version => 1)
 
 103         redaction = create(:redaction)
 
 104         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_redactions]
 
 106         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
 
 108         assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
 
 109         assert_equal redaction, old_node.reload.redaction
 
 113       # test the unredaction of an old version of a node, while not being
 
 115       def test_destroy_by_unauthorised
 
 116         node = create(:node, :with_history, :version => 2)
 
 117         old_node = node.old_nodes.find_by(:version => 1)
 
 118         redaction = create(:redaction)
 
 119         old_node.redact!(redaction)
 
 121         delete api_node_version_redaction_path(*old_node.id)
 
 123         assert_response :unauthorized, "should need to be authenticated to unredact."
 
 124         assert_equal redaction, old_node.reload.redaction
 
 128       # test the unredaction of an old version of a node, while being
 
 129       # authorised as a normal user.
 
 130       def test_destroy_by_normal_user
 
 131         node = create(:node, :with_history, :version => 2)
 
 132         old_node = node.old_nodes.find_by(:version => 1)
 
 133         redaction = create(:redaction)
 
 134         old_node.redact!(redaction)
 
 135         auth_header = bearer_authorization_header
 
 137         delete api_node_version_redaction_path(*old_node.id), :headers => auth_header
 
 139         assert_response :forbidden, "should need to be moderator to unredact."
 
 140         assert_equal redaction, old_node.reload.redaction
 
 144       # test the unredaction of an old version of a node, while being
 
 145       # authorised as a moderator.
 
 146       def test_destroy_by_moderator
 
 147         node = create(:node, :with_history, :version => 2)
 
 148         old_node = node.old_nodes.find_by(:version => 1)
 
 149         old_node.redact!(create(:redaction))
 
 150         auth_header = bearer_authorization_header create(:moderator_user)
 
 152         delete api_node_version_redaction_path(*old_node.id), :headers => auth_header
 
 154         assert_response :success, "should be OK to unredact old version as moderator."
 
 155         assert_nil old_node.reload.redaction
 
 158       def test_destroy_at_legacy_route
 
 159         node = create(:node, :with_history, :version => 2)
 
 160         old_node = node.old_nodes.find_by(:version => 1)
 
 161         old_node.redact!(create(:redaction))
 
 162         auth_header = bearer_authorization_header create(:moderator_user)
 
 164         post "/api/0.6/node/#{old_node.node_id}/#{old_node.version}/redact", :headers => auth_header
 
 166         assert_response :success, "should be OK to unredact old version as moderator."
 
 167         assert_nil old_node.reload.redaction