<commit_before>require "test_helper"

class CsvPreviewControllerTest < ActionController::TestCase
  def basename(attachment_data)
    File.basename(attachment_data.filename, '.' + attachment_data.file_extension)
  end

  view_test "GET #preview for a CSV attachment on a public edition renders the CSV preview" do
    visible_edition = create(:published_publication, :with_file_attachment, attachments: [
      attachment = build(:csv_attachment)
    ])
    attachment_data = attachment.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_equal visible_edition, assigns(:edition)
    assert_equal attachment, assigns(:attachment)
    assert assigns(:csv_preview).is_a?(CsvPreview)
    assert_response :success
    assert_select '.headings h1', attachment.title
  end

  view_test "GET #preview for a CSV attachment on a public edition has links to document organiastions" do
    org_1 = create(:organisation)
    org_2 = create(:organisation)
    org_3 = create(:organisation)

    attachment = build(:csv_attachment)
    attachment_data = attachment.attachment_data

    create(:published_publication, :with_file_attachment, attachments: [attachment], organisations: [org_1, org_2, org_3])

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_select 'a[href=?]', organisation_path(org_1)
    assert_select 'a[href=?]', organisation_path(org_2)
    assert_select 'a[href=?]', organisation_path(org_3)
  end

  test "GET #preview for a CSV attachment on a non-public edition returns a not found response" do
    unpublished_edition = create(:draft_publication, :with_file_attachment, attachments: [build(:csv_attachment)])
    attachment_data = unpublished_edition.attachments.first.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_response :not_found
  end

  test "GET #preview for a non-CSV file type returns a not found response" do
    visible_edition = create(:published_publication, :with_file_attachment, attachments: [build(:file_attachment)])
    attachment_data = visible_edition.attachments.first.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_response :not_found
  end

  test "GET #preview for a CSV attachment on an edition that has been unpublished redirects to the edition" do
    unpublished_publication = create(:draft_publication, :unpublished, :with_file_attachment, attachments: [build(:csv_attachment)])
    attachment_data = unpublished_publication.attachments.first.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_redirected_to publication_url(unpublished_publication.unpublishing.slug)
  end

  view_test "GET #preview handles CsvPreview::FileEncodingError errors" do
    visible_edition = create(:published_publication, :with_file_attachment, attachments: [
      attachment = build(:csv_attachment)
    ])
    attachment_data = attachment.attachment_data

    CsvPreview.expects(:new).raises(CsvPreview::FileEncodingError)

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_equal visible_edition, assigns(:edition)
    assert_equal attachment, assigns(:attachment)
    assert_response :success
    assert_select 'p.preview-error', text: /This file could not be previewed/
  end

  view_test "GET #preview handles malformed CSV" do
    attachment = build(:csv_attachment, file: fixture_file_upload('malformed.csv'))
    attachment_data = attachment.attachment_data
<commit_msg>Add missing test for replaced CSV attachments<commit_after>require "test_helper"

class CsvPreviewControllerTest < ActionController::TestCase
  def basename(attachment_data)
    File.basename(attachment_data.filename, '.' + attachment_data.file_extension)
  end

  view_test "GET #preview for a CSV attachment on a public edition renders the CSV preview" do
    visible_edition = create(:published_publication, :with_file_attachment, attachments: [
      attachment = build(:csv_attachment)
    ])
    attachment_data = attachment.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_equal visible_edition, assigns(:edition)
    assert_equal attachment, assigns(:attachment)
    assert assigns(:csv_preview).is_a?(CsvPreview)
    assert_response :success
    assert_select '.headings h1', attachment.title
  end

  view_test "GET #preview for a CSV attachment on a public edition has links to document organiastions" do
    org_1 = create(:organisation)
    org_2 = create(:organisation)
    org_3 = create(:organisation)

    attachment = build(:csv_attachment)
    attachment_data = attachment.attachment_data

    create(:published_publication, :with_file_attachment, attachments: [attachment], organisations: [org_1, org_2, org_3])

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_select 'a[href=?]', organisation_path(org_1)
    assert_select 'a[href=?]', organisation_path(org_2)
    assert_select 'a[href=?]', organisation_path(org_3)
  end

  test "GET #preview for a CSV attachment on a non-public edition returns a not found response" do
    unpublished_edition = create(:draft_publication, :with_file_attachment, attachments: [build(:csv_attachment)])
    attachment_data = unpublished_edition.attachments.first.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_response :not_found
  end

  test "GET #preview for a non-CSV file type returns a not found response" do
    visible_edition = create(:published_publication, :with_file_attachment, attachments: [build(:file_attachment)])
    attachment_data = visible_edition.attachments.first.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_response :not_found
  end

  test "GET #preview for a CSV attachment on an edition that has been unpublished redirects to the edition" do
    unpublished_publication = create(:draft_publication, :unpublished, :with_file_attachment, attachments: [build(:csv_attachment)])
    attachment_data = unpublished_publication.attachments.first.attachment_data

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_redirected_to publication_url(unpublished_publication.unpublishing.slug)
  end

  view_test "GET #preview handles CsvPreview::FileEncodingError errors" do
    visible_edition = create(:published_publication, :with_file_attachment, attachments: [
      attachment = build(:csv_attachment)
    ])
    attachment_data = attachment.attachment_data

    CsvPreview.expects(:new).raises(CsvPreview::FileEncodingError)

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_equal visible_edition, assigns(:edition)
    assert_equal attachment, assigns(:attachment)
    assert_response :success
    assert_select 'p.preview-error', text: /This file could not be previewed/
  end

  test "attachments that aren't visible and have been replaced are permanently redirected to the replacement attachment" do
    replacement = create(:csv_attachment)
    attachment_data = create(:attachment_data, replaced_by: replacement.attachment_data)

    get :show, params: { id: attachment_data.to_param, file: basename(attachment_data), extension: attachment_data.file_extension }

    assert_redirected_to replacement.url
    assert_equal 301, response.status
    assert_cache_control("max-age=#{Whitehall.uploads_cache_max_age}")
    assert_cache_control("public")
  end

  view_test "GET #preview handles malformed CSV" do
    attachment = build(:csv_attachment, file: fixture_file_upload('malformed.csv'))
    attachment_data = attachment.attachment_data

{'fraction_non_alphanumeric': 0.08542476729115117, 'fraction_numerical': 0.0027100271002710027, 'mean_word_length': 5.626073380171741, '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': '1361896', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2342, 'n_words': 598}