Skip to content

Commit

Permalink
Update email_alert_api.rb to support new params
Browse files Browse the repository at this point in the history
Email Alert API will soon support subscriber lists that have
both a content id and links. See:

alphagov/email-alert-api#1841
alphagov/email-alert-frontend#1438
  • Loading branch information
hannako committed Jan 27, 2023
1 parent 1760345 commit 6f9e640
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/gds_api/email_alert_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 35 additions & 1 deletion test/email_alert_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@
{
"title" => title,
"content_id" => "fd427f55-7492-440d-ae3f-0fb6c3592b21",
"links" => links,
"tags" => tags,
}
end

Expand All @@ -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
Expand Down

0 comments on commit 6f9e640

Please sign in to comment.