Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notifications: no long tasks email for certain users #14674

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions tests/foreman/api/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ def admin_user_with_localhost_email(target_sat):
user.delete()


@pytest.fixture
def admin_user_with_custom_settings(request, admin_user_with_localhost_email):
"""Admin user with custom properties set via parametrization.
`request.param` should be a dict-like value.
"""
for key, value in request.param.items():
setattr(admin_user_with_localhost_email, key, value)
admin_user_with_localhost_email.update(list(request.param.keys()))
return admin_user_with_localhost_email


@pytest.fixture
def sysadmin_user_with_subscription_reposync_fail(target_sat):
"""System admin user with `root@localhost` e-mail
Expand Down Expand Up @@ -162,6 +173,25 @@ def wait_for_failed_repo_sync_mail(
)


@pytest.fixture
def wait_for_no_long_running_task_mail(target_sat, clean_root_mailbox, long_running_task):
"""Wait and check that no long-running task ID is found in the Satellite's mbox file."""
timeout = 120
try:
wait_for_mail(
sat_obj=target_sat,
mailbox_file=clean_root_mailbox,
contains_string=long_running_task["task"]["id"],
timeout=timeout,
)
except AssertionError:
return True
raise AssertionError(
f'E-mail with long running task ID "{long_running_task["task"]["id"]}" '
f'should not have arrived to mailbox {clean_root_mailbox}!'
)


@pytest.fixture
def root_mailbox_copy(target_sat, clean_root_mailbox):
"""Parsed local system copy of the Satellite's root user mailbox.
Expand Down Expand Up @@ -362,3 +392,48 @@ def test_positive_notification_recipients(target_sat):
recipients = target_sat.api.NotificationRecipients().read()
for notification in recipients.notifications:
assert set(notification_keys) == set(notification.keys())


@pytest.mark.tier3
@pytest.mark.parametrize(
'admin_user_with_custom_settings',
[
pytest.param({'disabled': True, 'mail_enabled': True}, id='account_disabled'),
pytest.param({'disabled': False, 'mail_enabled': False}, id='mail_disabled'),
],
indirect=True,
)
@pytest.mark.usefixtures(
'reschedule_long_running_tasks_notification',
'wait_for_no_long_running_task_mail',
)
def test_negative_no_notification_for_long_running_tasks(
admin_user_with_custom_settings, long_running_task, root_mailbox_copy
Copy link
Member

@ogajduse ogajduse Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks rock-solid. One question here, why does this test require the root_mailbox_copy fixture?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I actually wanted to ensure that a clean mailbox file is created, but it is already done in fixture wait_for_no_long_running_task_mail via clean_root_mailbox.
But since you mentioned it, I decided to keep it and added few lines to check for the unexpected mail explicitly.
Read more below...

):
"""Check that an e-mail notification for a long-running task
(i.e., running or paused for more than two days)
is NOT sent to users with disabled account or disabled e-mail.

:id: 03b41216-f39b-11ee-b9ea-000c2989e153

:setup:
1. Create an admin user with e-mail address set and:
a. account disabled & mail enabled
b. account enabled & mail disabled

:steps:
1. Create a long-running task.
3. For each user, wait and check that the notification e-mail has NOT been sent.

:BZ: 2245056

:customerscenario: true
"""
assert admin_user_with_custom_settings
task_id = long_running_task['task']['id']
assert task_id

for email in root_mailbox_copy:
assert (
task_id not in email.as_string()
), f'Unexpected notification e-mail with long-running task ID {task_id} found in user mailbox!'
Comment on lines +436 to +439
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this gives the test more "believable" look since nothing much else looks to be happening here 😉
And it's also a double safety, because if this assert fails, it means that the unwanted mail somehow slipped through wait_for_no_long_running_task_mail fixture (meaning it's not working as it should).

Loading