def test_compose_and_send_sendmail(self, smtp_mock, mail_mock): with self.__service.test_request_context(path="/test"): # sendmail errors mytoken = 'mytoken_abc' toaddr = "user@remotehost" body = os.path.join(self._conf['verification_url'], mytoken) smtp_mock.return_value.sendmail.side_effect = smtplib.SMTPException with self.assertRaises(RuntimeError): HRService.compose_and_send(toaddr, mytoken, datetime.datetime.utcnow()) args = smtp_mock.return_value.sendmail.call_args assert args[0][0] == self._conf['smtp_server_login'] assert args[0][1] == toaddr assert body in args[0][2] # check the important part of the email
def test_compose_and_send(self, close_mock, connect_mock, mail_mock): with self.__service.test_request_context(path="/test"): # force connect to raise the SMTPException derived class. HRService wraps it into # RuntimeError connect_mock.return_value = (400, 'cannot connect message' ) # 220 is the success code with self.assertRaises(RuntimeError): HRService.compose_and_send("centos@localhost", 'mytoken_abc', datetime.datetime.utcnow() ) # timestamp does not matter here connect_mock.assert_called_with('localhost', None) # from conf{} # now allow for connect() to raise a socket.error import socket connect_mock.side_effect = socket.error with self.assertRaises(RuntimeError): HRService.compose_and_send("centos@localhost", 'mytoken_abc', datetime.datetime.utcnow())