-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from djpugh/fix/user-klass-reload
- Loading branch information
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import List | ||
import unittest | ||
import uuid | ||
|
||
from fastapi_aad_auth._base.state import AuthenticationState, User, AuthenticationOptions | ||
from fastapi_aad_auth._base.validators import SessionValidator | ||
|
||
|
||
class User2(User): | ||
b: int = 2 | ||
|
||
|
||
class User3(User2): | ||
|
||
@property | ||
def permissions(self): | ||
return [self.name, 'a'] | ||
|
||
|
||
class AuthenticationStateTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.serializer = SessionValidator.get_session_serializer(str(uuid.uuid4()), str(uuid.uuid4())) | ||
|
||
def test_create(self): | ||
user = User(name='Joe Bloggs', email='[email protected]', username='[email protected]') | ||
state = AuthenticationState(user=user, state=AuthenticationOptions.authenticated) | ||
self.assertIsInstance(state.user, User) | ||
self.assertEqual(state.user.name, user.name) | ||
|
||
def test_create_custom_user(self): | ||
user = User2(name='Joe Bloggs', email='[email protected]', username='[email protected]') | ||
state = AuthenticationState(user=user, state=AuthenticationOptions.authenticated) | ||
self.assertIsInstance(state.user, User2) | ||
self.assertEqual(state.user.name, user.name) | ||
self.assertEqual(state.user.b, user.b) | ||
self.assertEqual(state.user.b, 2) | ||
|
||
def test_load(self): | ||
user = User(name='Joe Bloggs', email='[email protected]', username='[email protected]') | ||
state = AuthenticationState(user=user, state=AuthenticationOptions.authenticated) | ||
loaded_state = AuthenticationState.load(self.serializer, state.store(self.serializer)) | ||
self.assertIsInstance(state.user, User) | ||
self.assertEqual(state.user.name, user.name) | ||
|
||
def test_load_custom_user(self): | ||
user = User2(name='Joe Bloggs', email='[email protected]', username='[email protected]') | ||
state = AuthenticationState(user=user, state=AuthenticationOptions.authenticated) | ||
loaded_state = AuthenticationState.load(self.serializer, state.store(self.serializer)) | ||
self.assertIsInstance(state.user, User2) | ||
self.assertEqual(state.user.name, user.name) | ||
self.assertEqual(state.user.b, user.b) | ||
self.assertEqual(state.user.b, 2) | ||
|
||
def test_load_custom_user_permissions(self): | ||
user = User3(name='Joe Bloggs', email='[email protected]', username='[email protected]', b=4) | ||
state = AuthenticationState(user=user, state=AuthenticationOptions.authenticated) | ||
loaded_state = AuthenticationState.load(self.serializer, state.store(self.serializer)) | ||
self.assertIsInstance(state.user, User3) | ||
self.assertEqual(state.user.name, user.name) | ||
self.assertEqual(state.user.b, user.b) | ||
self.assertEqual(state.user.b, 4) | ||
self.assertEqual(state.user.permissions, ['Joe Bloggs', 'a']) | ||
|
||
|
||
|