File: test_mailsyncer.py

package info (click to toggle)
mailnag 2.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 968 kB
  • sloc: python: 5,332; xml: 38; sh: 15; makefile: 11
file content (257 lines) | stat: -rw-r--r-- 7,328 bytes parent folder | download | duplicates (3)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# -*- coding: utf-8 -*-
#
# test_account.py
#
# Copyright 2018 Timo Kankare <timo.kankare@iki.fi>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#

"""Test cases for MailSyncer."""

import pytest

import email
from Mailnag.daemon.mails import MailSyncer
from Mailnag.common.accounts import Account
from Mailnag.backends.base import MailboxBackend


class FakeBackend(MailboxBackend):
	"""Fake mailbox backend implementation for testing."""

	def __init__(self, **kw):
		self._opened = False
		self.messages = []


	def open(self):
		self._opened = True


	def close(self):
		self._opened = False


	def is_open(self):
		return self._opened


	def list_messages(self):
		for msg in self.messages:
			yield "samplefolder", msg


	def request_folders(self):
		raise NotImplementedError("no folder support")


	def notify_next_change(self, callback=None, timeout=None):
		raise NotImplementedError("no notification support")


	def cancel_notifications(self):
		raise NotImplementedError("no notification support")


class FakeAccount(Account):
	"""Fake account implementation to use special test backend."""

	def __init__(self, **kw):
		Account.__init__(self, **kw)
		self._backend = FakeBackend()


	def set_current_messages(self, messages):
		self._backend.messages = messages

message_template = """\
From: You <you@example.org>
To: Me <me@example.org>
Subject: Hello...
Message-ID: mid-{mid}
Date: Tue, 01 May 2018 16:28:08 +0300

...World!
"""

message_template_without_mid = """\
From: You <you@example.org>
To: Me <me@example.org>
Subject: Hello...
Date: Tue, 01 May 2018 16:28:08 +0300

...World!
"""


def make_messages(a, b):
	"""Make list of messages with message ids in range(a, b)."""
	make_msg = email.message_from_string
	return [make_msg(message_template.format(mid=i)) for i in range(a, b)]


def make_messages_without_mid():
	"""Make list of messages with message ids in range(a, b)."""
	return [message_template_without_mid]


def test_no_mails_with_no_accounts():
	syncer = MailSyncer([])
	accounts = []
	mails = syncer.sync(accounts)
	assert len(mails) == 0


def test_no_mails_in_an_account():
	syncer = MailSyncer([])
	account = FakeAccount()
	mails = syncer.sync([account])
	assert len(mails) == 0


def test_mails_in_an_account():
	syncer = MailSyncer([])
	account = FakeAccount()
	messages = make_messages(0, 10)
	account.set_current_messages(messages)
	mails = syncer.sync([account])
	assert len(mails) == 10


def test_syncing_account_multiple_times_should_not_affect():
	syncer = MailSyncer([])
	account = FakeAccount()
	messages = make_messages(0, 10)
	account.set_current_messages(messages)
	mails_in_first_sync = syncer.sync([account])
	mails_in_second_sync = syncer.sync([account])
	assert mails_in_first_sync == mails_in_second_sync


def test_syncing_should_remove_read_messages():
	syncer = MailSyncer([])
	account = FakeAccount()
	messages = make_messages(0, 10)
	account.set_current_messages(messages)
	mails_in_first_sync = syncer.sync([account])
	account.set_current_messages(messages[3:])
	mails_in_second_sync = syncer.sync([account])
	assert len(mails_in_second_sync) == 7
	assert all(mail in mails_in_first_sync for mail in mails_in_second_sync)


def test_syncing_should_add_new_messages():
	syncer = MailSyncer([])
	account = FakeAccount()
	messages = make_messages(0, 10)
	account.set_current_messages(messages)
	mails_in_first_sync = syncer.sync([account])
	new_messages = make_messages(10, 14)
	account.set_current_messages(messages + new_messages)
	mails_in_second_sync = syncer.sync([account])
	assert len(mails_in_second_sync) == 14
	assert all(mail in mails_in_second_sync for mail in mails_in_first_sync)


def test_syncing_should_update_messages():
	syncer = MailSyncer([])
	account = FakeAccount()
	messages = make_messages(0, 10)
	account.set_current_messages(messages)
	mails_in_first_sync = syncer.sync([account])
	new_messages = make_messages(10, 14)
	account.set_current_messages(messages[3:] + new_messages)
	mails_in_second_sync = syncer.sync([account])
	assert len(mails_in_second_sync) == 11


def test_syncing_without_account_should_keep_already_synced_messages():
	syncer = MailSyncer([])
	account = FakeAccount()
	messages = make_messages(0, 10)
	account.set_current_messages(messages)
	mails_in_first_sync = syncer.sync([account])
	mails_in_second_sync = syncer.sync([])
	assert mails_in_first_sync == mails_in_second_sync


def test_syncing_multiple_accounts_should_collect_all_messages():
	syncer = MailSyncer([])
	account1 = FakeAccount()
	account2 = FakeAccount()
	account3 = FakeAccount()
	account1.set_current_messages(make_messages(0, 10))
	account2.set_current_messages(make_messages(10, 20))
	account3.set_current_messages(make_messages(20, 30))
	mails = syncer.sync([account1, account2, account3])
	assert len(mails) == 30


def test_syncing_multiple_account_separately_should_collect_all_messages():
	syncer = MailSyncer([])
	# Note: Accounts are identified by user, server and folders attributes, 
	#       see Account.get_id()
	account1 = FakeAccount(name='1', user='a')
	account2 = FakeAccount(name='2', server='b')
	account3 = FakeAccount(name='3')
	account1.set_current_messages(make_messages(0, 10))
	account2.set_current_messages(make_messages(10, 20))
	account3.set_current_messages(make_messages(20, 30))
	syncer.sync([account1])
	syncer.sync([account2])
	mails = syncer.sync([account3])
	assert len(mails) == 30


def test_syncing_same_mail_twice_from_same_account():
	syncer = MailSyncer([])
	account = FakeAccount()
	messages1 = make_messages(0, 1)
	messages2 = make_messages(0, 1)
	account.set_current_messages(messages1 + messages2)
	mails = syncer.sync([account])
	assert len(mails) == 1


def test_syncing_same_mail_from_different_accounts():
	syncer = MailSyncer([])
	account1 = FakeAccount(name='1', user='a')
	account2 = FakeAccount(name='2', server='b')
	messages1 = make_messages(0, 1)
	messages2 = make_messages(0, 1)
	account1.set_current_messages(messages1)
	account2.set_current_messages(messages2)
	mails = syncer.sync([account1, account2])

	# Note: Is this ok, or should it list message in both accounts?
	assert len(mails) == 1


def test_syncing_with_messages_without_message_ids():
	syncer = MailSyncer([])
	account1 = FakeAccount(name='1', user='a')
	account2 = FakeAccount(name='2', server='b')
	account3 = FakeAccount(name='3')
	messages1 = make_messages_without_mid()
	messages2 = make_messages_without_mid()
	messages3 = make_messages_without_mid()
	account1.set_current_messages(messages1)
	account2.set_current_messages(messages2)
	account3.set_current_messages(messages3)
	mails = syncer.sync([account1, account2, account3])
	assert len(mails) == 3