VineApiService, add voucher_redemptions

It is used to redeem a voucher
This commit is contained in:
Gaetan Craig-Riou
2024-10-21 14:14:28 +11:00
committed by Rachel Arnould
parent c89b4fb86b
commit b30096317c
3 changed files with 138 additions and 2 deletions

View File

@@ -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

View File

@@ -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:
- "<HIDDEN-VINE-TOKEN>"
Accept:
- application/json
User-Agent:
- Faraday v2.9.0
Content-Type:
- application/json
Authorization:
- "<HIDDEN-AUTHORIZATION-HEADER>"
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

View File

@@ -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