From b48f16b156887ed053513910f722001ebe729872 Mon Sep 17 00:00:00 2001 From: David Biddle Date: Thu, 29 Oct 2026 16:01:02 +0000 Subject: [PATCH] Use latest delivery configurations in batch submissions --- app/lib/batch_submissions_selector.rb | 35 ++-- app/resources/api/v3/forms_resource.rb | 16 ++ spec/lib/batch_submissions_selector_spec.rb | 174 +++++++++++-------- spec/resources/api/v3/forms_resource_spec.rb | 64 +++++++ 4 files changed, 198 insertions(+), 91 deletions(-) create mode 100644 app/resources/api/v3/forms_resource.rb create mode 100644 spec/resources/api/v3/forms_resource_spec.rb diff --git a/app/lib/batch_submissions_selector.rb b/app/lib/batch_submissions_selector.rb index 8b8aa3805..a0515708e 100644 --- a/app/lib/batch_submissions_selector.rb +++ b/app/lib/batch_submissions_selector.rb @@ -1,23 +1,12 @@ class BatchSubmissionsSelector Batch = Data.define(:form_id, :mode, :submissions) - DAILY_BATCH_FORM_DOCUMENT_CONDITION = <<~SQL.squish - form_document->'delivery_configurations' @> '[{"delivery_schedule":"daily"}]'::jsonb - SQL - WEEKLY_BATCH_FORM_DOCUMENT_CONDITION = <<~SQL.squish - form_document->'delivery_configurations' @> '[{"delivery_schedule":"weekly"}]'::jsonb - SQL - class << self def daily_batches(date) Enumerator.new do |yielder| form_ids_and_modes_with_send_daily_submission_batch(date).each do |form_id, mode| submissions = Submission.for_form_and_mode(form_id, mode).on_day(date).order(created_at: :desc) - # If the most recent submission is configured for daily batching, include all submissions on that day in the - # batch. If it is not, do not return a batch for any of the submissions on that day. - next unless submissions.any? && daily_delivery_configuration?(submissions.first.form_document) - yielder << Batch.new(form_id, mode, submissions) end end @@ -28,10 +17,6 @@ def weekly_batches(time_in_week) form_ids_and_modes_with_send_weekly_submission_batch(time_in_week).each do |form_id, mode| submissions = Submission.for_form_and_mode(form_id, mode).in_week(time_in_week).order(created_at: :desc) - # If the most recent submission is configured for weekly batching, include all submissions in that week in - # the batch. If it is not, do not return a batch for any of the submissions in that week. - next unless submissions.any? && weekly_delivery_configuration?(submissions.first.form_document) - yielder << Batch.new(form_id, mode, submissions) end end @@ -40,27 +25,31 @@ def weekly_batches(time_in_week) private def form_ids_and_modes_with_send_daily_submission_batch(date) - # Get all form_ids and modes that have at least one submission on the date with daily batches enabled Submission.on_day(date) - .where(DAILY_BATCH_FORM_DOCUMENT_CONDITION) .distinct .pluck(:form_id, :mode) + .filter { |form_id, mode| form_has_delivery_schedule?(form_id, mode, form_ids_and_states_with_daily_submission) } end def form_ids_and_modes_with_send_weekly_submission_batch(begin_at) - # Get all form_ids and modes that have at least one submission in the week with weekly batches enabled Submission.in_week(begin_at) - .where(WEEKLY_BATCH_FORM_DOCUMENT_CONDITION) .distinct .pluck(:form_id, :mode) + .filter { |form_id, mode| form_has_delivery_schedule?(form_id, mode, form_ids_and_states_with_weekly_submission) } + end + + def form_has_delivery_schedule?(form_id, mode, forms_with_delivery_schedule) + state = Mode.new(mode).preview_draft? ? "draft" : "current" + + { form_id:, state: }.in?(forms_with_delivery_schedule) end - def daily_delivery_configuration?(form_document) - form_document["delivery_configurations"]&.any? { |configuration| configuration["delivery_schedule"] == "daily" } + def form_ids_and_states_with_daily_submission + Api::V3::FormsResource.with_delivery_schedule("daily") end - def weekly_delivery_configuration?(form_document) - form_document["delivery_configurations"]&.any? { |configuration| configuration["delivery_schedule"] == "weekly" } + def form_ids_and_states_with_weekly_submission + Api::V3::FormsResource.with_delivery_schedule("weekly") end end end diff --git a/app/resources/api/v3/forms_resource.rb b/app/resources/api/v3/forms_resource.rb new file mode 100644 index 000000000..328633c9c --- /dev/null +++ b/app/resources/api/v3/forms_resource.rb @@ -0,0 +1,16 @@ +class Api::V3::FormsResource < ActiveResource::Base + self.element_name = "form" + self.site = Settings.forms_api.base_url + self.prefix = "/api/v3/" + self.include_format_in_path = false + + class << self + def with_delivery_schedule(delivery_schedule, **options) + forms_with_delivery_schedule = where(delivery_schedule:, **options) + + forms_with_delivery_schedule.map do |form| + { form_id: form.form_id, state: form.state } + end + end + end +end diff --git a/spec/lib/batch_submissions_selector_spec.rb b/spec/lib/batch_submissions_selector_spec.rb index 2d057cf47..028e9cf4a 100644 --- a/spec/lib/batch_submissions_selector_spec.rb +++ b/spec/lib/batch_submissions_selector_spec.rb @@ -3,41 +3,44 @@ RSpec.describe BatchSubmissionsSelector do let(:form_id) { 101 } + let(:forms_with_daily_batch_delivery) { [{ form_id:, state: "current" }, { form_id:, state: "draft" }] } + let(:forms_with_weekly_batch_delivery) { [{ form_id:, state: "current" }, { form_id:, state: "draft" }] } + + before do + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=daily", {}, forms_with_daily_batch_delivery.to_json, 200 + mock.get "/api/v3/forms?delivery_schedule=weekly", {}, forms_with_weekly_batch_delivery.to_json, 200 + end + end + describe ".daily_batches" do subject(:daily_batches) { described_class.daily_batches(date) } let(:date) { Time.zone.local(2022, 12, 1) } - let(:form_document_with_batch_enabled) do - build(:form_document, delivery_configurations: [ - build(:delivery_configuration, :daily_email), - ]) - end - let(:form_document_with_batch_disabled) do - build(:form_document, delivery_configurations: [ - build(:delivery_configuration, :immediate_email), - ]) + let(:form_document) do + build(:form_document, form_id:) end - context "when the form document has a daily delivery_configuration" do + context "when the form has a daily delivery configuration enabled" do context "when the date is during BST" do let(:date) { Time.zone.local(2022, 6, 1) } let!(:form_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 5, 31, 23, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 5, 31, 23, 0, 0), form_document:) end let!(:preview_draft_submission) do - create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2022, 6, 1, 22, 59, 59), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2022, 6, 1, 22, 59, 59), form_document:) end before do # create form/mode combinations that only have submissions outside the BST day - create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2022, 5, 31, 22, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2022, 6, 1, 23, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2022, 5, 31, 22, 59, 59), form_document:) + create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2022, 6, 1, 23, 0, 0), form_document:) # create submissions for the form/mode included in a batch outside the BST day to ensure they are excluded - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2022, 5, 31, 22, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2022, 6, 1, 23, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2022, 5, 31, 22, 59, 59), form_document:) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2022, 6, 1, 23, 0, 0), form_document:) end it "includes only forms/modes with submissions on the date" do @@ -57,20 +60,20 @@ let(:date) { Time.zone.local(2022, 12, 1) } let!(:form_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 12, 1, 0, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 12, 1, 0, 0, 0), form_document:) end let!(:preview_draft_submission) do - create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2022, 12, 1, 23, 59, 59), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2022, 12, 1, 23, 59, 59), form_document:) end before do # create form/mode combinations that only have submissions outside the day - create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2022, 11, 30, 23, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2022, 12, 2, 0, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2022, 11, 30, 23, 59, 59), form_document:) + create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2022, 12, 2, 0, 0, 0), form_document:) # create submissions for the form/mode included in a batch outside the day to ensure they are excluded - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2022, 11, 30, 23, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2022, 12, 2, 0, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2022, 11, 30, 23, 59, 59), form_document:) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2022, 12, 2, 0, 0, 0), form_document:) end it "includes only forms/modes with submissions on the date" do @@ -87,12 +90,22 @@ end end - context "when a daily delivery_configuration is added part-way through the day for the form document" do - let!(:latest_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_enabled) - end - let!(:earlier_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED2", created_at: Time.utc(2022, 12, 1, 9, 0, 0), form_document: form_document_with_batch_disabled) + context "when a daily delivery configuration is added part-way through the day for the form" do + let(:latest_submission_reference) { "INCLUDED2" } + let(:earlier_submission_reference) { "INCLUDED1" } + + before do + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=daily", {}, [].to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", reference: earlier_submission_reference, created_at: Time.utc(2022, 12, 1, 9, 0, 0), form_document:) + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=daily", {}, forms_with_daily_batch_delivery.to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", reference: latest_submission_reference, created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document:) end it "includes a batch for the form and mode" do @@ -103,13 +116,15 @@ it "includes all the submissions in the batch" do submissions = daily_batches.first.submissions - expect(submissions.pluck(:reference)).to contain_exactly(latest_submission.reference, earlier_submission.reference) + expect(submissions.pluck(:reference)).to contain_exactly(latest_submission_reference, earlier_submission_reference) end end - context "when a daily delivery_configuration does not exist for the form document" do + context "when a daily delivery configuration does not exist for the form" do + let(:forms_with_daily_batch_delivery) { [] } + before do - create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_disabled) + create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document:) end it "does not include a batch for the form and mode" do @@ -117,10 +132,19 @@ end end - context "when a daily delivery_configuration is removed part-way through the day for the form document" do + context "when a daily delivery configuration is removed part-way through the day for the form" do before do - create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_disabled) - create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 9, 0, 0), form_document: form_document_with_batch_enabled) + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=daily", {}, forms_with_daily_batch_delivery.to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 12, 1, 9, 0, 0), form_document:) + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=daily", {}, [].to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED2", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document:) end it "does not include a batch for the form and mode" do @@ -134,36 +158,31 @@ let(:date) { Time.zone.local(2025, 5, 19) } - let(:form_document_with_batch_enabled) do - build(:form_document, delivery_configurations: [ - build(:delivery_configuration, :weekly_email), - ]) - end - let(:form_document_with_batch_disabled) do - build(:form_document, delivery_configurations: [ - build(:delivery_configuration, :immediate_email), - ]) + let(:form_document) do + build(:form_document, form_id:) end - context "when the form document has a weekly delivery_configuration" do + context "when the form has a weekly delivery configuration enabled" do + let(:forms_with_weekly_batch_delivery) { [{ form_id:, state: "current" }, { form_id:, state: "draft" }] } + context "when the week is during BST" do let(:date) { Time.zone.local(2025, 5, 19) } let!(:form_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 5, 18, 23, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 5, 18, 23, 0, 0), form_document:) end let!(:preview_draft_submission) do - create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 5, 25, 22, 59, 59), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 5, 25, 22, 59, 59), form_document:) end before do # create form/mode combinations that only have submissions outside the BST week - create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document:) + create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document:) # create submissions for the form/mode included in a batch outside the BST week to ensure they are excluded - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document:) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document:) end it "includes only forms/modes with submissions in the week" do @@ -183,20 +202,20 @@ let(:date) { Time.zone.local(2025, 11, 3) } let!(:form_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 11, 3, 0, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 11, 3, 0, 0, 0), form_document:) end let!(:preview_draft_submission) do - create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 11, 9, 23, 59, 59), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 11, 9, 23, 59, 59), form_document:) end before do # create form/mode combinations that only have submissions outside the week - create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document:) + create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document:) # create submissions for the form/mode included in a batch outside the week to ensure they are excluded - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document: form_document_with_batch_enabled) - create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document: form_document_with_batch_enabled) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document:) + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document:) end it "includes only forms/modes with submissions in the week" do @@ -213,14 +232,23 @@ end end - context "when a weekly delivery_configuration is added part-way through the day for the form document" do + context "when a weekly delivery configuration is added part-way through the day for the form" do let(:date) { Time.zone.local(2025, 11, 3) } + let(:latest_submission_reference) { "INCLUDED2" } + let(:earlier_submission_reference) { "INCLUDED1" } - let!(:latest_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document: form_document_with_batch_enabled) - end - let!(:earlier_submission) do - create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED2", created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document: form_document_with_batch_disabled) + before do + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=weekly", {}, [].to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", reference: earlier_submission_reference, created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document:) + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=weekly", {}, forms_with_weekly_batch_delivery.to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", reference: latest_submission_reference, created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document:) end it "includes a batch for the form and mode" do @@ -231,15 +259,16 @@ it "includes all the submissions in the batch" do submissions = weekly_batches.first.submissions - expect(submissions.pluck(:reference)).to contain_exactly(latest_submission.reference, earlier_submission.reference) + expect(submissions.pluck(:reference)).to contain_exactly(latest_submission_reference, earlier_submission_reference) end end - context "when a weekly delivery_configuration does not exist for the form document" do + context "when a weekly delivery configuration does not exist for the form" do let(:date) { Time.zone.local(2025, 11, 3) } + let(:forms_with_weekly_batch_delivery) { [] } before do - create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 10, 0, 0), form_document: form_document_with_batch_disabled) + create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 10, 0, 0), form_document:) end it "does not include a batch for the form and mode" do @@ -247,12 +276,21 @@ end end - context "when a weekly delivery_configuration is removed part-way through the week for the form document" do + context "when a weekly delivery configuration is removed part-way through the week for the form" do let(:date) { Time.zone.local(2025, 11, 3) } before do - create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document: form_document_with_batch_disabled) - create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document: form_document_with_batch_enabled) + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=weekly", {}, forms_with_weekly_batch_delivery.to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document:) + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=weekly", {}, [].to_json, 200 + end + + create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document:) end it "does not include a batch for the form and mode" do diff --git a/spec/resources/api/v3/forms_resource_spec.rb b/spec/resources/api/v3/forms_resource_spec.rb new file mode 100644 index 000000000..a79760b57 --- /dev/null +++ b/spec/resources/api/v3/forms_resource_spec.rb @@ -0,0 +1,64 @@ +require "rails_helper" + +RSpec.describe Api::V3::FormsResource do + let(:req_headers) { { "Accept" => "application/json" } } + + describe ".with_delivery_schedule" do + let(:immediate_delivery_forms) { [{ form_id: 1, state: "draft" }, { form_id: 2, state: "draft" }, { form_id: 2, state: "draft" }] } + let(:daily_delivery_forms) { [{ form_id: 3, state: "draft" }, { form_id: 4, state: "draft" }, { form_id: 4, state: "draft" }] } + let(:weekly_delivery_forms) { [{ form_id: 5, state: "draft" }, { form_id: 5, state: "draft" }, { form_id: 5, state: "draft" }] } + + before do + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=immediate", req_headers, immediate_delivery_forms.to_json, 200 + mock.get "/api/v3/forms?delivery_schedule=daily", req_headers, daily_delivery_forms.to_json, 200 + mock.get "/api/v3/forms?delivery_schedule=weekly", req_headers, weekly_delivery_forms.to_json, 200 + end + end + + context "when requesting forms with the immediate delivery schedule" do + it "returns an array of form ids and states" do + form_document = described_class.with_delivery_schedule("immediate") + expect(form_document).to eq immediate_delivery_forms + end + end + + context "when requesting forms with the daily delivery schedule" do + it "returns an array of form ids and states" do + form_document = described_class.with_delivery_schedule("daily") + expect(form_document).to eq daily_delivery_forms + end + end + + context "when requesting forms with the weekly delivery schedule" do + it "returns an array of form ids and states" do + form_document = described_class.with_delivery_schedule("weekly") + expect(form_document).to eq weekly_delivery_forms + end + end + + it "raises an exception if the schedule is not valid" do + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v3/forms?delivery_schedule=fortnightly", req_headers, "", 400 + end + + expect { + described_class.with_delivery_schedule("fortnightly") + }.to raise_error(ActiveResource::BadRequest) + end + + context "when given options" do + let(:request_with_param) { ActiveResource::Request.new(:get, "/api/v3/forms?another=1&delivery_schedule=immediate¶m=value") } + + before do + mock_response = ActiveResource::Response.new("{}") + ActiveResource::HttpMock.respond_to(request_with_param => mock_response) + end + + it "adds params to the request" do + described_class.with_delivery_schedule("immediate", param: :value, another: 1) + expect(ActiveResource::HttpMock.requests).to include request_with_param + end + end + end +end