def test_try_send_throws(self): exp_addr = '*****@*****.**' exp_tpl = 'example' exp_vars = {'foo':'bar'} ps = PendingSend() ps.toaddr = exp_addr ps.template_name = exp_tpl ps.template_vars = exp_vars exc_msg = "I am a teapot" def throws(to, subject, msg): raise Exception(exc_msg) mailer.send_email = throws mailer.try_send(ps) ps_stored = PendingSend(ps.id) sent = ps_stored.is_sent assert not sent last_error = ps_stored.last_error assert exc_msg in last_error retries = ps_stored.retry_count assert 1 == retries
def test_template_vars_property(): print 'bacon' ps = PendingSend('abc') assert ps.template_vars is None ps.template_vars = {"foo": 'bar'} assert ps.template_vars == {'foo': 'bar'}
def test_simple_properties(): ps = PendingSend('abc') ps.toaddr = '*****@*****.**' ps.template_name = 'template-1' ps.last_error = 'bacon' assert ps.toaddr == '*****@*****.**' assert ps.template_name == 'template-1' assert ps.last_error == 'bacon'
def test_sent(): ps = PendingSend('abc') ps.toaddr = '*****@*****.**' assert ps.sent_time is None then = datetime.now() - timedelta(seconds=2) ps.mark_sent() sent_time = ps.sent_time assert sent_time > then assert ps.is_sent
def test_sent(): ps = PendingSend('abc') ps.toaddr = '*****@*****.**' assert ps.sent_time is None then = datetime.now() - timedelta(seconds = 2) ps.mark_sent() sent_time = ps.sent_time assert sent_time > then assert ps.is_sent
def last_email(): conn = sqlite_connect() cur = conn.cursor() cur.execute("SELECT id FROM outbox") row = cur.fetchone() assert row is not None, "Failed to get last email from SQLite." return PendingSend(row[0])
def test_unsent_one(): ps = PendingSend() ps.toaddr = '*****@*****.**' ps.template_name = 'template-1' ps.template_vars = {"foo": 'bar'} ps.save() unsent = list(PendingSend.Unsent()) assert len(unsent) == 1 ps = unsent[0] toaddr = ps.toaddr assert toaddr == '*****@*****.**'
def last_n_emails(num): conn = sqlite_connect() cur = conn.cursor() cur.execute("SELECT id FROM outbox LIMIT ?", (num, )) rows = cur.fetchall() assert len( rows) == num, "Failed to get last %d emails from SQLite." % (num, ) mails = [] for row in rows: mails.append(PendingSend(row[0])) return mails
def test_creation(): ps = PendingSend() ps.toaddr = '*****@*****.**' ps.template_name = 'template-1' ps.template_vars = {"foo": 'bar'} ps.last_error = 'bacon' assert ps.id is None ps.save() assert ps.in_db assert ps.id > 0 ps = PendingSend(ps.id) assert ps.in_db assert ps.toaddr == '*****@*****.**' assert ps.template_name == 'template-1' assert ps.template_vars == {'foo': 'bar'} assert ps.last_error == 'bacon' assert ps.retry_count == 0 assert ps.age > timedelta() assert ps.age < timedelta(minutes=1)
def test_retried(): ps = PendingSend('abc') ps.toaddr = '*****@*****.**' assert ps.retry_count == 0 ps.retried() ps.retried() ps.retried() assert ps.retry_count == 3
def test_try_send_throws(self): exp_addr = '*****@*****.**' exp_tpl = 'example' exp_vars = {'foo': 'bar'} ps = PendingSend() ps.toaddr = exp_addr ps.template_name = exp_tpl ps.template_vars = exp_vars exc_msg = "I am a teapot" def throws(to, subject, msg): raise Exception(exc_msg) mailer.send_email = throws mailer.try_send(ps) ps_stored = PendingSend(ps.id) sent = ps_stored.is_sent assert not sent last_error = ps_stored.last_error assert exc_msg in last_error retries = ps_stored.retry_count assert 1 == retries
def store_template(toaddr, template_name, template_vars): ps = PendingSend() ps.toaddr = toaddr ps.template_name = template_name ps.template_vars = template_vars ps.save() return ps
def test_try_send_ok(self): exp_addr = '*****@*****.**' exp_tpl = 'example' exp_vars = {'foo':'bar'} ps = PendingSend() ps.toaddr = exp_addr ps.template_name = exp_tpl ps.template_vars = exp_vars mailer.try_send(ps) tpl_lines = test_helpers.template(exp_tpl + '.txt') exp_subject = tpl_lines[0] assert exp_addr == self._to assert self._subject in exp_subject assert 'foo' not in self._msg assert 'bar' in self._msg ps_stored = PendingSend(ps.id) sent = ps_stored.is_sent assert sent
def test_creation(): ps = PendingSend() ps.toaddr = '*****@*****.**' ps.template_name = 'template-1' ps.template_vars = {"foo": 'bar'} ps.last_error = 'bacon' assert ps.id is None ps.save() assert ps.in_db assert ps.id > 0 ps = PendingSend(ps.id) assert ps.in_db assert ps.toaddr == '*****@*****.**' assert ps.template_name == 'template-1' assert ps.template_vars == {'foo': 'bar'} assert ps.last_error == 'bacon' assert ps.retry_count == 0 assert ps.age > timedelta() assert ps.age < timedelta(minutes = 1)
def test_try_send_ok(self): exp_addr = '*****@*****.**' exp_tpl = 'example' exp_vars = {'foo': 'bar'} ps = PendingSend() ps.toaddr = exp_addr ps.template_name = exp_tpl ps.template_vars = exp_vars mailer.try_send(ps) tpl_lines = test_helpers.template(exp_tpl + '.txt') exp_subject = tpl_lines[0] assert exp_addr == self._to assert self._subject in exp_subject assert 'foo' not in self._msg assert 'bar' in self._msg ps_stored = PendingSend(ps.id) sent = ps_stored.is_sent assert sent
def test_unsent_none(): unsent = list(PendingSend.Unsent()) assert len(unsent) == 0
nemesis_root = os.path.dirname(os.path.abspath(__file__)) + "/../" sys.path.insert(0, nemesis_root) import config from sqlitewrapper import PendingSend if __name__ == "__main__": parser = argparse.ArgumentParser( description="List pending email sends.", ) parser.add_argument( '--limit', default=1000, type=int, help="The maximum number of emails to list (default: %(default)s).", ) parser.add_argument( '--max-retries', default=5, type=int, help="Restrict to emails with up to this many retry attempts (default: " "%(default)s).", ) args = parser.parse_args() for ps in PendingSend.Unsent( max_results=args.limit, max_retry=args.max_retries + 1, ): print(ps)
def send_emails(limit=50, max_retry=5): unsent_mails = PendingSend.Unsent(max_retry=max_retry, max_results=limit) for ps in unsent_mails: mailer.try_send(ps)