Skip to content

Commit

Permalink
fix: Create default tenant for newly created social users (#572)
Browse files Browse the repository at this point in the history
* Create default tenant for newly created social users

* Add social pipeline tests
  • Loading branch information
wojcikmat authored May 22, 2024
1 parent 774650c commit 0365d9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/backend/apps/multitenancy/pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .models import Tenant


def create_default_tenant(user=None, is_new=False, *args, **kwargs):
if user and is_new:
Tenant.objects.get_or_create_user_default_tenant(user)
25 changes: 25 additions & 0 deletions packages/backend/apps/multitenancy/tests/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from ..pipeline import create_default_tenant

from ..models import Tenant
from ..constants import TenantType

pytestmark = pytest.mark.django_db


class TestCreateTenantPipeline:
def test_create_default_tenant_user_and_is_new(self, user):
create_default_tenant(user=user, is_new=True)
tenant_exists = Tenant.objects.filter(creator=user, type=TenantType.DEFAULT).exists()
assert tenant_exists

def test_create_default_tenant_user_not_new(self, user):
count_before = Tenant.objects.filter(creator=user, type=TenantType.DEFAULT).count()
create_default_tenant(user=user, is_new=False)
count_after = Tenant.objects.filter(creator=user, type=TenantType.DEFAULT).count()
assert count_before == count_after

def test_create_default_tenant_no_user(self):
create_default_tenant(user=None, is_new=True)
tenant_exists = Tenant.objects.exists()
assert not tenant_exists
1 change: 1 addition & 0 deletions packages/backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'apps.multitenancy.pipeline.create_default_tenant',
'social_core.pipeline.user.user_details',
)
SOCIAL_AUTH_ALLOWED_REDIRECT_HOSTS = env.list('SOCIAL_AUTH_ALLOWED_REDIRECT_HOSTS', default=[])
Expand Down

0 comments on commit 0365d9d

Please sign in to comment.