Coverage for imap_cli_check.py: 0%

24 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-06 17:03 +0800

1#!/usr/bin/env python 

2"""Test IMAP connection - run via: python manage.py shell < test_imap.py""" 

3import base64 

4import pytest 

5from modules.email.models import EmailAccount 

6from modules.email.imap_service import IMAPService 

7 

8 

9@pytest.mark.django_db 

10def test_imap_connections(): 

11 """Test IMAP connection for all configured email accounts.""" 

12 for account in EmailAccount.objects.all(): 

13 print(f"\nTesting: {account.email_address}") 

14 print(f"IMAP: {account.imap_host}:{account.imap_port}") 

15 

16 try: 

17 password = base64.b64decode(account.encrypted_password.encode()).decode() 

18 print(f"Decrypted password length: {len(password)}") 

19 print(f"Decrypted password (first 4 chars): {password[:4]}...") 

20 except Exception as e: 

21 print(f"Decrypt error: {e}") 

22 password = None 

23 

24 if password: 

25 imap = IMAPService( 

26 host=account.imap_host, 

27 port=account.imap_port, 

28 username=account.email_address, 

29 password=password, 

30 ssl=account.use_ssl 

31 ) 

32 if imap.connect(): 

33 print("IMAP connected!") 

34 folders = imap.list_folders() 

35 print(f"Found {len(folders)} folders") 

36 imap.disconnect() 

37 else: 

38 print("IMAP connection failed")