From 475c9e2fa233101e51dc11478d94b9d6be2fc867 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 19 Jun 2024 10:13:03 -0700 Subject: [PATCH] feat: add option to not verify ssl certs (#913) * feat: add option to not verify ssl certs * formatting * update _test_login function * update tests, sort imports --- .../mail_and_packages/__init__.py | 15 ++++++- .../mail_and_packages/config_flow.py | 11 +++-- custom_components/mail_and_packages/const.py | 1 + .../mail_and_packages/helpers.py | 28 ++++++++++--- .../mail_and_packages/strings.json | 6 ++- .../mail_and_packages/translations/ca.json | 6 ++- .../mail_and_packages/translations/de.json | 6 ++- .../mail_and_packages/translations/en.json | 6 ++- .../mail_and_packages/translations/es.json | 6 ++- .../translations/es_419.json | 6 ++- .../mail_and_packages/translations/fi.json | 6 ++- .../mail_and_packages/translations/fr.json | 6 ++- .../mail_and_packages/translations/hu.json | 6 ++- .../mail_and_packages/translations/it.json | 6 ++- .../mail_and_packages/translations/ko.json | 6 ++- .../mail_and_packages/translations/nl.json | 6 ++- .../mail_and_packages/translations/no.json | 6 ++- .../mail_and_packages/translations/pl.json | 6 ++- .../mail_and_packages/translations/pt.json | 6 ++- .../mail_and_packages/translations/pt_BR.json | 6 ++- .../mail_and_packages/translations/ru.json | 6 ++- .../mail_and_packages/translations/sk.json | 6 ++- .../mail_and_packages/translations/sl.json | 6 ++- .../mail_and_packages/translations/sv.json | 6 ++- .../translations/zh_Hant_HK.json | 6 ++- tests/conftest.py | 19 +++++---- tests/const.py | 13 +++++- tests/test_binary_sensor.py | 6 +-- tests/test_camera.py | 4 +- tests/test_config_flow.py | 42 +++++++++++++++++-- tests/test_diagnostics.py | 5 +-- tests/test_helpers.py | 2 +- tests/test_init.py | 3 +- 33 files changed, 200 insertions(+), 75 deletions(-) diff --git a/custom_components/mail_and_packages/__init__.py b/custom_components/mail_and_packages/__init__.py index b2f68c88..73895928 100644 --- a/custom_components/mail_and_packages/__init__.py +++ b/custom_components/mail_and_packages/__init__.py @@ -18,6 +18,7 @@ CONF_IMAP_TIMEOUT, CONF_PATH, CONF_SCAN_INTERVAL, + CONF_VERIFY_SSL, COORDINATOR, DEFAULT_AMAZON_DAYS, DEFAULT_AMAZON_FWDS, @@ -147,7 +148,7 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> Non async def async_migrate_entry(hass, config_entry): """Migrate an old config entry.""" version = config_entry.version - new_version = 5 + new_version = 6 # 1 -> 4: Migrate format if version == 1: @@ -208,6 +209,18 @@ async def async_migrate_entry(hass, config_entry): config_entry, data=updated_config, version=new_version ) + if version == 5: + _LOGGER.debug("Migrating from version %s", version) + updated_config = config_entry.data.copy() + + if CONF_VERIFY_SSL not in updated_config: + updated_config[CONF_VERIFY_SSL] = True + + if updated_config != config_entry.data: + hass.config_entries.async_update_entry( + config_entry, data=updated_config, version=new_version + ) + _LOGGER.debug("Migration complete to version %s", new_version) return True diff --git a/custom_components/mail_and_packages/config_flow.py b/custom_components/mail_and_packages/config_flow.py index 9290745d..c7f8836d 100644 --- a/custom_components/mail_and_packages/config_flow.py +++ b/custom_components/mail_and_packages/config_flow.py @@ -29,6 +29,7 @@ CONF_IMAP_TIMEOUT, CONF_PATH, CONF_SCAN_INTERVAL, + CONF_VERIFY_SSL, DEFAULT_ALLOW_EXTERNAL, DEFAULT_AMAZON_DAYS, DEFAULT_AMAZON_FWDS, @@ -123,9 +124,9 @@ async def _validate_user_input(user_input: dict) -> tuple: return errors, user_input -def _get_mailboxes(host: str, port: int, user: str, pwd: str) -> list: +def _get_mailboxes(host: str, port: int, user: str, pwd: str, verify: bool) -> list: """Get list of mailbox folders from mail server.""" - account = login(host, port, user, pwd) + account = login(host, port, user, pwd, verify) status, folderlist = account.list() mailboxes = [] @@ -163,6 +164,7 @@ def _get_default(key: str, fallback_default: Any = None) -> None: vol.Required(CONF_PORT, default=_get_default(CONF_PORT, 993)): cv.port, vol.Required(CONF_USERNAME, default=_get_default(CONF_USERNAME)): cv.string, vol.Required(CONF_PASSWORD, default=_get_default(CONF_PASSWORD)): cv.string, + vol.Required(CONF_VERIFY_SSL, default=_get_default(CONF_VERIFY_SSL)): bool, } ) @@ -184,6 +186,7 @@ def _get_default(key: str, fallback_default: Any = None) -> None: data[CONF_PORT], data[CONF_USERNAME], data[CONF_PASSWORD], + data[CONF_VERIFY_SSL], ) ), vol.Required( @@ -238,7 +241,7 @@ def _get_default(key: str, fallback_default: Any = None) -> None: class MailAndPackagesFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Config flow for Mail and Packages.""" - VERSION = 5 + VERSION = 6 CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL def __init__(self): @@ -257,6 +260,7 @@ async def async_step_user(self, user_input=None): user_input[CONF_PORT], user_input[CONF_USERNAME], user_input[CONF_PASSWORD], + user_input[CONF_VERIFY_SSL], ) if not valid: self._errors["base"] = "communication" @@ -372,6 +376,7 @@ async def async_step_init(self, user_input=None): user_input[CONF_PORT], user_input[CONF_USERNAME], user_input[CONF_PASSWORD], + user_input[CONF_VERIFY_SSL], ) if not valid: self._errors["base"] = "communication" diff --git a/custom_components/mail_and_packages/const.py b/custom_components/mail_and_packages/const.py index 0e2ccc30..554c419a 100644 --- a/custom_components/mail_and_packages/const.py +++ b/custom_components/mail_and_packages/const.py @@ -54,6 +54,7 @@ CONF_GENERATE_MP4 = "generate_mp4" CONF_AMAZON_FWDS = "amazon_fwds" CONF_AMAZON_DAYS = "amazon_days" +CONF_VERIFY_SSL = "verify_ssl" # Defaults DEFAULT_CAMERA_NAME = "Mail USPS Camera" diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index e80925ec..b577ae3f 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -11,11 +11,13 @@ import os import quopri import re +import ssl import subprocess # nosec import uuid from datetime import timezone from email.header import decode_header from shutil import copyfile, copytree, which +from ssl import Purpose from typing import Any, List, Optional, Type, Union import aiohttp @@ -75,6 +77,7 @@ CONF_FOLDER, CONF_GENERATE_MP4, CONF_PATH, + CONF_VERIFY_SSL, DEFAULT_AMAZON_DAYS, OVERLAY, SENSOR_DATA, @@ -107,14 +110,20 @@ async def _check_ffmpeg() -> bool: return which("ffmpeg") -async def _test_login(host: str, port: int, user: str, pwd: str) -> bool: +async def _test_login(host: str, port: int, user: str, pwd: str, verify: bool) -> bool: """Test IMAP login to specified server. Returns success boolean """ - # Attempt to catch invalid mail server hosts + context = ssl.create_default_context() + if not verify: + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE + else: + context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH) + # Catch invalid mail server / host names try: - account = imaplib.IMAP4_SSL(host, port) + account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context) except Exception as err: _LOGGER.error("Error connecting into IMAP Server: %s", str(err)) return False @@ -152,12 +161,13 @@ async def process_emails(hass: HomeAssistant, config: ConfigEntry) -> dict: pwd = config.get(CONF_PASSWORD) folder = config.get(CONF_FOLDER) resources = config.get(CONF_RESOURCES) + verify_ssl = config.get(CONF_VERIFY_SSL) # Create the dict container data = {} # Login to email server and select the folder - account = login(host, port, user, pwd) + account = login(host, port, user, pwd, verify_ssl) # Do not process if account returns false if not account: @@ -426,15 +436,21 @@ async def fetch( def login( - host: str, port: int, user: str, pwd: str + host: str, port: int, user: str, pwd: str, verify: bool = True ) -> Union[bool, Type[imaplib.IMAP4_SSL]]: """Login to IMAP server. Returns account object """ + context = ssl.create_default_context() + if not verify: + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE + else: + context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH) # Catch invalid mail server / host names try: - account = imaplib.IMAP4_SSL(host, port) + account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context) except Exception as err: _LOGGER.error("Network error while connecting to server: %s", str(err)) diff --git a/custom_components/mail_and_packages/strings.json b/custom_components/mail_and_packages/strings.json index 1f4b53f9..06bd9261 100644 --- a/custom_components/mail_and_packages/strings.json +++ b/custom_components/mail_and_packages/strings.json @@ -18,7 +18,8 @@ "host": "Host", "password": "Password", "port": "Port", - "username": "Username" + "username": "Username", + "verify_ssl": "Verify SSL Cert" }, "description": "Please enter the connection information of your mail server.", "title": "Mail and Packages (Step 1 of 2)" @@ -66,7 +67,8 @@ "host": "Host", "password": "Password", "port": "Port", - "username": "Username" + "username": "Username", + "verify_ssl": "Verify SSL Cert" }, "description": "Please enter the connection information of your mail server.", "title": "Mail and Packages (Step 1 of 2)" diff --git a/custom_components/mail_and_packages/translations/ca.json b/custom_components/mail_and_packages/translations/ca.json index b3d9264c..bbf572d3 100644 --- a/custom_components/mail_and_packages/translations/ca.json +++ b/custom_components/mail_and_packages/translations/ca.json @@ -18,7 +18,8 @@ "host": "Amfitrió", "password": "Contrasenya", "port": "Port", - "username": "Nom d'usuari" + "username": "Nom d'usuari", + "verify_ssl": "Verify SSL Cert" }, "description": "Introduïu la informació de connexió del vostre servidor de correu.", "title": "Correu i paquets (pas 1 de 2)" @@ -67,7 +68,8 @@ "host": "Amfitrió", "password": "Contrasenya", "port": "Port", - "username": "Nom d'usuari" + "username": "Nom d'usuari", + "verify_ssl": "Verify SSL Cert" }, "description": "Introduïu la informació de connexió del vostre servidor de correu.", "title": "Correu i paquets (pas 1 de 2)" diff --git a/custom_components/mail_and_packages/translations/de.json b/custom_components/mail_and_packages/translations/de.json index 8049cdc3..e138d530 100644 --- a/custom_components/mail_and_packages/translations/de.json +++ b/custom_components/mail_and_packages/translations/de.json @@ -18,7 +18,8 @@ "host": "Host", "password": "Passwort", "port": "Port", - "username": "Nutzername" + "username": "Nutzername", + "verify_ssl": "Verify SSL Cert" }, "description": "Bitte geben Sie die Verbindungsinformationen Ihres Mailservers ein.", "title": "Briefe und Pakete (Schritt 1 von 2)" @@ -67,7 +68,8 @@ "host": "Host", "password": "Passwort", "port": "Port", - "username": "Nutzername" + "username": "Nutzername", + "verify_ssl": "Verify SSL Cert" }, "description": "Bitte geben Sie die Verbindungsinformationen Ihres Mailservers ein.", "title": "Briefe und Pakete (Schritt 1 von 2)" diff --git a/custom_components/mail_and_packages/translations/en.json b/custom_components/mail_and_packages/translations/en.json index 8e6ccf7e..6d66c059 100644 --- a/custom_components/mail_and_packages/translations/en.json +++ b/custom_components/mail_and_packages/translations/en.json @@ -18,7 +18,8 @@ "host": "Host", "password": "Password", "port": "Port", - "username": "Username" + "username": "Username", + "verify_ssl": "Verify SSL Cert" }, "description": "Please enter the connection information of your mail server.", "title": "Mail and Packages (Step 1 of 2)" @@ -67,7 +68,8 @@ "host": "Host", "password": "Password", "port": "Port", - "username": "Username" + "username": "Username", + "verify_ssl": "Verify SSL Cert" }, "description": "Please enter the connection information of your mail server.", "title": "Mail and Packages (Step 1 of 2)" diff --git a/custom_components/mail_and_packages/translations/es.json b/custom_components/mail_and_packages/translations/es.json index 70f237fd..d46bddcc 100644 --- a/custom_components/mail_and_packages/translations/es.json +++ b/custom_components/mail_and_packages/translations/es.json @@ -18,7 +18,8 @@ "host": "Anfitrión", "password": "Contraseña", "port": "Puerto", - "username": "Nombre de usuario" + "username": "Nombre de usuario", + "verify_ssl": "Verify SSL Cert" }, "description": "Ingrese la información de conexión de su servidor de correo.", "title": "Correo y paquetes (Paso 1 de 2)" @@ -67,7 +68,8 @@ "host": "Anfitrión", "password": "Contraseña", "port": "Puerto", - "username": "Nombre de usuario" + "username": "Nombre de usuario", + "verify_ssl": "Verify SSL Cert" }, "description": "Ingrese la información de conexión de su servidor de correo.", "title": "Correo y paquetes (Paso 1 de 2)" diff --git a/custom_components/mail_and_packages/translations/es_419.json b/custom_components/mail_and_packages/translations/es_419.json index 4165d6dc..8224a721 100644 --- a/custom_components/mail_and_packages/translations/es_419.json +++ b/custom_components/mail_and_packages/translations/es_419.json @@ -18,7 +18,8 @@ "host": "Anfitrión", "password": "Contraseña", "port": "Puerto", - "username": "Nombre de usuario" + "username": "Nombre de usuario", + "verify_ssl": "Verify SSL Cert" }, "description": "Ingrese la información de conexión de su servidor de correo.", "title": "Correo y paquetes (Paso 1 de 2)" @@ -67,7 +68,8 @@ "host": "Anfitrión", "password": "Contraseña", "port": "Puerto", - "username": "Nombre de usuario" + "username": "Nombre de usuario", + "verify_ssl": "Verify SSL Cert" }, "description": "Ingrese la información de conexión de su servidor de correo.", "title": "Correo y paquetes (Paso 1 de 2)" diff --git a/custom_components/mail_and_packages/translations/fi.json b/custom_components/mail_and_packages/translations/fi.json index 7c1159ac..db5bff5b 100644 --- a/custom_components/mail_and_packages/translations/fi.json +++ b/custom_components/mail_and_packages/translations/fi.json @@ -18,7 +18,8 @@ "host": "isäntä", "password": "Salasana", "port": "portti", - "username": "Käyttäjätunnus" + "username": "Käyttäjätunnus", + "verify_ssl": "Verify SSL Cert" }, "description": "Anna sähköpostipalvelimesi yhteydetiedot.", "title": "Posti ja paketit (vaihe 1/2)" @@ -67,7 +68,8 @@ "host": "isäntä", "password": "Salasana", "port": "portti", - "username": "Käyttäjätunnus" + "username": "Käyttäjätunnus", + "verify_ssl": "Verify SSL Cert" }, "description": "Anna sähköpostipalvelimesi yhteydetiedot.", "title": "Posti ja paketit (vaihe 1/2)" diff --git a/custom_components/mail_and_packages/translations/fr.json b/custom_components/mail_and_packages/translations/fr.json index 704c8aaf..e760829a 100644 --- a/custom_components/mail_and_packages/translations/fr.json +++ b/custom_components/mail_and_packages/translations/fr.json @@ -18,7 +18,8 @@ "host": "Hôte", "password": "Mot de passe", "port": "Port", - "username": "Nom d'utilisateur" + "username": "Nom d'utilisateur", + "verify_ssl": "Verify SSL Cert" }, "description": "Veuillez saisir les informations de connexion de votre serveur de messagerie.", "title": "Courrier et colis (étape 1 sur 2)" @@ -67,7 +68,8 @@ "host": "Hôte", "password": "Mot de passe", "port": "Port", - "username": "Nom d'utilisateur" + "username": "Nom d'utilisateur", + "verify_ssl": "Verify SSL Cert" }, "description": "Veuillez saisir les informations de connexion de votre serveur de messagerie.", "title": "Courrier et colis (étape 1 sur 2)" diff --git a/custom_components/mail_and_packages/translations/hu.json b/custom_components/mail_and_packages/translations/hu.json index a789dc68..5ef1cbf4 100644 --- a/custom_components/mail_and_packages/translations/hu.json +++ b/custom_components/mail_and_packages/translations/hu.json @@ -18,7 +18,8 @@ "host": "Házigazda", "password": "Jelszó", "port": "Kikötő", - "username": "Felhasználónév" + "username": "Felhasználónév", + "verify_ssl": "Verify SSL Cert" }, "description": "Kérjük, adja meg a levelezőszerver csatlakozási adatait.", "title": "Levél és csomagok (1. lépés a 2-ből)" @@ -67,7 +68,8 @@ "host": "Házigazda", "password": "Jelszó", "port": "Kikötő", - "username": "Felhasználónév" + "username": "Felhasználónév", + "verify_ssl": "Verify SSL Cert" }, "description": "Kérjük, adja meg a levelezőszerver csatlakozási adatait.", "title": "Levél és csomagok (1. lépés a 2-ből)" diff --git a/custom_components/mail_and_packages/translations/it.json b/custom_components/mail_and_packages/translations/it.json index 84db77df..df4c6366 100644 --- a/custom_components/mail_and_packages/translations/it.json +++ b/custom_components/mail_and_packages/translations/it.json @@ -18,7 +18,8 @@ "host": "Ospite", "password": "Parola d'ordine", "port": "Porta", - "username": "Nome utente" + "username": "Nome utente", + "verify_ssl": "Verify SSL Cert" }, "description": "Inserisci le informazioni di connessione del tuo server di posta.", "title": "Posta e pacchi (passaggio 1 di 2)" @@ -67,7 +68,8 @@ "host": "Ospite", "password": "Parola d'ordine", "port": "Porta", - "username": "Nome utente" + "username": "Nome utente", + "verify_ssl": "Verify SSL Cert" }, "description": "Inserisci le informazioni di connessione del tuo server di posta.", "title": "Posta e pacchi (passaggio 1 di 2)" diff --git a/custom_components/mail_and_packages/translations/ko.json b/custom_components/mail_and_packages/translations/ko.json index 8122a348..2d5a542e 100644 --- a/custom_components/mail_and_packages/translations/ko.json +++ b/custom_components/mail_and_packages/translations/ko.json @@ -18,7 +18,8 @@ "host": "주최자", "password": "암호", "port": "포트", - "username": "사용자 이름" + "username": "사용자 이름", + "verify_ssl": "Verify SSL Cert" }, "description": "메일 서버의 연결 정보를 입력하십시오.", "title": "메일 및 패키지 (1/2 단계)" @@ -67,7 +68,8 @@ "host": "주최자", "password": "암호", "port": "포트", - "username": "사용자 이름" + "username": "사용자 이름", + "verify_ssl": "Verify SSL Cert" }, "description": "메일 서버의 연결 정보를 입력하십시오.", "title": "메일 및 패키지 (1/2 단계)" diff --git a/custom_components/mail_and_packages/translations/nl.json b/custom_components/mail_and_packages/translations/nl.json index 301a8f53..4bb1880f 100644 --- a/custom_components/mail_and_packages/translations/nl.json +++ b/custom_components/mail_and_packages/translations/nl.json @@ -18,7 +18,8 @@ "host": "Gastheer", "password": "Wachtwoord", "port": "Haven", - "username": "Gebruikersnaam" + "username": "Gebruikersnaam", + "verify_ssl": "Verify SSL Cert" }, "description": "Voer de verbindingsgegevens van uw mailserver in.", "title": "Mail en pakketten (stap 1 van 2)" @@ -67,7 +68,8 @@ "host": "Gastheer", "password": "Wachtwoord", "port": "Haven", - "username": "Gebruikersnaam" + "username": "Gebruikersnaam", + "verify_ssl": "Verify SSL Cert" }, "description": "Voer de verbindingsgegevens van uw mailserver in.", "title": "Mail en pakketten (stap 1 van 2)" diff --git a/custom_components/mail_and_packages/translations/no.json b/custom_components/mail_and_packages/translations/no.json index 61a5e8a7..e4354bce 100644 --- a/custom_components/mail_and_packages/translations/no.json +++ b/custom_components/mail_and_packages/translations/no.json @@ -18,7 +18,8 @@ "host": "Vert", "password": "Passord", "port": "Havn", - "username": "Brukernavn" + "username": "Brukernavn", + "verify_ssl": "Verify SSL Cert" }, "description": "Vennligst skriv inn tilkoblingsinformasjonen til din postserver.", "title": "E-post og pakker (trinn 1 av 2)" @@ -67,7 +68,8 @@ "host": "Vert", "password": "Passord", "port": "Havn", - "username": "Brukernavn" + "username": "Brukernavn", + "verify_ssl": "Verify SSL Cert" }, "description": "Vennligst skriv inn tilkoblingsinformasjonen til din postserver.", "title": "E-post og pakker (trinn 1 av 2)" diff --git a/custom_components/mail_and_packages/translations/pl.json b/custom_components/mail_and_packages/translations/pl.json index 8f5be91e..92476ce0 100644 --- a/custom_components/mail_and_packages/translations/pl.json +++ b/custom_components/mail_and_packages/translations/pl.json @@ -18,7 +18,8 @@ "host": "Gospodarz", "password": "Hasło", "port": "Port", - "username": "Nazwa Użytkownika" + "username": "Nazwa Użytkownika", + "verify_ssl": "Verify SSL Cert" }, "description": "Podaj informacje o połączeniu swojego serwera pocztowego.", "title": "Poczta i paczki (krok 1 z 2)" @@ -67,7 +68,8 @@ "host": "Gospodarz", "password": "Hasło", "port": "Port", - "username": "Nazwa Użytkownika" + "username": "Nazwa Użytkownika", + "verify_ssl": "Verify SSL Cert" }, "description": "Podaj informacje o połączeniu swojego serwera pocztowego.", "title": "Poczta i paczki (krok 1 z 2)" diff --git a/custom_components/mail_and_packages/translations/pt.json b/custom_components/mail_and_packages/translations/pt.json index df23e64a..454c757a 100644 --- a/custom_components/mail_and_packages/translations/pt.json +++ b/custom_components/mail_and_packages/translations/pt.json @@ -18,7 +18,8 @@ "host": "Hospedeiro", "password": "Senha", "port": "Porta", - "username": "Nome do usuário" + "username": "Nome do usuário", + "verify_ssl": "Verify SSL Cert" }, "description": "Por favor, insira as informações de conexão do seu servidor de correio.", "title": "Correio e pacotes (Etapa 1 de 2)" @@ -67,7 +68,8 @@ "host": "Hospedeiro", "password": "Senha", "port": "Porta", - "username": "Nome do usuário" + "username": "Nome do usuário", + "verify_ssl": "Verify SSL Cert" }, "description": "Por favor, insira as informações de conexão do seu servidor de correio.", "title": "Correio e pacotes (Etapa 1 de 2)" diff --git a/custom_components/mail_and_packages/translations/pt_BR.json b/custom_components/mail_and_packages/translations/pt_BR.json index 7af4642c..27b4bfe8 100644 --- a/custom_components/mail_and_packages/translations/pt_BR.json +++ b/custom_components/mail_and_packages/translations/pt_BR.json @@ -18,7 +18,8 @@ "host": "Hospedeiro", "password": "Senha", "port": "Porta", - "username": "Nome do usuário" + "username": "Nome do usuário", + "verify_ssl": "Verify SSL Cert" }, "description": "Por favor, insira as informações de conexão do seu servidor de correio.", "title": "Correio e pacotes (Etapa 1 de 2)" @@ -67,7 +68,8 @@ "host": "Hospedeiro", "password": "Senha", "port": "Porta", - "username": "Nome do usuário" + "username": "Nome do usuário", + "verify_ssl": "Verify SSL Cert" }, "description": "Por favor, insira as informações de conexão do seu servidor de correio.", "title": "Correio e pacotes (Etapa 1 de 2)" diff --git a/custom_components/mail_and_packages/translations/ru.json b/custom_components/mail_and_packages/translations/ru.json index 409543d6..ff5ee93f 100644 --- a/custom_components/mail_and_packages/translations/ru.json +++ b/custom_components/mail_and_packages/translations/ru.json @@ -18,7 +18,8 @@ "host": "хозяин", "password": "пароль", "port": "порт", - "username": "имя пользователя" + "username": "имя пользователя", + "verify_ssl": "Verify SSL Cert" }, "description": "Пожалуйста, введите информацию о соединении вашего почтового сервера.", "title": "Почта и Пакеты (Шаг 1 из 2)" @@ -67,7 +68,8 @@ "host": "хозяин", "password": "пароль", "port": "порт", - "username": "имя пользователя" + "username": "имя пользователя", + "verify_ssl": "Verify SSL Cert" }, "description": "Пожалуйста, введите информацию о соединении вашего почтового сервера.", "title": "Почта и Пакеты (Шаг 1 из 2)" diff --git a/custom_components/mail_and_packages/translations/sk.json b/custom_components/mail_and_packages/translations/sk.json index da893e47..cc87b6ac 100644 --- a/custom_components/mail_and_packages/translations/sk.json +++ b/custom_components/mail_and_packages/translations/sk.json @@ -18,7 +18,8 @@ "host": "Host", "password": "Heslo", "port": "Port", - "username": "Užívateľské meno" + "username": "Užívateľské meno", + "verify_ssl": "Verify SSL Cert" }, "description": "Zadajte informácie o pripojení vášho poštového servera.", "title": "Pošta a balíky (1. krok z 2)" @@ -67,7 +68,8 @@ "host": "Host", "password": "Heslo", "port": "Port", - "username": "Užívateľské meno" + "username": "Užívateľské meno", + "verify_ssl": "Verify SSL Cert" }, "description": "Zadajte informácie o pripojení vášho poštového servera.", "title": "Pošta a balíky (1. krok z 2)" diff --git a/custom_components/mail_and_packages/translations/sl.json b/custom_components/mail_and_packages/translations/sl.json index 8efa4f62..0d203c0c 100644 --- a/custom_components/mail_and_packages/translations/sl.json +++ b/custom_components/mail_and_packages/translations/sl.json @@ -18,7 +18,8 @@ "host": "Gostitelj", "password": "Geslo", "port": "Pristanišče", - "username": "Uporabniško ime" + "username": "Uporabniško ime", + "verify_ssl": "Verify SSL Cert" }, "description": "Vnesite podatke o povezavi vašega poštnega strežnika.", "title": "Pošta in paketi (1. korak od 2)" @@ -67,7 +68,8 @@ "host": "Gostitelj", "password": "Geslo", "port": "Pristanišče", - "username": "Uporabniško ime" + "username": "Uporabniško ime", + "verify_ssl": "Verify SSL Cert" }, "description": "Vnesite podatke o povezavi vašega poštnega strežnika.", "title": "Pošta in paketi (1. korak od 2)" diff --git a/custom_components/mail_and_packages/translations/sv.json b/custom_components/mail_and_packages/translations/sv.json index ad3edeee..1c755461 100644 --- a/custom_components/mail_and_packages/translations/sv.json +++ b/custom_components/mail_and_packages/translations/sv.json @@ -18,7 +18,8 @@ "host": "Värd", "password": "Lösenord", "port": "Hamn", - "username": "Användarnamn" + "username": "Användarnamn", + "verify_ssl": "Verify SSL Cert" }, "description": "Ange anslutningsinformationen för din postserver.", "title": "Mail och paket (steg 1 av 2)" @@ -67,7 +68,8 @@ "host": "Värd", "password": "Lösenord", "port": "Hamn", - "username": "Användarnamn" + "username": "Användarnamn", + "verify_ssl": "Verify SSL Cert" }, "description": "Ange anslutningsinformationen för din postserver.", "title": "Mail och paket (steg 1 av 2)" diff --git a/custom_components/mail_and_packages/translations/zh_Hant_HK.json b/custom_components/mail_and_packages/translations/zh_Hant_HK.json index f180c569..ff49c4a0 100644 --- a/custom_components/mail_and_packages/translations/zh_Hant_HK.json +++ b/custom_components/mail_and_packages/translations/zh_Hant_HK.json @@ -18,7 +18,8 @@ "host": "主辦", "password": "密碼", "port": "港口", - "username": "用戶名" + "username": "用戶名", + "verify_ssl": "Verify SSL Cert" }, "description": "請輸入您的郵件服務器的連接信息。", "title": "郵件和包裹(第1步,共2步)" @@ -67,7 +68,8 @@ "host": "主辦", "password": "密碼", "port": "港口", - "username": "用戶名" + "username": "用戶名", + "verify_ssl": "Verify SSL Cert" }, "description": "請輸入您的郵件服務器的連接信息。", "title": "郵件和包裹(第1步,共2步)" diff --git a/tests/conftest.py b/tests/conftest.py index 1eae9153..ff821595 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,6 @@ """Fixtures for Mail and Packages tests.""" import asyncio -import aiohttp import datetime import errno import imaplib @@ -10,23 +9,23 @@ from unittest import mock from unittest.mock import patch +import aiohttp import pytest from aioresponses import aioresponses +from pytest_homeassistant_custom_component.common import MockConfigEntry +from custom_components.mail_and_packages.const import DOMAIN from tests.const import ( - FAKE_UPDATE_DATA, - FAKE_UPDATE_DATA_BIN, FAKE_CONFIG_DATA, - FAKE_CONFIG_DATA_CUSTOM_IMG, - FAKE_CONFIG_DATA_NO_PATH, - FAKE_CONFIG_DATA_MISSING_TIMEOUT, FAKE_CONFIG_DATA_AMAZON_FWD_STRING, + FAKE_CONFIG_DATA_CUSTOM_IMG, FAKE_CONFIG_DATA_EXTERNAL, + FAKE_CONFIG_DATA_MISSING_TIMEOUT, + FAKE_CONFIG_DATA_NO_PATH, FAKE_CONFIG_DATA_V4_MIGRATE, + FAKE_UPDATE_DATA, + FAKE_UPDATE_DATA_BIN, ) -from pytest_homeassistant_custom_component.common import MockConfigEntry - -from custom_components.mail_and_packages.const import DOMAIN pytest_plugins = "pytest_homeassistant_custom_component" pytestmark = pytest.mark.asyncio @@ -136,6 +135,7 @@ async def integration_fixture_6(hass): return entry + @pytest.fixture(name="integration_v4_migration") async def integration_fixture_7(hass): """Set up the mail_and_packages integration.""" @@ -150,6 +150,7 @@ async def integration_fixture_7(hass): return entry + @pytest.fixture() def mock_imap(): """Mock imap class values.""" diff --git a/tests/const.py b/tests/const.py index e76bff6d..0acbd0f6 100644 --- a/tests/const.py +++ b/tests/const.py @@ -117,6 +117,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_EXTERNAL = { @@ -178,6 +179,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_CORRECTED_EXTERNAL = { @@ -239,6 +241,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_CORRECTED = { @@ -306,6 +309,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_NO_PATH = { @@ -364,6 +368,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_NO_RND = { @@ -420,6 +425,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_MP4 = { @@ -474,6 +480,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_UPDATE_DATA = { @@ -581,6 +588,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_AMAZON_FWD_STRING = { @@ -641,6 +649,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_CONFIG_DATA_CUSTOM_IMG = { "allow_external": False, @@ -702,6 +711,7 @@ ], "scan_interval": 20, "username": "user@fake.email", + "verify_ssl": False, } FAKE_UPDATE_DATA_REDACTED = { @@ -861,4 +871,5 @@ ], "scan_interval": 20, "username": "user@fake.email", -} \ No newline at end of file + "verify_ssl": False, +} diff --git a/tests/test_binary_sensor.py b/tests/test_binary_sensor.py index 3fec1feb..24e4ba83 100644 --- a/tests/test_binary_sensor.py +++ b/tests/test_binary_sensor.py @@ -1,13 +1,13 @@ """Test Mail and Packages binary sensors.""" +from unittest.mock import patch + import pytest from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.mail_and_packages.const import DOMAIN from tests.const import FAKE_CONFIG_DATA -from unittest.mock import patch - @pytest.mark.asyncio async def test_binary_sensor_no_updates(hass, mock_imap_no_email): @@ -47,7 +47,7 @@ async def test_binary_sensor_no_updates(hass, mock_imap_no_email): # mock_hash_file.side_effect = hash_side_effect # assert await hass.config_entries.async_setup(entry.entry_id) # await hass.async_block_till_done() -# assert "mail_and_packages" in hass.config.components +# assert "mail_and_packages" in hass.config.components # state = hass.states.get("binary_sensor.usps_image_updated") # assert state diff --git a/tests/test_camera.py b/tests/test_camera.py index ab14fa08..6b3c6907 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -1,13 +1,13 @@ """Tests for camera component.""" +from unittest.mock import mock_open, patch + import pytest from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.mail_and_packages.const import CAMERA, DOMAIN from tests.const import FAKE_CONFIG_DATA, FAKE_CONFIG_DATA_CUSTOM_IMG -from unittest.mock import mock_open, patch - pytestmark = pytest.mark.asyncio diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index a340809c..f774402d 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -27,6 +27,7 @@ "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -116,6 +117,7 @@ "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -188,6 +190,7 @@ async def test_form( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -277,6 +280,7 @@ async def test_form( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -349,6 +353,7 @@ async def test_form_no_fwds( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -438,6 +443,7 @@ async def test_form_no_fwds( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -504,6 +510,7 @@ async def test_form_invalid_custom_img_path( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "user", ), @@ -549,6 +556,7 @@ async def test_form_connection_error(input_1, step_id_2, hass, mock_imap): "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -630,6 +638,7 @@ async def test_form_connection_error(input_1, step_id_2, hass, mock_imap): "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -683,6 +692,7 @@ async def test_form_invalid_ffmpeg( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -767,6 +777,7 @@ async def test_form_invalid_ffmpeg( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -829,6 +840,7 @@ async def test_form_index_error( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -913,6 +925,7 @@ async def test_form_index_error( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -975,6 +988,7 @@ async def test_form_index_error_2( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -1058,6 +1072,7 @@ async def test_form_index_error_2( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -1126,20 +1141,24 @@ async def test_invalid_ffmpeg(test_invalid_ffmpeg): @pytest.mark.asyncio async def test_imap_login(mock_imap): result = await _test_login( - "127.0.0.1", 993, "fakeuser@test.email", "suchfakemuchpassword" + "127.0.0.1", 993, "fakeuser@test.email", "suchfakemuchpassword", False ) assert result @pytest.mark.asyncio async def test_imap_connection_error(caplog): - await _test_login("127.0.0.1", 993, "fakeuser@test.email", "suchfakemuchpassword") + await _test_login( + "127.0.0.1", 993, "fakeuser@test.email", "suchfakemuchpassword", False + ) assert "Error connecting into IMAP Server:" in caplog.text @pytest.mark.asyncio async def test_imap_login_error(mock_imap_login_error, caplog): - await _test_login("127.0.0.1", 993, "fakeuser@test.email", "suchfakemuchpassword") + await _test_login( + "127.0.0.1", 993, "fakeuser@test.email", "suchfakemuchpassword", True + ) assert "Error logging into IMAP Server:" in caplog.text @@ -1152,6 +1171,7 @@ async def test_imap_login_error(mock_imap_login_error, caplog): "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "options_2", { @@ -1244,6 +1264,7 @@ async def test_imap_login_error(mock_imap_login_error, caplog): "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -1328,6 +1349,7 @@ async def test_options_flow( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "options_2", { @@ -1420,6 +1442,7 @@ async def test_options_flow( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -1500,6 +1523,7 @@ async def test_options_flow_invalid_custom_img_path( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "init", ), @@ -1557,6 +1581,7 @@ async def test_options_flow_connection_error( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "options_2", { @@ -1641,6 +1666,7 @@ async def test_options_flow_connection_error( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -1705,6 +1731,7 @@ async def test_options_flow_invalid_ffmpeg( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "options_2", { @@ -1789,6 +1816,7 @@ async def test_options_flow_invalid_ffmpeg( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -1854,6 +1882,7 @@ async def test_options_flow_index_error( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "options_2", { @@ -1938,6 +1967,7 @@ async def test_options_flow_index_error( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -2003,6 +2033,7 @@ async def test_options_flow_index_error_2( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "options_2", { @@ -2087,6 +2118,7 @@ async def test_options_flow_index_error_2( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -2152,6 +2184,7 @@ async def test_options_flow_mailbox_format2( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "options_2", { @@ -2238,6 +2271,7 @@ async def test_options_flow_mailbox_format2( "inpost_pl_delivering", "inpost_pl_packages", ], + "verify_ssl": False, }, ), ], @@ -2309,6 +2343,7 @@ async def test_options_flow_bad( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { @@ -2403,6 +2438,7 @@ async def test_form_amazon_error( "port": "993", "username": "test@test.email", "password": "notarealpassword", + "verify_ssl": False, }, "config_2", { diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py index 43bc8269..96819ece 100644 --- a/tests/test_diagnostics.py +++ b/tests/test_diagnostics.py @@ -3,15 +3,14 @@ from unittest.mock import patch import pytest +from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME +from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.mail_and_packages.const import DOMAIN from custom_components.mail_and_packages.diagnostics import ( async_get_config_entry_diagnostics, async_get_device_diagnostics, ) -from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME -from pytest_homeassistant_custom_component.common import MockConfigEntry - from tests.const import FAKE_CONFIG_DATA, FAKE_UPDATE_DATA, FAKE_UPDATE_DATA_REDACTED diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 54ff05c9..0958cf2f 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -2,12 +2,12 @@ import datetime import errno -from freezegun import freeze_time from datetime import date, timezone from unittest import mock from unittest.mock import call, mock_open, patch import pytest +from freezegun import freeze_time from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from pytest_homeassistant_custom_component.common import MockConfigEntry diff --git a/tests/test_init.py b/tests/test_init.py index 1c4d309e..5c7a9631 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -144,6 +144,7 @@ async def test_custom_img( entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 + @pytest.mark.asyncio async def test_v4_migration( hass, @@ -163,4 +164,4 @@ async def test_v4_migration( assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 42 entries = hass.config_entries.async_entries(DOMAIN) - assert len(entries) == 1 \ No newline at end of file + assert len(entries) == 1