From 5d732d80a64476784a27caac49115958dbcf2de4 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 1 Aug 2024 13:45:18 +1000 Subject: [PATCH] Add connected apps settings screen I considered adding a request spec, but figured it still doesnt' test the form, so better to use a full system spec. --- .../connected_app_settings_controller.rb | 21 +++++++++++++++ .../connected_app_settings/edit.html.haml | 22 ++++++++++++++++ .../shared/_configuration_menu.html.haml | 1 + app/views/spree/admin/shared/_tabs.html.haml | 2 +- config/locales/en.yml | 11 ++++++++ config/routes/admin.rb | 2 ++ .../connected_app_settings_spec.rb | 26 +++++++++++++++++++ .../admin/enterprises/connected_apps_spec.rb | 1 - 8 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 app/controllers/admin/connected_app_settings_controller.rb create mode 100644 app/views/admin/connected_app_settings/edit.html.haml create mode 100644 spec/system/admin/configuration/connected_app_settings_spec.rb diff --git a/app/controllers/admin/connected_app_settings_controller.rb b/app/controllers/admin/connected_app_settings_controller.rb new file mode 100644 index 0000000000..3b9cc0585b --- /dev/null +++ b/app/controllers/admin/connected_app_settings_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Admin + class ConnectedAppSettingsController < Spree::Admin::BaseController + def update + Spree::Config.set(connected_apps_enabled:) + + respond_to do |format| + format.html { + redirect_to main_app.edit_admin_connected_app_settings_path + } + end + end + + private + + def connected_apps_enabled + params.require(:preferences).require(:connected_apps_enabled).join(",") + end + end +end diff --git a/app/views/admin/connected_app_settings/edit.html.haml b/app/views/admin/connected_app_settings/edit.html.haml new file mode 100644 index 0000000000..cdadebd9f0 --- /dev/null +++ b/app/views/admin/connected_app_settings/edit.html.haml @@ -0,0 +1,22 @@ += render :partial => 'spree/admin/shared/configuration_menu' + +- content_for :page_title do + = t('.title') + += form_tag main_app.admin_connected_app_settings_path, :method => :put do + + %fieldset + %legend= t('.enabled_legend') + + = t('.info_html') + + .field + - ConnectedApp::TYPES.each do |type| + %label + = check_box_tag("preferences[connected_apps_enabled][]", type, + Spree::Config.connected_apps_enabled&.split(',')&.include?(type)) + = t('.connected_apps_enabled.' + type) + %br + + .form-buttons + = button t(:update), 'icon-refresh' diff --git a/app/views/spree/admin/shared/_configuration_menu.html.haml b/app/views/spree/admin/shared/_configuration_menu.html.haml index af00f89928..215011f6b6 100644 --- a/app/views/spree/admin/shared/_configuration_menu.html.haml +++ b/app/views/spree/admin/shared/_configuration_menu.html.haml @@ -23,3 +23,4 @@ = configurations_sidebar_menu_item t('admin.invoice_settings.edit.title'), main_app.edit_admin_invoice_settings_path = configurations_sidebar_menu_item t('admin.matomo_settings.edit.title'), main_app.edit_admin_matomo_settings_path = configurations_sidebar_menu_item t('admin.stripe_connect_settings.edit.title'), main_app.edit_admin_stripe_connect_settings_path + = configurations_sidebar_menu_item t('admin.connected_app_settings.edit.title'), main_app.edit_admin_connected_app_settings_path diff --git a/app/views/spree/admin/shared/_tabs.html.haml b/app/views/spree/admin/shared/_tabs.html.haml index 019b45d948..f9a9c6613f 100644 --- a/app/views/spree/admin/shared/_tabs.html.haml +++ b/app/views/spree/admin/shared/_tabs.html.haml @@ -3,7 +3,7 @@ = tab :order_cycles, url: main_app.admin_order_cycles_path, icon: 'icon-refresh' = tab :orders, :subscriptions, :customer_details, :adjustments, :payments, :return_authorizations, url: admin_orders_path, icon: 'icon-shopping-cart' = tab :reports, url: main_app.admin_reports_path, icon: 'icon-file' -= tab :general_settings, :terms_of_service_files, :mail_methods, :tax_categories, :tax_rates, :tax_settings, :zones, :countries, :states, :payment_methods, :taxonomies, :shipping_methods, :shipping_categories, :enterprise_fees, :contents, :invoice_settings, :matomo_settings, :stripe_connect_settings, label: 'configuration', icon: 'icon-wrench', url: edit_admin_general_settings_path += tab :general_settings, :terms_of_service_files, :mail_methods, :tax_categories, :tax_rates, :tax_settings, :zones, :countries, :states, :payment_methods, :taxonomies, :shipping_methods, :shipping_categories, :enterprise_fees, :contents, :invoice_settings, :matomo_settings, :stripe_connect_settings, :connected_app_settings, label: 'configuration', icon: 'icon-wrench', url: edit_admin_general_settings_path = tab :enterprises, :enterprise_relationships, :vouchers, :oidc_settings, url: main_app.admin_enterprises_path = tab :customers, url: main_app.admin_customers_path = tab :enterprise_groups, url: main_app.admin_enterprise_groups_path, label: 'groups' diff --git a/config/locales/en.yml b/config/locales/en.yml index 83735ca5af..c2ac80a4dc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -683,6 +683,8 @@ en: terms_of_service_have_been_updated_html: "Open Food Network's Terms of Service have been updated: %{tos_link}" terms_of_service: Read Terms of Service accept_terms_of_service: Accept Terms of Service + + # Pages shopfront_settings: embedded_shopfront_settings: "Embedded Shopfront Settings" enable_embedded_shopfronts: "Enable Embedded Shopfronts" @@ -739,6 +741,15 @@ en: config_instructions_html: "Here you can configure the OFN Matomo integration. The Matomo URL below should point to the Matomo instance where the user tracking information will be sent to; if it is left empty, Matomo user tracking will be disabled. The Site ID field is not mandatory but useful if you are tracking more than one website on a single Matomo instance; it can be found on the Matomo instance console." config_instructions_tag_manager_html: "Setting the Matomo Tag Manager URL enables Matomo Tag Manager. This tool allows you to set up analytics events. The Matomo Tag Manager URL is copied from the Install Code section of Matomo Tag Manager. Ensure you select the right container and environment as these options change the URL." + connected_app_settings: + edit: + title: "Connected app settings" + info_html: "Enabled apps will appear under Enterprise Settings > Connected Apps." + enabled_legend: "Enabled connected apps" + connected_apps_enabled: + discover_regen: Discover Regenerative portal + affiliate_sales_data: DFC anonymised orders API for research purposes + customers: index: new_customer: "New Customer" diff --git a/config/routes/admin.rb b/config/routes/admin.rb index a8bf999f1b..fe91db5633 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -108,6 +108,8 @@ Openfoodnetwork::Application.routes.draw do resource :matomo_settings, only: [:edit, :update] + resource :connected_app_settings, only: [:edit, :update] + resources :stripe_accounts, only: [:destroy] do get :connect, on: :collection get :status, on: :collection diff --git a/spec/system/admin/configuration/connected_app_settings_spec.rb b/spec/system/admin/configuration/connected_app_settings_spec.rb new file mode 100644 index 0000000000..6d413d3473 --- /dev/null +++ b/spec/system/admin/configuration/connected_app_settings_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'system_helper' + +RSpec.describe "Connected App Settings", feature: :connected_apps do + include AuthenticationHelper + + before do + login_as_admin + visit spree.admin_dashboard_path + click_link "Configuration" + click_link "Connected app settings" + end + + it "should update connected app enabled preferences" do + expect(page).to have_field "Discover Regenerative portal", checked: false + expect(page).to have_field "DFC anonymised orders API for research purposes", checked: false + + check "Discover Regenerative portal" + check "DFC anonymised orders API for research purposes" + + expect{ + click_button "Update" + }.to change{ Spree::Config.connected_apps_enabled }.to("discover_regen,affiliate_sales_data") + end +end diff --git a/spec/system/admin/enterprises/connected_apps_spec.rb b/spec/system/admin/enterprises/connected_apps_spec.rb index cba070861e..8d1e9fa2c1 100644 --- a/spec/system/admin/enterprises/connected_apps_spec.rb +++ b/spec/system/admin/enterprises/connected_apps_spec.rb @@ -42,7 +42,6 @@ RSpec.describe "Connected Apps", feature: :connected_apps, vcr: true do expect(page).to have_content "CONNECTED APPS" end - it "only shows enabled apps" do allow(Spree::Config).to receive(:connected_apps_enabled).and_return("discover_regen")