<commit_before>require "test_helper"

class EditionAuthBypassUpdaterTest < ActiveSupport::TestCase
  extend Minitest::Spec::DSL

  describe "#call" do
    let(:user) { create(:user) }
    let(:updater) { MiniTest::Mock.new }
    let(:uid) { SecureRandom.uuid }

    before do
      updater.expect :perform!, true
      SecureRandom.stubs(uuid: uid)
    end

    test "updates the editions auth_bypass_id, saves it with the current user and calls 'perform!' on the updater" do
      edition = create(:draft_edition)

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      service.call

      assert_equal edition.auth_bypass_id, uid
      assert_equal edition.edition_authors.last.user, user
    end

    test "updates attachments with auth_bypass_ids" do
      edition = create(:draft_edition)
      file_attachment = create(:file_attachment, attachable: edition)
      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        file_attachment.attachment_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "updates an image with auth_bypass_id" do
      edition = create(:draft_case_study)
      image = create(:image, edition: edition)
      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        image.image_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "does not attempt to update html attachments via the update whitehall asset worker" do
      edition = create(:draft_edition)
      create(:html_attachment, attachable: edition)

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).never

      service.call
    end

    test "updates a consultation response form asset with auth_bypass_id" do
      edition = create(:consultation)
      participation = create(:consultation_participation, consultation: edition)
      consultation_response_form = create(:consultation_response_form, consultation_participation: participation)

      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        consultation_response_form.consultation_response_form_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "updates a consultation outcome's attachments with auth_bypass_id" do
      edition = create(:draft_consultation)
      outcome = create(:consultation_outcome, consultation: edition)
      file_attachment = create(:file_attachment, attachable: outcome)

      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        file_attachment.attachment_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "updates a consultation's public feedback attachments with auth_bypass_id" do
      edition = create(:draft_consultation)
      feedback = create(:consultation_public_feedback, consultation: edition)
      file_attachment = create(:file_attachment, attachable: feedback)

      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        file_attachment.attachment_data.to_global_id,
        expected_attributes,
      )

      service.call
    end
  end<commit_msg>Ensure that the auth_bypass_id changes after update in tests<commit_after>require "test_helper"

class EditionAuthBypassUpdaterTest < ActiveSupport::TestCase
  extend Minitest::Spec::DSL

  describe "#call" do
    let(:user) { create(:user) }
    let(:updater) { MiniTest::Mock.new }
    let(:uid) { SecureRandom.uuid }

    before do
      updater.expect :perform!, true
    end

    test "updates the editions auth_bypass_id, saves it with the current user and calls 'perform!' on the updater" do
      edition = create(:draft_edition)
      auth_bypass_id_to_revoke = edition.auth_bypass_id
      SecureRandom.stubs(uuid: uid)

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      service.call

      assert_equal edition.auth_bypass_id, uid
      assert_not_equal edition.auth_bypass_id, auth_bypass_id_to_revoke
      assert_equal edition.edition_authors.last.user, user
    end

    test "updates attachments with auth_bypass_ids" do
      edition = create(:draft_edition)
      file_attachment = create(:file_attachment, attachable: edition)

      SecureRandom.stubs(uuid: uid)
      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        file_attachment.attachment_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "updates an image with auth_bypass_id" do
      edition = create(:draft_case_study)
      image = create(:image, edition: edition)

      SecureRandom.stubs(uuid: uid)
      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        image.image_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "does not attempt to update html attachments via the update whitehall asset worker" do
      edition = create(:draft_edition)
      create(:html_attachment, attachable: edition)

      SecureRandom.stubs(uuid: uid)

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).never

      service.call
    end

    test "updates a consultation response form asset with auth_bypass_id" do
      edition = create(:consultation)
      participation = create(:consultation_participation, consultation: edition)
      consultation_response_form = create(:consultation_response_form, consultation_participation: participation)

      SecureRandom.stubs(uuid: uid)
      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        consultation_response_form.consultation_response_form_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "updates a consultation outcome's attachments with auth_bypass_id" do
      edition = create(:draft_consultation)
      outcome = create(:consultation_outcome, consultation: edition)
      file_attachment = create(:file_attachment, attachable: outcome)

      SecureRandom.stubs(uuid: uid)
      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        file_attachment.attachment_data.to_global_id,
        expected_attributes,
      )

      service.call
    end

    test "updates a consultation's public feedback attachments with auth_bypass_id" do
      edition = create(:draft_consultation)
      feedback = create(:consultation_public_feedback, consultation: edition)
      file_attachment = create(:file_attachment, attachable: feedback)

      SecureRandom.stubs(uuid: uid)
      expected_attributes = { auth_bypass_ids: [uid] }

      service = EditionAuthBypassUpdater.new(
        edition: edition,
        current_user: user,
        updater: updater,
      )

      AssetManagerUpdateWhitehallAssetWorker.expects(:perform_async_in_queue).with(
        "asset_manager_updater",
        file_attachment.attachment_data.to_global_id,
        expected_attributes,
      )

      service.call
    end
  end
{'fraction_non_alphanumeric': 0.07332490518331226, 'fraction_numerical': 0.0, 'mean_word_length': 3.1309834638816363, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6884561', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2760, 'n_words': 546}