Skip to content

Commit

Permalink
feat: ✨ Add method email_address to CRUDUser to retrieve the email ad…
Browse files Browse the repository at this point in the history
…dress of a user

Also add associated tests (and modify existing tests to accomodate the users' email address)
  • Loading branch information
egrelier committed Aug 11, 2023
1 parent 576deeb commit 9bbc3e8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
15 changes: 10 additions & 5 deletions tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def create_and_delete_temporary_database():
with all the necessary tables and values, and delete the database file at the
end of the session
"""
users.append(crud.user._user_name())
users.append([crud.user._user_name(), "current_user@domain.org"])

with get_db() as session:
# Ensure we work with a test database before creating tables
Expand All @@ -121,7 +121,7 @@ def create_and_delete_temporary_database():
)
session.add_all(categories_lines_of_sight)
session.add_all([Method(name=x) for x in methods])
session.add_all([User(name=x) for x in users])
session.add_all([User(name=x[0], email=x[1]) for x in users])
session.add_all([Severity(name=x) for x in severity_types])
session.add_all([AnalysisStatus(name=x) for x in analysis_status])
session.add_all(
Expand Down Expand Up @@ -240,7 +240,7 @@ def test_thermal_event_get_by_columns():
)
)

thermal_event.user = users[0]
thermal_event.user = users[0][0]
thermal_event.experiment_id = 123
thermal_event.method = methods[0]
thermal_event.line_of_sight = line_of_sight
Expand All @@ -249,7 +249,7 @@ def test_thermal_event_get_by_columns():
# Send the thermal event to the database
crud.thermal_event.create(thermal_event)

events = crud.thermal_event.get_by_columns(user=users[0])
events = crud.thermal_event.get_by_columns(user=users[0][0])
assert len(events) == 1

ids = crud.thermal_event.get_by_columns(experiment_id=123, return_columns=["id"])
Expand Down Expand Up @@ -458,12 +458,17 @@ def test_thermal_event_change_analysis_status():

def test_user():
# Check the users list
assert crud.user.list() == sorted(users, key=lambda s: s.lower())
assert crud.user.list() == sorted([x[0] for x in users], key=lambda s: s.lower())

# Check that the current user has read and write rights
assert crud.user.has_read_rights()
assert crud.user.has_write_rights()

# Check that the user's email address is correctly retrieved
assert crud.user.email_address(users[0][0]) == users[0][1]
assert crud.user.email_address(crud.user._user_name()) == users[-1][1]
assert crud.user.email_address("fake user") is None


def test_thermal_event_category():
# Check the categories list
Expand Down
6 changes: 3 additions & 3 deletions tests/test_thermal_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"method 2",
]
users = [
"user 1",
"user 2",
["user 1", "user_1@domain.org"],
["user 2", "user_2@domain.org"],
]
severity_types = [
"severity type 1",
Expand Down Expand Up @@ -70,7 +70,7 @@ def random_event(n_instances=10, compat=None):
category=category,
is_automatic_detection=False,
method=random.choice(methods),
user=random.choice(users),
user=random.choice([x[0] for x in users]),
severity=random.choice(severity_types),
analysis_status=random.choice(analysis_status),
)
Expand Down
15 changes: 15 additions & 0 deletions thermal_events/crud/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def list(self):
res = session.query(User.name).all()
return [x[0] for x in res]

def email_address(self, user):
"""Return the email address of a user.
Args:
user (str): The name of the user
Returns:
str: The email address of the user. Returns None if the user does not exist
"""
with session_scope() as session:
res = session.query(User.email).filter(User.name == user).first()
if res is not None:
res = res[0]
return res

def has_write_rights(self):
"""Check if the current user has write rights.
Expand Down

0 comments on commit 9bbc3e8

Please sign in to comment.