-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_test.py
95 lines (82 loc) · 3.31 KB
/
system_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import requests
import unittest
class TestIMAPAPI(unittest.TestCase):
def setUp(self):
self.api_url = os.environ.get('API_URL', 'http://localhost:4001')
def test_index(self):
response = requests.get(f'{self.api_url}/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, 'ImapHub Ready.')
def test_v1_index(self):
response = requests.get(f'{self.api_url}/v1')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {
'server': 'imaphub',
'readyStatus': 'READY'
})
def test_messages(self):
response = requests.get(f'{self.api_url}/v1/messages')
messages = response.json()
self.assertIsInstance(messages, list)
self.assertGreater(len(messages), 0)
message = messages[0]
self.assertIn('id', message)
self.assertIn('subject', message)
self.assertIn('from', message)
self.assertIn('to', message)
self.assertIn('date', message)
self.assertIn('body', message)
def test_post_message(self):
with open('samples/email.eml', 'rb') as f:
payload = f.read()
f.close()
headers = {'Content-Type': 'message/rfc822'}
response = requests.post(f'{self.api_url}/v1/messages', data=payload, headers=headers)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), {'message': 'Email created successfully.'})
def test_mailboxes(self):
response = requests.get(f'{self.api_url}/v1/mailboxes')
mailboxes = response.json()
self.assertIsInstance(mailboxes, list)
self.assertGreater(len(mailboxes), 0)
mailbox = mailboxes[0]
self.assertIn('name', mailbox)
self.assertIn('flags', mailbox)
self.assertIn('delimiter', mailbox)
self.assertIn('ref', mailbox)
self.assertIn('mailboxes', mailbox['ref'])
def test_mailbox_messages(self):
# Create a test mailbox
requests.post(f'{self.api_url}/v1/messages', json={
'subject': 'Test Mailbox Creation',
'from': '[email protected]',
'to': '[email protected]',
'body': 'This is a test email for creating a mailbox.'
})
# Get the list of mailboxes
response = requests.get(f'{self.api_url}/v1/mailboxes')
mailboxes = response.json()
self.assertIsInstance(mailboxes, list)
self.assertGreater(len(mailboxes), 0)
# Select a mailbox with messages
mailbox_name = None
for mailbox in mailboxes:
if len(mailbox['flags']) > 0:
mailbox_name = mailbox['name']
break
self.assertIsNotNone(mailbox_name)
# Get the list of messages for the mailbox
response = requests.get(f'{self.api_url}/v1/mailboxes/{mailbox_name}/messages')
messages = response.json()
self.assertIsInstance(messages, list)
self.assertGreater(len(messages), 0)
message = messages[0]
self.assertIn('id', message)
self.assertIn('subject', message)
self.assertIn('from', message)
self.assertIn('to', message)
self.assertIn('date', message)
self.assertIn('body', message)
if __name__ == '__main__':
unittest.main()