<commit_before>require 'test_helper'
require 'capybara/rails'

class AttachmentRedirectDueToUnpublishingIntegrationTest < ActionDispatch::IntegrationTest
  extend Minitest::Spec::DSL
  include Capybara::DSL
  include Rails.application.routes.url_helpers

  let(:filename) { 'sample.docx' }
  let(:file) { File.open(path_to_attachment(filename)) }
  let(:attachment) { build(:file_attachment, attachable: attachable, file: file) }
  let(:attachable) { edition }
  let(:asset_id) { 'asset-id' }
  let(:redirect_url) { Whitehall.url_maker.public_document_path(edition) }

  before do
    login_as create(:managing_editor)
    setup_publishing_api_for(edition)
    attachable.attachments << attachment
    VirusScanHelpers.simulate_virus_scan
    stub_whitehall_asset(filename, id: asset_id)

    @test_mode = Sidekiq::Testing.__test_mode
    Sidekiq::Testing.fake!
  end

  after do
    Sidekiq::Testing.__test_mode = @test_mode
  end

  context 'given a published document with file attachment' do
    let(:edition) { create(:published_news_article) }

    it 'sets redirect URL for attachment in Asset Manager when document is unpublished' do
      visit admin_news_article_path(edition)
      unpublish_document_published_in_error
      logout
      get attachment.url
      assert_redirected_to redirect_url
      assert_sets_redirect_url_in_asset_manager_to redirect_url
    end

    it 'sets redirect URL for attachment in Asset Manager when document is consolidated' do
      visit admin_news_article_path(edition)<commit_msg>Remove redundant Sidekiq setup from attachment integration test<commit_after>require 'test_helper'
require 'capybara/rails'

class AttachmentRedirectDueToUnpublishingIntegrationTest < ActionDispatch::IntegrationTest
  extend Minitest::Spec::DSL
  include Capybara::DSL
  include Rails.application.routes.url_helpers

  let(:filename) { 'sample.docx' }
  let(:file) { File.open(path_to_attachment(filename)) }
  let(:attachment) { build(:file_attachment, attachable: attachable, file: file) }
  let(:attachable) { edition }
  let(:asset_id) { 'asset-id' }
  let(:redirect_url) { Whitehall.url_maker.public_document_path(edition) }

  before do
    login_as create(:managing_editor)
    setup_publishing_api_for(edition)
    attachable.attachments << attachment
    VirusScanHelpers.simulate_virus_scan
    stub_whitehall_asset(filename, id: asset_id)
  end

  context 'given a published document with file attachment' do
    let(:edition) { create(:published_news_article) }

    it 'sets redirect URL for attachment in Asset Manager when document is unpublished' do
      visit admin_news_article_path(edition)
      unpublish_document_published_in_error
      logout
      get attachment.url
      assert_redirected_to redirect_url
      assert_sets_redirect_url_in_asset_manager_to redirect_url
    end

    it 'sets redirect URL for attachment in Asset Manager when document is consolidated' do
      visit admin_news_article_path(edition)
{'fraction_non_alphanumeric': 0.07469717362045761, 'fraction_numerical': 0.0, 'mean_word_length': 5.006060606060606, 'pattern_counts': {'":': 0, '<': 9, '<?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': '985087', 'n_tokens_mistral': 905, 'n_tokens_neox': 879, 'n_words': 200}