105 lines
4.5 KiB
Python
105 lines
4.5 KiB
Python
import pytest
|
|
from django.contrib.auth.models import User
|
|
from rest_framework.test import APIClient
|
|
from crm_core.models import Account, Contact, Lead, Opportunity
|
|
from custom_objects.models import CustomObjectDefinition, CustomRecord
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestTenantAccessIsolation:
|
|
|
|
def setup_method(self):
|
|
self.user_a = User.objects.create_user(username='tenant_a', password='password123')
|
|
self.user_b = User.objects.create_user(username='tenant_b', password='password123')
|
|
self.client_a = APIClient()
|
|
self.client_a.force_authenticate(user=self.user_a)
|
|
self.client_b = APIClient()
|
|
self.client_b.force_authenticate(user=self.user_b)
|
|
|
|
def test_tenant_cannot_read_other_tenant_account(self):
|
|
acct_a = Account.objects.create(name='Tenant A Account', owner=self.user_a)
|
|
|
|
# Tenant A can read
|
|
res_a = self.client_a.get(f'/api/accounts/{acct_a.id}/')
|
|
assert res_a.status_code == 200
|
|
|
|
# Tenant B gets 404
|
|
res_b = self.client_b.get(f'/api/accounts/{acct_a.id}/')
|
|
assert res_b.status_code == 404
|
|
|
|
def test_tenant_cannot_update_other_tenant_account(self):
|
|
acct_a = Account.objects.create(name='Tenant A Account', owner=self.user_a)
|
|
|
|
# Tenant B attempts write
|
|
res_b = self.client_b.patch(f'/api/accounts/{acct_a.id}/', {'name': 'Hacked Name'})
|
|
assert res_b.status_code == 404
|
|
acct_a.refresh_from_db()
|
|
assert acct_a.name == 'Tenant A Account'
|
|
|
|
def test_tenant_cannot_read_other_tenant_custom_records(self):
|
|
obj = CustomObjectDefinition.objects.create(name='Widget__c', label='Widget')
|
|
rec_a = CustomRecord.objects.create(custom_object=obj, owner=self.user_a, data={'title': 'Secret A'})
|
|
|
|
# Tenant A can list and retrieve
|
|
res_a = self.client_a.get(f'/api/custom/Widget__c/{rec_a.id}/')
|
|
assert res_a.status_code == 200
|
|
|
|
# Tenant B is blocked
|
|
res_b = self.client_b.get(f'/api/custom/Widget__c/{rec_a.id}/')
|
|
assert res_b.status_code == 404
|
|
|
|
def test_tenant_cannot_update_other_tenant_custom_records(self):
|
|
obj = CustomObjectDefinition.objects.create(name='Gadget__c', label='Gadget', created_by=self.user_a)
|
|
rec_a = CustomRecord.objects.create(custom_object=obj, owner=self.user_a, data={'title': 'Protected'})
|
|
|
|
res_b = self.client_b.patch(f'/api/custom/Gadget__c/{rec_a.id}/', {'data': {'title': 'Overwritten'}}, format='json')
|
|
assert res_b.status_code == 404
|
|
rec_a.refresh_from_db()
|
|
assert rec_a.data.get('title') == 'Protected'
|
|
|
|
def test_tenant_cannot_patch_other_tenant_custom_object_definition(self):
|
|
obj = CustomObjectDefinition.objects.create(name='Invoice__c', label='Invoice', created_by=self.user_a)
|
|
res_b = self.client_b.patch(f'/api/custom-objects/{obj.id}/', {'label': 'Hacked Invoice'})
|
|
assert res_b.status_code == 404
|
|
obj.refresh_from_db()
|
|
assert obj.label == 'Invoice'
|
|
|
|
def test_tenant_cannot_attach_foreign_relation(self):
|
|
acct_a = Account.objects.create(name='Tenant A Account', owner=self.user_a)
|
|
res_b = self.client_b.post('/api/contacts/', {
|
|
'first_name': 'Sneaky',
|
|
'last_name': 'Contact',
|
|
'account': acct_a.id
|
|
}, format='json')
|
|
assert res_b.status_code == 400
|
|
assert 'account' in res_b.data
|
|
|
|
def test_tenant_cannot_modify_foreign_record_via_flow_execution(self):
|
|
from workflows.models import FlowDefinition
|
|
acct_a = Account.objects.create(name='Untouchable Account', owner=self.user_a)
|
|
|
|
hack_graph = {
|
|
'nodes': [
|
|
{'id': 'start', 'type': 'trigger', 'data': {}},
|
|
{'id': 'update', 'type': 'update_record', 'data': {
|
|
'target_model': 'Account',
|
|
'record_id_source': 'context.record_id',
|
|
'field_updates': {'name': 'Changed by Bob'}
|
|
}}
|
|
],
|
|
'edges': [{'source': 'start', 'target': 'update'}]
|
|
}
|
|
flow_b = FlowDefinition.objects.create(
|
|
name='Bob Flow',
|
|
created_by=self.user_b,
|
|
trigger_type='manual',
|
|
graph=hack_graph,
|
|
is_active=True
|
|
)
|
|
|
|
res_b = self.client_b.post(f'/api/workflows/{flow_b.id}/execute/', {
|
|
'context': {'record_id': acct_a.id}
|
|
}, format='json')
|
|
assert res_b.status_code == 400
|
|
acct_a.refresh_from_db()
|
|
assert acct_a.name == 'Untouchable Account'
|