Exemplo n.º 1
0
 def test_tenant_set_while_saving(self):
     # Tenant aware objects setup
     set_current_tenant(self.tenant1)
     obj1 = TestTenantAwareModel(name='obj1')
     obj1.clean()    # This is where the tenant gets set automatically
     obj1.save()
     self.assertEqual(obj1.tenant, get_current_tenant())
Exemplo n.º 2
0
    def test_tenant_get_object_or_404(self):
        set_current_tenant(self.tenant1)
        obj1 = TestTenantAwareModel.objects.create(name='obj1', tenant=self.tenant1)
        obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)

        self.assertEqual(tenant_get_object_or_404(TestTenantAwareModel, id=obj1.id), obj1)
        self.failUnlessRaises(Http404, lambda : tenant_get_object_or_404(TestTenantAwareModel, id=obj2.id))
Exemplo n.º 3
0
    def test_tenant_model_form(self):
        set_current_tenant(self.tenant1)
        obj1 = TestTenantAwareModel.objects.create(name='obj1',
                                                   tenant=self.tenant1)
        obj2 = TestTenantAwareModel.objects.create(name='obj2',
                                                   tenant=self.tenant2)

        class TestForm(TenantModelForm):
            class Meta:
                model = TestTenantAwareModel
                exclude = ['tenant']

        form = TestForm({'name': 'blah', 'm2mfield': [obj1.id]})
        fk_options = form.fields['fkfield'].queryset
        self.assertEqual(
            len(fk_options), 1,
            'Wrong number of options in form queryset for foreign key field.')
        self.assertEqual(fk_options[0].name, 'obj1',
                         'Wrong object in foreign key queryset.')

        m2m_options = form.fields['m2mfield'].queryset
        self.assertEqual(
            len(m2m_options), 1,
            'Wrong number of options in form queryset for many-to-many field.')
        self.assertEqual(m2m_options[0].name, 'obj1',
                         'Wrong object in many-to-many queryset.')

        self.assertEqual(form.is_valid(), True, 'Form is not valid')
        instance = form.save()
        self.assertEqual(instance.tenant, self.tenant1)
Exemplo n.º 4
0
    def test_tenant_get_object_or_404(self):
        set_current_tenant(self.tenant1)
        obj1 = TestTenantAwareModel.objects.create(name='obj1', tenant=self.tenant1)
        obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)

        self.assertEqual(tenant_get_object_or_404(TestTenantAwareModel, id=obj1.id), obj1)
        self.failUnlessRaises(Http404, lambda : tenant_get_object_or_404(TestTenantAwareModel, id=obj2.id))
Exemplo n.º 5
0
 def test_tenant_set_while_saving(self):
     # Tenant aware objects setup
     set_current_tenant(self.tenant1)
     obj1 = TestTenantAwareModel(name='obj1')
     obj1.clean()  # This is where the tenant gets set automatically
     obj1.save()
     self.assertEqual(obj1.tenant, get_current_tenant())
Exemplo n.º 6
0
 def test_current_tenant_owns_object(self):
     set_current_tenant(self.tenant1)
     obj1 = TestTenantAwareModel.objects.create(name='obj1',
                                                tenant=self.tenant1)
     obj2 = TestTenantAwareModel.objects.create(name='obj2',
                                                tenant=self.tenant2)
     self.assertTrue(current_tenant_owns_object(obj1))
     self.assertFalse(current_tenant_owns_object(obj2))
Exemplo n.º 7
0
    def test_tenant_filter(self):
        set_current_tenant(self.tenant1)
        obj1 = TestTenantAwareModel.objects.create(name='obj1', tenant=self.tenant1)
        obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)

        objects = TestTenantAwareModel.objects.all()
        self.assertEqual( len(objects), 2, 'Incorrect number of objects in queryset.' )
        objects = tenant_filter(objects)
        self.assertEqual( len(objects), 1, 'Incorrect number of objects in tenant-filtered queryset.' )
        self.assertEqual( objects[0].name, 'obj1', 'Wrong object shows up in tenant-filtered queryset')
Exemplo n.º 8
0
    def test_tenant_filter(self):
        set_current_tenant(self.tenant1)
        obj1 = TestTenantAwareModel.objects.create(name='obj1', tenant=self.tenant1)
        obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)

        objects = TestTenantAwareModel.objects.all()
        self.assertEqual( len(objects), 2, 'Incorrect number of objects in queryset.' )
        objects = tenant_filter(objects)
        self.assertEqual( len(objects), 1, 'Incorrect number of objects in tenant-filtered queryset.' )
        self.assertEqual( objects[0].name, 'obj1', 'Wrong object shows up in tenant-filtered queryset')
Exemplo n.º 9
0
 def test_custom_manager(self):
     # Verify that the custom manager filters the queryset as per the currently logged in user
     set_current_tenant(self.tenant1)
     obj1 = TestTenantAwareModel(name='obj1')
     obj1.clean()    # This is where the tenant gets set automatically
     obj1.save()
     obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)
     tenant_objects = TestTenantAwareModel.tenant_objects.all()
     self.assertEqual(len(tenant_objects), 1,  "While using custom manager: Expected just one object in tenant-filtered queryset.")
     self.assertEqual(tenant_objects[0], obj1,  "While using custom manager: Object in tenant-filtered queryset is not the right one." )
Exemplo n.º 10
0
 def test_custom_manager(self):
     # Verify that the custom manager filters the queryset as per the currently logged in user
     set_current_tenant(self.tenant1)
     obj1 = TestTenantAwareModel(name='obj1')
     obj1.clean()    # This is where the tenant gets set automatically
     obj1.save()
     obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)
     tenant_objects = TestTenantAwareModel.tenant_objects.all()
     self.assertEqual(len(tenant_objects), 1,  "While using custom manager: Expected just one object in tenant-filtered queryset.")
     self.assertEqual(tenant_objects[0], obj1,  "While using custom manager: Object in tenant-filtered queryset is not the right one." )
Exemplo n.º 11
0
    def test_tenant_model_form(self):
        set_current_tenant(self.tenant1)
        obj1 = TestTenantAwareModel.objects.create(name='obj1', tenant=self.tenant1)
        obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)

        class TestForm(TenantModelForm):
            class Meta:
                model = TestTenantAwareModel
                exclude = ['tenant']


        form = TestForm({ 'name': 'blah', 'm2mfield': [ obj1.id ] })
        fk_options = form.fields['fkfield'].queryset
        self.assertEqual(len(fk_options), 1, 'Wrong number of options in form queryset for foreign key field.')
        self.assertEqual(fk_options[0].name, 'obj1', 'Wrong object in foreign key queryset.')

        m2m_options = form.fields['m2mfield'].queryset
        self.assertEqual(len(m2m_options), 1, 'Wrong number of options in form queryset for many-to-many field.')
        self.assertEqual(m2m_options[0].name, 'obj1', 'Wrong object in many-to-many queryset.')

        self.assertEqual(form.is_valid(), True, 'Form is not valid')
        instance = form.save()
        self.assertEqual(instance.tenant, self.tenant1)
Exemplo n.º 12
0
 def test_current_tenant_owns_object(self):
     set_current_tenant(self.tenant1)
     obj1 = TestTenantAwareModel.objects.create(name='obj1', tenant=self.tenant1)
     obj2 = TestTenantAwareModel.objects.create(name='obj2', tenant=self.tenant2)
     self.assertTrue( current_tenant_owns_object(obj1) )
     self.assertFalse( current_tenant_owns_object(obj2) )