File: test_accountmanager.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 (299 lines) | stat: -rw-r--r-- 7,890 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# -*- coding: utf-8 -*-
#
# test_accountmanager.py
#
# Copyright 2016, 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 account manager."""

from configparser import RawConfigParser
from io import StringIO
import pytest

from Mailnag.backends import get_mailbox_parameter_specs
from Mailnag.common.accounts import AccountManager
from Mailnag.common.credentialstore import CredentialStore


class FakeCredentialStore(CredentialStore):
	"""Helper class to be used in tests."""

	def __init__(self):
		self.secrets = {}

	def get(self, key):
		return self.secrets[key]

	def set(self, key, secret):
		self.secrets[key] = secret

	def remove(self, key):
		if key in self.secrets:
			del self.secrets[key]


sample_config_file = """
[account1]
enabled = 1
name = IMAP mailbox config
user = you
password = drowssap
server = imap.example.org
port =
ssl = 1
imap = 1
idle = 1
folder = []

[account2]
enabled = 1
name = POP3 mailbox config
user = me
password = poppoppop
server = pop.example.org
port =
ssl = 1
imap = 0
idle = 0
folder = []

[account3]
enabled = 1
name = Empty account config for testing default values

[account4]
enabled = 1
name = Imap config with empty folder option
folder =

[account5]
enabled = 1
name = Imap config with old style folder option
folder = folderA, folderB, folderC

[account6]
enabled = 1
name = Imap config with json folder option
folder = ["folderA", "folderB", "folderC"]
"""

@pytest.fixture
def config():
	cp = RawConfigParser()
	cp.readfp(StringIO(sample_config_file), filename='sample_config_file')
	return cp


def test_imap_config_options(config):
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('imap')
	options = am._get_cfg_options(config, 'account1', option_spec)
	expected_options = {
		'user': 'you',
		'password': 'drowssap',
		'server': 'imap.example.org',
		'port': '',
		'ssl': True,
		'imap': True,
		'idle': True,
		'folders': [],
	}
	assert expected_options == options


def test_imap_config_defaults(config):
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('imap')
	options = am._get_cfg_options(config, 'account3', option_spec)
	expected_options = {
		'user': '',
		'password': '',
		'server': '',
		'port': '',
		'ssl': True,
		'imap': True,
		'idle': True,
		'folders': [],
	}
	assert expected_options == options


def test_imap_empty_folder_option(config):
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('imap')
	options = am._get_cfg_options(config, 'account4', option_spec)
	assert options['folders'] == []


def test_imap_old_folder_option(config):
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('imap')
	options = am._get_cfg_options(config, 'account5', option_spec)
	assert options['folders'] == ['folderA', 'folderB', 'folderC']


def test_imap_new_folder_option(config):
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('imap')
	options = am._get_cfg_options(config, 'account6', option_spec)
	assert options['folders'] == ['folderA', 'folderB', 'folderC']


def test_pop3_config_options(config):
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('pop3')
	options = am._get_cfg_options(config, 'account2', option_spec)
	expected_options = {
		'user': 'me',
		'password': 'poppoppop',
		'server': 'pop.example.org',
		'port': '',
		'ssl': True,
		'imap': False,
		'idle': False,
	}
	assert expected_options == options


def test_pop3_config_defaults(config):
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('pop3')
	options = am._get_cfg_options(config, 'account3', option_spec)
	expected_options = {
		'user': '',
		'password': '',
		'server': '',
		'port': '',
		'ssl': True,
		'imap': False,
		'idle': False,
	}
	assert expected_options == options


def test_imap_config_values_should_be_stored():
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('imap')
	options = {
		'user': 'you',
		'password': '',
		'server': 'imap.example.org',
		'port': '',
		'ssl': True,
		'imap': True,
		'idle': True,
		'folders': ['a', 'b'],
	}
	config = RawConfigParser()
	config.add_section('account1')
	am._set_cfg_options(config, 'account1', options, option_spec)
	expected_config_items = [
		('user', 'you'),
		('password', ''),
		('server', 'imap.example.org'),
		('port', ''),
		('ssl', '1'),
		('imap', '1'),
		('idle', '1'),
		('folder', '["a", "b"]'),
	]
	assert set(expected_config_items) == set(config.items('account1'))


# Load from config

def get_account(accounts, name):
	"""Finds and returns account which has given name."""
	return next(account for account in accounts if account.name == name)


def test_load_from_config(config):
	am = AccountManager()
	am.load_from_cfg(config, enabled_only=False)
	accounts = am.to_list()
	assert len(accounts) == 6
	imap_account = get_account(am.to_list(), 'IMAP mailbox config')
	pop3_account = get_account(am.to_list(), 'POP3 mailbox config')
	assert imap_account.get_config()['password'] == 'drowssap'
	assert pop3_account.get_config()['password'] == 'poppoppop'


def test_load_from_config_with_credential_store(config):
	cs = FakeCredentialStore()
	cs.set('Mailnag password for imap://you@imap.example.org', 'verry seecret')
	cs.set('Mailnag password for pop://me@pop.example.org', 'seecret too')
	am = AccountManager(cs)
	am.load_from_cfg(config, enabled_only=False)
	accounts = am.to_list()
	assert len(accounts) == 6
	imap_account = get_account(am.to_list(), 'IMAP mailbox config')
	pop3_account = get_account(am.to_list(), 'POP3 mailbox config')
	assert imap_account.get_config()['password'] == 'verry seecret'
	assert pop3_account.get_config()['password'] == 'seecret too'


# Save to config

def test_save_zero_accounts_to_config(config):
	am = AccountManager()
	am.save_to_cfg(config)
	assert len(config.sections()) == 0


def test_save_all_accounts_to_config(config):
	am = AccountManager()
	am.load_from_cfg(config, enabled_only=False)
	am.save_to_cfg(config)
	assert len(config.sections()) == 6


def test_save_zero_accounts_to_config_with_credential_store(config):
	cs = FakeCredentialStore()
	am = AccountManager(cs)
	am.save_to_cfg(config)
	assert len(config.sections()) == 0
	assert cs.secrets == {}


def test_save_all_accounts_to_config_with_credential_store(config):
	cs = FakeCredentialStore()
	cs.set('Mailnag password for imap://you@imap.example.org', 'verry seecret')
	cs.set('Mailnag password for pop://me@pop.example.org', 'seecret too')
	am = AccountManager(cs)
	am.load_from_cfg(config, enabled_only=False)
	am.save_to_cfg(config)
	assert len(config.sections()) == 6
	assert cs.secrets == {
	    'Mailnag password for imap://@': '',
	    'Mailnag password for imap://you@imap.example.org': 'verry seecret',
	    'Mailnag password for pop://me@pop.example.org': 'seecret too'
	}


def test_save_removed_accounts_to_config_with_credential_store(config):
	cs = FakeCredentialStore()
	cs.set('Mailnag password for imap://you@imap.example.org', 'verry seecret')
	cs.set('Mailnag password for pop://me@pop.example.org', 'seecret too')
	am = AccountManager(cs)
	am.load_from_cfg(config, enabled_only=False)
	am.clear()
	am.save_to_cfg(config)
	assert len(config.sections()) == 0
	assert cs.secrets == {}