require "spec_helper"

RSpec.describe "When account is enabled", type: :request do
  before do
    allow(Rails.configuration).to receive(:feature_flag_govuk_accounts).and_return(true)
  end

  context "and accounts is returning a 503 error code" do
    before { stub_request(:get, Services.accounts_api).to_return(status: 503) }

    describe "/login" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_new_session_path

        expect(response).to redirect_to(Services.accounts_api)
      end
    end

    describe "/login/callback" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_new_session_callback_path

        expect(response).to redirect_to(Services.accounts_api)
      end
    end

    describe "/logout" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_end_session_path

        expect(response.body).to eq("Redirecting to #{Services.accounts_api}/sign-out?continue=1")
      end

      context "With a continue parameter" do
        it "redirects a user to the accounts 503 error page" do
          get transition_checker_end_session_path, params: { continue: 1 }

          expect(response.body).to eq("Redirecting to #{Services.accounts_api}/sign-out?done=1")
        end
      end

      context "With a done parameter" do
        it "redirects a user to /transition" do
          get transition_checker_end_session_path, params: { done: 1 }

          expect(response.body).to eq("Redirecting to /transition")
        end
      end
    end

    describe "/save-your-results" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_save_results_path

        expect(response).to redirect_to(Services.accounts_api)
      end
    end

    describe "/save-your-results/confirm" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_save_results_confirm_path

        expect(response).to redirect_to(Services.accounts_api)
      end
    end

    describe "/save-your-results/email-signup" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_save_results_email_signup_path

        expect(response).to redirect_to(Services.accounts_api)
      end
    end

    describe "/saved-results" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_saved_results_path

        expect(response).to redirect_to(Services.accounts_api)
      end
    end

    describe "/edit-saved-results" do
      it "redirects a user to the accounts 503 error page" do
        get transition_checker_edit_saved_results_path

        expect(response).to redirect_to(Services.accounts_api)
      end
    end
  end
end

Add requests specs for account suspension
Add requests specs for account suspension

We have implimented an "emergency brake" feature on accounts that will
allow us to suspend the account. This will temporarily remove public
access to accounts to allow us to either perform some maintance or
respond to an incident.

In these cases the account will return a 503 HTTP status code.

The checker should then revert all views back to email only
subscriptions, allowing it to gracefully handle the lack of accounts and
prevent new user journies into the account world.

Routes that are exclusive to checker accounts integration (so for a
small number of users mid-journey when we suspend) we want to halt their
journey and redirect them to the account 503 page which will have a
message explaining what is currently happening with accounts.

The paths targeted here are all those used by the `check_account_enabled`
callback, defining expected behaviour.
