source: mailjam/mailjam/tests/mta.py@ 16:cd4170142d87

Last change on this file since 16:cd4170142d87 was 16:cd4170142d87, checked in by Borja Lopez <borja@…>, 12 years ago

Renamed the project to Mailjam

File size: 3.7 KB
RevLine 
[2]1# -*- coding: utf-8 -*-
2
[15]3import os, sys, multiprocessing, time
[2]4from unittest import TestCase
[16]5from mailjam.mta import MTAClient
6from mailjam.daemon import MailjamDaemon
7from mailjam.models import Member, MailingList
[2]8
[15]9
10class TestMTAClient(TestCase):
11 """
12 FIXME: These are dummy tests, they cover almost nothing from the
[16]13 real mailjam mta client (yet)
[15]14 """
[2]15 def setUp(self):
[15]16 self.mta_configfile = os.path.join(os.path.dirname(__file__),
[16]17 'mailjam-mta.conf')
[10]18 self.configfile = os.path.join(os.path.dirname(__file__),
[16]19 'mailjam.conf')
[2]20 self.mailing_list = MailingList('test_list', 'test_list@example.com',
[10]21 members={}, configfile=self.configfile)
[2]22 self.member = Member('test@example.com')
23 self.raw_email_file = os.path.join(os.path.dirname(__file__),
24 'sample_raw_email.txt')
25 tmp_to_read = open(self.raw_email_file, 'r')
26 self.raw_email = tmp_to_read.read()
27 tmp_to_read.close()
[15]28
[2]29 def test___init__(self):
[16]30 # in order to start mta client instances, we need to have a mailjam
[15]31 # daemon running on the background
32 # This should be added to the setUp() method, but then it would be
33 # more difficult to terminate the process after all the methods
34 # were called
[16]35 daemon = MailjamDaemon(self.configfile)
[15]36 daemon.port = 9100
37 p = multiprocessing.Process(target=daemon.run)
38 p.start()
39 # Add a delay here to allow the server to be fully started before
40 # starting to send requests from the client
41 time.sleep(1)
42
[2]43 with self.assertRaises(ValueError):
[15]44 mta = MTAClient(self.mailing_list, self.mta_configfile)
45 mta = MTAClient(self.member, self.mta_configfile)
46 mta = MTAClient(None, self.mta_configfile)
47 mta = MTAClient(self.mailing_list.address, self.mta_configfile)
48 self.assertTrue(isinstance(mta, MTAClient))
49 self.assertEqual(mta.address, self.mailing_list.address)
[2]50 self.assertEqual(mta.suscriptors, self.mailing_list.members_addresses)
51 self.assertEqual(mta.reply_to, self.mailing_list.address)
52
[15]53 p.terminate()
54
[2]55 def test_get_raw_email(self):
[16]56 # in order to start mta client instances, we need to have a mailjam
[15]57 # daemon running on the background
58 # This should be added to the setUp() method, but then it would be
59 # more difficult to terminate the process after all the methods
60 # were called
[16]61 daemon = MailjamDaemon(self.configfile)
[15]62 daemon.port = 9100
63 p = multiprocessing.Process(target=daemon.run)
64 p.start()
65 # Add a delay here to allow the server to be fully started before
66 # starting to send requests from the client
67 time.sleep(1)
68
69 mta = MTAClient(self.mailing_list.address, self.mta_configfile)
70
[2]71 sys_stdin = sys.stdin
72 sys.stdin = open(self.raw_email_file, 'r')
[15]73 self.assertEqual(mta.get_raw_email(),
[2]74 self.raw_email)
75 sys.stdin.close()
76 with self.assertRaises(IOError):
[15]77 mta.get_raw_email()
78
79 tmp_file= open(self.raw_email_file('r'))
80 raw_data = tmp_file.read()
81 tmp_file.close()
82 self.assertEqual(mta.get_raw_email(raw_data),
83 self.raw_email)
84
85 p.terminate()
[2]86
87 #def save_raw_email(self):
88 # sys_stdin = sys.stdin
89 # sys.stdin = open(self.raw_email_file, 'r')
[16]90 # self.mta.save_raw_email.filename = '/tmp/mailjam-test-mta-save-raw-email'
[2]91
92
93
94
95
96
97
Note: See TracBrowser for help on using the repository browser.