multi-tenant-support 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -0
- data/lib/multi_tenant_support/minitest.rb +7 -0
- data/lib/multi_tenant_support/rspec.rb +13 -0
- data/lib/multi_tenant_support/test/capybara.rb +57 -0
- data/lib/multi_tenant_support/test/integration.rb +29 -0
- data/lib/multi_tenant_support/test/system.rb +38 -0
- data/lib/multi_tenant_support/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dce423fece46127c6f8f4f83f53b738524be0c7c84173d4aff9b02f51c077cae
|
4
|
+
data.tar.gz: d8e4328c0e0338831065b54bd38317c761af13a1b21025e2d98b14bcb20ac136
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46a70af78f3fb46731e8e5a989d3816d999eee75e17f13db11e111a18b7923549904621a787511455e6301234117d06a77a3df82f31c041c5f2930a7e39d1396
|
7
|
+
data.tar.gz: 18b80a4ca7dbbb0e24976d0748699abde53c7dfc00b370bcb2d44222c8b4c1990d6ad728073da5f08ffd7512f2d9bb67d1a1c369b0b717ad94e14c12f6bca26a
|
data/README.md
CHANGED
@@ -237,6 +237,12 @@ MultiTenantSupport.under_tenant amazon do
|
|
237
237
|
end
|
238
238
|
```
|
239
239
|
|
240
|
+
### Set current tenant global
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
MultiTenantSupport::Current.tenant_account = account
|
244
|
+
```
|
245
|
+
|
240
246
|
### Disallow read across tenant by default
|
241
247
|
|
242
248
|
This gem disallow read across tenant by default. You can check current state through:
|
@@ -329,6 +335,44 @@ Console does not allow read across tenant by default. But you have several ways
|
|
329
335
|
$ irb(main):001:0> MultiTenantSupport.allow_read_across_tenant
|
330
336
|
```
|
331
337
|
|
338
|
+
## Testing
|
339
|
+
### Minitest (Rails default)
|
340
|
+
|
341
|
+
```ruby
|
342
|
+
# test/test_helper.rb
|
343
|
+
require 'multi_tenant_support/minitet'
|
344
|
+
```
|
345
|
+
### RSpec (with Capybara)
|
346
|
+
|
347
|
+
```ruby
|
348
|
+
# spec/rails_helper.rb or spec/spec_helper.rb
|
349
|
+
require 'multi_tenant_support/rspec'
|
350
|
+
```
|
351
|
+
|
352
|
+
Above code will make sure the `MultiTenantSupport.current_tenant` won't accidentally be reset during integration and system tests. For example:
|
353
|
+
|
354
|
+
With above testing requre code
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
# Integration test
|
358
|
+
test "a integration test" do
|
359
|
+
host! "apple.example.com"
|
360
|
+
|
361
|
+
assert_no_changes "MultiTenantSupport.current_tenant" do
|
362
|
+
get users_path
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
# System test
|
367
|
+
test "a system test" do
|
368
|
+
Capybara.app_host = "http://apple.example.com"
|
369
|
+
|
370
|
+
assert_no_changes "MultiTenantSupport.current_tenant" do
|
371
|
+
visit users_path
|
372
|
+
end
|
373
|
+
end
|
374
|
+
```
|
375
|
+
|
332
376
|
## Code Example
|
333
377
|
|
334
378
|
### Database Schema
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require_relative "./test/integration"
|
2
|
+
require_relative "./test/system"
|
3
|
+
require_relative "./test/capybara"
|
4
|
+
|
5
|
+
ActionDispatch::IntegrationTest.prepend(MultiTenantSupport::Test::Integration)
|
6
|
+
ActionDispatch::SystemTestCase.prepend(MultiTenantSupport::Test::System)
|
7
|
+
Capybara::Node::Element.prepend(MultiTenantSupport::Test::Capybara)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative "./test/integration"
|
2
|
+
require_relative "./test/system"
|
3
|
+
require_relative "./test/capybara"
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.include MultiTenantSupport::Test::Integration, type: :request
|
7
|
+
config.include MultiTenantSupport::Test::Integration, type: :controller
|
8
|
+
|
9
|
+
config.include MultiTenantSupport::Test::System, type: :system
|
10
|
+
config.include MultiTenantSupport::Test::System, type: :feature
|
11
|
+
end
|
12
|
+
|
13
|
+
Capybara::Node::Element.prepend(MultiTenantSupport::Test::Capybara)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module MultiTenantSupport
|
2
|
+
module Test
|
3
|
+
module Capybara
|
4
|
+
|
5
|
+
def set(value, **options)
|
6
|
+
keep_context_tenant_unchange do
|
7
|
+
super(value, **options)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def select_option(wait: nil)
|
12
|
+
keep_context_tenant_unchange do
|
13
|
+
super(wait: wait)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def unselect_option(wait: nil)
|
18
|
+
keep_context_tenant_unchange do
|
19
|
+
super(wait: wait)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def perform_click_action(keys, wait: nil, **options)
|
24
|
+
keep_context_tenant_unchange do
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def trigger(event)
|
30
|
+
keep_context_tenant_unchange do
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def evaluate_script(script, *args)
|
36
|
+
keep_context_tenant_unchange do
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def evaluate_async_script(script, *args)
|
42
|
+
keep_context_tenant_unchange do
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def keep_context_tenant_unchange
|
48
|
+
_current_tenant = MultiTenantSupport::Current.tenant_account
|
49
|
+
MultiTenantSupport::Current.tenant_account = nil # Simulate real circumstance
|
50
|
+
yield
|
51
|
+
ensure
|
52
|
+
MultiTenantSupport::Current.tenant_account = _current_tenant
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MultiTenantSupport
|
2
|
+
module Test
|
3
|
+
module Integration
|
4
|
+
|
5
|
+
%i[get post patch put delete head options].each do |method|
|
6
|
+
define_method method do |path, **args|
|
7
|
+
keep_context_tenant_unchange do
|
8
|
+
super(path, **args)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def follow_redirect(**args)
|
14
|
+
keep_context_tenant_unchange do
|
15
|
+
super(**args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def keep_context_tenant_unchange
|
20
|
+
_current_tenant = MultiTenantSupport::Current.tenant_account
|
21
|
+
MultiTenantSupport::Current.tenant_account = nil # Simulate real circumstance
|
22
|
+
yield
|
23
|
+
ensure
|
24
|
+
MultiTenantSupport::Current.tenant_account = _current_tenant
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module MultiTenantSupport
|
2
|
+
module Test
|
3
|
+
module System
|
4
|
+
|
5
|
+
%i[
|
6
|
+
visit refresh click_on go_back go_forward
|
7
|
+
check choose click_button click_link
|
8
|
+
fill_in uncheck check unselect select
|
9
|
+
execute_script evaluate_script
|
10
|
+
].each do |method|
|
11
|
+
if RUBY_VERSION >= '2.7'
|
12
|
+
class_eval <<~METHOD, __FILE__, __LINE__ + 1
|
13
|
+
def #{method}(...)
|
14
|
+
keep_context_tenant_unchange do
|
15
|
+
super(...)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
METHOD
|
19
|
+
else
|
20
|
+
define_method method do |*args, &block|
|
21
|
+
keep_context_tenant_unchange do
|
22
|
+
super(*args, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def keep_context_tenant_unchange
|
29
|
+
_current_tenant = MultiTenantSupport::Current.tenant_account
|
30
|
+
MultiTenantSupport::Current.tenant_account = nil # Simulate real circumstance
|
31
|
+
yield
|
32
|
+
ensure
|
33
|
+
MultiTenantSupport::Current.tenant_account = _current_tenant
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi-tenant-support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hopper Gee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -51,8 +51,13 @@ files:
|
|
51
51
|
- lib/multi_tenant_support/current.rb
|
52
52
|
- lib/multi_tenant_support/errors.rb
|
53
53
|
- lib/multi_tenant_support/find_tenant_account.rb
|
54
|
+
- lib/multi_tenant_support/minitest.rb
|
54
55
|
- lib/multi_tenant_support/railtie.rb
|
56
|
+
- lib/multi_tenant_support/rspec.rb
|
55
57
|
- lib/multi_tenant_support/sidekiq.rb
|
58
|
+
- lib/multi_tenant_support/test/capybara.rb
|
59
|
+
- lib/multi_tenant_support/test/integration.rb
|
60
|
+
- lib/multi_tenant_support/test/system.rb
|
56
61
|
- lib/multi_tenant_support/version.rb
|
57
62
|
- lib/tasks/multi_tenant_support_tasks.rake
|
58
63
|
homepage: https://github.com/hoppergee/multi-tenant-support
|