diff --git a/api_tests/requests/views/test_node_request_institutional_access.py b/api_tests/requests/views/test_node_request_institutional_access.py index 72b0fcce03b..c3fdc4e111b 100644 --- a/api_tests/requests/views/test_node_request_institutional_access.py +++ b/api_tests/requests/views/test_node_request_institutional_access.py @@ -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): diff --git a/osf/utils/machines.py b/osf/utils/machines.py index 9687e19749d..ac63b1b7894 100644 --- a/osf/utils/machines.py +++ b/osf/utils/machines.py @@ -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