manage aliases

This commit is contained in:
Emmanuel Garette 2022-10-01 19:12:00 +02:00
parent cacc4afc4d
commit 3316ae70d3
3 changed files with 77 additions and 42 deletions

View file

@ -136,7 +136,7 @@ user_attrs = homeDirectory=home
# %d - domain part in user@domain, empty if user there's no domain # %d - domain part in user@domain, empty if user there's no domain
#user_filter = (&(objectClass=posixAccount)(uid=%u)) #user_filter = (&(objectClass=posixAccount)(uid=%u))
#>GNUNUX #>GNUNUX
user_filter = (&(objectClass=inetOrgPerson)(mail=%u)) user_filter = (&(objectClass=inetOrgPerson)(mailLocalAddress=%u))
#<GNUNUX #<GNUNUX
# Password checking attributes: # Password checking attributes:

View file

@ -8,3 +8,5 @@ password: %%get_password(server_name='test', username=%%username, description="t
username_family: %%username_family username_family: %%username_family
password_family: %%get_password(server_name='test', username=%%username_family, description='test', type="cleartext", hide=%%hide_secret, temporary=True) password_family: %%get_password(server_name='test', username=%%username_family, description='test', type="cleartext", hide=%%hide_secret, temporary=True)
name_family: %%name_family name_family: %%name_family
smtp: %%get_ip(%%smtp_relay_address)
ext_username: 'test@example.net'

View file

@ -1,6 +1,7 @@
from yaml import load, SafeLoader from yaml import load, SafeLoader
from os import environ from os import environ
import pytest import pytest
import datetime
from imaplib2 import IMAP4_SSL from imaplib2 import IMAP4_SSL
from smtplib import SMTP, SMTPNotSupportedError, SMTPAuthenticationError from smtplib import SMTP, SMTPNotSupportedError, SMTPAuthenticationError
@ -10,17 +11,29 @@ from smtplib import SMTP, SMTPNotSupportedError, SMTPAuthenticationError
conf_file = f'{environ["MACHINE_TEST_DIR"]}/imap.yml' conf_file = f'{environ["MACHINE_TEST_DIR"]}/imap.yml'
with open(conf_file) as yaml: with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader) data = load(yaml, Loader=SafeLoader)
parameters = (('user', data['username'], [data['password']]), parameters = (
('family', data['username_family'], [data['password_family'], data['password_family'] + "2"]), (1, 5, 'user', data['username'], data['username'], data['username'], [data['password']]),
(2, 5, 'user', data['username'], data['username'], 'alias_' + data['username'], [data['password']]),
(1, 3, 'family', data['username_family'], data['username_family'], data['username_family'], [data['password_family'], data['password_family'] + "2"]),
(3, 5, 'user', data['username'], data['ext_username'], data['username'], [data['password']]),
(4, 5, 'user', data['username'], data['ext_username'], 'alias_' + data['username'], [data['password']]),
(2, 3, 'family', data['username_family'], data['ext_username'], data['username_family'], [data['password_family'], data['password_family'] + "2"]),
) )
def get_msg(username, msg='MESSAGE'): def get_msg(username, dest, msg='MESSAGE', with_date=True):
return f'From: {username}\r\nTo: {username}\r\n\r\nSubject: TEST\r\n{msg}\r\n' date = datetime.datetime.now()
ret = f'From: {username}\r\nTo: {dest}\r\n\r\nSubject: TEST\r\n{msg}\r\n'
if with_date:
date_str = date.strftime('%a, %d %b %Y %H:%M:%S +0200 (CEST)')
ret = f'Date: {date_str}\r\n{ret}'
return ret
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_imap_wrong_password(typ, username, passwords): def test_imap_wrong_password(idx, maxi, typ, login_username, username, dest, passwords):
if username == data['ext_username']:
return
imap = IMAP4_SSL(data['address']) imap = IMAP4_SSL(data['address'])
try: try:
imap.LOGIN(username, 'b') imap.LOGIN(username, 'b')
@ -30,9 +43,13 @@ def test_imap_wrong_password(typ, username, passwords):
raise Exception('wrong login !') raise Exception('wrong login !')
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_imap_migration(typ, username, passwords): def test_imap_migration(idx, maxi, typ, login_username, username, dest, passwords):
msg = get_msg(username, 'MIGRATION') if dest.startswith('alias_'):
return
if username == data['ext_username']:
return
msg = get_msg(username, dest, 'MIGRATION', False)
if 'FIRST_RUN' in environ: if 'FIRST_RUN' in environ:
smtp = SMTP(data['address'], '587') smtp = SMTP(data['address'], '587')
smtp.starttls() smtp.starttls()
@ -45,7 +62,7 @@ def test_imap_migration(typ, username, passwords):
error = err error = err
else: else:
raise error from error raise error from error
smtp.sendmail(username, username, msg) smtp.sendmail(username, dest, msg)
smtp.quit() smtp.quit()
imap = IMAP4_SSL(data['address']) imap = IMAP4_SSL(data['address'])
error = None error = None
@ -69,15 +86,19 @@ def test_imap_migration(typ, username, passwords):
imap.LOGOUT() imap.LOGOUT()
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_smtp_no_tls(typ, username, passwords): def test_smtp_no_tls(idx, maxi, typ, login_username, username, dest, passwords):
if username == data['ext_username']:
return
smtp = SMTP(data['address'], '587') smtp = SMTP(data['address'], '587')
with pytest.raises(SMTPNotSupportedError): with pytest.raises(SMTPNotSupportedError):
smtp.login(username, passwords[0]) smtp.login(username, passwords[0])
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_smtp_wrong_passwd(typ, username, passwords): def test_smtp_wrong_passwd(idx, maxi, typ, login_username, username, dest, passwords):
if username == data['ext_username']:
return
smtp = SMTP(data['address'], '587') smtp = SMTP(data['address'], '587')
smtp.starttls() smtp.starttls()
with pytest.raises(SMTPAuthenticationError): with pytest.raises(SMTPAuthenticationError):
@ -85,8 +106,10 @@ def test_smtp_wrong_passwd(typ, username, passwords):
smtp.quit() smtp.quit()
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_smtp_login(typ, username, passwords): def test_smtp_login(idx, maxi, typ, login_username, username, dest, passwords):
if username == data['ext_username']:
return
smtp = SMTP(data['address'], '587') smtp = SMTP(data['address'], '587')
smtp.starttls() smtp.starttls()
error = None error = None
@ -101,8 +124,11 @@ def test_smtp_login(typ, username, passwords):
smtp.quit() smtp.quit()
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_smtp_sendmail(typ, username, passwords): def test_smtp_sendmail(idx, maxi, typ, login_username, username, dest, passwords):
if username == data['ext_username']:
smtp = SMTP(data['smtp'], '25')
else:
smtp = SMTP(data['address'], '587') smtp = SMTP(data['address'], '587')
smtp.starttls() smtp.starttls()
error = None error = None
@ -114,17 +140,17 @@ def test_smtp_sendmail(typ, username, passwords):
error = err error = err
else: else:
raise error from error raise error from error
smtp.sendmail(username, username, get_msg(username)) smtp.sendmail(username, dest, get_msg(username, dest))
smtp.quit() smtp.quit()
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_imap_read_mail(typ, username, passwords): def test_imap_read_mail(idx, maxi, typ, login_username, username, dest, passwords):
imap = IMAP4_SSL(data['address']) imap = IMAP4_SSL(data['address'])
error = None error = None
for password in passwords: for password in passwords:
try: try:
imap.LOGIN(username, password) imap.LOGIN(login_username, password)
break break
except Exception as err: except Exception as err:
error = err error = err
@ -134,24 +160,31 @@ def test_imap_read_mail(typ, username, passwords):
typ, req = imap.SEARCH(None, 'ALL') typ, req = imap.SEARCH(None, 'ALL')
assert typ == 'OK' assert typ == 'OK'
assert len(req) == 1 assert len(req) == 1
msg = get_msg(username) msg = get_msg(username, dest, with_date=False)
msg_no = req[0].split() msg_no = req[0].split()
assert len(msg_no) == 2 assert len(msg_no) == maxi
for num in msg_no[1:]: num = msg_no[idx]
field = imap.FETCH(num, '(RFC822)') field = imap.FETCH(num, '(RFC822)')
assert field[0] == 'OK' assert field[0] == 'OK'
assert field[1][-2][-1].decode().endswith(msg) fdata = field[1][-2][-1].decode().split('\r\n')
if fdata[-2].startswith('--'):
fdata = fdata[:-2]
fdata = '\r\n'.join(fdata)
assert 'Undelivered' not in fdata
assert fdata.endswith(msg)
imap.CLOSE() imap.CLOSE()
imap.LOGOUT() imap.LOGOUT()
@pytest.mark.parametrize('typ, username, passwords', parameters) @pytest.mark.parametrize('idx, maxi, typ, login_username, username, dest, passwords', parameters)
def test_imap_delete_mail(typ, username, passwords): def test_imap_delete_mail(idx, maxi, typ, login_username, username, dest, passwords):
if username == data['ext_username']:
return
imap = IMAP4_SSL(data['address']) imap = IMAP4_SSL(data['address'])
error = None error = None
for password in passwords: for password in passwords:
try: try:
imap.LOGIN(username, password) imap.LOGIN(login_username, password)
break break
except Exception as err: except Exception as err:
error = err error = err