Skip to content

Commit

Permalink
test: update to cater for tenancy object in request
Browse files Browse the repository at this point in the history
ref: #473 #474
  • Loading branch information
jon-nfc committed Jan 18, 2025
1 parent 7f79c84 commit 3a982b6
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 107 deletions.
57 changes: 42 additions & 15 deletions app/access/tests/functional/team/test_team_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

from rest_framework.exceptions import ValidationError

from access.middleware.auth import Tenancy
from access.models import Organization, Permission

from access.serializers.teams import (
Team,
TeamModelSerializer
)

from settings.models.app_settings import AppSettings



class MockView:
Expand All @@ -21,12 +24,36 @@ class MockView:

kwargs: dict = {}

request = None


def __init__(self, user: User):

app_settings = AppSettings.objects.select_related('global_organization').get(
owner_organization = None
)

self.request = MockRequest( user = user, app_settings = app_settings)



class MockRequest:

tenancy: Tenancy = None

user = None

def __init__(self, user: User, app_settings):

self.user = user

self.app_settings = app_settings

self.tenancy = Tenancy(
user = user,
app_settings = app_settings
)



class TeamValidationAPI(
Expand Down Expand Up @@ -82,21 +109,21 @@ def test_serializer_valid_data(self):
"""


mock_view = MockView()
mock_view = MockView( user = self.user )
mock_view.action = 'create'
mock_view.kwargs: dict = {
'organization_id': self.organization.id
}

mock_request = MockRequest()
mock_request.user = self.user
# mock_request = MockRequest()
# mock_request.user = self.user

mock_view.request = mock_request
# mock_view.request = mock_request


serializer = TeamModelSerializer(
context = {
'request': mock_request,
'request': mock_view.request,
'view': mock_view,
},
data = self.valid_data
Expand All @@ -113,16 +140,16 @@ def test_serializer_validation_no_name(self):
"""


mock_view = MockView()
mock_view = MockView( user = self.user)
mock_view.action = 'create'
mock_view.kwargs: dict = {
'organization_id': self.organization.id
}

mock_request = MockRequest()
mock_request.user = self.user
# mock_request = MockRequest()
# mock_request.user = self.user

mock_view.request = mock_request
# mock_view.request = mock_request


data = self.valid_data.copy()
Expand All @@ -133,7 +160,7 @@ def test_serializer_validation_no_name(self):

serializer = TeamModelSerializer(
context = {
'request': mock_request,
'request': mock_view.request,
'view': mock_view,
},
data = data
Expand All @@ -153,16 +180,16 @@ def test_serializer_validation_permissions_optional(self):
"""


mock_view = MockView()
mock_view = MockView( user = self.user)
mock_view.action = 'create'
mock_view.kwargs: dict = {
'organization_id': self.organization.id
}

mock_request = MockRequest()
mock_request.user = self.user
# mock_request = MockRequest()
# mock_request.user = self.user

mock_view.request = mock_request
# mock_view.request = mock_request


data = self.valid_data.copy()
Expand All @@ -171,7 +198,7 @@ def test_serializer_validation_permissions_optional(self):

serializer = TeamModelSerializer(
context = {
'request': mock_request,
'request': mock_view.request,
'view': mock_view,
},
data = data
Expand Down
2 changes: 1 addition & 1 deletion app/core/serializers/ticket_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def is_valid(self, *, raise_exception=False):

if self._context['view'].action == 'create':

self.validated_data['user'] = self.request._user
self.validated_data['user'] = self.request.user

if 'ticket_id' in self._kwargs['context']['view'].kwargs:

Expand Down
Loading

0 comments on commit 3a982b6

Please sign in to comment.