diff --git a/app/controllers/sections_controller.rb b/app/controllers/sections_controller.rb index 6df6210..bb98094 100644 --- a/app/controllers/sections_controller.rb +++ b/app/controllers/sections_controller.rb @@ -13,7 +13,7 @@ redirect_to admin_show_phase_path(id: @section.phase_id, :section_id => @section.id, edit: 'true'), notice: _('Information was successfully created.') else - render action: "phases/admin_show" + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id , edit: 'true'), notice: generate_error_notice(@section) end end @@ -27,7 +27,7 @@ if @section.update_attributes(params[:section]) redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id , edit: 'true'), notice: _('Information was successfully updated.') else - render action: "phases/admin_show" + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id , edit: 'true'), notice: generate_error_notice(@section) end end diff --git a/test/functional/sections_controller_test.rb b/test/functional/sections_controller_test.rb new file mode 100644 index 0000000..cb0dc88 --- /dev/null +++ b/test/functional/sections_controller_test.rb @@ -0,0 +1,113 @@ +class PhasesControllerTest < ActionDispatch::IntegrationTest + + include Devise::Test::IntegrationHelpers + + setup do + scaffold_template + @phase = @template.phases.first + + # Get the first Org Admin + @user = User.where(org: @template.org).select{|u| u.can_org_admin?}.first + end + +# TODO: The following methods SHOULD replace the old 'admin_' prefixed methods. The routes file already has +# these defined. They are defined multiple times though and we need to clean this up! In particular +# look at the unnamed routes after 'new_plan_phase' below. They are not named because they are duplicates. +# We should just have: +# +# SHOULD BE: +# -------------------------------------------------- +# sections GET /templates/:template_id/phases/:phase_id/sections sections#index +# POST /templates/:template_id/phases/:phase_id/sections sections#create +# section GET /templates/:template_id/phases/:phase_id/section/:id sections#show +# PATCH /templates/:template_id/phases/:phase_id/section/:id sections#update +# PUT /templates/:template_id/phases/:phase_id/section/:id sections#update +# DELETE /templates/:template_id/phases/:phase_id/section/:id sections#destroy +# +# CURRENT RESULTS OF `rake routes` +# -------------------------------------------------- +# admin_create_section POST /org/admin/templates/sections/:id/admin_create sections#admin_create +# admin_update_section PUT /org/admin/templates/sections/:id/admin_update sections#admin_update +# admin_destroy_section DELETE /org/admin/templates/sections/:id/admin_destroy sections#admin_destroy +# +# admin_sections GET /admin/sections(.:format) admin/sections#index +# POST /admin/sections(.:format) admin/sections#create +# new_admin_section GET /admin/sections/new(.:format) admin/sections#new +# edit_admin_section GET /admin/sections/:id/edit(.:format) admin/sections#edit +# admin_section GET /admin/sections/:id(.:format) admin/sections#show +# PATCH /admin/sections/:id(.:format) admin/sections#update +# PUT /admin/sections/:id(.:format) admin/sections#update +# DELETE /admin/sections/:id(.:format) admin/sections#destroy + + + + # POST /org/admin/templates/sections/:id/admin_create (admin_create_section_path) + # ---------------------------------------------------------- + test "create a new section" do + params = {phase_id: @phase.id, title: 'Section Tester', number: 99} + + # Should redirect user to the root path if they are not logged in! + post admin_create_section_path(@phase), {section: params} + assert_unauthorized_redirect_to_root_path + + sign_in @user + + post admin_create_section_path(@phase), {section: params} + assert_response :redirect + assert_redirected_to admin_show_phase_url(id: @phase.id, edit: 'true', section_id: Section.last.id) + assert_equal _('Information was successfully created.'), flash[:notice] + + # Invalid object + post admin_create_section_path(@phase), {section: {phase_id: @phase.id, title: nil}} + assert_response :redirect + assert_redirected_to admin_show_phase_url(id: @phase.id, edit: 'true') + assert assigns(:section) + assert assigns(:phase) + assert flash[:notice].starts_with?(_('Unable to save your changes.')) + end + + # PUT /org/admin/templates/sections/:id/admin_update (admin_update_section_path) + # ---------------------------------------------------------- + test "update the section" do + params = {title: 'Phase - UPDATE'} + + # Should redirect user to the root path if they are not logged in! + put admin_update_section_path(@phase.sections.first), {section: params} + assert_unauthorized_redirect_to_root_path + + sign_in @user + + # Valid save + put admin_update_section_path(@phase.sections.first), {section: params} + assert_response :redirect + assert_redirected_to admin_show_phase_url(id: @phase.id, section_id: @phase.sections.first.id, edit: 'true') + assert_equal _('Information was successfully updated.'), flash[:notice] + + # Invalid save + put admin_update_section_path(@phase.sections.first), {section: {title: nil}} + assert_response :redirect + assert_redirected_to admin_show_phase_url(id: @phase.id, section_id: @phase.sections.first.id, edit: 'true') + assert assigns(:section) + assert assigns(:phase) + assert flash[:notice].starts_with?(_('Unable to save your changes.')) + end + + # DELETE /org/admin/templates/sections/:id/admin_destroy (admin_destroy_section_path) + # ---------------------------------------------------------- + test "delete the section" do + # Should redirect user to the root path if they are not logged in! + delete admin_destroy_section_path(id: @phase.id, section_id: @phase.sections.first.id) + assert_unauthorized_redirect_to_root_path + + sign_in @user + + delete admin_destroy_section_path(id: @phase.id, section_id: @phase.sections.first.id) + assert_response :redirect + assert assigns(:section) + assert assigns(:phase) + + assert_redirected_to admin_show_phase_url(id: @phase.id, edit: 'true' ) + assert_equal _('Information was successfully deleted.'), flash[:notice] + end + +end \ No newline at end of file