From da7bbcf82fb2cf2d66065d6818d157f34abb06ad Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 16 Jul 2024 15:51:46 +1000 Subject: [PATCH] Enable/disable affiliate sales data --- .../admin/connected_apps_controller.rb | 14 +++-- .../connected_apps/affiliate_sales_data.rb | 4 +- .../_affiliate_sales_data.html.haml | 4 +- .../connected_apps/_discover_regen.html.haml | 2 +- .../admin/enterprises/connected_apps_spec.rb | 52 ++++++++++++++++--- 5 files changed, 61 insertions(+), 15 deletions(-) diff --git a/app/controllers/admin/connected_apps_controller.rb b/app/controllers/admin/connected_apps_controller.rb index 9ddf7d9405..3531c7371a 100644 --- a/app/controllers/admin/connected_apps_controller.rb +++ b/app/controllers/admin/connected_apps_controller.rb @@ -5,8 +5,12 @@ module Admin def create authorize! :admin, enterprise - app = ConnectedApp.create!(enterprise_id: enterprise.id) - app.connect(api_key: spree_current_user.spree_api_key, channel: SessionChannel.for_request(request)) + attributes = {} + attributes[:type] = connected_app_params[:type] if connected_app_params[:type] + + app = ConnectedApp.create!(enterprise_id: enterprise.id, **attributes) + app.connect(api_key: spree_current_user.spree_api_key, + channel: SessionChannel.for_request(request)) render_panel end @@ -14,7 +18,7 @@ module Admin def destroy authorize! :admin, enterprise - app = enterprise.connected_apps.first + app = enterprise.connected_apps.find(params.require(:id)) app.destroy render_panel @@ -29,5 +33,9 @@ module Admin def render_panel redirect_to "#{edit_admin_enterprise_path(enterprise)}#/connected_apps_panel" end + + def connected_app_params + params.permit(:type) + end end end diff --git a/app/models/connected_apps/affiliate_sales_data.rb b/app/models/connected_apps/affiliate_sales_data.rb index a9425a1303..51d9d1b5df 100644 --- a/app/models/connected_apps/affiliate_sales_data.rb +++ b/app/models/connected_apps/affiliate_sales_data.rb @@ -4,7 +4,9 @@ # module ConnectedApps class AffiliateSalesData < ConnectedApp - def connect; end + def connect(_opts) + update! data: true # not-nil value indicates it is ready + end def disconnect; end end diff --git a/app/views/admin/enterprises/form/connected_apps/_affiliate_sales_data.html.haml b/app/views/admin/enterprises/form/connected_apps/_affiliate_sales_data.html.haml index 1674d00564..52ec355a35 100644 --- a/app/views/admin/enterprises/form/connected_apps/_affiliate_sales_data.html.haml +++ b/app/views/admin/enterprises/form/connected_apps/_affiliate_sales_data.html.haml @@ -5,11 +5,11 @@ %p= t ".tagline" %div - if connected_app.nil? - = button_to t(".enable"), admin_enterprise_connected_apps_path(enterprise.id), method: :post, disabled: !managed_by_user?(enterprise) + = button_to t(".enable"), admin_enterprise_connected_apps_path(enterprise.id, type: "ConnectedApps::AffiliateSalesData"), method: :post, disabled: !managed_by_user?(enterprise) -# This is only seen by super-admins: %em= t(".need_to_be_manager") unless managed_by_user?(enterprise) - else - = button_to t(".disable"), admin_enterprise_connected_app_path(0, enterprise_id: enterprise.id), method: :delete + = button_to t(".disable"), admin_enterprise_connected_app_path(connected_app.id, enterprise_id: enterprise.id), method: :delete %hr .connected-app__description = t ".description_html" diff --git a/app/views/admin/enterprises/form/connected_apps/_discover_regen.html.haml b/app/views/admin/enterprises/form/connected_apps/_discover_regen.html.haml index 5011de7d1f..1bab8f75ec 100644 --- a/app/views/admin/enterprises/form/connected_apps/_discover_regen.html.haml +++ b/app/views/admin/enterprises/form/connected_apps/_discover_regen.html.haml @@ -14,7 +14,7 @@   = t ".loading" - else - = button_to t(".disable"), admin_enterprise_connected_app_path(0, enterprise_id: enterprise.id), method: :delete + = button_to t(".disable"), admin_enterprise_connected_app_path(connected_app.id, enterprise_id: enterprise.id), method: :delete .connected-app__connection - if connected_app&.ready? diff --git a/spec/system/admin/enterprises/connected_apps_spec.rb b/spec/system/admin/enterprises/connected_apps_spec.rb index ba9fd3ce7c..4f1a67b3f7 100644 --- a/spec/system/admin/enterprises/connected_apps_spec.rb +++ b/spec/system/admin/enterprises/connected_apps_spec.rb @@ -29,25 +29,25 @@ RSpec.describe "Connected Apps", feature: :connected_apps, vcr: true do end describe "Discover Regenerative" do + let(:section_heading) { self.class.description } + it "can be enabled and disabled" do visit edit_admin_enterprise_path(enterprise) scroll_to :bottom click_link "Connected apps" - within section_containing_heading "Discover Regenerative" do + within section_containing_heading do click_button "Allow data sharing" end # (page is reloaded so we need to evaluate within block again) - within section_containing_heading "Discover Regenerative" do + within section_containing_heading do expect(page).not_to have_button "Allow data sharing" expect(page).to have_button "Loading", disabled: true perform_enqueued_jobs(only: ConnectAppJob) - end - within section_containing_heading "Discover Regenerative" do expect(page).not_to have_button "Loading", disabled: true expect(page).to have_content "account is connected" expect(page).to have_link "Manage listing" @@ -55,7 +55,7 @@ RSpec.describe "Connected Apps", feature: :connected_apps, vcr: true do click_button "Stop sharing" end - within section_containing_heading "Discover Regenerative" do + within section_containing_heading do expect(page).to have_button "Allow data sharing" expect(page).not_to have_button "Stop sharing" expect(page).not_to have_content "account is connected" @@ -68,15 +68,51 @@ RSpec.describe "Connected Apps", feature: :connected_apps, vcr: true do visit "#{edit_admin_enterprise_path(enterprise)}#/connected_apps_panel" - within section_containing_heading "Discover Regenerative" do + within section_containing_heading do expect(page).to have_button("Allow data sharing", disabled: true) expect(page).to have_content "Only managers can connect apps." end end end - def section_containing_heading(heading) + describe "Affiliate Sales Data" do + let(:section_heading) { "INRAE / UFC QUE CHOISIR Research" } + + it "can be enabled and disabled" do + visit edit_admin_enterprise_path(enterprise) + + scroll_to :bottom + click_link "Connected apps" + + within section_containing_heading do + click_button "Allow data sharing" + end + + # (page is reloaded so we need to evaluate within block again) + within section_containing_heading do + expect(page).not_to have_button "Allow data sharing" + click_button "Stop sharing" + end + + within section_containing_heading do + expect(page).to have_button "Allow data sharing" + expect(page).not_to have_button "Stop sharing" + end + end + + it "can't be enabled by non-manager" do + login_as create(:admin_user) + + visit "#{edit_admin_enterprise_path(enterprise)}#/connected_apps_panel" + + within section_containing_heading do + expect(page).to have_button("Allow data sharing", disabled: true) + expect(page).to have_content "Only managers can connect apps." + end + end + end + + def section_containing_heading(heading = section_heading) page.find("h3", text: heading).ancestor("section") end end -