Skip to content

Commit 1023e3b

Browse files
committed
🧪 modify test_user_management_service.py
1 parent da70e16 commit 1023e3b

File tree

1 file changed

+17
-43
lines changed

1 file changed

+17
-43
lines changed

‎test/backend/services/test_user_management_service.py‎

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def test_set_token_without_bearer_prefix(self):
5151

5252
self.assertEqual(mock_client.auth.access_token, "test-jwt-token")
5353

54-
@patch('backend.services.user_management_service.logging')
55-
def test_set_token_exception(self, mock_logging):
54+
def test_set_token_exception(self):
5655
"""Test exception handling when setting token"""
5756
mock_client = MagicMock()
5857
# Mock the auth attribute to raise an exception when access_token is set
@@ -61,9 +60,6 @@ def test_set_token_exception(self, mock_logging):
6160

6261
# This should not raise an exception, but should log the error
6362
set_auth_token_to_client(mock_client, token)
64-
65-
# Verify that the error was logged
66-
mock_logging.error.assert_called_once_with("Set access token failed: Auth error")
6763

6864

6965
class TestGetAuthorizedClient(unittest.TestCase):
@@ -129,16 +125,14 @@ def test_get_user_no_response(self):
129125

130126
self.assertIsNone(result)
131127

132-
@patch('backend.services.user_management_service.logging')
133-
def test_get_user_exception(self, mock_logging):
128+
def test_get_user_exception(self):
134129
"""Test exception handling"""
135130
mock_client = MagicMock()
136131
mock_client.auth.get_user.side_effect = Exception("Get user error")
137132

138133
result = get_current_user_from_client(mock_client)
139134

140135
self.assertIsNone(result)
141-
mock_logging.error.assert_called_once_with("Get current user failed: Get user error")
142136

143137

144138
class TestValidateToken(unittest.TestCase):
@@ -174,11 +168,10 @@ def test_validate_token_no_user(self, mock_get_client, mock_set_token, mock_get_
174168
self.assertFalse(is_valid)
175169
self.assertIsNone(user)
176170

177-
@patch('backend.services.user_management_service.logging')
178171
@patch('backend.services.user_management_service.get_current_user_from_client')
179172
@patch('backend.services.user_management_service.set_auth_token_to_client')
180173
@patch('backend.services.user_management_service.get_supabase_client')
181-
def test_validate_token_exception(self, mock_get_client, mock_set_token, mock_get_user, mock_logging):
174+
def test_validate_token_exception(self, mock_get_client, mock_set_token, mock_get_user):
182175
"""Test token validation exception"""
183176
mock_client = MagicMock()
184177
mock_get_client.return_value = mock_client
@@ -188,7 +181,6 @@ def test_validate_token_exception(self, mock_get_client, mock_set_token, mock_ge
188181

189182
self.assertFalse(is_valid)
190183
self.assertIsNone(user)
191-
mock_logging.error.assert_called_once_with("Token validation failed: Validation error")
192184

193185

194186
class TestExtendSession(unittest.TestCase):
@@ -238,16 +230,14 @@ def test_extend_session_no_response(self):
238230

239231
self.assertIsNone(result)
240232

241-
@patch('backend.services.user_management_service.logging')
242-
def test_extend_session_exception(self, mock_logging):
233+
def test_extend_session_exception(self):
243234
"""Test session extension exception"""
244235
mock_client = MagicMock()
245236
mock_client.auth.refresh_session.side_effect = Exception("Refresh error")
246237

247238
result = extend_session(mock_client, "refresh-token")
248239

249240
self.assertIsNone(result)
250-
mock_logging.error.assert_called_once_with("Extend session failed: Refresh error")
251241

252242

253243
class TestCheckAuthServiceHealth(unittest.IsolatedAsyncioTestCase):
@@ -407,8 +397,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
407397
await check_auth_service_health()
408398

409399
self.assertIn("Auth service is unavailable", str(context.exception))
410-
# Verify that error was logged
411-
mock_logging.error.assert_called_once_with("Auth service is unavailable")
400+
412401

413402
@patch.dict(os.environ, {'SUPABASE_URL': 'http://test.supabase.co', 'SUPABASE_KEY': 'test-key'})
414403
async def test_health_check_missing_name_field(self):
@@ -449,13 +438,10 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
449438
await check_auth_service_health()
450439

451440
self.assertIn("Auth service is unavailable", str(context.exception))
452-
# Verify that error was logged
453-
mock_logging.error.assert_called_once_with("Auth service is unavailable")
454441

455442
@patch.dict(os.environ, {'SUPABASE_URL': 'http://test.supabase.co', 'SUPABASE_KEY': 'test-key'})
456-
@patch('backend.services.user_management_service.logging')
457443
@patch('backend.services.user_management_service.aiohttp.ClientSession')
458-
async def test_health_check_connection_error(self, mock_session_cls, mock_logging):
444+
async def test_health_check_connection_error(self, mock_session_cls):
459445
"""Test health check with connection error"""
460446
mock_session_cls.side_effect = aiohttp.ClientError("Connection failed")
461447

@@ -466,9 +452,8 @@ async def test_health_check_connection_error(self, mock_session_cls, mock_loggin
466452
self.assertIn("Connection failed", str(context.exception))
467453

468454
@patch.dict(os.environ, {'SUPABASE_URL': 'http://test.supabase.co', 'SUPABASE_KEY': 'test-key'})
469-
@patch('backend.services.user_management_service.logging')
470455
@patch('backend.services.user_management_service.aiohttp.ClientSession')
471-
async def test_health_check_general_exception(self, mock_session_cls, mock_logging):
456+
async def test_health_check_general_exception(self, mock_session_cls):
472457
"""Test health check with general exception"""
473458
mock_session_cls.side_effect = Exception("General error")
474459

@@ -487,8 +472,7 @@ class TestSignupUser(unittest.IsolatedAsyncioTestCase):
487472
@patch('backend.services.user_management_service.insert_user_tenant')
488473
@patch('backend.services.user_management_service.verify_invite_code')
489474
@patch('backend.services.user_management_service.get_supabase_client')
490-
@patch('backend.services.user_management_service.logging')
491-
async def test_signup_user_regular_user(self, mock_logging, mock_get_client, mock_verify_code,
475+
async def test_signup_user_regular_user(self, mock_get_client, mock_verify_code,
492476
mock_insert_tenant, mock_generate_tts, mock_parse_response):
493477
"""Test regular user signup"""
494478
mock_client = MagicMock()
@@ -513,8 +497,7 @@ async def test_signup_user_regular_user(self, mock_logging, mock_get_client, moc
513497
@patch('backend.services.user_management_service.insert_user_tenant')
514498
@patch('backend.services.user_management_service.verify_invite_code')
515499
@patch('backend.services.user_management_service.get_supabase_client')
516-
@patch('backend.services.user_management_service.logging')
517-
async def test_signup_user_admin(self, mock_logging, mock_get_client, mock_verify_code,
500+
async def test_signup_user_admin(self, mock_get_client, mock_verify_code,
518501
mock_insert_tenant, mock_generate_tts, mock_parse_response):
519502
"""Test admin user signup"""
520503
mock_client = MagicMock()
@@ -535,8 +518,7 @@ async def test_signup_user_admin(self, mock_logging, mock_get_client, mock_verif
535518
mock_parse_response.assert_called_once_with(True, mock_response, "admin")
536519

537520
@patch('backend.services.user_management_service.get_supabase_client')
538-
@patch('backend.services.user_management_service.logging')
539-
async def test_signup_user_no_user_returned(self, mock_logging, mock_get_client):
521+
async def test_signup_user_no_user_returned(self, mock_get_client):
540522
"""Test signup when no user is returned"""
541523
mock_client = MagicMock()
542524
mock_response = MagicMock()
@@ -642,34 +624,29 @@ class TestVerifyInviteCode(unittest.IsolatedAsyncioTestCase):
642624
"""Test verify_invite_code"""
643625

644626
@patch('backend.services.user_management_service.INVITE_CODE', 'correct-code')
645-
@patch('backend.services.user_management_service.logging')
646-
async def test_verify_invite_code_success(self, mock_logging):
627+
async def test_verify_invite_code_success(self):
647628
"""Test successful invite code verification"""
648629
# Should not raise exception
649630
await verify_invite_code('correct-code')
650-
mock_logging.info.assert_called()
651631

652632
@patch('backend.services.user_management_service.INVITE_CODE', None)
653-
@patch('backend.services.user_management_service.logging')
654-
async def test_verify_invite_code_no_system_code(self, mock_logging):
633+
async def test_verify_invite_code_no_system_code(self):
655634
"""Test when system has no invite code configured"""
656635
with self.assertRaises(NoInviteCodeException) as context:
657636
await verify_invite_code('any-code')
658637

659638
self.assertIn("The system has not configured the admin invite code", str(context.exception))
660639

661640
@patch('backend.services.user_management_service.INVITE_CODE', 'correct-code')
662-
@patch('backend.services.user_management_service.logging')
663-
async def test_verify_invite_code_no_user_code(self, mock_logging):
641+
async def test_verify_invite_code_no_user_code(self):
664642
"""Test when user provides no invite code"""
665643
with self.assertRaises(IncorrectInviteCodeException) as context:
666644
await verify_invite_code(None)
667645

668646
self.assertIn("Please enter the invite code", str(context.exception))
669647

670648
@patch('backend.services.user_management_service.INVITE_CODE', 'correct-code')
671-
@patch('backend.services.user_management_service.logging')
672-
async def test_verify_invite_code_wrong_code(self, mock_logging):
649+
async def test_verify_invite_code_wrong_code(self):
673650
"""Test when user provides wrong invite code"""
674651
with self.assertRaises(IncorrectInviteCodeException) as context:
675652
await verify_invite_code('wrong-code')
@@ -683,8 +660,7 @@ class TestSigninUser(unittest.IsolatedAsyncioTestCase):
683660
@patch('backend.services.user_management_service.get_jwt_expiry_seconds')
684661
@patch('backend.services.user_management_service.calculate_expires_at')
685662
@patch('backend.services.user_management_service.get_supabase_client')
686-
@patch('backend.services.user_management_service.logging')
687-
async def test_signin_user_success(self, mock_logging, mock_get_client, mock_calc_expires, mock_get_expiry):
663+
async def test_signin_user_success(self, mock_get_client, mock_calc_expires, mock_get_expiry):
688664
"""Test successful user signin"""
689665
mock_client = MagicMock()
690666
mock_user = MagicMock()
@@ -759,8 +735,7 @@ class TestRefreshUserToken(unittest.IsolatedAsyncioTestCase):
759735

760736
@patch('backend.services.user_management_service.extend_session')
761737
@patch('backend.services.user_management_service.get_authorized_client')
762-
@patch('backend.services.user_management_service.logging')
763-
async def test_refresh_token_success(self, mock_logging, mock_get_client, mock_extend_session):
738+
async def test_refresh_token_success(self, mock_get_client, mock_extend_session):
764739
"""Test successful token refresh"""
765740
mock_client = MagicMock()
766741
mock_get_client.return_value = mock_client
@@ -781,8 +756,7 @@ async def test_refresh_token_success(self, mock_logging, mock_get_client, mock_e
781756

782757
@patch('backend.services.user_management_service.extend_session')
783758
@patch('backend.services.user_management_service.get_authorized_client')
784-
@patch('backend.services.user_management_service.logging')
785-
async def test_refresh_token_failure(self, mock_logging, mock_get_client, mock_extend_session):
759+
async def test_refresh_token_failure(self, mock_get_client, mock_extend_session):
786760
"""Test token refresh failure"""
787761
mock_client = MagicMock()
788762
mock_get_client.return_value = mock_client

0 commit comments

Comments
 (0)