From 5d88ef96aeaca9d2659cbb75ca356ef8ac881316 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sat, 25 Nov 2023 12:42:59 +0100 Subject: [PATCH] DEV: update annotations, rspec session handling and test db --- Gemfile.lock | 2 +- app/models/subscription_client_notice.rb | 5 --- app/models/subscription_client_request.rb | 14 ++++++++ app/models/subscription_client_resource.rb | 9 ----- .../subscription_client_subscription.rb | 9 ----- app/models/subscription_client_supplier.rb | 6 +--- discourse_subscription_client.gemspec | 2 +- lib/discourse_subscription_client/engine.rb | 2 +- .../suppliers_controller_spec.rb | 6 +++- spec/support/session_double.rb | 36 +++++++++++++++++++ 10 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 spec/support/session_double.rb diff --git a/Gemfile.lock b/Gemfile.lock index ae9f3f9..5b2507b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -244,7 +244,7 @@ DEPENDENCIES message_bus mini_scheduler pg - rails (>= 7.0.4.1) + rails (~> 7.0.4.1) rails_multisite rspec-activemodel-mocks rspec-rails diff --git a/app/models/subscription_client_notice.rb b/app/models/subscription_client_notice.rb index 02faf93..6c10970 100644 --- a/app/models/subscription_client_notice.rb +++ b/app/models/subscription_client_notice.rb @@ -215,8 +215,3 @@ def self.expire_all(notice_type, notice_subject_type, notice_subject_id) # created_at :datetime not null # updated_at :datetime not null # -# Indexes -# -# index_subscription_client_notices_on_notice_subject (notice_subject_type,notice_subject_id) -# sc_unique_notices (notice_type,notice_subject_type,notice_subject_id,changed_at) UNIQUE -# diff --git a/app/models/subscription_client_request.rb b/app/models/subscription_client_request.rb index 09879c9..7676229 100644 --- a/app/models/subscription_client_request.rb +++ b/app/models/subscription_client_request.rb @@ -1,4 +1,18 @@ # frozen_string_literal: true +# == Schema Information +# +# Table name: subscription_client_requests +# +# id :bigint not null, primary key +# request_id :bigint +# request_type :string +# expired_at :datetime +# message :string +# count :integer +# response :json +# created_at :datetime not null +# updated_at :datetime not null +# class SubscriptionClientRequest < ActiveRecord::Base end diff --git a/app/models/subscription_client_resource.rb b/app/models/subscription_client_resource.rb index 02018fb..0e972a6 100644 --- a/app/models/subscription_client_resource.rb +++ b/app/models/subscription_client_resource.rb @@ -16,12 +16,3 @@ class SubscriptionClientResource < ActiveRecord::Base # created_at :datetime not null # updated_at :datetime not null # -# Indexes -# -# index_subscription_client_resources_on_supplier_id (supplier_id) -# index_subscription_client_resources_on_supplier_id_and_name (supplier_id,name) UNIQUE -# -# Foreign Keys -# -# fk_rails_... (supplier_id => subscription_client_suppliers.id) -# diff --git a/app/models/subscription_client_subscription.rb b/app/models/subscription_client_subscription.rb index 0d6ab91..d13c07b 100644 --- a/app/models/subscription_client_subscription.rb +++ b/app/models/subscription_client_subscription.rb @@ -48,12 +48,3 @@ def self.update_period # created_at :datetime not null # updated_at :datetime not null # -# Indexes -# -# index_subscription_client_subscriptions_on_resource_id (resource_id) -# sc_unique_subscriptions (resource_id,product_id,price_id) UNIQUE -# -# Foreign Keys -# -# fk_rails_... (resource_id => subscription_client_resources.id) -# diff --git a/app/models/subscription_client_supplier.rb b/app/models/subscription_client_supplier.rb index c495be6..7a5b16f 100644 --- a/app/models/subscription_client_supplier.rb +++ b/app/models/subscription_client_supplier.rb @@ -50,9 +50,5 @@ def self.publish_authorized_supplier_count # authorized_at :datetime # created_at :datetime not null # updated_at :datetime not null -# -# Indexes -# -# index_subscription_client_suppliers_on_url (url) UNIQUE -# index_subscription_client_suppliers_on_user_id (user_id) +# products :json # diff --git a/discourse_subscription_client.gemspec b/discourse_subscription_client.gemspec index 76578f1..3e094c0 100644 --- a/discourse_subscription_client.gemspec +++ b/discourse_subscription_client.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "message_bus" spec.add_development_dependency "mini_scheduler" spec.add_development_dependency "pg" - spec.add_development_dependency "rails", ">= 7.0.4.1" + spec.add_development_dependency "rails", "~> 7.0.4.1" spec.add_development_dependency "rails_multisite" spec.add_development_dependency "rspec-activemodel-mocks" spec.add_development_dependency "rspec-rails" diff --git a/lib/discourse_subscription_client/engine.rb b/lib/discourse_subscription_client/engine.rb index 1bb3391..a63fdbd 100644 --- a/lib/discourse_subscription_client/engine.rb +++ b/lib/discourse_subscription_client/engine.rb @@ -24,7 +24,7 @@ def configurable? config.after_initialize do gem_root = File.expand_path("../..", __dir__) - ActiveRecord::Tasks::DatabaseTasks.migrations_paths << "#{gem_root}/db/migrate" unless Rails.env.test? + ActiveRecord::Tasks::DatabaseTasks.migrations_paths << "#{gem_root}/db/migrate" %w[ ./request diff --git a/spec/requests/discourse_subscription_client/suppliers_controller_spec.rb b/spec/requests/discourse_subscription_client/suppliers_controller_spec.rb index 63a721e..322d73a 100644 --- a/spec/requests/discourse_subscription_client/suppliers_controller_spec.rb +++ b/spec/requests/discourse_subscription_client/suppliers_controller_spec.rb @@ -20,6 +20,8 @@ } end + include_context "session double" + context "with admin" do before do sign_in(admin) @@ -50,6 +52,7 @@ end it "handles authorization callbacks" do + session_hash[:final_landing_path] = "/admin/plugins/subscription-client/subscriptions" request_id = cookies[:user_api_request_id] = DiscourseSubscriptionClient::Authorization.request_id(supplier.id) payload = generate_auth_payload(admin.id, request_id) stub_subscription_request(200, resource, subscription_response) @@ -63,7 +66,7 @@ end it "handles authorization callbacks and redirects to prior requested landing path" do - session[:final_landing_path] = "/admin/wizards/wizard" + session_hash[:final_landing_path] = "/admin/wizards/wizard" request_id = cookies[:user_api_request_id] = DiscourseSubscriptionClient::Authorization.request_id(supplier.id) payload = generate_auth_payload(admin.id, request_id) stub_subscription_request(200, resource, subscription_response) @@ -119,6 +122,7 @@ end it "handles authorization callbacks" do + session_hash[:final_landing_path] = "/admin/plugins/subscription-client/subscriptions" request_id = cookies[:user_api_request_id] = DiscourseSubscriptionClient::Authorization.request_id(supplier.id) payload = generate_auth_payload(moderator.id, request_id) stub_subscription_request(200, resource, subscription_response) diff --git a/spec/support/session_double.rb b/spec/support/session_double.rb new file mode 100644 index 0000000..e322ac4 --- /dev/null +++ b/spec/support/session_double.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +shared_context "session double" do + let(:session_hash) { {} } + + before do + session_double = instance_double(ActionDispatch::Request::Session, enabled?: true, loaded?: false) + + allow(session_double).to receive(:[]) do |key| + session_hash[key] + end + + allow(session_double).to receive(:[]=) do |key, value| + session_hash[key] = value + end + + allow(session_double).to receive(:delete) do |key| + session_hash.delete(key) + end + + allow(session_double).to receive(:clear) do |_key| + session_hash.clear + end + + allow(session_double).to receive(:fetch) do |key| + session_hash.fetch(key) + end + + allow(session_double).to receive(:key?) do |key| + session_hash.key?(key) + end + + allow_any_instance_of(ActionDispatch::Request) + .to receive(:session).and_return(session_double) + end +end