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

[ENG-6882]fixed double mail sending #10919

Merged
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions api_tests/requests/views/test_node_request_institutional_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,53 @@ def test_institutional_admin_unauth_institution(self, app, project, institution_
assert res.status_code == 403
assert 'Institutional request access is not enabled.' in res.json['errors'][0]['detail']

@mock.patch('api.requests.serializers.send_mail')
@mock.patch('osf.utils.machines.mails.send_mail')
def test_email_send_institutional_request_specific_email(
self,
mock_send_mail_machines,
mock_send_mail_serializers,
user_with_affiliation,
app,
project,
url,
create_payload,
institutional_admin,
institution
):
"""
Test that the institutional request triggers email notifications to appropriate recipients.
"""
# Set up mock behaviors
project.is_public = True
project.save()

# Perform the action
res = app.post_json_api(url, create_payload, auth=institutional_admin.auth)

# Ensure response is successful
assert res.status_code == 201

assert mock_send_mail_serializers.call_count == 1
assert mock_send_mail_machines.call_count == 0

# Check calls for osf.utils.machines.mails.send_mail
mock_send_mail_serializers.assert_called_once_with(
to_addr=user_with_affiliation.username,
mail=NODE_REQUEST_INSTITUTIONAL_ACCESS_REQUEST,
user=user_with_affiliation,
bcc_addr=None,
reply_to=None,
**{
'sender': institutional_admin,
'recipient': user_with_affiliation,
'comment': create_payload['data']['attributes']['comment'],
'institution': institution,
'osf_url': mock.ANY,
'node': project,
}
)

@mock.patch('api.requests.serializers.send_mail')
def test_email_not_sent_without_recipient(self, mock_mail, app, project, institutional_admin, url,
create_payload, institution):
Expand Down
18 changes: 9 additions & 9 deletions osf/utils/machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ def notify_submit(self, ev):
context = self.get_context()
context['contributors_url'] = f'{self.machineable.target.absolute_url}contributors/'
context['project_settings_url'] = f'{self.machineable.target.absolute_url}settings/'

for admin in self.machineable.target.get_users_with_perm(permissions.ADMIN):
mails.send_mail(
admin.username,
mails.ACCESS_REQUEST_SUBMITTED,
admin=admin,
osf_contact_email=OSF_CONTACT_EMAIL,
**context
)
if not self.machineable.request_type == NodeRequestTypes.INSTITUTIONAL_REQUEST.value:
for admin in self.machineable.target.get_users_with_perm(permissions.ADMIN):
mails.send_mail(
admin.username,
mails.ACCESS_REQUEST_SUBMITTED,
admin=admin,
osf_contact_email=OSF_CONTACT_EMAIL,
**context
)

def notify_resubmit(self, ev):
""" Notify admins that someone is requesting access again
Expand Down
Loading