stages/email: make template tests less flaky

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2023-01-01 23:00:32 +01:00
parent ee615c2d22
commit 11b1eb4173
No known key found for this signature in database
2 changed files with 12 additions and 5 deletions

View File

@ -3,12 +3,12 @@ from dataclasses import asdict, is_dataclass
from enum import Enum
from typing import TYPE_CHECKING, Optional, TypedDict
from uuid import UUID
from rest_framework.request import Request
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.http import JsonResponse
from rest_framework.fields import CharField, ChoiceField, DictField
from rest_framework.request import Request
from authentik.core.api.utils import PassiveSerializer
from authentik.lib.utils.errors import exception_to_string

View File

@ -1,7 +1,8 @@
"""email tests"""
from os import chmod, unlink
from pathlib import Path
from tempfile import gettempdir, mkstemp
from shutil import rmtree
from tempfile import gettempdir, mkdtemp, mkstemp
from typing import Any
from django.conf import settings
@ -20,11 +21,17 @@ def get_templates_setting(temp_dir: str) -> dict[str, Any]:
class TestEmailStageTemplates(TestCase):
"""Email tests"""
def setUp(self) -> None:
self.dir = mkdtemp()
def tearDown(self) -> None:
rmtree(self.dir)
def test_custom_template(self):
"""Test with custom template"""
with self.settings(TEMPLATES=get_templates_setting(gettempdir())):
_, file = mkstemp(suffix=".html")
_, file2 = mkstemp(suffix=".html")
with self.settings(TEMPLATES=get_templates_setting(self.dir)):
_, file = mkstemp(suffix=".html", dir=self.dir)
_, file2 = mkstemp(suffix=".html", dir=self.dir)
chmod(file2, 0o000) # Remove all permissions so we can't read the file
choices = get_template_choices()
self.assertEqual(choices[-1][0], Path(file).name)