2021-04-18 14:34:55 +03:00
# frozen_string_literal: true
2020-11-04 21:29:52 +03: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
get :show , params : @crud [ :show_params ] . presence || { id : @crud [ :entity ] . id }
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
get :show , params : @crud [ :show_params ] . presence || { id : @crud [ :entity ] . id }
2022-10-30 19:08:52 +03:00
2020-11-04 21:29:52 +03: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 19:58:20 +03:00
if @crud [ :index_forbidden_status ] . present?
assert_response @crud [ :index_forbidden_status ]
2020-11-04 21:29:52 +03: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 19:08:52 +03:00
2020-11-04 21:29:52 +03: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 19:08:52 +03:00
2020-11-04 21:29:52 +03: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 14:34:55 +03:00
assert_difference " #{ @crud [ :entity ] . class . name } .count " do
2020-11-04 21:29:52 +03: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 09:53:54 +03:00
entity = @crud [ :entity ] . class . last
2020-11-04 21:29:52 +03: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 10:45:35 +03:00
assert_equal value , entity . send ( name ) , " Field #{ name } test failed "
2020-11-04 21:29:52 +03:00
end
end
def test_create_without_permission
return unless prepare_crud_test :create , no_permission : true
2021-04-18 14:34:55 +03:00
assert_no_difference " #{ @crud [ :entity ] . class . name } .count " do
2020-11-04 21:29:52 +03: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
get :edit , params : { id : @crud [ :entity ] . id }
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
get :edit , params : { id : @crud [ :entity ] . id }
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 21:10:29 +03:00
@crud [ :entity ] & . reload
2020-11-04 21:29:52 +03: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 12:34:53 +03:00
2020-11-04 21:29:52 +03: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
delete :destroy , params : { id : @crud [ :entity ] . id }
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 14:34:55 +03:00
assert_no_difference " #{ @crud [ :entity ] . class . name } .count " do
2020-11-04 21:29:52 +03:00
delete :destroy , params : { id : @crud [ :entity ] . id }
end
assert_response :forbidden
end
private
def form_params ( action )
crud_params = @crud [ " #{ action } _params " . to_sym ]
if @crud [ :form ]
{ id : @crud [ :entity ] . id , @crud [ :form ] = > crud_params }
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