<commit_before># encoding: utf-8

require "test_helper"

class DocumentsControllerTest < ActionController::TestCase
  include Rails.application.routes.url_helpers
  include PublicDocumentRoutesHelper
  default_url_options[:host] = 'test.host'

  setup do
    DocumentsController.any_instance.stubs(document_class: GenericEdition)
  end

  test "show responds with 'not found' and default cache control 'max-age' if no document (scheduled for publication or otherwise) exists" do
    user = login_as(:departmental_editor)
    edition = create(:draft_edition)

    get :show, id: edition.document

    assert_response :not_found
    assert_cache_control("max-age=#{Whitehall.default_cache_max_age}")
  end

  test "show responds with 'unpublished' and default cache control 'max-age' if document has been unpublished" do
    user = login_as(:departmental_editor)
    edition = create(:unpublished_edition)

    get :show, id: edition.unpublishing.slug

    assert_response :success
    assert_template :unpublished
    assert_cache_control("max-age=#{Whitehall.default_cache_max_age}")
  end

  test "show redirects to new location if the document has been unpublished and a redirect has been requested" do
    user = login_as(:departmental_editor)
    edition = create(:unpublished_edition)
    edition.unpublishing.update_attributes(redirect: true, alternative_url: "http://gov.uk/some-other-url")

    get :show, id: edition.unpublishing.slug

    assert_response :redirect
    assert_redirected_to edition.unpublishing.alternative_url
  end

  view_test "show responds with 'Coming soon' page and shorter cache control 'max-age' if document is scheduled for publication" do
    user = login_as(:departmental_editor)
    edition = create(:draft_edition, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2)
    edition.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: edition.document<commit_msg>fix cache time test on 404 pages<commit_after># encoding: utf-8

require "test_helper"

class DocumentsControllerTest < ActionController::TestCase
  include Rails.application.routes.url_helpers
  include PublicDocumentRoutesHelper
  default_url_options[:host] = 'test.host'

  setup do
    DocumentsController.any_instance.stubs(document_class: GenericEdition)
  end

  test "show responds with 'not found' and default cache control 'max-age' if no document (scheduled for publication or otherwise) exists" do
    user = login_as(:departmental_editor)
    edition = create(:draft_edition)

    get :show, id: edition.document

    assert_response :not_found
    assert_cache_control("max-age=#{5.minutes}")
  end

  test "show responds with 'unpublished' and default cache control 'max-age' if document has been unpublished" do
    user = login_as(:departmental_editor)
    edition = create(:unpublished_edition)

    get :show, id: edition.unpublishing.slug

    assert_response :success
    assert_template :unpublished
    assert_cache_control("max-age=#{Whitehall.default_cache_max_age}")
  end

  test "show redirects to new location if the document has been unpublished and a redirect has been requested" do
    user = login_as(:departmental_editor)
    edition = create(:unpublished_edition)
    edition.unpublishing.update_attributes(redirect: true, alternative_url: "http://gov.uk/some-other-url")

    get :show, id: edition.unpublishing.slug

    assert_response :redirect
    assert_redirected_to edition.unpublishing.alternative_url
  end

  view_test "show responds with 'Coming soon' page and shorter cache control 'max-age' if document is scheduled for publication" do
    user = login_as(:departmental_editor)
    edition = create(:draft_edition, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2)
    edition.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: edition.document
{'fraction_non_alphanumeric': 0.08202050512628158, 'fraction_numerical': 0.003000750187546887, 'mean_word_length': 4.822416302765648, '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': '5553951', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1169, 'n_words': 317}