From df67b53971a33eb89e36b66cf411258acfef52c6 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 8 Oct 2024 13:26:57 +1100 Subject: [PATCH] Re add VINE_API_URL env variable And add error handling if the variable is not set --- .env | 3 ++ .../admin/connected_apps_controller.rb | 3 ++ config/locales/en.yml | 1 + .../admin/connected_apps_controller_spec.rb | 32 +++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/.env b/.env index 14902e28e9..3dc380b571 100644 --- a/.env +++ b/.env @@ -67,3 +67,6 @@ SMTP_PASSWORD="f00d" # ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# VINE API settings +# VINE_API_URL="https://vine-staging.openfoodnetwork.org.au/api/v1" diff --git a/app/controllers/admin/connected_apps_controller.rb b/app/controllers/admin/connected_apps_controller.rb index ec4d259945..a5308d4984 100644 --- a/app/controllers/admin/connected_apps_controller.rb +++ b/app/controllers/admin/connected_apps_controller.rb @@ -57,6 +57,9 @@ module Admin rescue Faraday::Error => e log_and_notify_exception(e) handle_error(I18n.t("admin.enterprises.form.connected_apps.vine.connection_error")) + rescue KeyError => e + log_and_notify_exception(e) + handle_error(I18n.t("admin.enterprises.form.connected_apps.vine.setup_error")) end def enterprise diff --git a/config/locales/en.yml b/config/locales/en.yml index a193f820c8..a9354eda19 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1441,6 +1441,7 @@ en: api_parameters_empty: "Please enter an API key and a secret" api_parameters_error: "Check you entered your API key and secret correctly, contact your instance manager if the error persists" connection_error: "API connection error, please try again" + setup_error: "VINE API is not configured, please contact your instance manager" actions: edit_profile: Settings properties: Properties diff --git a/spec/requests/admin/connected_apps_controller_spec.rb b/spec/requests/admin/connected_apps_controller_spec.rb index 3b9879b8c6..f1330ae7e2 100644 --- a/spec/requests/admin/connected_apps_controller_spec.rb +++ b/spec/requests/admin/connected_apps_controller_spec.rb @@ -111,6 +111,38 @@ RSpec.describe "Admin ConnectedApp" do end end + context "when VINE API is not set up properly" do + before do + # VineApiService will raise a KeyError if VINE_API_URL is not set + allow(VineApiService).to receive(:new).and_raise(KeyError) + end + + it "redirects to enterprise edit page, with an error" do + params = { + type: ConnectedApps::Vine, + vine_api_key: "12345678", + vine_secret: "my_secret" + } + post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) + + expect(response).to redirect_to(edit_enterprise_url) + expect(flash[:error]).to eq( + "VINE API is not configured, please contact your instance manager" + ) + end + + it "notifies Bugsnag" do + expect(Bugsnag).to receive(:notify) + + params = { + type: ConnectedApps::Vine, + vine_api_key: "12345678", + vine_secret: "my_secret" + } + post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) + end + end + context "when there is a connection error" do before do allow(vine_api).to receive(:my_team).and_raise(Faraday::Error)