From b30096317c173fadd22ed2e73f459346bed6f17d Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 21 Oct 2024 14:14:28 +1100 Subject: [PATCH] VineApiService, add voucher_redemptions It is used to redeem a voucher --- app/services/vine_api_service.rb | 14 ++++ .../returns_the_response.yml | 55 ++++++++++++++ spec/services/vine_api_service_spec.rb | 71 ++++++++++++++++++- 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/VineApiService/_voucher_redemptions/when_a_request_succeed/returns_the_response.yml diff --git a/app/services/vine_api_service.rb b/app/services/vine_api_service.rb index 131e83b239..89b7b1a348 100644 --- a/app/services/vine_api_service.rb +++ b/app/services/vine_api_service.rb @@ -35,6 +35,20 @@ class VineApiService response end + def voucher_redemptions(voucher_id, voucher_set_id, amount) + voucher_redemptions_url = "#{@vine_api_url}/voucher-redemptions" + + response = connection.post( + voucher_redemptions_url, + { voucher_id:, voucher_set_id:, amount: amount.to_i }, + 'Content-Type': "application/json" + ) + + log_error("VineApiService#voucher_redemptions", response) + + response + end + private def connection diff --git a/spec/fixtures/vcr_cassettes/VineApiService/_voucher_redemptions/when_a_request_succeed/returns_the_response.yml b/spec/fixtures/vcr_cassettes/VineApiService/_voucher_redemptions/when_a_request_succeed/returns_the_response.yml new file mode 100644 index 0000000000..af1786e786 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/VineApiService/_voucher_redemptions/when_a_request_succeed/returns_the_response.yml @@ -0,0 +1,55 @@ +--- +http_interactions: +- request: + method: post + uri: https://vine-staging.openfoodnetwork.org.au/api/v1/voucher-redemptions + body: + encoding: UTF-8 + string: '{"voucher_id":"9d316d27-0dad-411a-8953-316a1aaf7742","voucher_set_id":"9d314daa-0878-4b73-922d-698047640cf4","amount":1}' + headers: + X-Authorization: + - "" + Accept: + - application/json + User-Agent: + - Faraday v2.9.0 + Content-Type: + - application/json + Authorization: + - "" + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Vary: + - Accept-Encoding + Cache-Control: + - no-cache, private + Date: + - Mon, 21 Oct 2024 03:07:09 GMT + Access-Control-Allow-Origin: + - "*" + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + body: + encoding: ASCII-8BIT + string: '{"meta":{"responseCode":200,"limit":50,"offset":0,"message":"Redemption + successful. This was a test redemption. Do NOT provide the person with goods + or services."},"data":{"voucher_id":"9d316d27-0dad-411a-8953-316a1aaf7742","voucher_set_id":"9d314daa-0878-4b73-922d-698047640cf4","redeemed_by_user_id":8,"redeemed_by_team_id":4,"redeemed_amount":1,"is_test":1,"updated_at":"2024-10-21T03:07:09.000000Z","created_at":"2024-10-21T03:07:09.000000Z","id":5}}' + recorded_at: Mon, 21 Oct 2024 03:07:09 GMT +recorded_with: VCR 6.2.0 diff --git a/spec/services/vine_api_service_spec.rb b/spec/services/vine_api_service_spec.rb index e9321fbb84..1a9f9977d4 100644 --- a/spec/services/vine_api_service_spec.rb +++ b/spec/services/vine_api_service_spec.rb @@ -78,8 +78,7 @@ RSpec.describe VineApiService do describe "#voucher_validation" do let(:voucher_validation_url) { "#{vine_api_url}/voucher-validation" } - let(:voucher_short_code) { "123456" } - # let(:voucher_short_code) { "CI3922" } + let(:voucher_short_code) { "CI3922" } it "send a POST request to the team VINE api endpoint" do stub_request(:post, voucher_validation_url).to_return(status: 200) @@ -143,6 +142,74 @@ RSpec.describe VineApiService do end end + describe "#voucher_redemptions" do + let(:voucher_redemptions_url) { "#{vine_api_url}/voucher-redemptions" } + let(:voucher_id) { "quia" } + let(:voucher_set_id) { "natus" } + let(:amount) { 500 } # $5.00 + + it "send a POST request to the voucher redemptions VINE api endpoint" do + stub_request(:post, voucher_redemptions_url).to_return(status: 200) + vine_api.voucher_redemptions(voucher_id, voucher_set_id, amount) + + expect(a_request( + :post, "https://vine-staging.openfoodnetwork.org.au/api/v1/voucher-redemptions" + ).with(body: { voucher_id:, voucher_set_id:, amount: })).to have_been_made + end + + it "sends the VINE api key via a header" do + stub_request(:post, voucher_redemptions_url).to_return(status: 200) + + vine_api.voucher_redemptions(voucher_id, voucher_set_id, amount) + + expect_request_with_api_key( + :post, "https://vine-staging.openfoodnetwork.org.au/api/v1/voucher-redemptions" + ) + end + + it "sends JWT token via a header" do + stub_request(:post, voucher_redemptions_url).to_return(status: 200) + mock_jwt_service + + vine_api.voucher_redemptions(voucher_id, voucher_set_id, amount) + + expect_request_with_jwt_token( + :post, "https://vine-staging.openfoodnetwork.org.au/api/v1/voucher-redemptions" + ) + end + + context "when a request succeed", :vcr do + it "returns the response" do + response = vine_api.voucher_redemptions(voucher_id, voucher_set_id, amount) + + expect(response.success?).to be(true) + expect(response.body).not_to be_empty + end + end + + context "when a request fails" do + it "logs the error" do + stub_request(:post, voucher_redemptions_url).to_return(body: "error", status: 401) + + expect(Rails.logger).to receive(:error).with( + match("VineApiService#voucher_redemptions") + ).twice + + response = vine_api.voucher_redemptions(voucher_id, voucher_set_id, amount) + + expect(response.success?).to be(false) + end + + it "return the response" do + stub_request(:post, voucher_redemptions_url).to_return(body: "error", status: 401) + response = vine_api.voucher_redemptions(voucher_id, voucher_set_id, amount) + + expect(response.success?).to be(false) + expect(response.body).not_to be_empty + end + end + end + def expect_request_with_api_key(method, url) expect(a_request(method, url).with( headers: { Authorization: "Bearer #{vine_api_key}" })) .to have_been_made