<commit_before>require 'test_helper'
module Admin
  class BundlesControllerTest < ActionController::TestCase
    include ActiveJob::TestHelper

    setup do
      FactoryBot.create(:admin_user)
      FactoryBot.create(:user_user)
      FactoryBot.create(:vendor_user)
      FactoryBot.create(:other_user)
      FactoryBot.create(:bundle)
      @vendor = FactoryBot.create(:vendor_with_points_of_contact)
      @static_bundle = FactoryBot.create(:static_bundle)
      FileUtils.rm_rf(APP_CONSTANTS['bundle_file_path'])
    end

    test 'should get index' do
      for_each_logged_in_user([ADMIN]) do
        get :index
        assert_redirected_to %r{/admin#bundles}
      end
    end

    test 'should deny access to index for non admins ' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        get :index
        assert_response 401
      end
    end

    test 'should get index without bundle install' do
      Bundle.destroy_all
      for_each_logged_in_user([ADMIN]) do
        get :index
        assert_redirected_to %r{/admin#bundles}
      end
    end

    test 'admin user should import new bundle successfully' do
      for_each_logged_in_user([ADMIN]) do
        orig_bundle_count = Bundle.count
        orig_measure_count = Measure.count
        orig_patient_count = Patient.count

        upload = Rack::Test::UploadedFile.new(Rails.root.join('test', 'fixtures', 'bundles', 'minimal_bundle_qdm_5_4.zip'), 'application/zip')
        perform_enqueued_jobs do
          post :create, params: { file: upload }
          assert_performed_jobs 2
          assert_equal orig_bundle_count + 1, Bundle.count, 'Should have added 1 new Bundle'
          assert orig_measure_count < Measure.count, 'Should have added new measures in the bundle'
          assert orig_patient_count < Patient.count, 'Should have added new patients in the bundle'
        end
      end
    end

    test 'should deny access to import for non admin users ' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        upload = Rack::Test::UploadedFile.new(Rails.root.join('test', 'fixtures', 'bundles', 'minimal_bundle_qdm_5_4.zip'), 'application/zip')
        post :create, params: { file: upload }
        assert_response 401
      end
    end

    test 'should not import file that is not a bundle' do
      for_each_logged_in_user([ADMIN]) do
        orig_count = Bundle.count

        upload = Rack::Test::UploadedFile.new(Rails.root.join('test', 'fixtures', 'artifacts', 'qrda.zip'), 'application/zip')
        assert_equal 0, BundleUploadJob.trackers.count, 'should be 0 trackers in db'
        perform_enqueued_jobs do
          post :create, params: { file: upload }
          assert_performed_jobs 1
          assert_equal 1, BundleUploadJob.trackers.count, 'should be 1 tracker in db'
          assert_equal :failed, BundleUploadJob.trackers.first.status, 'Status of tracker should be failed '
          assert_equal orig_count, Bundle.count, 'Should not have added new Bundle'
        end
      end
    end

    test 'should be able to change default bundle' do
      for_each_logged_in_user([ADMIN]) do
        flunk 'Should have at least 2 test bundles' if Bundle.count < 2

        active_bundle = Bundle.default
        inactive_bundle = Bundle.where('$or' => [{ 'active' => false }, { :active.exists => false }]).sample

        post :set_default, params: { id: inactive_bundle._id }

        active_bundle.reload
        inactive_bundle.reload

        assert inactive_bundle.active, 'new default bundle should be active'
        assert_not active_bundle.active, 'old default bundle should no longer be active'
      end
    end

    test 'should not allow a non admin to change default bundle' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        flunk 'Should have at least 2 test bundles' if Bundle.count < 2
        inactive_bundle = Bundle.where('$or' => [{ 'active' => false }, { :active.exists => false }]).sample

        post :set_default, params: { id: inactive_bundle._id }
        assert_response 401
      end
    end

    test 'should be able to remove bundle' do
      for_each_logged_in_user([ADMIN]) do
        orig_bundle_count = Bundle.count
        orig_measure_count = Measure.count
        orig_patient_count = Patient.count
        orig_results_count = QDM::IndividualResult.count
        id = @static_bundle.id

        delete :destroy, params: { id: id }

        assert_equal 0, Bundle.where(_id: id).count, 'Should have deleted bundle'
        assert_equal orig_bundle_count - 1, Bundle.count, 'Should have deleted Bundle'
        assert orig_measure_count > Measure.count, 'Should have removed measures in the bundle'
        assert orig_patient_count > Patient.count, 'Should have removed patients in the bundle'
        assert orig_results_count > QDM::IndividualResult.count, 'Should have removed individual results in the bundle'
      end
    end

    test 'should not allow non admins to remove bundle' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        id = Bundle.default._id
        delete :destroy, params: { id: id }
        assert_response 401
      end
    end

    test 'should be able to deprecate bundle' do<commit_msg>Add test for deleting bundle with vendor patients<commit_after>require 'test_helper'
module Admin
  class BundlesControllerTest < ActionController::TestCase
    include ActiveJob::TestHelper

    setup do
      FactoryBot.create(:admin_user)
      FactoryBot.create(:user_user)
      FactoryBot.create(:vendor_user)
      FactoryBot.create(:other_user)
      FactoryBot.create(:bundle)
      @vendor = FactoryBot.create(:vendor_with_points_of_contact)
      @static_bundle = FactoryBot.create(:static_bundle)
      FileUtils.rm_rf(APP_CONSTANTS['bundle_file_path'])
    end

    test 'should get index' do
      for_each_logged_in_user([ADMIN]) do
        get :index
        assert_redirected_to %r{/admin#bundles}
      end
    end

    test 'should deny access to index for non admins ' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        get :index
        assert_response 401
      end
    end

    test 'should get index without bundle install' do
      Bundle.destroy_all
      for_each_logged_in_user([ADMIN]) do
        get :index
        assert_redirected_to %r{/admin#bundles}
      end
    end

    test 'admin user should import new bundle successfully' do
      for_each_logged_in_user([ADMIN]) do
        orig_bundle_count = Bundle.count
        orig_measure_count = Measure.count
        orig_patient_count = Patient.count

        upload = Rack::Test::UploadedFile.new(Rails.root.join('test', 'fixtures', 'bundles', 'minimal_bundle_qdm_5_4.zip'), 'application/zip')
        perform_enqueued_jobs do
          post :create, params: { file: upload }
          assert_performed_jobs 2
          assert_equal orig_bundle_count + 1, Bundle.count, 'Should have added 1 new Bundle'
          assert orig_measure_count < Measure.count, 'Should have added new measures in the bundle'
          assert orig_patient_count < Patient.count, 'Should have added new patients in the bundle'
        end
      end
    end

    test 'should deny access to import for non admin users ' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        upload = Rack::Test::UploadedFile.new(Rails.root.join('test', 'fixtures', 'bundles', 'minimal_bundle_qdm_5_4.zip'), 'application/zip')
        post :create, params: { file: upload }
        assert_response 401
      end
    end

    test 'should not import file that is not a bundle' do
      for_each_logged_in_user([ADMIN]) do
        orig_count = Bundle.count

        upload = Rack::Test::UploadedFile.new(Rails.root.join('test', 'fixtures', 'artifacts', 'qrda.zip'), 'application/zip')
        assert_equal 0, BundleUploadJob.trackers.count, 'should be 0 trackers in db'
        perform_enqueued_jobs do
          post :create, params: { file: upload }
          assert_performed_jobs 1
          assert_equal 1, BundleUploadJob.trackers.count, 'should be 1 tracker in db'
          assert_equal :failed, BundleUploadJob.trackers.first.status, 'Status of tracker should be failed '
          assert_equal orig_count, Bundle.count, 'Should not have added new Bundle'
        end
      end
    end

    test 'should be able to change default bundle' do
      for_each_logged_in_user([ADMIN]) do
        flunk 'Should have at least 2 test bundles' if Bundle.count < 2

        active_bundle = Bundle.default
        inactive_bundle = Bundle.where('$or' => [{ 'active' => false }, { :active.exists => false }]).sample

        post :set_default, params: { id: inactive_bundle._id }

        active_bundle.reload
        inactive_bundle.reload

        assert inactive_bundle.active, 'new default bundle should be active'
        assert_not active_bundle.active, 'old default bundle should no longer be active'
      end
    end

    test 'should not allow a non admin to change default bundle' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        flunk 'Should have at least 2 test bundles' if Bundle.count < 2
        inactive_bundle = Bundle.where('$or' => [{ 'active' => false }, { :active.exists => false }]).sample

        post :set_default, params: { id: inactive_bundle._id }
        assert_response 401
      end
    end

    test 'should be able to remove bundle' do
      FactoryBot.create(:vendor_test_patient,
                        bundleId: @static_bundle._id, correlation_id: @vendor.id)
      for_each_logged_in_user([ADMIN]) do
        orig_bundle_count = Bundle.count
        orig_measure_count = Measure.count
        orig_patient_count = Patient.count
        orig_vendor_patient_count = CQM::VendorPatient.count
        orig_results_count = QDM::IndividualResult.count
        id = @static_bundle.id

        delete :destroy, params: { id: id }

        assert_equal 0, Bundle.where(_id: id).count, 'Should have deleted bundle'
        assert_equal orig_bundle_count - 1, Bundle.count, 'Should have deleted Bundle'
        assert orig_measure_count > Measure.count, 'Should have removed measures in the bundle'
        assert orig_patient_count > Patient.count, 'Should have removed patients in the bundle'
        assert orig_vendor_patient_count > CQM::VendorPatient.count, 'Should have vendor patients for the bundle'
        assert orig_results_count > QDM::IndividualResult.count, 'Should have removed individual results in the bundle'
      end
    end

    test 'should not allow non admins to remove bundle' do
      for_each_logged_in_user([USER, OWNER, VENDOR]) do
        id = Bundle.default._id
        delete :destroy, params: { id: id }
        assert_response 401
      end
    end

    test 'should be able to deprecate bundle' do
{'fraction_non_alphanumeric': 0.08169904266195743, 'fraction_numerical': 0.00557672646156706, 'mean_word_length': 2.9212827988338192, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3948510', 'n_tokens_mistral': 3255, 'n_tokens_neox': 3062, 'n_words': 915}