2021-04-18 13:34:55 +02:00
# frozen_string_literal: true
2020-11-04 19:29:52 +01:00
module CrudControllerBase
extend ActiveSupport :: Concern
included do
# Show
def test_show
unless prepare_crud_test :show
# no controller action should be available, test it
assert_raises ActionController :: UrlGenerationError do
get :show
end
return
end
2023-02-21 16:02:17 +01:00
get :show , params : @crud [ :show_params ] . presence || { id : id_value }
2020-11-04 19:29:52 +01:00
if @crud [ :show_assert_response ] . present?
assert_response @crud [ :show_assert_response ]
else
assert_response :success
end
assert_select ( * @crud [ :show_assert_select ] ) if @crud [ :show_assert_select ] . present?
end
def test_show_without_permission
return unless prepare_crud_test :show , no_permission : true
2023-02-21 16:02:17 +01:00
get :show , params : @crud [ :show_params ] . presence || { id : id_value }
2022-10-30 17:08:52 +01:00
2020-11-04 19:29:52 +01:00
assert_response :forbidden
end
# Index
def test_index
unless prepare_crud_test :index
# no controller action should be available, test it
assert_raises ActionController :: UrlGenerationError do
get :index
end
return
end
get :index , params : @crud [ :index_params ]
if @crud [ :index_assert_response ] . present?
assert_response @crud [ :index_assert_response ]
else
assert_response :success
end
assert_select ( * @crud [ :index_assert_select ] ) if @crud [ :index_assert_select ] . present?
end
def test_index_without_permission
return unless prepare_crud_test :index , no_permission : true
get :index , params : @crud [ :index_params ]
2022-05-12 18:58:20 +02:00
if @crud [ :index_forbidden_status ] . present?
assert_response @crud [ :index_forbidden_status ]
2020-11-04 19:29:52 +01:00
else
assert_response :forbidden
end
end
# New
def test_new
unless prepare_crud_test :new
# no controller action should be available, test it
assert_raises ActionController :: UrlGenerationError do
get :new
end
return
end
get :new , params : @crud [ :new_params ] . presence || { }
2022-10-30 17:08:52 +01:00
2020-11-04 19:29:52 +01:00
assert_response :success
end
def test_new_without_permission
return unless prepare_crud_test :new , no_permission : true
get :new , params : @crud [ :new_params ] . presence || { }
2022-10-30 17:08:52 +01:00
2020-11-04 19:29:52 +01:00
assert_response :forbidden
end
# Create
def test_create
unless prepare_crud_test :create
# no controller action should be available, test it
assert_raises ActionController :: UrlGenerationError do
post :create
end
return
end
2021-04-18 13:34:55 +02:00
assert_difference " #{ @crud [ :entity ] . class . name } .count " do
2020-11-04 19:29:52 +01:00
assert_no_difference 'Journal.count' do
post :create , params : form_params ( :create )
end
end
if @crud [ :create_redirect_to ]
assert_redirected_to @crud [ :create_redirect_to ]
else
assert_response 302
end
2021-07-31 08:53:54 +02:00
entity = @crud [ :entity ] . class . last
2020-11-04 19:29:52 +01:00
if @crud [ :created_assert ] . present?
@crud [ :created_assert ] . each do | name |
assert entity . send ( name ) , " Expected false to be truthy for #{ name } "
end
end
if @crud [ :create_assert_not ] . present?
@crud [ :create_assert_not ] . each do | name |
assert_not entity . send ( name ) , " Expected true to be nil or false for #{ name } "
end
end
return if @crud [ :create_assert_equals ] . blank?
@crud [ :create_assert_equals ] . each do | name , value |
2022-06-16 09:45:35 +02:00
assert_equal value , entity . send ( name ) , " Field #{ name } test failed "
2020-11-04 19:29:52 +01:00
end
end
def test_create_without_permission
return unless prepare_crud_test :create , no_permission : true
2021-04-18 13:34:55 +02:00
assert_no_difference " #{ @crud [ :entity ] . class . name } .count " do
2020-11-04 19:29:52 +01:00
post :create , params : form_params ( :create )
end
assert_response :forbidden
end
# Edit
def test_edit
unless prepare_crud_test :edit
# no controller action should be available, test it
assert_raises ActionController :: UrlGenerationError do
post :edit
end
return
end
2023-02-21 16:02:17 +01:00
get :edit , params : { id : id_value }
2020-11-04 19:29:52 +01:00
assert_response :success
if @crud [ :edit_assert_select ] . present?
assert_select ( * @crud [ :edit_assert_select ] )
else
assert_select " form # edit_ #{ @crud [ :form ] } "
end
end
def test_edit_without_permission
return unless prepare_crud_test :edit , no_permission : true
2023-02-21 16:02:17 +01:00
get :edit , params : { id : id_value }
2020-11-04 19:29:52 +01:00
assert_response :forbidden
end
# Update
def test_update
unless prepare_crud_test :update
# no controller action should be available, test it
assert_raises ActionController :: UrlGenerationError do
put :update
end
return
end
put :update , params : form_params ( :update )
if @crud [ :update_redirect_to ]
assert_redirected_to @crud [ :update_redirect_to ]
else
assert_response 302
end
2022-03-03 19:10:29 +01:00
@crud [ :entity ] & . reload
2020-11-04 19:29:52 +01:00
if @crud [ :update_assert ] . present?
@crud [ :update_assert ] . each do | name |
assert @crud [ :entity ] . send ( name ) , " Expected false to be truthy for #{ name } "
end
end
if @crud [ :update_assert_not ] . present?
@crud [ :update_assert_not ] . each do | name |
assert_not @crud [ :entity ] . send ( name ) , " Expected true to be nil or false for #{ name } "
end
end
return if @crud [ :update_assert_equals ] . blank?
@crud [ :update_assert_equals ] . each do | name , value |
assert_equal value , @crud [ :entity ] . send ( name )
end
end
def test_update_without_permission
return unless prepare_crud_test :update , no_permission : true
put :update , params : form_params ( :update )
assert_response :forbidden
return if @crud [ :update_equals ] . blank?
@crud [ :entity ] . reload
2022-11-07 10:34:53 +01:00
2020-11-04 19:29:52 +01:00
@crud [ :update_equals ] . each do | name , value |
assert_not_equal value , @crud [ :entity ] . send ( name )
end
end
# Delete
def test_delete
unless prepare_crud_test :delete
# no controller action should be available, test it
assert_raises ActionController :: UrlGenerationError do
delete :destroy
end
return
end
assert_difference ( " #{ @crud [ :entity ] . class . name } .count " , - 1 ) do
2023-02-21 16:02:17 +01:00
delete :destroy , params : { id : id_value }
2020-11-04 19:29:52 +01:00
end
return if @crud [ :delete_redirect_to ] . blank?
assert_redirected_to @crud [ :delete_redirect_to ]
end
def test_delete_without_permission
return unless prepare_crud_test :delete , no_permission : true
2021-04-18 13:34:55 +02:00
assert_no_difference " #{ @crud [ :entity ] . class . name } .count " do
2023-02-21 16:02:17 +01:00
delete :destroy , params : { id : id_value }
2020-11-04 19:29:52 +01:00
end
assert_response :forbidden
end
private
2023-02-21 16:02:17 +01:00
def id_value
primary_field = @crud [ :primary_field ] || = :id
@crud [ :entity ] . send primary_field
end
2020-11-04 19:29:52 +01:00
def form_params ( action )
2023-12-05 19:24:41 +01:00
crud_params = @crud [ :" #{ action } _params " ]
2020-11-04 19:29:52 +01:00
if @crud [ :form ]
2023-02-21 16:02:17 +01:00
{ id : id_value , @crud [ :form ] = > crud_params }
2020-11-04 19:29:52 +01:00
else
crud_params
end
end
def prepare_crud_test ( action , no_permission : false )
return false if @crud [ :without_actions ] . present? && @crud [ :without_actions ] . include? ( action )
@request . session [ :user_id ] = if no_permission
@user_without_permission . id
else
@user . id
end
true
end
end
end