1 # frozen_string_literal: true
 
   5 class RedactionsControllerTest < ActionDispatch::IntegrationTest
 
   7   # test all routes which lead to this controller
 
  10       { :path => "/redactions", :method => :get },
 
  11       { :controller => "redactions", :action => "index" }
 
  14       { :path => "/redactions/new", :method => :get },
 
  15       { :controller => "redactions", :action => "new" }
 
  18       { :path => "/redactions", :method => :post },
 
  19       { :controller => "redactions", :action => "create" }
 
  22       { :path => "/redactions/1", :method => :get },
 
  23       { :controller => "redactions", :action => "show", :id => "1" }
 
  26       { :path => "/redactions/1/edit", :method => :get },
 
  27       { :controller => "redactions", :action => "edit", :id => "1" }
 
  30       { :path => "/redactions/1", :method => :put },
 
  31       { :controller => "redactions", :action => "update", :id => "1" }
 
  34       { :path => "/redactions/1", :method => :delete },
 
  35       { :controller => "redactions", :action => "destroy", :id => "1" }
 
  43     assert_response :success
 
  44     assert_template :index
 
  45     assert_select "ul#redaction_list", 1 do
 
  46       assert_select "li", Redaction.count
 
  51     redaction = create(:redaction, :title => "tested-redaction")
 
  53     get redaction_path(redaction)
 
  54     assert_response :success
 
  55     assert_dom "h1", :text => /tested-redaction/
 
  56     assert_dom "a[href='#{user_path redaction.user}']", :text => redaction.user.display_name
 
  60     get new_redaction_path
 
  61     assert_redirected_to login_path(:referer => new_redaction_path)
 
  64   def test_new_moderator
 
  65     session_for(create(:moderator_user))
 
  67     get new_redaction_path
 
  68     assert_response :success
 
  72   def test_new_non_moderator
 
  73     session_for(create(:user))
 
  75     get new_redaction_path
 
  76     assert_redirected_to :controller => "errors", :action => "forbidden"
 
  79   def test_create_moderator
 
  80     session_for(create(:moderator_user))
 
  82     post redactions_path(:redaction => { :title => "Foo", :description => "Description here." })
 
  83     assert_redirected_to(redaction_path(Redaction.find_by(:title => "Foo")))
 
  86   def test_create_moderator_invalid
 
  87     session_for(create(:moderator_user))
 
  89     post redactions_path(:redaction => { :title => "Foo", :description => "" })
 
  90     assert_response :success
 
  94   def test_create_non_moderator
 
  95     session_for(create(:user))
 
  97     post redactions_path(:redaction => { :title => "Foo", :description => "Description here." })
 
  98     assert_redirected_to :controller => "errors", :action => "forbidden"
 
 101   def test_destroy_moderator_empty
 
 102     session_for(create(:moderator_user))
 
 104     # create an empty redaction
 
 105     redaction = create(:redaction)
 
 107     delete redaction_path(redaction)
 
 108     assert_redirected_to(redactions_path)
 
 111   def test_destroy_moderator_non_empty
 
 112     session_for(create(:moderator_user))
 
 114     # create elements in the redaction
 
 115     redaction = create(:redaction)
 
 116     create(:old_node, :redaction => redaction)
 
 118     delete redaction_path(redaction)
 
 119     assert_redirected_to(redaction_path(redaction))
 
 120     assert_match(/^Redaction is not empty/, flash[:error])
 
 123   def test_delete_non_moderator
 
 124     session_for(create(:user))
 
 126     delete redaction_path(create(:redaction))
 
 127     assert_redirected_to :controller => "errors", :action => "forbidden"
 
 131     redaction = create(:redaction)
 
 133     get edit_redaction_path(redaction)
 
 134     assert_redirected_to login_path(:referer => edit_redaction_path(redaction))
 
 137   def test_edit_moderator
 
 138     session_for(create(:moderator_user))
 
 140     get edit_redaction_path(create(:redaction))
 
 141     assert_response :success
 
 144   def test_edit_non_moderator
 
 145     session_for(create(:user))
 
 147     get edit_redaction_path(create(:redaction))
 
 148     assert_redirected_to :controller => "errors", :action => "forbidden"
 
 151   def test_update_moderator
 
 152     session_for(create(:moderator_user))
 
 154     redaction = create(:redaction)
 
 156     put redaction_path(redaction, :redaction => { :title => "Foo", :description => "Description here." })
 
 157     assert_redirected_to(redaction_path(redaction))
 
 160   def test_update_moderator_invalid
 
 161     session_for(create(:moderator_user))
 
 163     redaction = create(:redaction)
 
 165     put redaction_path(redaction, :redaction => { :title => "Foo", :description => "" })
 
 166     assert_response :success
 
 167     assert_template :edit
 
 170   def test_updated_non_moderator
 
 171     session_for(create(:user))
 
 173     redaction = create(:redaction)
 
 175     put redaction_path(redaction, :redaction => { :title => "Foo", :description => "Description here." })
 
 176     assert_redirected_to :controller => "errors", :action => "forbidden"