From edcd3d53cf1921ba5d915b9bfe6be8ed4ca35a76 Mon Sep 17 00:00:00 2001 From: Jessica Jones Date: Fri, 27 Jan 2023 13:40:35 +0000 Subject: [PATCH] Update email_alert_api.rb to support new params Email Alert API will soon support subscriber lists that have both a content id and links. See: https://github.com/alphagov/email-alert-api/pull/1841 https://github.com/alphagov/email-alert-frontend/pull/1438 --- lib/gds_api/email_alert_api.rb | 4 ++-- test/email_alert_api_test.rb | 36 +++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/gds_api/email_alert_api.rb b/lib/gds_api/email_alert_api.rb index 109dbdd8..a9854f6d 100644 --- a/lib/gds_api/email_alert_api.rb +++ b/lib/gds_api/email_alert_api.rb @@ -11,8 +11,8 @@ class GdsApi::EmailAlertApi < GdsApi::Base # @param attributes [Hash] document_type, links, tags used to search existing subscriber lists def find_or_create_subscriber_list(attributes) present_fields = [attributes["content_id"], attributes["links"], attributes["tags"]].compact.count - if present_fields > 1 - message = "please provide content_id, tags, or links (or none), but not more than one of them" + if (present_fields > 1) && (attributes["tags"]) + message = "Invalid attributes provided. Valid attributes are content_id only, tags only, links only, content_id AND links, or none." raise ArgumentError, message end diff --git a/test/email_alert_api_test.rb b/test/email_alert_api_test.rb index d0b69e34..5ae7bad6 100644 --- a/test/email_alert_api_test.rb +++ b/test/email_alert_api_test.rb @@ -312,7 +312,7 @@ { "title" => title, "content_id" => "fd427f55-7492-440d-ae3f-0fb6c3592b21", - "links" => links, + "tags" => tags, } end @@ -323,6 +323,40 @@ end end end + + describe "when content_id and links are provided" do + let(:content_id) { "fd427f55-7492-440d-ae3f-0fb6c3592b21" } + let(:slug) { "my-new-list" } + let(:links) { { "document_collections" => %w[a_content_id] } } + let(:params) do + { + "title" => title, + "content_id" => content_id, + "links" => links, + "slug" => slug, + } + end + + before do + stub_email_alert_api_creates_subscriber_list( + "title" => title, + "links" => links, + "content_id" => content_id, + "slug" => slug, + ) + end + + it "returns the subscriber list attributes" do + subscriber_list_attrs = api_client.find_or_create_subscriber_list(params) + .to_hash + .fetch("subscriber_list") + + assert_equal( + "my-new-list", + subscriber_list_attrs.fetch("slug"), + ) + end + end end describe "unsubscribing from a topic" do