import json
from pathlib import Path

from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.test import TestCase

from ocyan.core.fender import config


class SmokeTest(TestCase):
    def test_no_embedded_mail_password_default(self):
        payload = json.loads(
            Path(__file__).resolve().parents[4].joinpath("config.json").read_text()
        )
        email_password = next(
            item for item in payload["settings"] if item["id"] == "email_host_password"
        )
        self.assertEqual(email_password["default"], "")

    def test_mailto_setting_is_valid(self):
        mail = config.get("django", "email_to", "")
        if not mail:
            return
        try:
            if isinstance(mail, list):
                all(map(validate_email, mail))
            else:
                validate_email(mail)
        except ValidationError as e:
            self.fail(
                "Een van de emails geconfigureerd in email_to is geen valide mailadres: %s"
                % e
            )
