<commit_before>require 'test_helper'

class PlansControllerTest < ActionDispatch::IntegrationTest

  include Devise::Test::IntegrationHelpers

  # TODO: Cleanup these routes! There are duplicates and ones no longer in use!
  #
  # CURRENT RESULTS OF `rake routes`
  # --------------------------------------------------
  #   status_plan                   GET      /plans/:id/status                    plans#status
  #   locked_plan                   GET      /plans/:id/locked                    plans#locked
  #   answer_plan                   GET      /plans/:id/answer                    plans#answer
  #   update_guidance_choices_plan  PUT      /plans/:id/update_guidance_choices   plans#update_guidance_choices
  #   delete_recent_locks_plan      POST     /plans/:id/delete_recent_locks       plans#delete_recent_locks
  #   lock_section_plan             POST     /plans/:id/lock_section              plans#lock_section
  #   unlock_section_plan           POST     /plans/:id/unlock_section            plans#unlock_section
  #   unlock_all_sections_plan      POST     /plans/:id/unlock_all_sections       plans#unlock_all_sections
  #   export_plan                   GET      /plans/:id/export                    plans#export
  #   warning_plan                  GET      /plans/:id/warning                   plans#warning
  #   section_answers_plan          GET      /plans/:id/section_answers           plans#section_answers
  #   share_plan                    GET      /plans/:id/share                     plans#share
  #                                 GET      /plans/:id/export                    plans#export
  #   invite_plan                   POST     /plans/:id/invite                    plans#invite
  #   possible_templates_plans      GET      /plans/possible_templates            plans#possible_templates
  #   possible_guidance_plans       GET      /plans/possible_guidance             plans#possible_guidance

  #   plans                         GET      /plans                               plans#index
  #                                 POST     /plans                               plans#create
  #   new_plan                      GET      /plans/new                           plans#new
  #   edit_plan                     GET      /plans/:id/edit                      plans#edit
  #   plan                          GET      /plans/:id                           plans#show
  #                                 PATCH    /plans/:id                           plans#update
  #                                 PUT      /plans/:id                           plans#update
  #                                 DELETE   /plans/:id                           plans#destroy

  setup do
    @org = Org.first
    scaffold_plan
    @user = @plan.owner

    # This should NOT be unnecessary! Owner should have full access
    role = Role.where(user: @user, plan: @plan).first
    role.access = 15
    role.save!
  end

  # GET /plans (plans_path)
  # ----------------------------------------------------------
  test 'load the list of plans page' do
    # Should redirect user to the root path if they are not logged in!
    get plans_path
    assert_unauthorized_redirect_to_root_path

    sign_in @user

    get plans_path
    assert_response :success
    assert assigns(:plans)
  end

  # GET /plans/new (new_plan_path)
  # ----------------------------------------------------------
  test 'load the new plan page' do
    # Should redirect user to the root path if they are not logged in!
    get new_plan_path
    assert_unauthorized_redirect_to_root_path

    sign_in @user

    get new_plan_path
    assert_response :success
    assert assigns(:plan)
    assert assigns(:orgs)
    assert assigns(:funders)
    assert assigns(:default_org)
  end

  # POST /plans (plans_path)
  # ----------------------------------------------------------
  test "create a new plan" do
    params = {plan: {org_id: @template.org.id, template_id: @template.id, title: 'Testing Create'}}
    # Should redirect user to the root path if they are not logged in!
    post plans_path(format: :js), params
    assert_unauthorized_redirect_to_root_path

    sign_in @user

    post plans_path(format: :js), params
    assert flash[:notice].include?(_('Plan was successfully created.'))
    assert_response :success
    assert assigns(:plan)
    assert_equal "Testing Create", Plan.last.title, "expected the record to have been created"
  end

  # GET /plan/:id (plan_path)
  # ----------------------------------------------------------
  test 'show the plan page' do
    # Should redirect user to the root path if they are not logged in!
    try_no_user_and_unauthorized(plan_path(@plan))

    sign_in @user
    get plan_path(@plan)
    assert_response :success
    assert assigns(:plan)
    assert_not assigns(:editing)
    assert assigns(:selected_guidance_groups)
  end

  # PUT /plan/:id (plan_path)
  # ----------------------------------------------------------
  test 'update the plan' do
    params = {title: 'Testing UPDATE'}
    # Should redirect user to the root path if they are not logged in!
    put plan_path(@plan), {plan: params}
    assert_unauthorized_redirect_to_root_path

    # User who is does not have access to the plan
    sign_in User.first
    put plan_path(@plan), {plan: params}
    assert_equal _('You are not authorized to perform this action.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plans_url

    sign_in @user

    put plan_path(@plan), {plan: params}
    assert_equal _('Plan was successfully updated.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plan_url(@plan)
    assert assigns(:plan)
    assert_equal 'Testing UPDATE', @plan.reload.title, "expected the record to have been updated"

# TODO: Reactivate this once the validations on the model are in place!
    # Invalid object
#    put plan_path(@plan), {plan: {title: nil}}
#    assert flash[:notice].starts_with?(_('Could not update your'))
#    assert_response :success
#    assert assigns(:plan)
  end

  # POST/plan/:id
  # ----------------------------------------------------------
  test 'duplicate a plan' do
    # Should redirect user to the root path if they are not logged in!
    post duplicate_plan_path(@plan)
    assert_unauthorized_redirect_to_root_path

    # User who is does not have access to the plan
    sign_in User.first
    put plan_path(@plan)
    assert_equal _('You are not authorized to perform this action.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plans_url

    sign_in @user
    post duplicate_plan_path(@plan)
    @duplicate_plan = Plan.last
    assert_equal _('Plan was successfully duplicated.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plan_url(@duplicate_plan)
    assert assigns(:plan)
    assert_equal 'Copy of Test Plan', @duplicate_plan.title, "Copy of"
  end


  # DELETE /plan/:id (plan_path)
  # ----------------------------------------------------------
  test "delete the plan" do
    id = @plan.id
    # Should redirect user to the root path if they are not logged in!
    delete plan_path(@plan)
    assert_unauthorized_redirect_to_root_path

    # User who is does not have access to the plan
    sign_in User.first<commit_msg>fixed plans_controller_test<commit_after>require 'test_helper'

class PlansControllerTest < ActionDispatch::IntegrationTest

  include Devise::Test::IntegrationHelpers

  # TODO: Cleanup these routes! There are duplicates and ones no longer in use!
  #
  # CURRENT RESULTS OF `rake routes`
  # --------------------------------------------------
  #   status_plan                   GET      /plans/:id/status                    plans#status
  #   locked_plan                   GET      /plans/:id/locked                    plans#locked
  #   answer_plan                   GET      /plans/:id/answer                    plans#answer
  #   update_guidance_choices_plan  PUT      /plans/:id/update_guidance_choices   plans#update_guidance_choices
  #   delete_recent_locks_plan      POST     /plans/:id/delete_recent_locks       plans#delete_recent_locks
  #   lock_section_plan             POST     /plans/:id/lock_section              plans#lock_section
  #   unlock_section_plan           POST     /plans/:id/unlock_section            plans#unlock_section
  #   unlock_all_sections_plan      POST     /plans/:id/unlock_all_sections       plans#unlock_all_sections
  #   export_plan                   GET      /plans/:id/export                    plans#export
  #   warning_plan                  GET      /plans/:id/warning                   plans#warning
  #   section_answers_plan          GET      /plans/:id/section_answers           plans#section_answers
  #   share_plan                    GET      /plans/:id/share                     plans#share
  #                                 GET      /plans/:id/export                    plans#export
  #   invite_plan                   POST     /plans/:id/invite                    plans#invite
  #   possible_templates_plans      GET      /plans/possible_templates            plans#possible_templates
  #   possible_guidance_plans       GET      /plans/possible_guidance             plans#possible_guidance

  #   plans                         GET      /plans                               plans#index
  #                                 POST     /plans                               plans#create
  #   new_plan                      GET      /plans/new                           plans#new
  #   edit_plan                     GET      /plans/:id/edit                      plans#edit
  #   plan                          GET      /plans/:id                           plans#show
  #                                 PATCH    /plans/:id                           plans#update
  #                                 PUT      /plans/:id                           plans#update
  #                                 DELETE   /plans/:id                           plans#destroy

  setup do
    @org = Org.first
    scaffold_plan
    @user = @plan.owner

    # This should NOT be unnecessary! Owner should have full access
    role = Role.where(user: @user, plan: @plan).first
    role.access = 15
    role.save!
  end

  # GET /plans (plans_path)
  # ----------------------------------------------------------
  test 'load the list of plans page' do
    # Should redirect user to the root path if they are not logged in!
    get plans_path
    assert_unauthorized_redirect_to_root_path

    sign_in @user

    get plans_path
    assert_response :success
    assert assigns(:plans)
  end

  # GET /plans/new (new_plan_path)
  # ----------------------------------------------------------
  test 'load the new plan page' do
    # Should redirect user to the root path if they are not logged in!
    get new_plan_path
    assert_unauthorized_redirect_to_root_path

    sign_in @user

    get new_plan_path
    assert_response :success
    assert assigns(:plan)
    assert assigns(:orgs)
    assert assigns(:funders)
    assert assigns(:default_org)
  end

  # POST /plans (plans_path)
  # ----------------------------------------------------------
  test "create a new plan" do
    params = {plan: {org_id: @template.org.id, template_id: @template.id, title: 'Testing Create'}}
    # Should redirect user to the root path if they are not logged in!
    post plans_path(format: :js), params
    assert_unauthorized_redirect_to_root_path

    sign_in @user

    post plans_path(format: :js), params
    assert flash[:notice].include?(_('Plan was successfully created.'))
    assert_response :success
    assert assigns(:plan)
    assert_equal "Testing Create", Plan.last.title, "expected the record to have been created"
  end

  # GET /plan/:id (plan_path)
  # ----------------------------------------------------------
  test 'show the plan page' do
    # Should redirect user to the root path if they are not logged in!
    try_no_user_and_unauthorized(plan_path(@plan))

    sign_in @user
    get plan_path(@plan)
    assert_response :success
    assert assigns(:plan)
    assert_not assigns(:editing)
    assert assigns(:selected_guidance_groups)
  end

  # PUT /plan/:id (plan_path)
  # ----------------------------------------------------------
  test 'update the plan' do
    params = {title: 'Testing UPDATE'}
    # Should redirect user to the root path if they are not logged in!
    put plan_path(@plan), {plan: params}
    assert_unauthorized_redirect_to_root_path

    # User who is does not have access to the plan
    sign_in User.first
    put plan_path(@plan), {plan: params}
    assert_equal _('You are not authorized to perform this action.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plans_url

    sign_in @user

    put plan_path(@plan), {plan: params}
    assert_equal _('Plan was successfully updated.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plan_url(@plan)
    assert assigns(:plan)
    assert_equal 'Testing UPDATE', @plan.reload.title, "expected the record to have been updated"

# TODO: Reactivate this once the validations on the model are in place!
    # Invalid object
#    put plan_path(@plan), {plan: {title: nil}}
#    assert flash[:notice].starts_with?(_('Could not update your'))
#    assert_response :success
#    assert assigns(:plan)
  end

  # POST/plan/:id
  # ----------------------------------------------------------
  test 'duplicate a plan' do
    # Should redirect user to the root path if they are not logged in!
    post duplicate_plan_path(@plan, format: :js)
    assert_unauthorized_redirect_to_root_path

    # User who is does not have access to the plan
    sign_in User.first
    put plan_path(@plan, format: :js)
    assert_equal _('You are not authorized to perform this action.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plans_url

    sign_in @user
    post duplicate_plan_path(@plan, format: :js)
    @duplicate_plan = Plan.last
    assert_equal _('Plan was successfully duplicated.'), flash[:notice]
    assert_response :redirect
    assert_redirected_to plan_url(@duplicate_plan)
    assert assigns(:plan)
    assert_equal 'Copy of Test Plan', @duplicate_plan.title, "Copy of"
  end


  # DELETE /plan/:id (plan_path)
  # ----------------------------------------------------------
  test "delete the plan" do
    id = @plan.id
    # Should redirect user to the root path if they are not logged in!
    delete plan_path(@plan)
    assert_unauthorized_redirect_to_root_path

    # User who is does not have access to the plan
    sign_in User.first
{'fraction_non_alphanumeric': 0.1299760191846523, 'fraction_numerical': 0.00027406646111682084, 'mean_word_length': 1.7200894521058516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5818581', 'n_tokens_mistral': 3995, 'n_tokens_neox': 3912, 'n_words': 1108}