From d1af26eed9202b46f136c13779b8258eee386181 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Fri, 13 Nov 2020 13:55:33 -0600 Subject: [PATCH] Clean up test suite --- .gitignore | 10 +- CHANGELOG.md | 1 + Gemfile | 15 +- Rakefile | 4 + _config.yml | 1 - acts_as_tenant.gemspec | 1 - bin/rails | 13 + bin/test | 5 + rails/init.rb | 2 - spec/active_record_helper.rb | 21 - spec/active_record_models.rb | 151 ------ spec/acts_as_tenant/configuration_spec.rb | 24 +- spec/acts_as_tenant/model_extensions_spec.rb | 441 +++++++----------- spec/acts_as_tenant/sidekiq_spec.rb | 13 +- .../test_tenant_middleware_spec.rb | 6 +- spec/database.yml | 3 - spec/dummy/.ruby-version | 1 + spec/dummy/Rakefile | 6 + spec/dummy/app/assets/config/manifest.js | 2 + spec/dummy/app/assets/images/.keep | 0 .../app/assets/stylesheets/application.css | 15 + .../app/channels/application_cable/channel.rb | 4 + .../channels/application_cable/connection.rb | 4 + .../app/controllers/application_controller.rb | 2 + spec/dummy/app/controllers/concerns/.keep | 0 spec/dummy/app/helpers/application_helper.rb | 2 + .../dummy/app/javascript/packs/application.js | 15 + spec/dummy/app/jobs/application_job.rb | 7 + spec/dummy/app/mailers/application_mailer.rb | 4 + spec/dummy/app/mailers/user_mailer.rb | 5 + spec/dummy/app/models/account.rb | 4 + spec/dummy/app/models/aliased_task.rb | 4 + spec/dummy/app/models/article.rb | 3 + spec/dummy/app/models/comment.rb | 5 + spec/dummy/app/models/concerns/.keep | 0 .../app/models/custom_counter_cache_task.rb | 4 + .../app/models/custom_foreign_key_task.rb | 4 + .../app/models/custom_primary_key_task.rb | 4 + spec/dummy/app/models/global_project.rb | 6 + spec/dummy/app/models/manager.rb | 4 + .../app/models/polymorphic_tenant_comment.rb | 5 + spec/dummy/app/models/project.rb | 8 + spec/dummy/app/models/task.rb | 7 + spec/dummy/app/models/unique_task.rb | 5 + spec/dummy/app/models/unscoped_model.rb | 3 + .../app/views/layouts/application.html.erb | 14 + spec/dummy/app/views/layouts/mailer.html.erb | 13 + spec/dummy/app/views/layouts/mailer.text.erb | 1 + spec/dummy/bin/rails | 4 + spec/dummy/bin/rake | 4 + spec/dummy/bin/setup | 33 ++ spec/dummy/config.ru | 5 + spec/dummy/config/application.rb | 15 + spec/dummy/config/boot.rb | 5 + spec/dummy/config/cable.yml | 10 + spec/dummy/config/database.yml | 19 + spec/dummy/config/environment.rb | 5 + spec/dummy/config/environments/development.rb | 62 +++ spec/dummy/config/environments/production.rb | 112 +++++ spec/dummy/config/environments/test.rb | 49 ++ .../application_controller_renderer.rb | 8 + spec/dummy/config/initializers/assets.rb | 12 + .../initializers/backtrace_silencers.rb | 7 + .../initializers/content_security_policy.rb | 28 ++ .../config/initializers/cookies_serializer.rb | 5 + .../initializers/filter_parameter_logging.rb | 4 + spec/dummy/config/initializers/inflections.rb | 16 + spec/dummy/config/initializers/mime_types.rb | 4 + .../config/initializers/wrap_parameters.rb | 14 + spec/dummy/config/locales/en.yml | 40 ++ spec/dummy/config/puma.rb | 38 ++ spec/dummy/config/routes.rb | 4 + spec/dummy/config/spring.rb | 6 + spec/dummy/config/storage.yml | 34 ++ spec/dummy/db/schema.rb | 84 ++++ spec/dummy/lib/assets/.keep | 0 spec/dummy/log/.keep | 0 spec/dummy/public/404.html | 67 +++ spec/dummy/public/422.html | 67 +++ spec/dummy/public/500.html | 66 +++ .../public/apple-touch-icon-precomposed.png | 0 spec/dummy/public/apple-touch-icon.png | 0 spec/dummy/public/favicon.ico | 0 spec/dummy/tmp/development_secret.txt | 1 + spec/fixtures/accounts.yml | 5 + spec/fixtures/custom_primary_key_tasks.yml | 2 + spec/fixtures/global_projects.yml | 13 + spec/fixtures/projects.yml | 10 + spec/spec_helper.rb | 20 +- 89 files changed, 1245 insertions(+), 505 deletions(-) delete mode 100644 _config.yml create mode 100755 bin/rails create mode 100755 bin/test delete mode 100644 rails/init.rb delete mode 100644 spec/active_record_helper.rb delete mode 100644 spec/active_record_models.rb delete mode 100644 spec/database.yml create mode 100644 spec/dummy/.ruby-version create mode 100644 spec/dummy/Rakefile create mode 100644 spec/dummy/app/assets/config/manifest.js create mode 100644 spec/dummy/app/assets/images/.keep create mode 100644 spec/dummy/app/assets/stylesheets/application.css create mode 100644 spec/dummy/app/channels/application_cable/channel.rb create mode 100644 spec/dummy/app/channels/application_cable/connection.rb create mode 100644 spec/dummy/app/controllers/application_controller.rb create mode 100644 spec/dummy/app/controllers/concerns/.keep create mode 100644 spec/dummy/app/helpers/application_helper.rb create mode 100644 spec/dummy/app/javascript/packs/application.js create mode 100644 spec/dummy/app/jobs/application_job.rb create mode 100644 spec/dummy/app/mailers/application_mailer.rb create mode 100644 spec/dummy/app/mailers/user_mailer.rb create mode 100644 spec/dummy/app/models/account.rb create mode 100644 spec/dummy/app/models/aliased_task.rb create mode 100644 spec/dummy/app/models/article.rb create mode 100644 spec/dummy/app/models/comment.rb create mode 100644 spec/dummy/app/models/concerns/.keep create mode 100644 spec/dummy/app/models/custom_counter_cache_task.rb create mode 100644 spec/dummy/app/models/custom_foreign_key_task.rb create mode 100644 spec/dummy/app/models/custom_primary_key_task.rb create mode 100644 spec/dummy/app/models/global_project.rb create mode 100644 spec/dummy/app/models/manager.rb create mode 100644 spec/dummy/app/models/polymorphic_tenant_comment.rb create mode 100644 spec/dummy/app/models/project.rb create mode 100644 spec/dummy/app/models/task.rb create mode 100644 spec/dummy/app/models/unique_task.rb create mode 100644 spec/dummy/app/models/unscoped_model.rb create mode 100644 spec/dummy/app/views/layouts/application.html.erb create mode 100644 spec/dummy/app/views/layouts/mailer.html.erb create mode 100644 spec/dummy/app/views/layouts/mailer.text.erb create mode 100755 spec/dummy/bin/rails create mode 100755 spec/dummy/bin/rake create mode 100755 spec/dummy/bin/setup create mode 100644 spec/dummy/config.ru create mode 100644 spec/dummy/config/application.rb create mode 100644 spec/dummy/config/boot.rb create mode 100644 spec/dummy/config/cable.yml create mode 100644 spec/dummy/config/database.yml create mode 100644 spec/dummy/config/environment.rb create mode 100644 spec/dummy/config/environments/development.rb create mode 100644 spec/dummy/config/environments/production.rb create mode 100644 spec/dummy/config/environments/test.rb create mode 100644 spec/dummy/config/initializers/application_controller_renderer.rb create mode 100644 spec/dummy/config/initializers/assets.rb create mode 100644 spec/dummy/config/initializers/backtrace_silencers.rb create mode 100644 spec/dummy/config/initializers/content_security_policy.rb create mode 100644 spec/dummy/config/initializers/cookies_serializer.rb create mode 100644 spec/dummy/config/initializers/filter_parameter_logging.rb create mode 100644 spec/dummy/config/initializers/inflections.rb create mode 100644 spec/dummy/config/initializers/mime_types.rb create mode 100644 spec/dummy/config/initializers/wrap_parameters.rb create mode 100644 spec/dummy/config/locales/en.yml create mode 100644 spec/dummy/config/puma.rb create mode 100644 spec/dummy/config/routes.rb create mode 100644 spec/dummy/config/spring.rb create mode 100644 spec/dummy/config/storage.yml create mode 100644 spec/dummy/db/schema.rb create mode 100644 spec/dummy/lib/assets/.keep create mode 100644 spec/dummy/log/.keep create mode 100644 spec/dummy/public/404.html create mode 100644 spec/dummy/public/422.html create mode 100644 spec/dummy/public/500.html create mode 100644 spec/dummy/public/apple-touch-icon-precomposed.png create mode 100644 spec/dummy/public/apple-touch-icon.png create mode 100644 spec/dummy/public/favicon.ico create mode 100644 spec/dummy/tmp/development_secret.txt create mode 100644 spec/fixtures/accounts.yml create mode 100644 spec/fixtures/custom_primary_key_tasks.yml create mode 100644 spec/fixtures/global_projects.yml create mode 100644 spec/fixtures/projects.yml diff --git a/.gitignore b/.gitignore index 158b8e4..2bc6352 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,11 @@ .bundle Gemfile.lock pkg/* -spec/*.sqlite3 -spec/*.log +spec/dummy/db/*.sqlite3 +spec/dummy/db/*.sqlite3-journal +spec/dummy/db/*.sqlite3-* +spec/dummy/log/*.log +spec/dummy/storage/ +spec/dummy/tmp/ +.byebug_history +.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md index 93dbf12..7067492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Unreleased ---------- +* Refactor test suite and use dummy Rails app - @excid3 * Remove tenant getter override. Fixes caching issues with association. - @bernardeli 0.4.6 diff --git a/Gemfile b/Gemfile index 77f3677..63e3ea8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,15 @@ -source "http://rubygems.org" +source "https://rubygems.org" +git_source(:github) { |repo| "https://github.com/#{repo}.git" } -# Specify your gem's dependencies in acts_as_tenant.gemspec +# Declare your gem's dependencies in noticed.gemspec. +# Bundler will treat runtime dependencies like base dependencies, and +# development dependencies will be added by default to the :development group. gemspec + +# Declare any dependencies that are still in development here instead of in +# your gemspec. These might include edge Rails or gems from your path or +# Git. Remember to move these dependencies to your gemspec before releasing +# your gem to rubygems.org. + +# To use a debugger +gem "byebug", group: [:development, :test] diff --git a/Rakefile b/Rakefile index fd36e8a..329c919 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,9 @@ require "bundler/gem_tasks" +APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__) +load "rails/tasks/engine.rake" +load "rails/tasks/statistics.rake" + require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) task default: :spec diff --git a/_config.yml b/_config.yml deleted file mode 100644 index 259a24e..0000000 --- a/_config.yml +++ /dev/null @@ -1 +0,0 @@ -theme: jekyll-theme-tactile \ No newline at end of file diff --git a/acts_as_tenant.gemspec b/acts_as_tenant.gemspec index 454299a..6f5ccb0 100644 --- a/acts_as_tenant.gemspec +++ b/acts_as_tenant.gemspec @@ -21,7 +21,6 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec", ">=3.0" s.add_development_dependency "rspec-rails" - s.add_development_dependency "database_cleaner", "~> 1.8" s.add_development_dependency "sqlite3" s.add_development_dependency "sidekiq", "~> 6.1", ">= 6.1.2" s.add_development_dependency "standard" diff --git a/bin/rails b/bin/rails new file mode 100755 index 0000000..de173dd --- /dev/null +++ b/bin/rails @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby +# This command will automatically be run when you run "rails" with Rails gems +# installed from the root of your application. + +ENGINE_ROOT = File.expand_path('..', __dir__) +APP_PATH = File.expand_path('../test/dummy/config/application', __dir__) + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) + +require 'rails/all' +require 'rails/engine/commands' diff --git a/bin/test b/bin/test new file mode 100755 index 0000000..5516a12 --- /dev/null +++ b/bin/test @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +$: << File.expand_path("../test", __dir__) + +require "bundler/setup" +require "rails/plugin/test" diff --git a/rails/init.rb b/rails/init.rb deleted file mode 100644 index 579d935..0000000 --- a/rails/init.rb +++ /dev/null @@ -1,2 +0,0 @@ -ActiveRecord::Base.send(:include, ActsAsTenant::ModelExtensions) -ActionController::Base.extend ActsAsTenant::ControllerExtensions diff --git a/spec/active_record_helper.rb b/spec/active_record_helper.rb deleted file mode 100644 index 00686b0..0000000 --- a/spec/active_record_helper.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "database_cleaner" -require "yaml" - -dbconfig = YAML.safe_load(IO.read(File.join(File.dirname(__FILE__), "database.yml"))) -ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log")) -ActiveRecord::Base.establish_connection(dbconfig[ENV["DB"] || "sqlite"]) - -RSpec.configure do |config| - config.before(:suite) do - DatabaseCleaner[:active_record].strategy = :transaction - DatabaseCleaner[:active_record].clean_with(:truncation) - end - - config.before(:each) do - DatabaseCleaner[:active_record].start - end - - config.after(:each) do - DatabaseCleaner[:active_record].clean - end -end diff --git a/spec/active_record_models.rb b/spec/active_record_models.rb deleted file mode 100644 index 7132ab3..0000000 --- a/spec/active_record_models.rb +++ /dev/null @@ -1,151 +0,0 @@ -ActiveRecord::Schema.define(version: 1) do - create_table :accounts, force: true do |t| - t.column :name, :string - t.column :subdomain, :string - t.column :domain, :string - t.column :projects_count, :integer, default: 0 - end - - create_table :projects, force: true do |t| - t.column :name, :string - t.column :account_id, :integer - end - - create_table :managers, force: true do |t| - t.column :name, :string - t.column :project_id, :integer - t.column :account_id, :integer - end - - create_table :tasks, force: true do |t| - t.column :name, :string - t.column :account_id, :integer - t.column :project_id, :integer - t.column :completed, :boolean - end - - create_table :countries, force: true do |t| - t.column :name, :string - end - - create_table :unscoped_models, force: true do |t| - t.column :name, :string - end - - create_table :aliased_tasks, force: true do |t| - t.column :name, :string - t.column :project_alias_id, :integer - t.column :account_id, :integer - end - - create_table :unique_tasks, force: true do |t| - t.column :name, :string - t.column :user_defined_scope, :string - t.column :project_id, :integer - t.column :account_id, :integer - end - - create_table :custom_foreign_key_tasks, force: true do |t| - t.column :name, :string - t.column :accountID, :integer - end - - create_table :custom_primary_key_tasks, force: true do |t| - t.column :name, :string - end - - create_table :articles, force: true do |t| - t.column :title, :string - end - - create_table :comments, force: true do |t| - t.column :commentable_id, :integer - t.column :commentable_type, :string - t.column :account_id, :integer - end - - create_table :polymorphic_tenant_comments, force: true do |t| - t.column :polymorphic_tenant_commentable_id, :integer - t.column :polymorphic_tenant_commentable_type, :string - t.column :account_id, :integer - end -end - -class Account < ActiveRecord::Base - has_many :projects -end - -class Project < ActiveRecord::Base - has_one :manager - has_many :tasks - has_many :polymorphic_tenant_comments, as: :polymorphic_tenant_commentable - acts_as_tenant :account - - validates_uniqueness_to_tenant :name -end - -class Manager < ActiveRecord::Base - belongs_to :project - acts_as_tenant :account -end - -class Task < ActiveRecord::Base - belongs_to :project - default_scope -> { where(completed: nil).order("name") } - - acts_as_tenant :account - validates_uniqueness_of :name -end - -class UnscopedModel < ActiveRecord::Base - validates_uniqueness_of :name -end - -class AliasedTask < ActiveRecord::Base - acts_as_tenant(:account) - belongs_to :project_alias, class_name: "Project" -end - -class UniqueTask < ActiveRecord::Base - acts_as_tenant(:account) - belongs_to :project - validates_uniqueness_to_tenant :name, scope: :user_defined_scope -end - -class CustomForeignKeyTask < ActiveRecord::Base - acts_as_tenant(:account, foreign_key: "accountID") - validates_uniqueness_to_tenant :name -end - -class CustomPrimaryKeyTask < ActiveRecord::Base - acts_as_tenant(:account, foreign_key: "name", primary_key: "name") - validates_presence_of :name -end - -class CustomCounterCacheTask < ActiveRecord::Base - self.table_name = "projects" - acts_as_tenant(:account, counter_cache: "projects_count") -end - -class Comment < ActiveRecord::Base - belongs_to :commentable, polymorphic: true - belongs_to :task, -> { where(comments: {commentable_type: "Task"}) }, foreign_key: "commentable_id" - acts_as_tenant :account -end - -class Article < ActiveRecord::Base - has_many :polymorphic_tenant_comments, as: :polymorphic_tenant_commentable -end - -class PolymorphicTenantComment < ActiveRecord::Base - belongs_to :polymorphic_tenant_commentable, polymorphic: true - belongs_to :account - acts_as_tenant :polymorphic_tenant_commentable, polymorphic: true -end - -class GlobalProject < ActiveRecord::Base - self.table_name = "projects" - - acts_as_tenant :account, has_global_records: true - validates_uniqueness_to_tenant :name -end diff --git a/spec/acts_as_tenant/configuration_spec.rb b/spec/acts_as_tenant/configuration_spec.rb index 6860e98..6555291 100644 --- a/spec/acts_as_tenant/configuration_spec.rb +++ b/spec/acts_as_tenant/configuration_spec.rb @@ -1,27 +1,17 @@ require "spec_helper" describe ActsAsTenant::Configuration do - describe "no configuration given" do - before do - ActsAsTenant.configure - end + after { ActsAsTenant.configure } - it "provides defaults" do - expect(ActsAsTenant.configuration.require_tenant).not_to be_truthy - end + it "provides defaults" do + expect(ActsAsTenant.configuration.require_tenant).not_to be_truthy end - describe "with config block" do - after do - ActsAsTenant.configure + it "stores config" do + ActsAsTenant.configure do |config| + config.require_tenant = true end - it "stores config" do - ActsAsTenant.configure do |config| - config.require_tenant = true - end - - expect(ActsAsTenant.configuration.require_tenant).to eq(true) - end + expect(ActsAsTenant.configuration.require_tenant).to eq(true) end end diff --git a/spec/acts_as_tenant/model_extensions_spec.rb b/spec/acts_as_tenant/model_extensions_spec.rb index ec569f0..7345d48 100644 --- a/spec/acts_as_tenant/model_extensions_spec.rb +++ b/spec/acts_as_tenant/model_extensions_spec.rb @@ -1,212 +1,152 @@ require "spec_helper" describe ActsAsTenant do + fixtures :all + after { ActsAsTenant.current_tenant = nil } - # Setting and getting - describe "Setting the current tenant" do - before { ActsAsTenant.current_tenant = :foo } - it { expect(ActsAsTenant.current_tenant).to eq(:foo) } - end + let(:account) { accounts(:foo) } - describe "is_scoped_as_tenant should return the correct value when true" do - it { expect(Project.respond_to?(:scoped_by_tenant?)).to eq(true) } + it "can set the current tenant" do + ActsAsTenant.current_tenant = :foo + expect(ActsAsTenant.current_tenant).to eq(:foo) end - describe "is_scoped_as_tenant should return the correct value when false" do - it { expect(UnscopedModel.respond_to?(:scoped_by_tenant?)).to eq(false) } + it "is_scoped_as_tenant should return the correct value when true" do + expect(Project.respond_to?(:scoped_by_tenant?)).to eq(true) end - describe "tenant_id should be immutable, if already set" do - before do - @account = Account.create!(name: "foo") - @project = @account.projects.create!(name: "bar") - end - - it { expect { @project.account_id = @account.id + 1 }.to raise_error(ActsAsTenant::Errors::TenantIsImmutable) } + it "is_scoped_as_tenant should return the correct value when false" do + expect(UnscopedModel.respond_to?(:scoped_by_tenant?)).to eq(false) end - describe "setting tenant_id to the same value should not error" do - before do - @account = Account.create!(name: "foo") - @project = @account.projects.create!(name: "bar") - end - - it { expect { @project.account_id = @account.id }.not_to raise_error } + it "tenant_id should be immutable, if already set" do + project = account.projects.create!(name: "bar") + expect { project.account_id = account.id + 1 }.to raise_error(ActsAsTenant::Errors::TenantIsImmutable) end - describe "setting tenant_id to a string with same to_i value should not error" do - before do - @account = Account.create!(name: "foo") - @project = @account.projects.create!(name: "bar") - end - - it { expect { @project.account_id = @account.id.to_s }.not_to raise_error } + it "setting tenant_id to the same value should not error" do + project = account.projects.create!(name: "bar") + expect { project.account_id = account.id }.not_to raise_error end - describe "tenant_id should be mutable, if not already set" do - before do - @account = Account.create!(name: "foo") - @project = Project.create!(name: "bar") - end - - it { expect(@project.account_id).to be_nil } - it { expect { @project.account = @account }.not_to raise_error } + it "setting tenant_id to a string with same to_i value should not error" do + project = account.projects.create!(name: "bar") + expect { project.account_id = account.id.to_s }.not_to raise_error end - describe "tenant_id should auto populate after initialization" do - before do - @account = Account.create!(name: "foo") - ActsAsTenant.current_tenant = @account - end - it { expect(Project.new.account_id).to eq(@account.id) } + it "tenant_id should be mutable, if not already set" do + project = projects(:without_account) + expect(project.account_id).to be_nil + expect { project.account = account }.not_to raise_error end - describe "Handles custom foreign_key on tenant model" do - before do - @account = Account.create!(name: "foo") - ActsAsTenant.current_tenant = @account - @custom_foreign_key_task = CustomForeignKeyTask.create!(name: "foo") - end - - it { expect(@custom_foreign_key_task.account).to eq(@account) } + it "tenant_id should auto populate after initialization" do + ActsAsTenant.current_tenant = account + expect(Project.new.account_id).to eq(account.id) end - describe "Handles custom primary_key on tenant model" do - before do - @account = Account.create!(name: "foo") - CustomPrimaryKeyTask.create!(name: "bar") - ActsAsTenant.current_tenant = @account - @custom_primary_key_task = CustomPrimaryKeyTask.create! - end - - it { expect(@custom_primary_key_task.account).to eq(@account) } - it { expect(CustomPrimaryKeyTask.count).to eq(1) } + it "handles custom foreign_key on tenant model" do + ActsAsTenant.current_tenant = account + custom_foreign_key_task = CustomForeignKeyTask.create!(name: "foo") + expect(custom_foreign_key_task.account).to eq(account) end - describe "Handles counter_cache on tenant model" do - before do - @account = Account.create!(name: "foo") - ActsAsTenant.current_tenant = @account - @project = CustomCounterCacheTask.create!(name: "bar") - end - - it "should correctly increment and decrement the tenants column" do - expect(@account.reload.projects_count).to eq(1) - @project.destroy - expect(@account.reload.projects_count).to eq(0) - end + it "handles custom primary_key on tenant model" do + ActsAsTenant.current_tenant = account + custom_primary_key_task = CustomPrimaryKeyTask.create! + expect(custom_primary_key_task.account).to eq(account) + expect(CustomPrimaryKeyTask.count).to eq(1) end - describe "Handles tenant model updates" do - before do - @account = Account.create!(name: "foo") - @project = @account.projects.create!(name: "bar") - end - - it "should fetch tenant object through belongs_to to avoid accessing stale tenant object if that was updated" do - # Sets current tenant in a way that it does not share the same memory reference of `@account`. - ActsAsTenant.current_tenant = Account.find_by(name: "foo") + it "should correctly increment and decrement the tenants counter_cache column" do + ActsAsTenant.current_tenant = account + project = CustomCounterCacheTask.create!(name: "bar") + expect(account.reload.projects_count).to eq(1) + project.destroy + expect(account.reload.projects_count).to eq(0) + end - expect(@project.account.name).to eq("foo") - @account.update!(name: "Acme") - expect(@project.account.name).to eq("Acme") - end + it "does not cache account association" do + project = account.projects.first + ActsAsTenant.current_tenant = account + expect(project.account.name).to eq(account.name) + account.update!(name: "Acme") + expect(project.account.name).to eq("Acme") end # Scoping models - describe "Project.all should be scoped to the current tenant if set" do - before do - @account1 = Account.create!(name: "foo") - @account2 = Account.create!(name: "bar") - - @project1 = @account1.projects.create!(name: "foobar") - @project2 = @account2.projects.create!(name: "baz") - - ActsAsTenant.current_tenant = @account1 - @projects = Project.all - end - - it { expect(@projects.length).to eq(1) } - it { expect(@projects).to eq([@project1]) } + it "Project.all should be scoped to the current tenant if set" do + ActsAsTenant.current_tenant = account + expect(Project.count).to eq(account.projects.count) + expect(Project.all).to eq(account.projects) end - describe "Project.unscoped.all should return the unscoped value" do - before do - @account1 = Account.create!(name: "foo") - @account2 = Account.create!(name: "bar") - - @project1 = @account1.projects.create!(name: "foobar") - @project2 = @account2.projects.create!(name: "baz") - - ActsAsTenant.current_tenant = @account1 - @projects = Project.unscoped - end - - it { expect(@projects.count).to eq(2) } + it "Project.unscoped.all should return the unscoped value" do + ActsAsTenant.current_tenant = account + expect(Project.unscoped.count).to be > account.projects.count end - describe "Querying the tenant from a scoped model without a tenant set" do - before do - @project = Project.create!(name: "bar") - end - - it { @project.account } + it "Querying the tenant from a scoped model without a tenant set" do + expect(projects(:foo).account).to_not be_nil end - describe "Querying the tenant from a scoped model with a tenant set" do - before do - @account = Account.create!(name: "foo") - @project = @account.projects.create!(name: "foobar") - ActsAsTenant.current_tenant = @account1 - end - - it { @project.account } + it "Querying the tenant from a scoped model with a tenant set" do + ActsAsTenant.current_tenant = account + expect(projects(:foo).account).to eq(accounts(:foo)) + expect(projects(:bar).account).to eq(accounts(:bar)) end describe "A tenant model with global records" do before do - @account = Account.create!(name: "foo") - @project1 = GlobalProject.create!(name: "foobar global") - @project2 = GlobalProject.create!(name: "unaccessible project", account: Account.create!) - ActsAsTenant.current_tenant = @account - @project3 = GlobalProject.create!(name: "foobar") + ActsAsTenant.current_tenant = account end - it "should return two projects" do - expect(GlobalProject.all.count).to eq(2) + it "should return global and tenant projects" do + expect(GlobalProject.count).to eq(GlobalProject.unscoped.where(account: [nil, account]).count) end - it "should validate the project name against the global records too" do - expect(GlobalProject.new(name: "foobar").valid?).to be(false) - expect(GlobalProject.new(name: "foobar new").valid?).to be(true) - expect(GlobalProject.new(name: "foobar global").valid?).to be(false) - expect(@project1.valid?).to be(true) + context "should validate tenant records against global & tenant records" do + it "global records are valid" do + expect(global_projects(:global).valid?).to be(true) + end + + it "allows separate global and tenant records" do + expect(GlobalProject.new(name: "foo new").valid?).to be(true) + end + + it "is invalid with with duplicate tenant records" do + expect(GlobalProject.new(name: "global foo").valid?).to be(false) + end + + it "is invalid if tenant record conflicts with global record" do + expect(GlobalProject.new(name: "global").valid?).to be(false) + end end it "should add the model to ActsAsTenant.models_with_global_records" do - expect(ActsAsTenant.models_with_global_records.include?(GlobalProject)).to be(true) - expect(ActsAsTenant.models_with_global_records.include?(Project)).to be(false) + expect(ActsAsTenant.models_with_global_records.include?(GlobalProject)).to be_truthy + expect(ActsAsTenant.models_with_global_records.include?(Project)).to be_falsy end end # Associations - describe "Associations should be correctly scoped by current tenant" do + context "Associations should be correctly scoped by current tenant" do before do - @account = Account.create!(name: "foo") - @project = Project.create!(name: "foobar", account: @account) + @project = account.projects.create!(name: "foobar") + # the next line should normally be (nearly) impossible: a task assigned to a tenant project, # but the task has no tenant assigned @task1 = Task.create!(name: "no_tenant", project: @project) - ActsAsTenant.current_tenant = @account + ActsAsTenant.current_tenant = account @task2 = @project.tasks.create!(name: "baz") @project.reload end it "should correctly set the tenant on the task created with current_tenant set" do - expect(@task2.account).to eq(@account) + expect(@task2.account).to eq(account) end it "should filter out the non-tenant task from the project" do @@ -214,49 +154,36 @@ end end - describe "Associations can only be made with in-scope objects" do - before do - @account = Account.create!(name: "foo") - @project1 = Project.create!(name: "inaccessible_project", account: Account.create!) - ActsAsTenant.current_tenant = @account + it "associations can only be made with in-scope objects" do + project1 = accounts(:bar).projects.create!(name: "inaccessible_project") + ActsAsTenant.current_tenant = account - @project2 = Project.create!(name: "accessible_project") - @task = @project2.tasks.create!(name: "bar") - end + project2 = Project.create!(name: "accessible_project") + task = project2.tasks.create!(name: "bar") - it { expect(@task.update(project_id: @project1.id)).to eq(false) } + expect(task.update(project_id: project1.id)).to eq(false) end - describe "Create and save an AaT-enabled child without it having a parent" do - before do - @account = Account.create!(name: "baz") - ActsAsTenant.current_tenant = @account - end - it { expect(Task.create(name: "bar").valid?).to eq(true) } + it "can create and save an AaT-enabled child without it having a parent" do + ActsAsTenant.current_tenant = account + expect(Task.new(name: "bar").valid?).to eq(true) end - describe "It should be possible to use aliased associations" do - it { expect(AliasedTask.create(name: "foo", project_alias: @project2).valid?).to eq(true) } + it "should be possible to use aliased associations" do + expect(AliasedTask.create(name: "foo", project_alias: @project2).valid?).to eq(true) end describe "It should be possible to use associations with foreign_key from polymorphic" do - context "tenanted objects have a polymorphic association" do - before do - @account = Account.create!(name: "foo") - ActsAsTenant.current_tenant = @account - @project = Project.create!(name: "project", account: @account) - @comment = Comment.new commentable: @project, account: @account - end - - it { expect(@comment.save!).to eq(true) } + it "tenanted objects have a polymorphic association" do + ActsAsTenant.current_tenant = account + expect { Comment.create!(commentable: account.projects.first) }.not_to raise_error end context "tenant is polymorphic" do before do - @account = Account.create!(name: "foo") @project = Project.create!(name: "polymorphic project") ActsAsTenant.current_tenant = @project - @comment = PolymorphicTenantComment.new(account: @account) + @comment = PolymorphicTenantComment.new(account: account) end it "populates commentable_type with the current tenant" do @@ -282,61 +209,50 @@ end # Additional default_scopes - describe "When dealing with a user defined default_scope" do - before do - @account = Account.create!(name: "foo") - @project1 = Project.create!(name: "inaccessible") - @task1 = Task.create!(name: "no_tenant", project: @project1) + it "should apply both the tenant scope and the user defined default_scope, including :order" do + project1 = Project.create!(name: "inaccessible") + task1 = Task.create!(name: "no_tenant", project: project1) - ActsAsTenant.current_tenant = @account - @project2 = Project.create!(name: "accessible") - @task2 = @project2.tasks.create!(name: "bar") - @task3 = @project2.tasks.create!(name: "baz") - @task4 = @project2.tasks.create!(name: "foo") - @task5 = @project2.tasks.create!(name: "foobar", completed: true) + ActsAsTenant.current_tenant = account + project2 = Project.create!(name: "accessible") + task2 = project2.tasks.create!(name: "bar") + task3 = project2.tasks.create!(name: "baz") + task4 = project2.tasks.create!(name: "foo") + task5 = project2.tasks.create!(name: "foobar", completed: true) - @tasks = Task.all - end + tasks = Task.all - it "should apply both the tenant scope and the user defined default_scope, including :order" do - expect(@tasks.length).to eq(3) - expect(@tasks).to eq([@task2, @task3, @task4]) - end + expect(tasks.length).to eq(3) + expect(tasks).to eq([task2, task3, task4]) end # Validates_uniqueness - describe "When using validates_uniqueness_to_tenant in a aat model" do + context "When using validates_uniqueness_to_tenant in a aat model" do before do - account = Account.create!(name: "foo") + @name = "existing_name" ActsAsTenant.current_tenant = account - Project.create!(name: "existing_name") + Project.create!(name: @name) end it "should not be possible to create a duplicate within the same tenant" do - expect(Project.create(name: "existing_name").valid?).to eq(false) + expect(Project.new(name: @name).valid?).to eq(false) end - it "should be possible to create a duplicate outside the tenant scope" do - account = Account.create!(name: "baz") - ActsAsTenant.current_tenant = account - expect(Project.create(name: "bar").valid?).to eq(true) + it "should be possible to create a duplicate in another tenant" do + ActsAsTenant.current_tenant = accounts(:bar) + expect(Project.create(name: @name).valid?).to eq(true) end end - describe "Handles user defined scopes" do - before do - UniqueTask.create!(name: "foo", user_defined_scope: "unique_scope") - end - - it { expect(UniqueTask.create(name: "foo", user_defined_scope: "another_scope")).to be_valid } - it { expect(UniqueTask.create(name: "foo", user_defined_scope: "unique_scope")).not_to be_valid } + it "handles user defined scopes" do + UniqueTask.create!(name: "foo", user_defined_scope: "unique_scope") + expect(UniqueTask.create(name: "foo", user_defined_scope: "another_scope")).to be_valid + expect(UniqueTask.create(name: "foo", user_defined_scope: "unique_scope")).not_to be_valid end - describe "When using validates_uniqueness_of in a NON-aat model" do - before do - UnscopedModel.create!(name: "foo") - end + context "When using validates_uniqueness_of in a NON-aat model" do it "should not be possible to create duplicates" do + UnscopedModel.create!(name: "foo") expect(UnscopedModel.create(name: "foo").valid?).to eq(false) end end @@ -344,31 +260,20 @@ # ::with_tenant describe "::with_tenant" do it "should set current_tenant to the specified tenant inside the block" do - @account = Account.create!(name: "baz") - - ActsAsTenant.with_tenant(@account) do - expect(ActsAsTenant.current_tenant).to eq(@account) + ActsAsTenant.with_tenant(account) do + expect(ActsAsTenant.current_tenant).to eq(account) end end it "should reset current_tenant to the previous tenant once exiting the block" do - @account1 = Account.create!(name: "foo") - @account2 = Account.create!(name: "bar") - - ActsAsTenant.current_tenant = @account1 - ActsAsTenant.with_tenant @account2 do - end - - expect(ActsAsTenant.current_tenant).to eq(@account1) + ActsAsTenant.current_tenant = account + ActsAsTenant.with_tenant(accounts(:bar)) {} + expect(ActsAsTenant.current_tenant).to eq(account) end it "should return the value of the block" do - @account1 = Account.create!(name: "foo") - @account2 = Account.create!(name: "bar") - - ActsAsTenant.current_tenant = @account1 - value = ActsAsTenant.with_tenant(@account2) { "something" } - + ActsAsTenant.current_tenant = account + value = ActsAsTenant.with_tenant(accounts(:bar)) { "something" } expect(value).to eq "something" end @@ -395,20 +300,13 @@ end it "should reset current_tenant to the previous tenant once exiting the block" do - @account1 = Account.create!(name: "foo") - - ActsAsTenant.current_tenant = @account1 - ActsAsTenant.without_tenant do - end - - expect(ActsAsTenant.current_tenant).to eq(@account1) + ActsAsTenant.current_tenant = account + ActsAsTenant.without_tenant {} + expect(ActsAsTenant.current_tenant).to eq(account) end it "should return the value of the block" do - value = ActsAsTenant.without_tenant { - "something" - } - + value = ActsAsTenant.without_tenant { "something" } expect(value).to eq "something" end @@ -420,80 +318,59 @@ # Tenant required context "tenant required" do before do - @account1 = Account.create!(name: "foo") - @project1 = @account1.projects.create!(name: "foobar") + account.projects.create!(name: "foobar") allow(ActsAsTenant.configuration).to receive_messages(require_tenant: true) end - describe "raises exception if no tenant specified" do - it "should raise an error when no tenant is provided" do - expect { Project.all }.to raise_error(ActsAsTenant::Errors::NoTenantSet) - end + it "should raise an error when no tenant is provided" do + expect { Project.all }.to raise_error(ActsAsTenant::Errors::NoTenantSet) end - describe "does not raise exception when run in unscoped mode" do - it "should not raise an error when no tenant is provided" do - expect { - ActsAsTenant.without_tenant { Project.all } - }.to_not raise_error - end + it "should not raise an error when no tenant is provided" do + expect { ActsAsTenant.without_tenant { Project.all } }.to_not raise_error end end context "no tenant required" do - describe "does not raise exception if no tenant specified" do - before do - @account1 = Account.create!(name: "foo") - @project1 = @account1.projects.create!(name: "foobar") - end - - it "should not raise an error when no tenant is provided" do - expect { Project.all }.to_not raise_error - end + it "should not raise an error when no tenant is provided" do + expect { Project.all }.to_not raise_error end end describe "ActsAsTenant.default_tenant=" do - before(:each) do - @account = Account.create! - end - after(:each) do ActsAsTenant.default_tenant = nil end it "provides current_tenant" do - ActsAsTenant.default_tenant = @account - expect(ActsAsTenant.current_tenant).to eq(@account) + ActsAsTenant.default_tenant = account + expect(ActsAsTenant.current_tenant).to eq(account) end it "can be overridden by assignment" do - ActsAsTenant.default_tenant = @account - @account2 = Account.create! - ActsAsTenant.current_tenant = @account2 - expect(ActsAsTenant.current_tenant).not_to eq(@account) + ActsAsTenant.default_tenant = account + ActsAsTenant.current_tenant = accounts(:bar) + expect(ActsAsTenant.current_tenant).to eq(accounts(:bar)) end it "can be overridden by with_tenant" do - ActsAsTenant.default_tenant = @account - @account2 = Account.create! - ActsAsTenant.with_tenant @account2 do - expect(ActsAsTenant.current_tenant).to eq(@account2) + ActsAsTenant.default_tenant = account + ActsAsTenant.with_tenant accounts(:bar) do + expect(ActsAsTenant.current_tenant).to eq(accounts(:bar)) end - expect(ActsAsTenant.current_tenant).to eq(@account) + expect(ActsAsTenant.current_tenant).to eq(account) end it "doesn't override existing current_tenant" do - @account2 = Account.create! - ActsAsTenant.current_tenant = @account2 - ActsAsTenant.default_tenant = @account - expect(ActsAsTenant.current_tenant).to eq(@account2) + ActsAsTenant.current_tenant = accounts(:bar) + ActsAsTenant.default_tenant = account + expect(ActsAsTenant.current_tenant).to eq(accounts(:bar)) end it "survives request resets" do - ActsAsTenant.default_tenant = @account + ActsAsTenant.default_tenant = account RequestStore.clear! - expect(ActsAsTenant.current_tenant).to eq(@account) + expect(ActsAsTenant.current_tenant).to eq(account) end end end diff --git a/spec/acts_as_tenant/sidekiq_spec.rb b/spec/acts_as_tenant/sidekiq_spec.rb index a8164cc..c468dac 100644 --- a/spec/acts_as_tenant/sidekiq_spec.rb +++ b/spec/acts_as_tenant/sidekiq_spec.rb @@ -4,6 +4,7 @@ describe ActsAsTenant::Sidekiq do after { ActsAsTenant.current_tenant = nil } + let(:account) { Account.new(id: 1234) } let(:message) { {"acts_as_tenant" => {"class" => "Account", "id" => 1234}} } @@ -51,13 +52,9 @@ end end - describe "Sidekiq configuration" do - describe "client configuration" do - it "includes ActsAsTenant client" do - expect(Sidekiq.client_middleware.exists?(ActsAsTenant::Sidekiq::Client)).to eq(true) - end - end - - # unable to test server configuration + it "includes ActsAsTenant client middleware" do + expect(Sidekiq.client_middleware.exists?(ActsAsTenant::Sidekiq::Client)).to eq(true) end + + # unable to test server configuration end diff --git a/spec/acts_as_tenant/test_tenant_middleware_spec.rb b/spec/acts_as_tenant/test_tenant_middleware_spec.rb index 05f1421..b5b4b54 100644 --- a/spec/acts_as_tenant/test_tenant_middleware_spec.rb +++ b/spec/acts_as_tenant/test_tenant_middleware_spec.rb @@ -23,14 +23,16 @@ def self.assert_current_id(id) end describe ActsAsTenant::TestTenantMiddleware do + fixtures :accounts + after { ActsAsTenant.current_tenant = nil } subject { request.get("/some/path") } let(:middleware) { described_class.new(app) } let(:request) { Rack::MockRequest.new(middleware) } - let!(:account1) { Account.create } - let!(:account2) { Account.create } + let!(:account1) { accounts(:foo) } + let!(:account2) { accounts(:bar) } context "when test_tenant is nil before processing" do before { ActsAsTenant.test_tenant = nil } diff --git a/spec/database.yml b/spec/database.yml deleted file mode 100644 index 4d41915..0000000 --- a/spec/database.yml +++ /dev/null @@ -1,3 +0,0 @@ -sqlite: - adapter: sqlite3 - database: spec/actsastenant.sqlite3 \ No newline at end of file diff --git a/spec/dummy/.ruby-version b/spec/dummy/.ruby-version new file mode 100644 index 0000000..bff6ce5 --- /dev/null +++ b/spec/dummy/.ruby-version @@ -0,0 +1 @@ +ruby-2.7.1 diff --git a/spec/dummy/Rakefile b/spec/dummy/Rakefile new file mode 100644 index 0000000..9a5ea73 --- /dev/null +++ b/spec/dummy/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require_relative "config/application" + +Rails.application.load_tasks diff --git a/spec/dummy/app/assets/config/manifest.js b/spec/dummy/app/assets/config/manifest.js new file mode 100644 index 0000000..5918193 --- /dev/null +++ b/spec/dummy/app/assets/config/manifest.js @@ -0,0 +1,2 @@ +//= link_tree ../images +//= link_directory ../stylesheets .css diff --git a/spec/dummy/app/assets/images/.keep b/spec/dummy/app/assets/images/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/app/assets/stylesheets/application.css b/spec/dummy/app/assets/stylesheets/application.css new file mode 100644 index 0000000..0ebd7fe --- /dev/null +++ b/spec/dummy/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS + * files in this directory. Styles in this file should be added after the last require_* statement. + * It is generally better to create a new file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/spec/dummy/app/channels/application_cable/channel.rb b/spec/dummy/app/channels/application_cable/channel.rb new file mode 100644 index 0000000..d672697 --- /dev/null +++ b/spec/dummy/app/channels/application_cable/channel.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Channel < ActionCable::Channel::Base + end +end diff --git a/spec/dummy/app/channels/application_cable/connection.rb b/spec/dummy/app/channels/application_cable/connection.rb new file mode 100644 index 0000000..0ff5442 --- /dev/null +++ b/spec/dummy/app/channels/application_cable/connection.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Connection < ActionCable::Connection::Base + end +end diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb new file mode 100644 index 0000000..09705d1 --- /dev/null +++ b/spec/dummy/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::Base +end diff --git a/spec/dummy/app/controllers/concerns/.keep b/spec/dummy/app/controllers/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/app/helpers/application_helper.rb b/spec/dummy/app/helpers/application_helper.rb new file mode 100644 index 0000000..de6be79 --- /dev/null +++ b/spec/dummy/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/spec/dummy/app/javascript/packs/application.js b/spec/dummy/app/javascript/packs/application.js new file mode 100644 index 0000000..67ce467 --- /dev/null +++ b/spec/dummy/app/javascript/packs/application.js @@ -0,0 +1,15 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// compiled file. JavaScript code in this file should be added after the last require_* statement. +// +// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details +// about supported directives. +// +//= require rails-ujs +//= require activestorage +//= require_tree . diff --git a/spec/dummy/app/jobs/application_job.rb b/spec/dummy/app/jobs/application_job.rb new file mode 100644 index 0000000..d394c3d --- /dev/null +++ b/spec/dummy/app/jobs/application_job.rb @@ -0,0 +1,7 @@ +class ApplicationJob < ActiveJob::Base + # Automatically retry jobs that encountered a deadlock + # retry_on ActiveRecord::Deadlocked + + # Most jobs are safe to ignore if the underlying records are no longer available + # discard_on ActiveJob::DeserializationError +end diff --git a/spec/dummy/app/mailers/application_mailer.rb b/spec/dummy/app/mailers/application_mailer.rb new file mode 100644 index 0000000..dee2676 --- /dev/null +++ b/spec/dummy/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: "from@example.com", to: "to@example.com" + layout "mailer" +end diff --git a/spec/dummy/app/mailers/user_mailer.rb b/spec/dummy/app/mailers/user_mailer.rb new file mode 100644 index 0000000..cba5fb6 --- /dev/null +++ b/spec/dummy/app/mailers/user_mailer.rb @@ -0,0 +1,5 @@ +class UserMailer < ApplicationMailer + def comment_notification + mail(body: "") + end +end diff --git a/spec/dummy/app/models/account.rb b/spec/dummy/app/models/account.rb new file mode 100644 index 0000000..75ed86c --- /dev/null +++ b/spec/dummy/app/models/account.rb @@ -0,0 +1,4 @@ +class Account < ActiveRecord::Base + has_many :projects + has_many :global_projects +end diff --git a/spec/dummy/app/models/aliased_task.rb b/spec/dummy/app/models/aliased_task.rb new file mode 100644 index 0000000..bebd20d --- /dev/null +++ b/spec/dummy/app/models/aliased_task.rb @@ -0,0 +1,4 @@ +class AliasedTask < ActiveRecord::Base + acts_as_tenant(:account) + belongs_to :project_alias, class_name: "Project" +end diff --git a/spec/dummy/app/models/article.rb b/spec/dummy/app/models/article.rb new file mode 100644 index 0000000..4799625 --- /dev/null +++ b/spec/dummy/app/models/article.rb @@ -0,0 +1,3 @@ +class Article < ActiveRecord::Base + has_many :polymorphic_tenant_comments, as: :polymorphic_tenant_commentable +end diff --git a/spec/dummy/app/models/comment.rb b/spec/dummy/app/models/comment.rb new file mode 100644 index 0000000..2e7cc50 --- /dev/null +++ b/spec/dummy/app/models/comment.rb @@ -0,0 +1,5 @@ +class Comment < ActiveRecord::Base + belongs_to :commentable, polymorphic: true + belongs_to :task, -> { where(comments: {commentable_type: "Task"}) }, foreign_key: "commentable_id" + acts_as_tenant :account +end diff --git a/spec/dummy/app/models/concerns/.keep b/spec/dummy/app/models/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/app/models/custom_counter_cache_task.rb b/spec/dummy/app/models/custom_counter_cache_task.rb new file mode 100644 index 0000000..55effe5 --- /dev/null +++ b/spec/dummy/app/models/custom_counter_cache_task.rb @@ -0,0 +1,4 @@ +class CustomCounterCacheTask < ActiveRecord::Base + self.table_name = "projects" + acts_as_tenant(:account, counter_cache: "projects_count") +end diff --git a/spec/dummy/app/models/custom_foreign_key_task.rb b/spec/dummy/app/models/custom_foreign_key_task.rb new file mode 100644 index 0000000..e83a665 --- /dev/null +++ b/spec/dummy/app/models/custom_foreign_key_task.rb @@ -0,0 +1,4 @@ +class CustomForeignKeyTask < ActiveRecord::Base + acts_as_tenant(:account, foreign_key: "accountID") + validates_uniqueness_to_tenant :name +end diff --git a/spec/dummy/app/models/custom_primary_key_task.rb b/spec/dummy/app/models/custom_primary_key_task.rb new file mode 100644 index 0000000..1260203 --- /dev/null +++ b/spec/dummy/app/models/custom_primary_key_task.rb @@ -0,0 +1,4 @@ +class CustomPrimaryKeyTask < ActiveRecord::Base + acts_as_tenant(:account, foreign_key: "name", primary_key: "name") + validates_presence_of :name +end diff --git a/spec/dummy/app/models/global_project.rb b/spec/dummy/app/models/global_project.rb new file mode 100644 index 0000000..067b790 --- /dev/null +++ b/spec/dummy/app/models/global_project.rb @@ -0,0 +1,6 @@ +class GlobalProject < ActiveRecord::Base + self.table_name = "projects" + + acts_as_tenant :account, has_global_records: true + validates_uniqueness_to_tenant :name +end diff --git a/spec/dummy/app/models/manager.rb b/spec/dummy/app/models/manager.rb new file mode 100644 index 0000000..ea794f6 --- /dev/null +++ b/spec/dummy/app/models/manager.rb @@ -0,0 +1,4 @@ +class Manager < ActiveRecord::Base + belongs_to :project + acts_as_tenant :account +end diff --git a/spec/dummy/app/models/polymorphic_tenant_comment.rb b/spec/dummy/app/models/polymorphic_tenant_comment.rb new file mode 100644 index 0000000..eaef713 --- /dev/null +++ b/spec/dummy/app/models/polymorphic_tenant_comment.rb @@ -0,0 +1,5 @@ +class PolymorphicTenantComment < ActiveRecord::Base + belongs_to :polymorphic_tenant_commentable, polymorphic: true + belongs_to :account + acts_as_tenant :polymorphic_tenant_commentable, polymorphic: true +end diff --git a/spec/dummy/app/models/project.rb b/spec/dummy/app/models/project.rb new file mode 100644 index 0000000..39084ef --- /dev/null +++ b/spec/dummy/app/models/project.rb @@ -0,0 +1,8 @@ +class Project < ActiveRecord::Base + has_one :manager + has_many :tasks + has_many :polymorphic_tenant_comments, as: :polymorphic_tenant_commentable + acts_as_tenant :account + + validates_uniqueness_to_tenant :name +end diff --git a/spec/dummy/app/models/task.rb b/spec/dummy/app/models/task.rb new file mode 100644 index 0000000..4776789 --- /dev/null +++ b/spec/dummy/app/models/task.rb @@ -0,0 +1,7 @@ +class Task < ActiveRecord::Base + belongs_to :project + default_scope -> { where(completed: nil).order("name") } + + acts_as_tenant :account + validates_uniqueness_of :name +end diff --git a/spec/dummy/app/models/unique_task.rb b/spec/dummy/app/models/unique_task.rb new file mode 100644 index 0000000..3f64701 --- /dev/null +++ b/spec/dummy/app/models/unique_task.rb @@ -0,0 +1,5 @@ +class UniqueTask < ActiveRecord::Base + acts_as_tenant(:account) + belongs_to :project + validates_uniqueness_to_tenant :name, scope: :user_defined_scope +end diff --git a/spec/dummy/app/models/unscoped_model.rb b/spec/dummy/app/models/unscoped_model.rb new file mode 100644 index 0000000..8b8e460 --- /dev/null +++ b/spec/dummy/app/models/unscoped_model.rb @@ -0,0 +1,3 @@ +class UnscopedModel < ActiveRecord::Base + validates_uniqueness_of :name +end diff --git a/spec/dummy/app/views/layouts/application.html.erb b/spec/dummy/app/views/layouts/application.html.erb new file mode 100644 index 0000000..24307d3 --- /dev/null +++ b/spec/dummy/app/views/layouts/application.html.erb @@ -0,0 +1,14 @@ + + + + Dummy + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + <%= stylesheet_link_tag 'application', media: 'all' %> + + + + <%= yield %> + + diff --git a/spec/dummy/app/views/layouts/mailer.html.erb b/spec/dummy/app/views/layouts/mailer.html.erb new file mode 100644 index 0000000..cbd34d2 --- /dev/null +++ b/spec/dummy/app/views/layouts/mailer.html.erb @@ -0,0 +1,13 @@ + + + + + + + + + <%= yield %> + + diff --git a/spec/dummy/app/views/layouts/mailer.text.erb b/spec/dummy/app/views/layouts/mailer.text.erb new file mode 100644 index 0000000..37f0bdd --- /dev/null +++ b/spec/dummy/app/views/layouts/mailer.text.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/spec/dummy/bin/rails b/spec/dummy/bin/rails new file mode 100755 index 0000000..efc0377 --- /dev/null +++ b/spec/dummy/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path("../config/application", __dir__) +require_relative "../config/boot" +require "rails/commands" diff --git a/spec/dummy/bin/rake b/spec/dummy/bin/rake new file mode 100755 index 0000000..4fbf10b --- /dev/null +++ b/spec/dummy/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative "../config/boot" +require "rake" +Rake.application.run diff --git a/spec/dummy/bin/setup b/spec/dummy/bin/setup new file mode 100755 index 0000000..5635f8e --- /dev/null +++ b/spec/dummy/bin/setup @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby +require "fileutils" + +# path to your application root. +APP_ROOT = File.expand_path("..", __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +FileUtils.chdir APP_ROOT do + # This script is a way to setup or update your development environment automatically. + # This script is idempotent, so that you can run it at anytime and get an expectable outcome. + # Add necessary setup steps to this file. + + puts "== Installing dependencies ==" + system! "gem install bundler --conservative" + system("bundle check") || system!("bundle install") + + # puts "\n== Copying sample files ==" + # unless File.exist?('config/database.yml') + # FileUtils.cp 'config/database.yml.sample', 'config/database.yml' + # end + + puts "\n== Preparing database ==" + system! "bin/rails db:prepare" + + puts "\n== Removing old logs and tempfiles ==" + system! "bin/rails log:clear tmp:clear" + + puts "\n== Restarting application server ==" + system! "bin/rails restart" +end diff --git a/spec/dummy/config.ru b/spec/dummy/config.ru new file mode 100644 index 0000000..441e6ff --- /dev/null +++ b/spec/dummy/config.ru @@ -0,0 +1,5 @@ +# This file is used by Rack-based servers to start the application. + +require_relative "config/environment" + +run Rails.application diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb new file mode 100644 index 0000000..a049d7d --- /dev/null +++ b/spec/dummy/config/application.rb @@ -0,0 +1,15 @@ +require_relative "boot" + +require "rails/all" + +Bundler.require(*Rails.groups) +require "acts_as_tenant" + +module Dummy + class Application < Rails::Application + # Settings in config/environments/* take precedence over those specified here. + # Application configuration can go into files in config/initializers + # -- all .rb files in that directory are automatically loaded after loading + # the framework and any gems in your application. + end +end diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb new file mode 100644 index 0000000..116591a --- /dev/null +++ b/spec/dummy/config/boot.rb @@ -0,0 +1,5 @@ +# Set up gems listed in the Gemfile. +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__) + +require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) +$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__) diff --git a/spec/dummy/config/cable.yml b/spec/dummy/config/cable.yml new file mode 100644 index 0000000..98367f8 --- /dev/null +++ b/spec/dummy/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: test + +production: + adapter: redis + url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> + channel_prefix: dummy_production diff --git a/spec/dummy/config/database.yml b/spec/dummy/config/database.yml new file mode 100644 index 0000000..95b19bc --- /dev/null +++ b/spec/dummy/config/database.yml @@ -0,0 +1,19 @@ +# SQLite. Versions 3.8.0 and up are supported. +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem 'sqlite3' +# +default: &default + adapter: sqlite3 + +development: + <<: *default + database: db/acts_as_tenant_dev.sqlite3 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: db/acts_as_tenant_test.sqlite3 diff --git a/spec/dummy/config/environment.rb b/spec/dummy/config/environment.rb new file mode 100644 index 0000000..cac5315 --- /dev/null +++ b/spec/dummy/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative "application" + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb new file mode 100644 index 0000000..09692df --- /dev/null +++ b/spec/dummy/config/environments/development.rb @@ -0,0 +1,62 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join("tmp", "caching-dev.txt").exist? + config.action_controller.perform_caching = true + config.action_controller.enable_fragment_cache_logging = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + config.action_mailer.perform_caching = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + # Debug mode disables concatenation and preprocessing of assets. + # This option may cause significant delays in view rendering with a large + # number of complex assets. + config.assets.debug = true + + # Suppress logger output for asset requests. + config.assets.quiet = true + + # Raises error for missing translations. + # config.action_view.raise_on_missing_translations = true + + # Use an evented file watcher to asynchronously detect changes in source code, + # routes, locales, etc. This feature depends on the listen gem. + # config.file_watcher = ActiveSupport::EventedFileUpdateChecker +end diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb new file mode 100644 index 0000000..8d6cdab --- /dev/null +++ b/spec/dummy/config/environments/production.rb @@ -0,0 +1,112 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # config.require_master_key = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? + + # Compress CSS using a preprocessor. + # config.assets.css_compressor = :sass + + # Do not fallback to assets pipeline if a precompiled asset is missed. + config.assets.compile = false + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = 'http://assets.example.com' + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache + # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Mount Action Cable outside main process or domain. + # config.action_cable.mount_path = nil + # config.action_cable.url = 'wss://example.com/cable' + # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Use the lowest log level to ensure availability of diagnostic information + # when problems arise. + config.log_level = :debug + + # Prepend all log lines with the following tags. + config.log_tags = [:request_id] + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Use a real queuing backend for Active Job (and separate queues per environment). + # config.active_job.queue_adapter = :resque + # config.active_job.queue_name_prefix = "dummy_production" + + config.action_mailer.perform_caching = false + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners. + config.active_support.deprecation = :notify + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Use a different logger for distributed setups. + # require 'syslog/logger' + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + + if ENV["RAILS_LOG_TO_STDOUT"].present? + logger = ActiveSupport::Logger.new($stdout) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + end + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false + + # Inserts middleware to perform automatic connection switching. + # The `database_selector` hash is used to pass options to the DatabaseSelector + # middleware. The `delay` is used to determine how long to wait after a write + # to send a subsequent read to the primary. + # + # The `database_resolver` class is used by the middleware to determine which + # database is appropriate to use based on the time delay. + # + # The `database_resolver_context` class is used by the middleware to set + # timestamps for the last write to the primary. The resolver uses the context + # class timestamps to determine how long to wait before reading from the + # replica. + # + # By default Rails will store a last write timestamp in the session. The + # DatabaseSelector middleware is designed as such you can define your own + # strategy for connection switching and pass that into the middleware through + # these configuration options. + # config.active_record.database_selector = { delay: 2.seconds } + # config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver + # config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session +end diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb new file mode 100644 index 0000000..09ece9f --- /dev/null +++ b/spec/dummy/config/environments/test.rb @@ -0,0 +1,49 @@ +# The test environment is used exclusively to run your application's +# test suite. You never need to work with it otherwise. Remember that +# your test database is "scratch space" for the test suite and is wiped +# and recreated between test runs. Don't rely on the data there! + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + config.cache_classes = false + config.action_view.cache_template_loading = true + + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + config.cache_store = :null_store + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory. + config.active_storage.service = :test + + config.action_mailer.perform_caching = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raises error for missing translations. + # config.action_view.raise_on_missing_translations = true +end diff --git a/spec/dummy/config/initializers/application_controller_renderer.rb b/spec/dummy/config/initializers/application_controller_renderer.rb new file mode 100644 index 0000000..89d2efa --- /dev/null +++ b/spec/dummy/config/initializers/application_controller_renderer.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# ActiveSupport::Reloader.to_prepare do +# ApplicationController.renderer.defaults.merge!( +# http_host: 'example.org', +# https: false +# ) +# end diff --git a/spec/dummy/config/initializers/assets.rb b/spec/dummy/config/initializers/assets.rb new file mode 100644 index 0000000..2eeef96 --- /dev/null +++ b/spec/dummy/config/initializers/assets.rb @@ -0,0 +1,12 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = "1.0" + +# Add additional assets to the asset load path. +# Rails.application.config.assets.paths << Emoji.images_path + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in the app/assets +# folder are already added. +# Rails.application.config.assets.precompile += %w( admin.js admin.css ) diff --git a/spec/dummy/config/initializers/backtrace_silencers.rb b/spec/dummy/config/initializers/backtrace_silencers.rb new file mode 100644 index 0000000..59385cd --- /dev/null +++ b/spec/dummy/config/initializers/backtrace_silencers.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. +# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } + +# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. +# Rails.backtrace_cleaner.remove_silencers! diff --git a/spec/dummy/config/initializers/content_security_policy.rb b/spec/dummy/config/initializers/content_security_policy.rb new file mode 100644 index 0000000..41c4301 --- /dev/null +++ b/spec/dummy/config/initializers/content_security_policy.rb @@ -0,0 +1,28 @@ +# Be sure to restart your server when you modify this file. + +# Define an application-wide content security policy +# For further information see the following documentation +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy + +# Rails.application.config.content_security_policy do |policy| +# policy.default_src :self, :https +# policy.font_src :self, :https, :data +# policy.img_src :self, :https, :data +# policy.object_src :none +# policy.script_src :self, :https +# policy.style_src :self, :https + +# # Specify URI for violation reports +# # policy.report_uri "/csp-violation-report-endpoint" +# end + +# If you are using UJS then enable automatic nonce generation +# Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) } + +# Set the nonce only to specific directives +# Rails.application.config.content_security_policy_nonce_directives = %w(script-src) + +# Report CSP violations to a specified URI +# For further information see the following documentation: +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only +# Rails.application.config.content_security_policy_report_only = true diff --git a/spec/dummy/config/initializers/cookies_serializer.rb b/spec/dummy/config/initializers/cookies_serializer.rb new file mode 100644 index 0000000..5a6a32d --- /dev/null +++ b/spec/dummy/config/initializers/cookies_serializer.rb @@ -0,0 +1,5 @@ +# Be sure to restart your server when you modify this file. + +# Specify a serializer for the signed and encrypted cookie jars. +# Valid options are :json, :marshal, and :hybrid. +Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/spec/dummy/config/initializers/filter_parameter_logging.rb b/spec/dummy/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000..4a994e1 --- /dev/null +++ b/spec/dummy/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Configure sensitive parameters which will be filtered from the log file. +Rails.application.config.filter_parameters += [:password] diff --git a/spec/dummy/config/initializers/inflections.rb b/spec/dummy/config/initializers/inflections.rb new file mode 100644 index 0000000..ac033bf --- /dev/null +++ b/spec/dummy/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, '\1en' +# inflect.singular /^(ox)en/i, '\1' +# inflect.irregular 'person', 'people' +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym 'RESTful' +# end diff --git a/spec/dummy/config/initializers/mime_types.rb b/spec/dummy/config/initializers/mime_types.rb new file mode 100644 index 0000000..dc18996 --- /dev/null +++ b/spec/dummy/config/initializers/mime_types.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Add new mime types for use in respond_to blocks: +# Mime::Type.register "text/richtext", :rtf diff --git a/spec/dummy/config/initializers/wrap_parameters.rb b/spec/dummy/config/initializers/wrap_parameters.rb new file mode 100644 index 0000000..bbfc396 --- /dev/null +++ b/spec/dummy/config/initializers/wrap_parameters.rb @@ -0,0 +1,14 @@ +# Be sure to restart your server when you modify this file. + +# This file contains settings for ActionController::ParamsWrapper which +# is enabled by default. + +# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. +ActiveSupport.on_load(:action_controller) do + wrap_parameters format: [:json] +end + +# To enable root element in JSON for ActiveRecord objects. +# ActiveSupport.on_load(:active_record) do +# self.include_root_in_json = true +# end diff --git a/spec/dummy/config/locales/en.yml b/spec/dummy/config/locales/en.yml new file mode 100644 index 0000000..c3deb6d --- /dev/null +++ b/spec/dummy/config/locales/en.yml @@ -0,0 +1,40 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t 'hello' +# +# In views, this is aliased to just `t`: +# +# <%= t('hello') %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# The following keys must be escaped otherwise they will not be retrieved by +# the default I18n backend: +# +# true, false, on, off, yes, no +# +# Instead, surround them with single quotes. +# +# en: +# 'true': 'foo' +# +# To learn more, please read the Rails Internationalization guide +# available at https://guides.rubyonrails.org/i18n.html. + +en: + hello: "Hello world" + notifications: + noticed/i18n_example: + message: "This is a notification" + noticed: + scoped_i18n_example: + message: "This is a custom scoped trnaslation" + diff --git a/spec/dummy/config/puma.rb b/spec/dummy/config/puma.rb new file mode 100644 index 0000000..0064140 --- /dev/null +++ b/spec/dummy/config/puma.rb @@ -0,0 +1,38 @@ +# Puma can serve each request in a thread from an internal thread pool. +# The `threads` method setting takes two numbers: a minimum and maximum. +# Any libraries that use thread pools should be configured to match +# the maximum value specified for Puma. Default is set to 5 threads for minimum +# and maximum; this matches the default thread size of Active Record. +# +max_threads_count = ENV.fetch("RAILS_MAX_THREADS", 5) +min_threads_count = ENV.fetch("RAILS_MIN_THREADS", max_threads_count) +threads min_threads_count, max_threads_count + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +# +port ENV.fetch("PORT", 3000) + +# Specifies the `environment` that Puma will run in. +# +environment ENV.fetch("RAILS_ENV", "development") + +# Specifies the `pidfile` that Puma will use. +pidfile ENV.fetch("PIDFILE", "tmp/pids/server.pid") + +# Specifies the number of `workers` to boot in clustered mode. +# Workers are forked web server processes. If using threads and workers together +# the concurrency of the application would be max `threads` * `workers`. +# Workers do not work on JRuby or Windows (both of which do not support +# processes). +# +# workers ENV.fetch("WEB_CONCURRENCY") { 2 } + +# Use the `preload_app!` method when specifying a `workers` number. +# This directive tells Puma to first boot the application and load code +# before forking the application. This takes advantage of Copy On Write +# process behavior so workers use less memory. +# +# preload_app! + +# Allow puma to be restarted by `rails restart` command. +plugin :tmp_restart diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb new file mode 100644 index 0000000..130fd76 --- /dev/null +++ b/spec/dummy/config/routes.rb @@ -0,0 +1,4 @@ +Rails.application.routes.draw do + # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + root to: "main#index" +end diff --git a/spec/dummy/config/spring.rb b/spec/dummy/config/spring.rb new file mode 100644 index 0000000..db5bf13 --- /dev/null +++ b/spec/dummy/config/spring.rb @@ -0,0 +1,6 @@ +Spring.watch( + ".ruby-version", + ".rbenv-vars", + "tmp/restart.txt", + "tmp/caching-dev.txt" +) diff --git a/spec/dummy/config/storage.yml b/spec/dummy/config/storage.yml new file mode 100644 index 0000000..d32f76e --- /dev/null +++ b/spec/dummy/config/storage.yml @@ -0,0 +1,34 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) +# amazon: +# service: S3 +# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> +# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> +# region: us-east-1 +# bucket: your_own_bucket + +# Remember not to checkin your GCS keyfile to a repository +# google: +# service: GCS +# project: your_project +# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> +# bucket: your_own_bucket + +# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) +# microsoft: +# service: AzureStorage +# storage_account_name: your_account_name +# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> +# container: your_container_name + +# mirror: +# service: Mirror +# primary: local +# mirrors: [ amazon, google, microsoft ] diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb new file mode 100644 index 0000000..ec13a95 --- /dev/null +++ b/spec/dummy/db/schema.rb @@ -0,0 +1,84 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 1) do + create_table :accounts, force: true do |t| + t.column :name, :string + t.column :subdomain, :string + t.column :domain, :string + t.column :projects_count, :integer, default: 0 + end + + create_table :projects, force: true do |t| + t.column :name, :string + t.column :account_id, :integer + end + + create_table :managers, force: true do |t| + t.column :name, :string + t.column :project_id, :integer + t.column :account_id, :integer + end + + create_table :tasks, force: true do |t| + t.column :name, :string + t.column :account_id, :integer + t.column :project_id, :integer + t.column :completed, :boolean + end + + create_table :countries, force: true do |t| + t.column :name, :string + end + + create_table :unscoped_models, force: true do |t| + t.column :name, :string + end + + create_table :aliased_tasks, force: true do |t| + t.column :name, :string + t.column :project_alias_id, :integer + t.column :account_id, :integer + end + + create_table :unique_tasks, force: true do |t| + t.column :name, :string + t.column :user_defined_scope, :string + t.column :project_id, :integer + t.column :account_id, :integer + end + + create_table :custom_foreign_key_tasks, force: true do |t| + t.column :name, :string + t.column :accountID, :integer + end + + create_table :custom_primary_key_tasks, force: true do |t| + t.column :name, :string + end + + create_table :articles, force: true do |t| + t.column :title, :string + end + + create_table :comments, force: true do |t| + t.column :commentable_id, :integer + t.column :commentable_type, :string + t.column :account_id, :integer + end + + create_table :polymorphic_tenant_comments, force: true do |t| + t.column :polymorphic_tenant_commentable_id, :integer + t.column :polymorphic_tenant_commentable_type, :string + t.column :account_id, :integer + end +end diff --git a/spec/dummy/lib/assets/.keep b/spec/dummy/lib/assets/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/log/.keep b/spec/dummy/log/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/public/404.html b/spec/dummy/public/404.html new file mode 100644 index 0000000..2be3af2 --- /dev/null +++ b/spec/dummy/public/404.html @@ -0,0 +1,67 @@ + + + + The page you were looking for doesn't exist (404) + + + + + + +
+
+

The page you were looking for doesn't exist.

+

You may have mistyped the address or the page may have moved.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/spec/dummy/public/422.html b/spec/dummy/public/422.html new file mode 100644 index 0000000..c08eac0 --- /dev/null +++ b/spec/dummy/public/422.html @@ -0,0 +1,67 @@ + + + + The change you wanted was rejected (422) + + + + + + +
+
+

The change you wanted was rejected.

+

Maybe you tried to change something you didn't have access to.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/spec/dummy/public/500.html b/spec/dummy/public/500.html new file mode 100644 index 0000000..78a030a --- /dev/null +++ b/spec/dummy/public/500.html @@ -0,0 +1,66 @@ + + + + We're sorry, but something went wrong (500) + + + + + + +
+
+

We're sorry, but something went wrong.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/spec/dummy/public/apple-touch-icon-precomposed.png b/spec/dummy/public/apple-touch-icon-precomposed.png new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/public/apple-touch-icon.png b/spec/dummy/public/apple-touch-icon.png new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/public/favicon.ico b/spec/dummy/public/favicon.ico new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/tmp/development_secret.txt b/spec/dummy/tmp/development_secret.txt new file mode 100644 index 0000000..74606c3 --- /dev/null +++ b/spec/dummy/tmp/development_secret.txt @@ -0,0 +1 @@ +f1e8981c98a3cd825ba410f2080160547945e8c380645e0dfaeba509fa55291dcd4e317d6c2ba08bc94a4110b6c567987a66f616f67e3b5f62e11a4b216820e6 \ No newline at end of file diff --git a/spec/fixtures/accounts.yml b/spec/fixtures/accounts.yml new file mode 100644 index 0000000..d0b766f --- /dev/null +++ b/spec/fixtures/accounts.yml @@ -0,0 +1,5 @@ +foo: + name: foo + +bar: + name: bar diff --git a/spec/fixtures/custom_primary_key_tasks.yml b/spec/fixtures/custom_primary_key_tasks.yml new file mode 100644 index 0000000..28818e8 --- /dev/null +++ b/spec/fixtures/custom_primary_key_tasks.yml @@ -0,0 +1,2 @@ +without_account: + name: "bar" diff --git a/spec/fixtures/global_projects.yml b/spec/fixtures/global_projects.yml new file mode 100644 index 0000000..84904f1 --- /dev/null +++ b/spec/fixtures/global_projects.yml @@ -0,0 +1,13 @@ +global: + name: "global" + +global2: + name: "global 2" + +global_foo: + account: foo + name: "global foo" + +global_bar: + account: bar + name: "global bar" diff --git a/spec/fixtures/projects.yml b/spec/fixtures/projects.yml new file mode 100644 index 0000000..3c82c05 --- /dev/null +++ b/spec/fixtures/projects.yml @@ -0,0 +1,10 @@ +without_account: + name: without_account + +foo: + account: foo + name: foo + +bar: + account: bar + name: bar diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4aed89b..589be42 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,24 +1,18 @@ -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) -$LOAD_PATH.unshift(File.dirname(__FILE__)) +# Configure Rails Environment +ENV["RAILS_ENV"] = "test" -require "rails/all" - -# Setup a test app -module Rollcall - class Application < Rails::Application; end -end - -Rollcall::Application.config.secret_key_base = "1234567890123456789012345678901234567890" +require_relative "../spec/dummy/config/environment" +ActiveRecord::Migrator.migrations_paths = [File.expand_path("../spec/dummy/db/migrate", __dir__)] +ActiveRecord::Migration.maintain_test_schema! require "rspec/rails" -require "acts_as_tenant" -require "active_record_helper" -require "active_record_models" RSpec.configure do |config| config.after(:each) do ActsAsTenant.current_tenant = nil end + config.fixture_path = "spec/fixtures" + config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = true end