def sendSMS(self, message="<otp>"): """ send sms :param message: the sms submit message - could contain placeholders like <otp> or <serial> :type message: string :return: submitted message :rtype: string """ log.debug("[sendSMS] begin. process the submitting of " "the sms message %r" % (message)) ret = None if not SMSPROVIDER_IMPORTED: raise Exception( "The SMSProvider could not be imported. Maybe you " "didn't install the package (Debian " "linotp-smsprovider or PyPI SMSProvider)" ) phone = self.getPhone() otp = self.getNextOtp() serial = self.getSerial() message = message.replace("<otp>", otp) message = message.replace("<serial>", serial) log.debug("[sendSMS] sending SMS to phone number %s " % phone) (SMSProvider, SMSProviderClass) = self.loadLinOtpSMSProvider() log.debug("[sendSMS] smsprovider: %s, class: %s" % (SMSProvider, SMSProviderClass)) try: sms = getSMSProviderClass(SMSProvider, SMSProviderClass)() except Exception as exc: log.exception("[sendSMS] Failed to load SMSProvider: %r" % exc) raise exc try: # # now we need the config from the env log.debug("[sendSMS] loading SMS configuration for class %s" % sms) config = self.loadLinOtpSMSProviderConfig() log.debug("[sendSMS] config: %r" % config) sms.loadConfig(config) except Exception as exc: log.exception("[sendSMS] Failed to load SMSProviderConfig: %r" % exc) raise Exception("Failed to load SMSProviderConfig: %r" % exc) log.debug("[sendSMS] submitMessage: %r, to phone %r" % (message, phone)) ret = sms.submitMessage(phone, message) log.debug("[sendSMS] message submitted") # # after submit set validity time self.setValidUntil() # return OTP for selftest purposes log.debug("[sendSMS] end. sms message submitted: message %r" % (message)) return ret, message
def test_02_http(self): ''' Test the HTTP sms provider ''' phone = "1234567890" message = "123456" ret = False clickatell_config = { 'URL': 'http://api.clickatell.com/http/sendmsg', 'PARAMETER': { 'user': '******', 'password': '******', 'api_id': 'askme', }, 'SMS_TEXT_KEY': 'text', 'SMS_PHONENUMBER_KEY': 'to', 'HTTP_Method': 'GET', "RETURN_SUCCESS": "ID" } config = { 'URL': 'http://localhost/cgi-perl/prepaid/private/smsversand.cgi', 'PARAMETER': { 'von': 'OWN_NUMBER', 'passwort': 'PASSWORD', 'absender': 'TYPE', 'konto': '1' }, 'SMS_TEXT_KEY': 'text', 'SMS_PHONENUMBER_KEY': 'ziel', 'HTTP_Method': 'GET', "RETURN_SUCCESS": "ID" } sms = getSMSProviderClass("HttpSMSProvider", "HttpSMSProvider")() with self.assertRaisesRegexp( Exception, "Failed to send SMS. We received a none success reply from the SMS Gateway." ): sms.loadConfig(clickatell_config) ret = sms.submitMessage(phone, message) assert ret == False with self.assertRaisesRegexp(Exception, "HTTP Error 401: Authorization Required"): sms.loadConfig(config) ret = sms.submitMessage(phone, message) assert ret == False
def test_02_http(self): ''' Test the HTTP sms provider ''' phone = "1234567890" message = "123456" ret = False clickatell_config = { 'URL' : 'http://api.clickatell.com/http/sendmsg', 'PARAMETER' : { 'user':'******', 'password':'******', 'api_id':'askme', }, 'SMS_TEXT_KEY':'text', 'SMS_PHONENUMBER_KEY':'to', 'HTTP_Method':'GET', "RETURN_SUCCESS":"ID" } config = {'URL':'http://localhost/cgi-perl/prepaid/private/smsversand.cgi', 'PARAMETER': { 'von':'OWN_NUMBER', 'passwort':'PASSWORD', 'absender':'TYPE', 'konto':'1' }, 'SMS_TEXT_KEY':'text', 'SMS_PHONENUMBER_KEY':'ziel', 'HTTP_Method':'GET', "RETURN_SUCCESS":"ID" } sms = getSMSProviderClass("HttpSMSProvider", "HttpSMSProvider")() with self.assertRaisesRegexp(Exception, "Failed to send SMS. We received a none success reply from the SMS Gateway."): sms.loadConfig(clickatell_config) ret = sms.submitMessage(phone, message) assert ret == False with self.assertRaisesRegexp(Exception, "HTTP Error 401: Authorization Required"): sms.loadConfig(config) ret = sms.submitMessage(phone, message) assert ret == False
def test_01_smtp(self): ''' This test will fail, since the mailserver does not exist ''' phone = "1234567890" message = "123456" smtp_config = { 'mailserver': 'xxx.yyy.zz', 'mailsender': "*****@*****.**", #'mailuser' : "useraccount", #'mailpassword' : "somesecret", 'mailto': '*****@*****.**', 'subject': '<phone>', 'body': 'This is the otp value: <otp>' } sms = getSMSProviderClass("SmtpSMSProvider", "SmtpSMSProvider")() sms.loadConfig(smtp_config) with self.assertRaisesRegexp(Exception, "Name or service not known"): sms.submitMessage(phone, message, True) smtp_config = { 'mailserver': 'localhost:1025', 'mailsender': "*****@*****.**", #'mailuser' : "useraccount", #'mailpassword' : "somesecret", 'mailto': '*****@*****.**', 'subject': '<phone>', 'body': 'This is the otp value: <otp>' } sms.loadConfig(smtp_config) ret = sms.submitMessage(phone, message, exception=False) print ret assert ret == False with self.assertRaisesRegexp(Exception, "Connection refused"): sms.submitMessage(phone, message, True)
def test_01_smtp(self): ''' This test will fail, since the mailserver does not exist ''' phone = "1234567890" message = "123456" smtp_config = { 'mailserver' : 'xxx.yyy.zz', 'mailsender' : "*****@*****.**", #'mailuser' : "useraccount", #'mailpassword' : "somesecret", 'mailto' : '*****@*****.**', 'subject' : '<phone>', 'body' : 'This is the otp value: <otp>' } sms = getSMSProviderClass("SmtpSMSProvider", "SmtpSMSProvider")() sms.loadConfig(smtp_config) with self.assertRaisesRegexp(Exception, "Name or service not known"): sms.submitMessage(phone, message, True) smtp_config = { 'mailserver' : 'localhost:1025', 'mailsender' : "*****@*****.**", #'mailuser' : "useraccount", #'mailpassword' : "somesecret", 'mailto' : '*****@*****.**', 'subject' : '<phone>', 'body' : 'This is the otp value: <otp>' } sms.loadConfig(smtp_config) ret = sms.submitMessage(phone, message, exception=False) print ret assert ret == False with self.assertRaisesRegexp(Exception, "Connection refused"): sms.submitMessage(phone, message, True)
def sendSMS(self, message="<otp>"): ''' send sms :param message: the sms submit message - could contain placeholders like <otp> or <serial> :type message: string :return: submitted message :rtype: string ''' log.debug("[sendSMS] begin. process the submitting of " + "the sms message %r" % (message)) ret = None if not SMSPROVIDER_IMPORTED: raise Exception("The SMSProvider could not be imported. Maybe you " "didn't install the package (Debian " "linotp-smsprovider or PyPI SMSProvider)") phone = self.getPhone() otp = self.getNextOtp() serial = self.getSerial() message = message.replace("<otp>", otp) message = message.replace("<serial>", serial) log.debug("[sendSMS] sending SMS to phone number %s " % phone) (SMSProvider, SMSProviderClass) = self.loadLinOtpSMSProvider() log.debug("[sendSMS] smsprovider: %s, class: %s" % (SMSProvider, SMSProviderClass)) try: sms = getSMSProviderClass(SMSProvider, SMSProviderClass)() except Exception as exc: log.error("[sendSMS] Failed to load SMSProvider: %r" % exc) log.error("[sendSMS] %s" % traceback.format_exc()) raise exc try: # # now we need the config from the env log.debug("[sendSMS] loading SMS configuration for class %s" % sms) config = self.loadLinOtpSMSProviderConfig() log.debug("[sendSMS] config: %r" % config) sms.loadConfig(config) except Exception as exc: log.error("[sendSMS] Failed to load SMSProviderConfig: %r" % exc) log.error("[sendSMS] %s" % traceback.format_exc()) raise Exception("Failed to load SMSProviderConfig: %r" % exc) log.debug("[sendSMS] submitMessage: %r, to phone %r" % (message, phone)) ret = sms.submitMessage(phone, message) log.debug("[sendSMS] message submitted") # # after submit set validity time self.setValidUntil() # return OTP for selftest purposes log.debug("[sendSMS] end. sms message submitted: message %r" % (message)) return ret, message