Skip to content

Commit

Permalink
Merge pull request #10919 from bodintsov/feature/double_email_fix
Browse files Browse the repository at this point in the history
[ENG-6882]fixed double mail sending
  • Loading branch information
Johnetordoff authored Jan 15, 2025
2 parents 83a6049 + 979b157 commit e5cc7fe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
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

0 comments on commit e5cc7fe

Please sign in to comment.