-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a505944
commit 377a77c
Showing
20 changed files
with
657 additions
and
471 deletions.
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 |
---|---|---|
|
@@ -3,40 +3,30 @@ | |
from django.test import TestCase | ||
|
||
from core import services as core_services | ||
from user.exceptions import UserExists | ||
|
||
|
||
class TestCoreService(TestCase): | ||
User = get_user_model() | ||
|
||
@pytest.mark.django_db | ||
def test_core_get_or_create_user(self): | ||
user_details = { | ||
"is_active": True, | ||
} | ||
@pytest.mark.skip() | ||
def test_create_user(self): | ||
# User is created | ||
user, created = core_services.get_or_create_user( | ||
user = core_services.create_user( | ||
id="[email protected]", | ||
**user_details, | ||
first_name="Billy", | ||
last_name="Joel", | ||
emails=[{"address": "[email protected]"}], | ||
) | ||
self.assertTrue(created) | ||
self.assertEqual(user.sso_email_id, "[email protected]") | ||
self.assertEqual(user.is_active, True) | ||
|
||
# User already exists | ||
existing_user, is_created = core_services.get_or_create_user( | ||
id="[email protected]", | ||
**user_details, | ||
) | ||
self.assertFalse(is_created) | ||
|
||
@pytest.mark.django_db | ||
def test_core_get_user_by_id(self): | ||
|
||
test_user = self.User.objects.create_user( | ||
sso_email_id="test_user", | ||
is_active=True, | ||
) | ||
|
||
user = core_services.get_user_by_id(test_user.sso_email_id) | ||
self.assertEqual(user.sso_email_id, "test_user") | ||
self.assertEqual(user.is_active, True) | ||
with self.assertRaises(UserExists) as ex: | ||
existing_user = core_services.create_user( | ||
id="[email protected]", | ||
first_name="Billy", | ||
last_name="Joel", | ||
emails=[{"address": "[email protected]"}], | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
class ProfileIsArchived(Exception): | ||
def __init__(self, message): | ||
self.message = message | ||
|
||
|
||
class ProfileIsNotArchived(Exception): | ||
def __init__(self, message): | ||
self.message = message | ||
|
||
|
||
class ProfileExists(Exception): | ||
def __init__(self, message): | ||
self.message = message |
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,27 @@ | ||
# This is the entrypoint service that coordinates between the sub-services | ||
# If in doubt about what to use, you should probably be using this | ||
|
||
from profiles.models import Email | ||
from profiles.services import combined, staff_sso | ||
|
||
|
||
def generate_combined_profile_fields(sso_user_id: str): | ||
""" | ||
Figures out which info we have in specific profiles and runs through the | ||
field hierarchy per data type to generate the values for the combined. | ||
create method | ||
""" | ||
raise NotImplementedError() | ||
|
||
|
||
def create_from_sso( | ||
sso_user_id: str, | ||
first_name: str, | ||
last_name: str, | ||
emails: list[dict], | ||
): | ||
"""A central function to create all relevant profile details""" | ||
# staff_sso.create() | ||
# payload = generate_combined_profile_values() | ||
# combined.create(sso_user_id, **payload) | ||
raise NotImplementedError() |
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,86 @@ | ||
from typing import Optional | ||
|
||
from profiles.exceptions import ProfileExists, ProfileIsArchived, ProfileIsNotArchived | ||
from profiles.models import Profile | ||
|
||
|
||
############################################################### | ||
# Base data methods | ||
############################################################### | ||
|
||
|
||
#### ID is required - no getting around it #### | ||
|
||
|
||
def get_by_id(sso_email_id: str) -> Profile: | ||
# NB we're not raising more specific exceptions here because we're optimising this for speed | ||
return Profile.objects.get(sso_email_id=sso_email_id, is_active=True) | ||
|
||
|
||
def create( | ||
sso_email_id: str, | ||
first_name: str, | ||
last_name: str, | ||
emails: list[str], | ||
preferred_email: Optional[str], | ||
) -> Profile: | ||
try: | ||
get_by_id(sso_email_id) | ||
except Profile.DoesNotExist: | ||
return Profile.objects.create( | ||
sso_email_id=sso_email_id, | ||
first_name=first_name, | ||
last_name=last_name, | ||
emails=emails, | ||
preferred_email=preferred_email, | ||
) | ||
raise ProfileExists("Profile has been previously created") | ||
|
||
|
||
#### Standard profile-object methods #### | ||
|
||
|
||
def update( | ||
profile: Profile, | ||
first_name: Optional[str], | ||
last_name: Optional[str], | ||
preferred_email: Optional[str], | ||
emails: Optional[list[str]], | ||
) -> None: | ||
update_fields = [] | ||
if first_name is not None: | ||
update_fields.append("first_name") | ||
profile.first_name = first_name | ||
if last_name is not None: | ||
update_fields.append("last_name") | ||
profile.last_name = last_name | ||
if preferred_email is not None: | ||
update_fields.append("preferred_email") | ||
profile.preferred_email = preferred_email | ||
if emails is not None: | ||
update_fields.append("emails") | ||
profile.emails = emails | ||
profile.save(update_fields=update_fields) | ||
|
||
|
||
def archive(profile: Profile) -> None: | ||
"""Soft-delete a profile""" | ||
if not profile.is_active: | ||
raise ProfileIsArchived("Profile is already archived") | ||
|
||
profile.is_active = False | ||
profile.save(update_fields=("is_active",)) | ||
|
||
|
||
def unarchive(profile: Profile) -> None: | ||
"""Restore a soft-deleted profile.""" | ||
if profile.is_active: | ||
raise ProfileIsNotArchived("Profile is not archived") | ||
|
||
profile.is_active = True | ||
profile.save(update_fields=("is_active",)) | ||
|
||
|
||
def delete_from_database(profile: Profile) -> None: | ||
"""Really delete a Profile. Only to be used in data cleaning (i.e. non-standard) operations""" | ||
profile.delete() |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.