Files
openfoodnetwork/spec/models/connected_apps/vine_spec.rb
Gaetan Craig-Riou b14a1e72f3 Handle api secret
The VINE Api require a secret and an API key to be used. The secret is
used to sign the request. The secret is linked to the API key so we need
to store it along side the key.
2024-10-07 15:09:58 +11:00

47 lines
1.5 KiB
Ruby

# 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(:secret) { "my_secret" }
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, secret:, vine_api: )
end
context "when request succeed" do
it "store the vine api key and secret" do
allow(vine_api).to receive(:my_team).and_return(mock_api_response(true))
expect(connected_app.connect(api_key: vine_api_key, secret:, vine_api:)).to eq(true)
expect(connected_app.data).to eql({ "api_key" => vine_api_key, "secret" => secret })
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, secret:, 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