From 61e46eca4a6ee771e05cfa7c8f48786d69cfc911 Mon Sep 17 00:00:00 2001 From: D Chua Date: Tue, 28 May 2024 14:39:42 +0100 Subject: [PATCH 1/4] Add Support API mock service This will be used to run Pact tests against Support API. --- test/support/pact_helper.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/support/pact_helper.rb b/test/support/pact_helper.rb index 06c68312..7bcb8378 100644 --- a/test/support/pact_helper.rb +++ b/test/support/pact_helper.rb @@ -9,6 +9,7 @@ LOCATIONS_API_PORT = 3008 ASSET_MANAGER_API_PORT = 3009 EMAIL_ALERT_API_PORT = 3010 +SUPPORT_API_PORT = 3011 def publishing_api_host "http://localhost:#{PUBLISHING_API_PORT}" @@ -46,6 +47,10 @@ def email_alert_api_host "http://localhost:#{EMAIL_ALERT_API_PORT}" end +def support_api_host + "http://localhost:#{SUPPORT_API_PORT}" +end + Pact.service_consumer "GDS API Adapters" do has_pact_with "Publishing API" do mock_service :publishing_api do @@ -100,4 +105,10 @@ def email_alert_api_host port EMAIL_ALERT_API_PORT end end + + has_pact_with "Support API" do + mock_service :support_api do + port SUPPORT_API_PORT + end + end end From 9aabf9d7c36f075143fc96cc9c3d877c71d64890 Mon Sep 17 00:00:00 2001 From: D Chua Date: Thu, 30 May 2024 11:57:01 +0100 Subject: [PATCH 2/4] Update support-api test helpers We should also stub when an invalid post request is sent to the `/support-tickets` endpoint. --- lib/gds_api/support_api.rb | 6 ++---- lib/gds_api/test_helpers/support_api.rb | 10 ++++++++-- test/support_api_test.rb | 21 ++++++++++++++++++--- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/gds_api/support_api.rb b/lib/gds_api/support_api.rb index 2b879076..18eb76a0 100644 --- a/lib/gds_api/support_api.rb +++ b/lib/gds_api/support_api.rb @@ -31,10 +31,8 @@ def create_global_export_request(request_details) # SupportApi.raise_support_ticket( # subject: "Feedback for app", # tags: ["app_name"] - # body: { - # "User agent": "Safari", - # "Details": "Ticket details go here.", - # } + # user_agent: "Safari", + # description: "Ticket details go here.", # ) def raise_support_ticket(params) post_json("#{endpoint}/support-tickets", params) diff --git a/lib/gds_api/test_helpers/support_api.rb b/lib/gds_api/test_helpers/support_api.rb index 58997191..9f1f0fdd 100644 --- a/lib/gds_api/test_helpers/support_api.rb +++ b/lib/gds_api/test_helpers/support_api.rb @@ -150,10 +150,16 @@ def stub_support_api_feedback_export_request(id, response_body = nil) .to_return(status: 200, body: response_body.to_json) end - def stub_support_api_raise_support_ticket(params) + def stub_support_api_valid_raise_support_ticket(params) post_stub = stub_http_request(:post, "#{SUPPORT_API_ENDPOINT}/support-tickets") post_stub.with(body: params) - post_stub.to_return(status: 201) + post_stub.to_return(status: 201, body: { status: "success" }.to_json) + end + + def stub_support_api_invalid_raise_support_ticket(params) + post_stub = stub_http_request(:post, "#{SUPPORT_API_ENDPOINT}/support-tickets") + post_stub.with(body: params) + post_stub.to_return(status: 422, body: { status: "error" }.to_json) end def stub_any_support_api_call diff --git a/test/support_api_test.rb b/test/support_api_test.rb index e75ae16c..6452b8ca 100644 --- a/test/support_api_test.rb +++ b/test/support_api_test.rb @@ -242,13 +242,28 @@ end describe "POST /support-tickets" do - it "makes a POST request to the support API" do - params = { subject: "Feedback for app", tags: "app_name", details: "Ticket details go here." } - stub_post = stub_support_api_raise_support_ticket(params) + it "makes a valid POST request to the support API" do + params = { + subject: "Feedback for app", + tags: "app_name", + user_agent: "Safari", + description: "There is something wrong with this page.", + } + stub_post = stub_support_api_valid_raise_support_ticket(params) @api.raise_support_ticket(params) assert_requested(stub_post) end + + it "makes an invalid POST request to the support API" do + params = { subject: "Ticket without body" } + + stub_support_api_invalid_raise_support_ticket(params) + + assert_raises GdsApi::HTTPUnprocessableEntity do + @api.raise_support_ticket(params) + end + end end end From 82226b9b74bf347b348c6c1b92526a2c9c50b01c Mon Sep 17 00:00:00 2001 From: D Chua Date: Fri, 31 May 2024 17:19:01 +0100 Subject: [PATCH 3/4] Add Pacts tests for Support API We expect a 201 success if the parameters provided for the `raise_support_ticket` are valid, and a 422 error if not. When these tests are run they output a JSON pactfile, which is published to our [pact broker](https://github.com/alphagov/govuk-pact-broker). --- test/pacts/support_api_pact_test.rb | 67 +++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/pacts/support_api_pact_test.rb diff --git a/test/pacts/support_api_pact_test.rb b/test/pacts/support_api_pact_test.rb new file mode 100644 index 00000000..e8cd7e4c --- /dev/null +++ b/test/pacts/support_api_pact_test.rb @@ -0,0 +1,67 @@ +require "test_helper" +require "gds_api/support_api" + +describe "GdsApi::SupportApi pact tests" do + include PactTest + + describe "#raise_support_ticket" do + let(:api_client) { GdsApi::SupportApi.new(support_api_host) } + + it "responds with a 201 Success if the parameters provided are valid" do + support_api + .given("the parameters are valid") + .upon_receiving("a raise ticket request") + .with( + method: :post, + path: "/support-tickets", + headers: GdsApi::JsonClient.default_request_with_json_body_headers, + body: { + subject: "Feedback for app", + tags: %w[app_name], + user_agent: "Safari", + description: "There is something wrong with this page.", + }, + ) + .will_respond_with( + status: 201, + body: { + status: "success", + }, + headers: { + "Content-Type" => "application/json; charset=utf-8", + }, + ) + + api_client.raise_support_ticket( + subject: "Feedback for app", + tags: %w[app_name], + user_agent: "Safari", + description: "There is something wrong with this page.", + ) + end + + it "responds with 422 Error when required parameters are not provided" do + support_api + .given("the required parameters are not provided") + .upon_receiving("a raise ticket request") + .with( + method: :post, + path: "/support-tickets", + headers: GdsApi::JsonClient.default_request_with_json_body_headers, + body: { + subject: "Ticket without body", + }, + ) + .will_respond_with( + status: 422, + body: { + status: "error", + }, + ) + + assert_raises GdsApi::HTTPUnprocessableEntity do + api_client.raise_support_ticket(subject: "Ticket without body") + end + end + end +end From 4512948e10884aa1466a94b3020734f41f32754c Mon Sep 17 00:00:00 2001 From: D Chua Date: Wed, 5 Jun 2024 17:03:11 +0100 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29e7df63..0e5c7e3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# Unreleased +* Add Pact tests for support-api [PR](https://github.com/alphagov/gds-api-adapters/pull/1273). + # 96.0.1 * Update Pact specs to match the email-alert-api [PR](https://github.com/alphagov/email-alert-api/pull/2136)