Skip to content

Commit

Permalink
fix: Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kuipumu committed Jun 20, 2024
1 parent c0ca30e commit eb21b9d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 9 additions & 1 deletion openedx_lti_tool_plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ def name(self) -> str:
@property
def username(self) -> str:
"""str: Username."""
# Get username from user.
if getattr(self, 'user', None):
return self.user.username
# Generate username string without name.
if not self.given_name:
return f'{self.short_uuid}'

# Generate username string with name.
return (
f'{self.given_name.lower()[:30]}'
f'{self.family_name.lower()[:1]}'
Expand All @@ -129,6 +133,10 @@ def username(self) -> str:
@property
def email(self) -> str:
"""str: Email address."""
# Get email from user.
if getattr(self, 'user', None):
return self.user.email
# Generate email string.
return f'{self.uuid}@{app_config.name}'

def user_collision(self) -> bool:
Expand Down
22 changes: 18 additions & 4 deletions openedx_lti_tool_plugin/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def test_configure_user_without_user_with_user_collision(

self.lti_profile.configure_user()

getattr_mock.assert_called_once_with(self.lti_profile, 'user', None)
getattr_mock.assert_has_calls([
call(self.lti_profile, 'user', None),
call(self.lti_profile, 'user', None),
call(self.lti_profile, 'user', None),
])
user_collision_mock.assert_has_calls([call(), call()])
uuid4_mock.assert_called_once_with()
user_get_or_create_mock.assert_called_once_with(
Expand All @@ -114,7 +118,11 @@ def test_configure_user_without_user_witout_user_collision(

self.lti_profile.configure_user()

getattr_mock.assert_called_once_with(self.lti_profile, 'user', None)
getattr_mock.assert_has_calls([
call(self.lti_profile, 'user', None),
call(self.lti_profile, 'user', None),
call(self.lti_profile, 'user', None),
])
user_collision_mock.assert_called_once_with()
uuid4_mock.assert_not_called()
user_get_or_create_mock.assert_called_once_with(
Expand Down Expand Up @@ -219,25 +227,31 @@ def test_name_property(self, name_data: dict, name_return: str):

@ddt.data(
(
False,
{
'given_name': GIVEN_NAME_LARGER,
},
f'{GIVEN_NAME_LARGER.lower()[:30]}.',
),
(
False,
{
'given_name': GIVEN_NAME_LARGER,
'family_name': FAMILY_NAME,
},
f'{GIVEN_NAME_LARGER.lower()[:30]}{FAMILY_NAME.lower()[:1]}.',
),
({}, ''),
(False, {}, ''),
(True, {}, ''),
)
@ddt.unpack
def test_username_property(self, name_data: dict, name_return: str):
def test_username_property(self, has_user: bool, name_data: dict, name_return: str):
"""Test username property."""
self.lti_profile.pii = name_data

if not has_user:
self.lti_profile.user = None

self.assertEqual(
self.lti_profile.username,
f'{name_return}{self.lti_profile.short_uuid}',
Expand Down

0 comments on commit eb21b9d

Please sign in to comment.