Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Citi OFN Voucher] Add VINE connected app #12886

Merged
merged 12 commits into from
Oct 14, 2024
39 changes: 39 additions & 0 deletions app/services/vine_api_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "faraday"

class VineApiService
rioug marked this conversation as resolved.
Show resolved Hide resolved
attr_reader :api_key, :jwt_generator

def initialize(api_key:, jwt_generator:)
@vine_api_url = ENV.fetch("VINE_API_URL")
@api_key = api_key
@jwt_generator = jwt_generator
end

def my_team
my_team_url = "#{@vine_api_url}/my-team"

jwt = jwt_generator.generate_token
connection = Faraday.new(
request: { timeout: 30 },
headers: {
'X-Authorization': "JWT #{jwt}",
Accept: "application/json"
}
) do |f|
f.request :json
f.response :json
f.request :authorization, 'Bearer', api_key
end

response = connection.get(my_team_url)

if !response.success?
Rails.logger.error "VineApiService#my_team -- response_status: #{response.status}"
Rails.logger.error "VineApiService#my_team -- response: #{response.body}"
end

response
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions spec/services/vine_api_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe VineApiService do
subject(:vine_api) { described_class.new(api_key: vine_api_key, jwt_generator: jwt_service) }

let(:vine_api_url) { "https://vine-staging.openfoodnetwork.org.au/api/v1" }
let(:vine_api_key) { "12345" }
let(:jwt_service) { VineJwtService.new(secret:) }
let(:secret) { "my_secret" }

before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with("VINE_API_URL").and_return(vine_api_url)
rioug marked this conversation as resolved.
Show resolved Hide resolved
end

describe "#my_team" do
let(:my_team_url) { "#{vine_api_url}/my-team" }

it "send a request to the team VINE api endpoint" do
stub_request(:get, my_team_url).to_return(status: 200)

vine_api.my_team

expect(a_request(
:get, "https://vine-staging.openfoodnetwork.org.au/api/v1/my-team"
)).to have_been_made
end

it "sends the VINE api key via a header" do
stub_request(:get, my_team_url).to_return(status: 200)

vine_api.my_team

expect(a_request(:get, "https://vine-staging.openfoodnetwork.org.au/api/v1/my-team").with(
headers: { Authorization: "Bearer #{vine_api_key}" }
)).to have_been_made
end

it "sends JWT token via a header" do
token = "some.jwt.token"
stub_request(:get, my_team_url).to_return(status: 200)

allow(jwt_service).to receive(:generate_token).and_return(token)

vine_api.my_team

expect(a_request(:get, "https://vine-staging.openfoodnetwork.org.au/api/v1/my-team").with(
headers: { 'X-Authorization': "JWT #{token}" }
)).to have_been_made
end

context "when a request succeed", :vcr do
it "returns the response do" do
rioug marked this conversation as resolved.
Show resolved Hide resolved
response = vine_api.my_team

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(:get, my_team_url).to_return(body: "error", status: 401)

expect(Rails.logger).to receive(:error).twice

response = vine_api.my_team

expect(response.success?).to be(false)
end

it "return the response" do
stub_request(:get, my_team_url).to_return(body: "error", status: 401)
response = vine_api.my_team

expect(response.success?).to be(false)
expect(response.body).not_to be_empty
end
end
end
end