<NME> smart_answers_controller_test.rb
<BEF> require_relative "../test_helper"
require_relative "smart_answers_controller_test_helper"

class SmartAnswersControllerTest < ActionController::TestCase
  include SmartAnswersControllerTestHelper

  setup { setup_fixture_flows }
  teardown { teardown_fixture_flows }

  context "GET /" do
    setup do
      @flow_a = stub(name: "flow-a",
                     class: stub(name: "FlowA"),
                     status: :published,
                     questions: stub(count: 2),
                     outcomes: stub(count: 3),
                     start_node: stub(presenter: stub(title: "Flow A")))

      @flow_b = stub(name: "flow-b",
                     class: stub(name: "FlowB"),
                     status: :draft,
                     questions: stub(count: 3),
                     outcomes: stub(count: 0),
                     start_node: stub(presenter: stub(title: "Flow B")))

      registry = stub("Flow registry")
      outcome :you_have_a_savoury_tooth
      outcome :you_have_a_sweet_tooth
    end
    load_path = fixture_file('smart_answers_controller_test')
    SmartAnswer::FlowRegistry.stubs(:instance).returns(stub("Flow registry", find: @flow, load_path: load_path))
  end

    end

    should "render index template" do
      get :index
      assert_template "index"
    end

    should "render list of links to flows" do
      get :index
      assert_select "table td a[href='/flow-a']", text: "/flow-a"
      assert_select "table td a[href='/flow-b']", text: "/flow-b"
    end

    should "render links to published flows and status" do
      get :index
      assert_select "table td a[href='https://www.gov.uk/flow-a']", text: "Published"
      assert_select "table td a[href='https://draft-origin.publishing.service.gov.uk/flow-b']", text: "Draft"
    end

    should "render links to visualise flows" do
      get :index
      assert_select "table td a[href='/flow-a/y/visualise']", text: "Visualise"
      assert_select "table td a[href='/flow-b/y/visualise']", text: "Visualise"
    end

    should "render links to code" do
      get :index
      assert_select "table td a[href='https://www.github.com/alphagov/smart-answers/blob/main/app/flows/flow_a.rb']", text: "Definition"
      assert_select "table td a[href='https://www.github.com/alphagov/smart-answers/blob/main/app/flows/flow_a']", text: "Content files"

      assert_select "table td a[href='https://www.github.com/alphagov/smart-answers/blob/main/app/flows/flow_b.rb']", text: "Definition"
      assert_select "table td a[href='https://www.github.com/alphagov/smart-answers/blob/main/app/flows/flow_b']", text: "Content files"
    end
  end

  context "GET /<slug>" do
    should "respond with 404 if not found" do
      @registry = stub("Flow registry")
      @registry.stubs(:find).raises(SmartAnswer::FlowRegistry::NotFound)
      @controller.stubs(:flow_registry).returns(@registry)
      get :show, params: { id: "radio-sample" }
      assert_response :missing
    end

    context "when a smart answer exist on the content store" do
      setup do
        @content_item = {
          base_path: "/radio-sample",
          cache_control: {
            "max-age" => 10.minutes.to_i,
            "public" => true,
          },
        }.with_indifferent_access

        ContentItemRetriever.stubs(:fetch)
          .returns(@content_item)

        enable_page_caching

        get :show, params: { id: "radio-sample" }
      end

      should "assign response from content store" do
        assert_equal @content_item, assigns(:content_item)
      end

      should "set Cache-Control header" do
        assert_cached_response(age: 10.minutes.to_i, public: true)
      end
    end

    context "when a smart answer does not exist on the content store" do
    end

    should "display first question after starting" do
      using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
        get :show, id: 'smart-answers-controller-sample', started: 'y'
      end
      assert_select ".step.current h2", /Do you like chocolate\?/
      end
    end
    end

    should "show outcome when smart answer is complete so that 'smartanswerOutcome' JS event is fired" do
      using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
        get :show, id: 'smart-answers-controller-sample', started: 'y', responses: 'yes'
      end
      assert_select ".outcome"
    end

    should "have meta robots noindex on question pages" do
      using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
        get :show, id: 'smart-answers-controller-sample', started: 'y'
      end
      assert_select "head meta[name=robots][content=noindex]"
      assert_select ".outcome"
    end

    should "show default outcome title when none is supplied" do
      get :show, params: { id: "radio-sample", started: "y", responses: "colder/no" }
      assert_contains css_select("title").first.content, /Outcome/
      assert_contains css_select("title").first.content, /Sample radio question/
      assert_select ".outcome"
    end

    should "return a 404 when providing more responses than accepted" do
      get :show, params: { id: "radio-sample", started: "y", responses: "colder/no/extra-response" }
      assert_response :missing
    end

    should "have meta robots noindex on question pages" do
      get :show, params: { id: "radio-sample", started: "y" }
      assert_select "head meta[name=robots][content=noindex]"
    end

    should "accept responses as GET params and redirect to canonical path" do
      submit_response "hotter"
      assert_redirected_to "/radio-sample/y/hotter"
    end

    context "a response has been accepted" do
      setup do
        get :show, params: { id: "radio-sample", started: "y", responses: "colder" }
      end

      should "show response summary" do
        assert_select ".govuk-summary-list", /Hotter or colder\?\s+Colder/
      end

      should "show the next question" do
        assert_select "#current-question", /Frozen\?/
      end

      should "link back to change the response" do
        assert_select ".govuk-summary-list__actions a", /Change/ do |link_nodes|
          assert_equal "/radio-sample/y?previous_response=colder", link_nodes.first["href"]
        end
      end
    end

    context "when content item is private" do
      setup do
        @content_item = {
          base_path: "/radio-sample",
          cache_control: {
            "max-age" => 30.minutes.to_i,
            "public" => false,
          },
        }.with_indifferent_access
        ContentItemRetriever.stubs(:fetch)
          .returns(@content_item)

        enable_page_caching
        get :show, params: { id: "radio-sample", started: "y", responses: "colder" }
      end

      should "set appropriate cache-control header directives" do
        assert_equal "max-age=#{30.minutes.to_i}, private", @response.header["Cache-Control"]
      end
    end

    context "debugging" do
      should "render debug information on the page when enabled" do
        @controller.stubs(:debug?).returns(true)
        get :show, params: { id: "radio-sample", started: "y", responses: "no", debug: "1" }

        assert_select "pre.debug"
      end

      should "not render debug information on the page when not enabled" do
        @controller.stubs(:debug?).returns(false)
        get :show, params: { id: "radio-sample", started: "y", responses: "no", debug: nil }

        assert_select "pre.debug", false, "The page should not render debug information"
      end
    end
  end

  context "GET /<slug>/visualise" do
    should "display the visualisation" do
      stub_content_store_has_item("/radio-sample")

      get :visualise, params: { id: "radio-sample" }

      assert_contains css_select("title").first.content, /Sample radio question/
      assert_select "h1", /Sample radio question/
    end
  end

  context "GET /<slug>/visualise.gz" do
    should "display the visualisation in graphviz format" do
      stub_content_store_has_item("/radio-sample")

      get :visualise, format: :gv, params: { id: "radio-sample" }

      assert_equal "text/vnd.graphviz", response.media_type
      assert_match "digraph", response.body
    end
  end
end

      context "label in translation file" do
        setup do
          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-label.yml')) do
            get :show, id: 'smart-answers-controller-sample', started: 'y'
          end
        end

      context "suffix_label in translation file" do
        setup do
          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-suffix-label.yml')) do
            get :show, id: 'smart-answers-controller-sample', started: 'y'
          end
        end

      context "suffix_label in translation file" do
        setup do
          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-suffix-label.yml')) do
            get :show, id: 'smart-answers-controller-sample', started: 'y'
          end
        end

      context "error message overridden in translation file" do
        setup do
          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-error-message.yml')) do
            submit_response amount: "bad_number"
          end
        end
      context "format=json" do
        context "no response given" do
          should "show an error message" do
            using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-cheese.yml')) do
              submit_json_response(nil)
            end
            data = JSON.parse(response.body)

    context "a response has been accepted" do
      setup do
        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
          get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no"
        end
      end

    context "format=json" do
      should "render content without layout" do
        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
          get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no", format: "json"
        end
        data = JSON.parse(response.body)
    context "debugging" do
      should "render debug information on the page when enabled" do
        @controller.stubs(:debug?).returns(true)
        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
          get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no", debug: "1"
        end


      should "not render debug information on the page when not enabled" do
        @controller.stubs(:debug?).returns(false)
        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
          get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no", debug: nil
        end


<MSG> Move smart_answers_controller_test test flow and locale files

From test/fixtures/smart_answers_controller_test to
test/fixtures/smart_answer_flows.

I'm moving all Smart Answer test flows and locale files to
test/fixtures/smart_answer_flows. The majority of test flows and locale files
already live in that directory but there are also a small number of them living
in test/fixtures or test/fixtures/a-n-other-directory.

<DFF> @@ -27,7 +27,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
       outcome :you_have_a_savoury_tooth
       outcome :you_have_a_sweet_tooth
     end
-    load_path = fixture_file('smart_answers_controller_test')
+    load_path = fixture_file('smart_answer_flows')
     SmartAnswer::FlowRegistry.stubs(:instance).returns(stub("Flow registry", find: @flow, load_path: load_path))
   end
 
@@ -106,7 +106,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
     end
 
     should "display first question after starting" do
-      using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
+      using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample.yml')) do
         get :show, id: 'smart-answers-controller-sample', started: 'y'
       end
       assert_select ".step.current h2", /Do you like chocolate\?/
@@ -115,14 +115,14 @@ class SmartAnswersControllerTest < ActionController::TestCase
     end
 
     should "show outcome when smart answer is complete so that 'smartanswerOutcome' JS event is fired" do
-      using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
+      using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample.yml')) do
         get :show, id: 'smart-answers-controller-sample', started: 'y', responses: 'yes'
       end
       assert_select ".outcome"
     end
 
     should "have meta robots noindex on question pages" do
-      using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
+      using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample.yml')) do
         get :show, id: 'smart-answers-controller-sample', started: 'y'
       end
       assert_select "head meta[name=robots][content=noindex]"
@@ -277,7 +277,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
 
       context "label in translation file" do
         setup do
-          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-label.yml')) do
+          using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample-with-label.yml')) do
             get :show, id: 'smart-answers-controller-sample', started: 'y'
           end
         end
@@ -289,7 +289,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
 
       context "suffix_label in translation file" do
         setup do
-          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-suffix-label.yml')) do
+          using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample-with-suffix-label.yml')) do
             get :show, id: 'smart-answers-controller-sample', started: 'y'
           end
         end
@@ -327,7 +327,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
 
       context "suffix_label in translation file" do
         setup do
-          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-suffix-label.yml')) do
+          using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample-with-suffix-label.yml')) do
             get :show, id: 'smart-answers-controller-sample', started: 'y'
           end
         end
@@ -359,7 +359,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
 
       context "error message overridden in translation file" do
         setup do
-          using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample-with-error-message.yml')) do
+          using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample-with-error-message.yml')) do
             submit_response amount: "bad_number"
           end
         end
@@ -421,7 +421,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
       context "format=json" do
         context "no response given" do
           should "show an error message" do
-            using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-cheese.yml')) do
+            using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-cheese.yml')) do
               submit_json_response(nil)
             end
             data = JSON.parse(response.body)
@@ -439,7 +439,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
 
     context "a response has been accepted" do
       setup do
-        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
+        using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample.yml')) do
           get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no"
         end
       end
@@ -461,7 +461,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
 
     context "format=json" do
       should "render content without layout" do
-        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
+        using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample.yml')) do
           get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no", format: "json"
         end
         data = JSON.parse(response.body)
@@ -516,7 +516,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
     context "debugging" do
       should "render debug information on the page when enabled" do
         @controller.stubs(:debug?).returns(true)
-        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
+        using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample.yml')) do
           get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no", debug: "1"
         end
 
@@ -525,7 +525,7 @@ class SmartAnswersControllerTest < ActionController::TestCase
 
       should "not render debug information on the page when not enabled" do
         @controller.stubs(:debug?).returns(false)
-        using_additional_translation_file(fixture_file('smart_answers_controller_test/smart-answers-controller-sample.yml')) do
+        using_additional_translation_file(fixture_file('smart_answer_flows/locales/en/smart-answers-controller-sample.yml')) do
           get :show, id: 'smart-answers-controller-sample', started: 'y', responses: "no", debug: nil
         end
 

{'addition_count': 13, 'commit_subject': 'Move smart_answers_controller_test test flow and locale files', 'deletion_count': 13, 'file_extension': '.rb', 'lang': 'rb', 'license': 'mit', 'repo_name': 'alphagov/smart-answers'}