def test_smtp_config_host_and_port_not_set(self): temp_dir = tempfile.mkdtemp() try: cfile = os.path.join(temp_dir, 'foo') con = configparser.ConfigParser() con.add_section(SmtpConfig.DEFAULT) con.set(SmtpConfig.DEFAULT, SmtpConfig.SMTP_FROM_ADDRESS, 'from') con.set(SmtpConfig.DEFAULT, SmtpConfig.SMTP_REPLYTO_ADDRESS, 'replyto') con.set(SmtpConfig.DEFAULT, SmtpConfig.SMTP_PASS, 'pass') f = open(cfile, 'w') con.write(f) f.flush() f.close() smtpconfig = SmtpConfig(configfile=cfile) self.assertEqual(smtpconfig.get_user(), None) self.assertEqual(smtpconfig.get_host(), 'localhost') self.assertEqual(smtpconfig.get_port(), 25) self.assertEqual(smtpconfig.get_password(), 'pass') self.assertEqual(smtpconfig.get_from_address(), 'from') self.assertEqual(smtpconfig.get_replyto_address(), 'replyto') finally: shutil.rmtree(temp_dir)
def test_smtp_config_noconfig(self): smtpconfig = SmtpConfig() self.assertEqual(smtpconfig.get_from_address(), None) self.assertEqual(smtpconfig.get_host(), 'localhost') self.assertEqual(smtpconfig.get_port(), 25) self.assertEqual(smtpconfig.get_password(), None) self.assertEqual(smtpconfig.get_from_address(), None) self.assertEqual(smtpconfig.get_replyto_address(), None)
def test_smtp_config_empty_config_file(self): temp_dir = tempfile.mkdtemp() try: empty_file = os.path.join(temp_dir, 'foo') open(empty_file, 'a').close() smtpconfig = SmtpConfig(configfile=empty_file) self.assertEqual(smtpconfig.get_from_address(), None) self.assertEqual(smtpconfig.get_host(), 'localhost') self.assertEqual(smtpconfig.get_port(), 25) finally: shutil.rmtree(temp_dir)
def test_smtp_config_nonexistantconfigfile(self): temp_dir = tempfile.mkdtemp() try: nonexist_file = os.path.join(temp_dir, 'foo') smtpconfig = SmtpConfig(configfile=nonexist_file) self.assertEqual(smtpconfig.get_from_address(), None) self.assertEqual(smtpconfig.get_host(), 'localhost') self.assertEqual(smtpconfig.get_port(), 25) finally: shutil.rmtree(temp_dir)
def test_smtp_config_port_not_a_number(self): temp_dir = tempfile.mkdtemp() try: cfile = os.path.join(temp_dir, 'foo') con = configparser.ConfigParser() con.add_section(SmtpConfig.DEFAULT) con.set(SmtpConfig.DEFAULT, SmtpConfig.SMTP_PORT, 'hello') f = open(cfile, 'w') con.write(f) f.flush() f.close() smtpconfig = SmtpConfig(configfile=cfile) self.assertEqual(smtpconfig.get_user(), None) self.assertEqual(smtpconfig.get_port(), 25) finally: shutil.rmtree(temp_dir)
def test_smtp_config_parse_error(self): temp_dir = tempfile.mkdtemp() try: cfile = os.path.join(temp_dir, 'foo') f = open(cfile, 'w') f.write('[smtp\nas = xx=12=\n') f.flush() f.close() # this is a coverage test. Basically the # _parse_config method called from constructor # below should catch the parsing error and # not complain smtpconfig = SmtpConfig(cfile) self.assertEqual(smtpconfig.get_from_address(), None) self.assertEqual(smtpconfig.get_host(), 'localhost') self.assertEqual(smtpconfig.get_port(), 25) self.assertEqual(smtpconfig.get_password(), None) self.assertEqual(smtpconfig.get_from_address(), None) self.assertEqual(smtpconfig.get_replyto_address(), None) finally: shutil.rmtree(temp_dir)