cirandas.net

ref: master

test/unit/multi_tenancy.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require_relative "../test_helper"
require 'noosfero/multi_tenancy'

class MultiTenancyTest < ActiveSupport::TestCase

  def test_get_mapping_if_is_set
    mapping = { :env => {} }
    Noosfero::MultiTenancy.instance_variable_set(:@mapping, mapping)
    assert_equal mapping, Noosfero::MultiTenancy.mapping
  end

  def test_set_mapping_if_is_not_set
    mapping = { :env => {} }
    Noosfero::MultiTenancy.expects(:load_map).returns(mapping)
    Noosfero::MultiTenancy.instance_variable_set(:@mapping, nil)
    assert_equal mapping, Noosfero::MultiTenancy.mapping
    assert_equal mapping, Noosfero::MultiTenancy.instance_variable_get(:@mapping)
  end

  def test_multitenancy_is_on_if_has_mapping
    Noosfero::MultiTenancy.expects(:mapping).returns({ :env => {} })
    assert Noosfero::MultiTenancy.on?
  end

  def test_multitenancy_is_on_if_it_is_a_hosted_environment
    Noosfero::MultiTenancy.expects(:mapping).returns({})
    Noosfero::MultiTenancy.expects(:is_hosted_environment?).returns(true)
    assert Noosfero::MultiTenancy.on?
  end

  def test_multitenancy_is_off_if_it_is_not_a_hosted_environment_and_there_is_no_mapping
    Noosfero::MultiTenancy.expects(:mapping).returns({})
    Noosfero::MultiTenancy.expects(:is_hosted_environment?).returns(false)
    refute Noosfero::MultiTenancy.on?
  end

  def test_set_schema_by_host
    Noosfero::MultiTenancy.expects(:mapping).returns({ 'host' => 'schema' })
    adapter = ApplicationRecord.connection.class
    adapter.any_instance.expects(:schema_search_path=).with('schema').returns(true)
    assert Noosfero::MultiTenancy.db_by_host = 'host'
  end

  def test_load_map
    YAML.expects(:load_file).returns(db_config)
    assert_equal({ 'test.one.com' => 'one', 'one.com' => 'one', 'two.com' => 'two' }, Noosfero::MultiTenancy.send(:load_map))
  end

  def test_if_is_hosted_environment
    YAML.expects(:load_file).returns(db_config)
    Rails.stubs(:env).returns('one_test')
    assert Noosfero::MultiTenancy.send(:is_hosted_environment?)
  end

  def test_if_is_not_hosted_environment
    YAML.expects(:load_file).returns(db_config)
    refute Noosfero::MultiTenancy.send(:is_hosted_environment?)
  end

  private

  def db_config
    {
      'one_test' => {
        'schema_search_path' => 'one',
        'domains' => ['test.one.com', 'one.com'],
        'adapter' => 'PostgreSQL'
      },
      'two_test' => {
        'schema_search_path' => 'two',
        'domains' => ['two.com'],
        'adapter' => 'PostgreSQL'
      },
      'test' => {
        'schema_search_path' => 'public',
        'domains' => ['test.com'],
        'adapter' => 'PostgreSQL'
      },
      'production' => {
        'schema_search_path' => 'production',
        'domains' => ['production.com'],
        'adapter' => 'PostgreSQL'
      }
    }
  end

end