Skip to content

Commit

Permalink
Add update_subscriber_list_details (for Email Alert API)
Browse files Browse the repository at this point in the history
Allow PATCH updates to subscriber list titles

See also: alphagov/email-alert-api#1704
  • Loading branch information
huwd committed Jan 21, 2022
1 parent 033ffc0 commit bb50efe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Add `update_subscriber_list_details` (for Email Alert API)

# 78.1.0

* Fix `bulk_unsubscribe` requires a `govuk_request_id` if you want to send a message
Expand Down
11 changes: 11 additions & 0 deletions lib/gds_api/email_alert_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ def bulk_unsubscribe(slug:, govuk_request_id: nil, body: nil, sender_message_id:
)
end

# Update subscriber list details, such as title
# @param [Hash] params A hash of detail paramaters that can be updated. For example title.
# For allowed parameters see:
# https://github.com/alphagov/email-alert-api/blob/main/docs/api.md#patch-subscriber-listsxxx
def update_subscriber_list_details(slug:, params: {})
patch_json(
"#{endpoint}/subscriber-lists/#{slug}",
params.compact,
)
end

private

def nested_query_string(params)
Expand Down
20 changes: 20 additions & 0 deletions lib/gds_api/test_helpers/email_alert_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,26 @@ def stub_email_alert_api_bulk_unsubscribe_bad_request(slug:, body:)
).to_return(status: 422)
end

def stub_update_subscriber_list_details(slug:, params:)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.with(body: params.to_json)
.to_return(
status: 200,
body: get_subscriber_list_response(params).to_json,
)
end

def stub_update_subscriber_list_details_not_found(slug:, params:)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.with(body: params.to_json)
.to_return(status: 404)
end

def stub_update_subscriber_list_details_bad_request(slug:)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.to_return(status: 400)
end

private

def get_subscriber_response(id, address, govuk_account_id)
Expand Down
32 changes: 32 additions & 0 deletions test/email_alert_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,4 +885,36 @@
end
end
end

describe "update_subscriber_list_details" do
let(:slug) { "i_am_a_subscriber_list_slug" }

it "returns 200 and subscriber list with updated title" do
params = { "title" => "New Title" }

stub_update_subscriber_list_details(slug: slug, params: params)
api_response = api_client.update_subscriber_list_details(slug: slug, params: params)

assert_equal(200, api_response.code)
assert_equal(params["title"], api_response.to_h.dig("subscriber_list", "title"))
end

it "returns 404 when subscriber list is not found" do
params = { "title" => "New Title" }

stub_update_subscriber_list_details_not_found(slug: slug, params: params)

assert_raises GdsApi::HTTPNotFound do
api_client.update_subscriber_list_details(slug: slug, params: params)
end
end

it "returns 400 when provided with no parameters" do
stub_update_subscriber_list_details_bad_request(slug: slug)

assert_raises GdsApi::HTTPBadRequest do
api_client.update_subscriber_list_details(slug: slug, params: {})
end
end
end
end

0 comments on commit bb50efe

Please sign in to comment.