forked from openfoodfoundation/openfoodnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConnectedApps controller, handle ConnectedApps::Vine
Add logiv to connect and disconnect VINE API plus spec
- Loading branch information
Showing
5 changed files
with
236 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
# Configure sensitive parameters which will be filtered from the log file. | ||
Rails.application.config.filter_parameters += [:password] | ||
Rails.application.config.filter_parameters += [:password, :vine_api_key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe Admin::ConnectedAppsController do | ||
let(:user) { create(:admin_user) } | ||
let(:enterprise) { create(:enterprise, owner: user) } | ||
let(:edit_enterprise_url) { "#{edit_admin_enterprise_url(enterprise)}#/connected_apps_panel" } | ||
|
||
before do | ||
allow(ENV).to receive(:fetch).and_call_original | ||
allow(ENV).to receive(:fetch).with("VINE_API_SECRET").and_return("my_secret") | ||
|
||
sign_in user | ||
end | ||
|
||
describe "POST /admin/enterprises/:enterprise_id/connected_apps" do | ||
context "with type ConnectedApps::Vine" do | ||
let(:vine_api) { instance_double(VineApiService) } | ||
|
||
before do | ||
allow(VineJwtService).to receive(:new).and_return(instance_double(VineJwtService)) | ||
allow(VineApiService).to receive(:new).and_return(vine_api) | ||
end | ||
|
||
it "creates a new connected app" do | ||
allow(vine_api).to receive(:my_team).and_return(mock_api_response(true)) | ||
|
||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "12345678" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
|
||
expect(ConnectedApps::Vine.find_by(enterprise_id: enterprise.id)).not_to be_nil | ||
end | ||
|
||
it "redirects to enterprise edit page" do | ||
allow(vine_api).to receive(:my_team).and_return(mock_api_response(true)) | ||
|
||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "12345678" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
|
||
expect(response).to redirect_to(edit_enterprise_url) | ||
end | ||
|
||
context "when api key is empty" do | ||
it "redirects to enterprise edit page, with an error" do | ||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
|
||
expect(response).to redirect_to(edit_enterprise_url) | ||
expect(flash[:error]).to eq("Please enter an API key") | ||
expect(ConnectedApps::Vine.find_by(enterprise_id: enterprise.id)).to be_nil | ||
end | ||
end | ||
|
||
context "when api key is not valid" do | ||
before do | ||
allow(vine_api).to receive(:my_team).and_return(mock_api_response(false)) | ||
end | ||
|
||
it "doesn't create a new connected app" do | ||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "12345678" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
|
||
expect(ConnectedApps::Vine.find_by(enterprise_id: enterprise.id)).to be_nil | ||
end | ||
|
||
it "redirects to enterprise edit page, with an error" do | ||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "12345678" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
|
||
expect(response).to redirect_to(edit_enterprise_url) | ||
expect(flash[:error]).to eq( | ||
"An error occured when connecting to Vine API. Check you entered your API key \ | ||
correctly, contact your instance manager if the error persists".squish | ||
) | ||
end | ||
end | ||
|
||
context "when there is a connection error" do | ||
before do | ||
allow(vine_api).to receive(:my_team).and_raise(Faraday::Error) | ||
end | ||
|
||
it "redirects to enterprise edit page, with an error" do | ||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "12345678" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
|
||
expect(response).to redirect_to(edit_enterprise_url) | ||
expect(flash[:error]).to eq("API connection error, please try again") | ||
end | ||
|
||
it "notifies Bugsnag" do | ||
expect(Bugsnag).to receive(:notify) | ||
|
||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "12345678" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
end | ||
end | ||
|
||
context "when VINE API is not set up properly" do | ||
before do | ||
allow(ENV).to receive(:fetch).and_call_original | ||
allow(ENV).to receive(:fetch).with("VINE_API_SECRET").and_raise(KeyError) | ||
end | ||
|
||
it "redirects to enterprise edit page, with an error" do | ||
params = { | ||
type: ConnectedApps::Vine, | ||
vine_api_key: "12345678" | ||
} | ||
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" | ||
} | ||
post("/admin/enterprises/#{enterprise.id}/connected_apps", params: ) | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE /admin/enterprises/:enterprise_id/connected_apps/:id" do | ||
it "deletes the connected app" do | ||
app = ConnectedApps::Vine.create!(enterprise:) | ||
delete("/admin/enterprises/#{enterprise.id}/connected_apps/#{app.id}") | ||
|
||
expect(ConnectedApps::Vine.find_by(enterprise_id: enterprise.id)).to be_nil | ||
end | ||
|
||
it "redirect to enterprise edit page" do | ||
app = ConnectedApps::Vine.create!(enterprise:, data: { api_key: "12345" }) | ||
delete("/admin/enterprises/#{enterprise.id}/connected_apps/#{app.id}") | ||
|
||
expect(response).to redirect_to(edit_enterprise_url) | ||
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 |