Skip to content

Commit

Permalink
Merge pull request #1227 from alphagov/gift-week-email-alert-api
Browse files Browse the repository at this point in the history
Gift week email alert api
  • Loading branch information
peteglondon authored Jan 11, 2024
2 parents b223b91 + ac80426 commit d35ea1f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/gds_api/email_alert_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ def get_subscriber_list(slug:)
get_json("#{endpoint}/subscriber-lists/#{uri_encode(slug)}")
end

# Get a Subscriber List
#
# @param [path] path of page for subscriber list
#
# @return [Hash] {
# subscriber_list_count
# all_notify_count
# }

def get_subscriber_list_metrics(path:)
get_json("#{endpoint}/subscriber-lists/metrics#{path}")
end

# Get a Subscription
#
# @return [Hash] subscription: {
Expand Down
10 changes: 10 additions & 0 deletions lib/gds_api/test_helpers/email_alert_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ def stub_any_email_alert_api_call
stub_request(:any, %r{\A#{EMAIL_ALERT_API_ENDPOINT}})
end

def stub_get_subscriber_list_metrics(path:, response:)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/metrics#{path}")
.to_return(status: 200, body: response)
end

def stub_get_subscriber_list_metrics_not_found(path:)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/metrics#{path}")
.to_return(status: 404)
end

def assert_email_alert_api_content_change_created(attributes = nil)
if attributes
matcher = lambda do |request|
Expand Down
22 changes: 22 additions & 0 deletions test/email_alert_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,28 @@
end
end

describe "get_subscribe_list_metrics" do
it "returns the parsed json" do
stub_get_subscriber_list_metrics(path: "/the/path", response: {
subscriber_list_count: 5,
all_notify_count: 12,
}.to_json)

api_response = api_client.get_subscriber_list_metrics(path: "/the/path")
assert_equal(200, api_response.code)
parsed_body = api_response.to_h
assert_equal({ "subscriber_list_count" => 5, "all_notify_count" => 12 }, parsed_body)
end

it "raises 404 when not found" do
stub_get_subscriber_list_metrics_not_found(path: "/the/path")

assert_raises GdsApi::HTTPNotFound do
api_client.get_subscriber_list_metrics(path: "/the/path")
end
end
end

describe "change_subscriber with an id that needs URI encoding" do
it "encodes the id" do
request = stub_email_alert_api_has_updated_subscriber("string%20id", "[email protected]")
Expand Down
20 changes: 20 additions & 0 deletions test/test_helpers/email_alert_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@
assert_equal(2, result["subscriptions"].count)
end
end

describe "#stub_get_subscriber_list_metrics_not_found" do
it "raises 404" do
stub_get_subscriber_list_metrics_not_found(path: "/some/path")
assert_raises(GdsApi::HTTPNotFound) do
email_alert_api.get_subscriber_list_metrics(path: "/some/path")
end
end
end

describe "#stub_get_subscriber_list_metrics" do
it "returns the stubbed data" do
json = { subscriber_list_count: 3, all_notify_count: 10 }.to_json
stub_get_subscriber_list_metrics(path: "/some/path", response: json)
response = email_alert_api.get_subscriber_list_metrics(path: "/some/path")
expected = { "subscriber_list_count" => 3, "all_notify_count" => 10 }
assert_equal 200, response.code
assert_equal expected, response.to_h
end
end
end

0 comments on commit d35ea1f

Please sign in to comment.