Sha256: 6aa01c03b5e6f32a7e35aa15cb7779289e3c1e92ba1d0f5ac624a4c22b12371f

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

# Create multiple orgs
# Create an item in each
# Make sure you can only see one org's item in one org

describe Item do

  let!(:item1) { Item.create! }

  let!(:org2) { Organization.create! }
  let!(:item2) { org2.as_current { Item.create! } }
  let!(:item3) { org2.as_current { Item.create! } }

  specify 'default org should have one item' do
    expect(Item.all).to eq [ item1 ]
  end

  it 'does not return item2' do
    expect(Item.where(id: item2.id)).to eq []
  end

  specify 'org2 should have two items' do
    org2.as_current do
      expect(Item.all).to eq [ item2, item3]
    end
  end

  it 'does not return item1' do
    org2.as_current do
      expect(Item.where(id: item1.id)).to eq []
    end
  end

  class DependentClass
    include RailsMultitenant::GlobalContextRegistry::Current
    global_context_dependent_on Organization
  end

  class SubOrganization < Organization

  end

  describe '.as_current' do
    it 'returns the correct items with an org supplied' do
      Organization.as_current(org2) do
        expect(Item.all).to eq [ item2, item3]
      end
    end

    it 'allows a nil org to be supplied' do
      Organization.as_current(nil) do
        expect(Item.all).to eq []
      end
    end

    it 'rejects models of the wrong type' do
      model = Item.new
      expect { Organization.as_current(model) {}}.to raise_error("#{model} is not a Organization")
    end

    it 'invalidates dependent models' do
      DependentModel.current = DependentModel.create!
      dependent = DependentModel.current

      SubOrganization.create!.as_current do
        expect(DependentModel.current).not_to equal(dependent)
      end
    end

    it 'invalidates dependent objects' do
      dependent = DependentClass.current

      SubOrganization.create!.as_current do
        expect(DependentClass.current).not_to equal(dependent)
      end
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_multitenant-0.12.0 spec/item_spec.rb
rails_multitenant-0.11.0 spec/item_spec.rb
rails_multitenant-0.10.0 spec/item_spec.rb
rails_multitenant-0.9.0 spec/item_spec.rb
rails_multitenant-0.8.0 spec/item_spec.rb
rails_multitenant-0.7.2 spec/item_spec.rb