Add logic for ConnectedApps::Vine#connect and disconnect

This commit is contained in:
Gaetan Craig-Riou
2024-10-02 11:00:42 +10:00
parent 097c6dee2f
commit f980cb45f6
3 changed files with 59 additions and 5 deletions

View File

@@ -4,12 +4,18 @@
#
module ConnectedApps
class Vine < ConnectedApp
def connect(api_key: )
# TODO
VINE_API_URL = "https://vine-staging.openfoodnetwork.org.au/api/v1/my-team"
def connect(api_key:, vine_api:, **_opts)
response = vine_api.my_team
return update data: { api_key: } if response.success?
errors.add(:base, I18n.t("activerecord.errors.models.connected_apps.vine.api_request_error"))
false
end
def disconnect
# TODO
end
def disconnect; end
end
end

View File

@@ -128,6 +128,9 @@ en:
count_on_hand:
using_producer_stock_settings_but_count_on_hand_set: "must be blank because using producer stock settings"
limited_stock_but_no_count_on_hand: "must be specified because forcing limited stock"
connected_apps:
vine:
api_request_error: "An error occured when connecting to Vine API"
messages:
confirmation: "doesn't match %{attribute}"
blank: "can't be blank"

View File

@@ -0,0 +1,45 @@
# frozen_string_literal: true
require "spec_helper"
RSpec.describe ConnectedApps::Vine do
subject(:connected_app) { ConnectedApps::Vine.new(enterprise: create(:enterprise)) }
let(:vine_api_key) { "12345" }
let(:vine_api) { instance_double(VineApiService) }
describe "#connect" do
it "send a request to VINE api" do
expect(vine_api).to receive(:my_team).and_return(mock_api_response(true))
connected_app.connect(api_key: vine_api_key, vine_api: )
end
context "when request succeed", :vcr do
it "store the vine api key" do
allow(vine_api).to receive(:my_team).and_return(mock_api_response(true))
expect(connected_app.connect(api_key: vine_api_key, vine_api:)).to eq(true)
expect(connected_app.data).to eql({ "api_key" => vine_api_key })
end
end
context "when request fails" do
it "doesn't store the vine api key" do
allow(vine_api).to receive(:my_team).and_return(mock_api_response(false))
expect(connected_app.connect(api_key: vine_api_key, vine_api:)).to eq(false)
expect(connected_app.data).to be_nil
expect(connected_app.errors[:base]).to include(
"An error occured when connecting to Vine API"
)
end
end
end
def mock_api_response(success)
mock_response = instance_double(Faraday::Response)
allow(mock_response).to receive(:success?).and_return(success)
mock_response
end
end