From 89e5a213e9e9cab9be53858e5ecd5c1e5ea59481 Mon Sep 17 00:00:00 2001 From: Alan Hill Date: Fri, 7 Jul 2017 00:23:54 -0400 Subject: [PATCH] Add after_install hook and generator --- .byebug_history | 5 ++ .../add_after_authenticate_job_generator.rb | 43 ++++++++++++++++ .../templates/after_authenticate_job.rb | 10 ++++ .../install/templates/shopify_app.rb | 1 + lib/shopify_app/configuration.rb | 1 + lib/shopify_app/sessions_concern.rb | 12 +++++ test/controllers/sessions_controller_test.rb | 50 +++++++++++++++++++ ...d_after_authenticate_job_generator_test.rb | 30 +++++++++++ test/generators/install_generator_test.rb | 1 + test/shopify_app/configuration_test.rb | 2 + 10 files changed, 155 insertions(+) create mode 100644 .byebug_history create mode 100644 lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb create mode 100644 lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb create mode 100644 test/generators/add_after_authenticate_job_generator_test.rb diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 000000000..0c11aad38 --- /dev/null +++ b/.byebug_history @@ -0,0 +1,5 @@ +exit +config[:inline].present? +config[:job].present? +config +next diff --git a/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb b/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb new file mode 100644 index 000000000..7ded8cdb7 --- /dev/null +++ b/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb @@ -0,0 +1,43 @@ +require 'rails/generators/base' + +module ShopifyApp + module Generators + class AddAfterAuthenticateJobGenerator < Rails::Generators::Base + source_root File.expand_path('../templates', __FILE__) + + hook_for :test_framework, as: :job, in: :rails do |instance, generator| + instance.invoke generator, [ instance.send(:job_file_name) ] + end + + def init_after_authenticate_config + initializer = load_initializer + + after_authenticate_job_config = " config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }\n" + + inject_into_file( + 'config/initializers/shopify_app.rb', + after_authenticate_job_config, + before: 'end' + ) + + unless initializer.include?(after_authenticate_job_config) + shell.say "Error adding after_authneticate_job to config. Add this line manually: #{after_authenticate_job_config}", :red + end + end + + def add_after_authenticate_job + template 'after_authenticate_job.rb', "app/jobs/#{job_file_name}_job.rb" + end + + private + + def load_initializer + File.read(File.join(destination_root, 'config/initializers/shopify_app.rb')) + end + + def job_file_name + 'shopify/after_authenticate' + end + end + end +end diff --git a/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb b/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb new file mode 100644 index 000000000..c944b98ba --- /dev/null +++ b/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb @@ -0,0 +1,10 @@ +module Shopify + class AfterAuthenticateJob < ActiveJob::Base + def perform(shop_domain:) + shop = Shop.find_by(shopify_domain: shop_domain) + + shop.with_shopify_session do + end + end + end +end diff --git a/lib/generators/shopify_app/install/templates/shopify_app.rb b/lib/generators/shopify_app/install/templates/shopify_app.rb index 6ea57f78c..da4a66527 100644 --- a/lib/generators/shopify_app/install/templates/shopify_app.rb +++ b/lib/generators/shopify_app/install/templates/shopify_app.rb @@ -4,4 +4,5 @@ config.secret = "<%= @secret %>" config.scope = "<%= @scope %>" config.embedded_app = <%= embedded_app? %> + config.after_authenticate_job = false end diff --git a/lib/shopify_app/configuration.rb b/lib/shopify_app/configuration.rb index f17f4dd66..ecafbb820 100644 --- a/lib/shopify_app/configuration.rb +++ b/lib/shopify_app/configuration.rb @@ -12,6 +12,7 @@ class Configuration alias_method :embedded_app?, :embedded_app attr_accessor :webhooks attr_accessor :scripttags + attr_accessor :after_authenticate_job # customise ActiveJob queue names attr_accessor :scripttags_manager_queue_name diff --git a/lib/shopify_app/sessions_concern.rb b/lib/shopify_app/sessions_concern.rb index 384310d51..01638dd1f 100644 --- a/lib/shopify_app/sessions_concern.rb +++ b/lib/shopify_app/sessions_concern.rb @@ -20,6 +20,7 @@ def callback login_shop install_webhooks install_scripttags + perform_after_authenticate_job redirect_to return_address else @@ -87,5 +88,16 @@ def return_address session.delete(:return_to) || main_app.root_url end + def perform_after_authenticate_job + config = ShopifyApp.configuration.after_authenticate_job + + return unless config && config[:job].present? + + if config[:inline] == true + config[:job].perform_now(shop_domain: session[:shopify_domain]) + else + config[:job].perform_later(shop_domain: session[:shopify_domain]) + end + end end end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index c0e32c7fe..0e62c7a44 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -1,5 +1,11 @@ require 'test_helper' +module Shopify + class AfterAuthenticateJob < ActiveJob::Base + def perform; end + end +end + module ShopifyApp class SessionsControllerTest < ActionController::TestCase @@ -140,6 +146,50 @@ class SessionsControllerTest < ActionController::TestCase assert_equal 'Cerrar sesiĆ³n', flash[:notice] end + test "#callback calls #perform_after_authenticate_job and performs inline when inline is true" do + ShopifyApp.configure do |config| + config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true } + end + + Shopify::AfterAuthenticateJob.expects(:perform_now) + + mock_shopify_omniauth + get :callback, params: { shop: 'shop' } + end + + test "#callback calls #perform_after_authenticate_job and performs asynchronous when inline isn't true" do + ShopifyApp.configure do |config| + config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false } + end + + Shopify::AfterAuthenticateJob.expects(:perform_later) + + mock_shopify_omniauth + get :callback, params: { shop: 'shop' } + end + + test "#callback doesn't call #perform_after_authenticate_job if job is nil" do + ShopifyApp.configure do |config| + config.after_authenticate_job = { job: nil, inline: false } + end + + Shopify::AfterAuthenticateJob.expects(:perform_later).never + + mock_shopify_omniauth + get :callback, params: { shop: 'shop' } + end + + test "#callback calls #perform_after_authenticate_job and performs async if inline isn't present" do + ShopifyApp.configure do |config| + config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob } + end + + Shopify::AfterAuthenticateJob.expects(:perform_later) + + mock_shopify_omniauth + get :callback, params: { shop: 'shop' } + end + private def mock_shopify_omniauth diff --git a/test/generators/add_after_authenticate_job_generator_test.rb b/test/generators/add_after_authenticate_job_generator_test.rb new file mode 100644 index 000000000..15c997954 --- /dev/null +++ b/test/generators/add_after_authenticate_job_generator_test.rb @@ -0,0 +1,30 @@ +require 'test_helper' +require 'generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator' + +class AddAfterAuthenticateJobGeneratorTest < Rails::Generators::TestCase + tests ShopifyApp::Generators::AddAfterAuthenticateJobGenerator + destination File.expand_path("../tmp", File.dirname(__FILE__)) + + setup do + prepare_destination + end + + test 'adds enable_after_authenticate_actions config' do + provide_existing_initializer_file + + run_generator + + assert_file "config/initializers/shopify_app.rb" do |config| + assert_match 'config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }', config + end + end + + test "adds the after_authenticate job" do + provide_existing_initializer_file + + run_generator + + assert_directory "app/jobs/shopify" + assert_file "app/jobs/shopify/after_authenticate_job.rb" + end +end diff --git a/test/generators/install_generator_test.rb b/test/generators/install_generator_test.rb index 71ecb08fe..d3cf7fcab 100644 --- a/test/generators/install_generator_test.rb +++ b/test/generators/install_generator_test.rb @@ -20,6 +20,7 @@ class InstallGeneratorTest < Rails::Generators::TestCase assert_match 'config.secret = ""', shopify_app assert_match 'config.scope = "read_orders, read_products"', shopify_app assert_match "config.embedded_app = true", shopify_app + assert_match "config.after_authenticate_job = false", shopify_app end end diff --git a/test/shopify_app/configuration_test.rb b/test/shopify_app/configuration_test.rb index 2049fe89c..c44466107 100644 --- a/test/shopify_app/configuration_test.rb +++ b/test/shopify_app/configuration_test.rb @@ -13,9 +13,11 @@ class ConfigurationTest < ActiveSupport::TestCase test "configure" do ShopifyApp.configure do |config| config.embedded_app = true + config.after_authenticate_job = false end assert_equal true, ShopifyApp.configuration.embedded_app + assert_equal false, ShopifyApp.configuration.after_authenticate_job end test "defaults to myshopify_domain" do