def test_write(self):
        self.storage.redis.hsetnx(Func(_is_prefixed_id), 'envelope',
                                  IsA(bytes)).AndReturn(0)
        self.storage.redis.hsetnx(Func(_is_prefixed_id), 'envelope',
                                  IsA(bytes)).AndReturn(1)
        pipe = self.mox.CreateMockAnything()
        self.storage.redis.pipeline().AndReturn(pipe)

        def _verify_hmset(val):
            self.assertEqual(1234567890, val['timestamp'])
            self.assertEqual(0, val['attempts'])
            self.assertFalse('envelope' in val)
            return True

        pipe.hmset(Func(_is_prefixed_id), Func(_verify_hmset))

        def _verify_rpush(val):
            timestamp, id = cPickle.loads(val)
            self.assertEqual(1234567890, timestamp)
            self.assertTrue(_is_id(id))
            return True

        pipe.rpush('test:queue', Func(_verify_rpush))
        pipe.execute()
        self.mox.ReplayAll()
        env = Envelope('*****@*****.**', ['*****@*****.**'])
        env.timestamp = 9876543210
        self.storage.write(env, 1234567890)
 def test_add_date_header(self):
     env = Envelope()
     env.parse(b'')
     env.timestamp = 1234567890
     adh = AddDateHeader()
     self.assertEqual(None, env.headers['Date'])
     adh.apply(env)
     self.assertTrue(env.headers['Date'])
 def test_add_date_header(self):
     env = Envelope()
     env.parse('')
     env.timestamp = 1234567890
     adh = AddDateHeader()
     self.assertEqual(None, env.headers['Date'])
     adh.apply(env)
     self.assertTrue(env.headers['Date'])
 def test_add_message_id_header(self):
     env = Envelope()
     env.parse(b'')
     env.timestamp = 1234567890
     amih = AddMessageIdHeader('example.com')
     self.assertEqual(None, env.headers['Message-Id'])
     amih.apply(env)
     pattern = r'^<[0-9a-fA-F]{32}\[email protected]>$'
     self.assertRegexpMatches(env.headers['Message-Id'], pattern)
 def test_add_message_id_header(self):
     env = Envelope()
     env.parse('')
     env.timestamp = 1234567890
     amih = AddMessageIdHeader('example.com')
     self.assertEqual(None, env.headers['Message-Id'])
     amih.apply(env)
     pattern = r'^<[0-9a-fA-F]{32}\[email protected]>$'
     self.assertRegexpMatches(env.headers['Message-Id'], pattern)
 def test_add_received_header(self):
     env = Envelope('*****@*****.**', ['*****@*****.**'])
     env.parse('')
     env.timestamp = 1234567890
     env.client['name'] = 'mail.example.com'
     env.client['ip'] = '1.2.3.4'
     env.client['protocol'] = 'ESMTPS'
     env.receiver = 'test.com'
     arh = AddReceivedHeader()
     arh.apply(env)
     self.assertRegexpMatches(env.headers['Received'],
             r'from mail\.example\.com \(unknown \[1.2.3.4\]\) by test.com '
             r'\(slimta [^\)]+\) with ESMTPS for <*****@*****.**>; ')
 def test_add_received_header(self):
     env = Envelope('*****@*****.**', ['*****@*****.**'])
     env.parse(b'From: [email protected]\r\n')
     env.timestamp = 1234567890
     env.client['name'] = 'mail.example.com'
     env.client['ip'] = '1.2.3.4'
     env.client['protocol'] = 'ESMTPS'
     env.receiver = 'test.com'
     arh = AddReceivedHeader()
     arh.apply(env)
     self.assertRegexpMatches(
         env.headers['Received'],
         r'from mail\.example\.com \(unknown \[1.2.3.4\]\) by test.com '
         r'\(slimta [^\)]+\) with ESMTPS for <*****@*****.**>; ')
 def test_write(self):
     self.storage.redis.hsetnx(Func(_is_prefixed_id), 'envelope', IsA(bytes)).AndReturn(0)
     self.storage.redis.hsetnx(Func(_is_prefixed_id), 'envelope', IsA(bytes)).AndReturn(1)
     pipe = self.mox.CreateMockAnything()
     self.storage.redis.pipeline().AndReturn(pipe)
     def _verify_hmset(val):
         self.assertEqual(1234567890, val['timestamp'])
         self.assertEqual(0, val['attempts'])
         self.assertFalse('envelope' in val)
         return True
     pipe.hmset(Func(_is_prefixed_id), Func(_verify_hmset))
     def _verify_rpush(val):
         timestamp, id = cPickle.loads(val)
         self.assertEqual(1234567890, timestamp)
         self.assertTrue(_is_id(id))
         return True
     pipe.rpush('test:queue', Func(_verify_rpush))
     pipe.execute()
     self.mox.ReplayAll()
     env = Envelope('*****@*****.**', ['*****@*****.**'])
     env.timestamp = 9876543210
     self.storage.write(env, 1234567890)
 def _write_test_envelope(self, rcpts=None):
     env = Envelope('*****@*****.**', rcpts or ['*****@*****.**'])
     env.timestamp = 9876543210
     id = self.dict.write(env, 1234567890)
     return id, env
 def _write_test_envelope(self, rcpts=None):
     env = Envelope('*****@*****.**', rcpts or ['*****@*****.**'])
     env.timestamp = 9876543210
     id = self.disk.write(env, 1234567890)
     return id, env