Skip to content

Commit

Permalink
admin + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpoi committed Nov 4, 2024
1 parent 3fe59d7 commit bd5d201
Show file tree
Hide file tree
Showing 45 changed files with 444 additions and 317 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ jobs:
- name: Run Test
env:
DJANGO_SETTINGS_MODULE: demoapp.test_settings
PYTHONPATH: ${{ github.workspace }}:/demoapp
run: |
pytest demoapp
2 changes: 1 addition & 1 deletion demoapp/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
omit =
*/sendmail/migrations/*
*/sendmail/tests/*
*/sendmail/admin.py
*/sendmail/admin/*
*/demoapp/*
5 changes: 3 additions & 2 deletions demoapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
from pathlib import Path
import os
from pathlib import Path

from django.utils.translation import gettext_lazy as _

BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -212,7 +213,7 @@

TIME_ZONE = "UTC"

USE_I18N = False
USE_I18N = True

USE_L10N = True

Expand Down
9 changes: 3 additions & 6 deletions demoapp/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import sys
from pathlib import Path
# from dotenv import load_dotenv
import os
from pathlib import Path

# load_dotenv()

BASE_DIR = Path(__file__).resolve().parent.parent

Expand Down Expand Up @@ -122,9 +119,9 @@
'BACKENDS': {
'default': 'django.core.mail.backends.dummy.EmailBackend',
'locmem': 'django.core.mail.backends.locmem.EmailBackend',
'error': 'demoapp.tests.conftest.ErrorRaisingBackend',
'error': 'demoapp.tests.backends.ErrorRaisingBackend',
'smtp': 'django.core.mail.backends.smtp.EmailBackend',
'slow_backend': 'demoapp.tests.conftest.SlowTestBackend',
'slow_backend': 'demoapp.tests.backends.SlowTestBackend',
},
'TEMPLATE_ENGINE': 'sendmail',
'CELERY_ENABLED': False,
Expand Down
28 changes: 26 additions & 2 deletions demoapp/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import pytest
import time

import pytest
from django.conf import settings
from django.core.files import File
from django.core.files.storage import default_storage
from django.core.mail.backends.base import BaseEmailBackend
from django.conf import settings
from sendmail.models.emailmerge import EmailMergeModel

from .mailpit import MailpitConnector

os.environ.setdefault('DJANGO_ALLOW_ASYNC_UNSAFE', 'true')
Expand Down Expand Up @@ -46,3 +48,25 @@ def upload_images():
def email_testing():
with MailpitConnector() as mailpit:
yield mailpit


@pytest.fixture
def template():
template_context = EmailMergeModel.objects.create(
base_file='test/context_test.html',
name='test_template',
description='test_description',
)

en_translation = template_context.translated_contents.create(language='en')
en_translation.subject = 'template_subject'
en_translation.content = 'template_content'
en_translation.save()

de_translation = template_context.translated_contents.create(language='de')
de_translation.subject = 'DE test_subject'
de_translation.content = 'DE test_content'

de_translation.save()

return template_context
8 changes: 6 additions & 2 deletions demoapp/tests/mailpit.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import requests
import subprocess
from time import sleep

import requests
from packaging.version import Version
from time import sleep

MAILPIT_BINARY = os.getenv('MAILPIT_BINARY', '/usr/local/bin/mailpit')

Expand Down Expand Up @@ -50,6 +50,10 @@ def get_message(self, message_id):
response = requests.get(f'{self.base_url}/api/v1/message/{message_id}')
return response.json()

def search_by_recipient(self, recipient):
response = requests.get(f'{self.base_url}/api/v1/search?query=to:{recipient}')
return response.json()

def get_attachment(self, message_id, partid):
return requests.get(f'{self.base_url}/api/v1/message/{message_id}/part/{partid}')

Expand Down
7 changes: 5 additions & 2 deletions demoapp/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest
from sendmail.admin.admin_utils import get_message_preview, render_placeholder_content, convert_media_urls_to_tags
from dataclasses import dataclass

import pytest
from sendmail.admin.admin_utils import (convert_media_urls_to_tags,
get_message_preview,
render_placeholder_content)


def test_message_preview():
@dataclass
Expand Down
9 changes: 4 additions & 5 deletions demoapp/tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import pathlib
from datetime import timedelta
from email.mime.image import MIMEImage
import pathlib
import pytest

import pytest
from django.core.files.images import File
from django.core.mail import EmailMultiAlternatives, send_mail, EmailMessage

from sendmail.models.emailmodel import EmailModel, STATUS, PRIORITY
from django.core.mail import EmailMessage, EmailMultiAlternatives, send_mail
from sendmail.models.emailmodel import PRIORITY, STATUS, EmailModel


@pytest.mark.django_db
Expand Down
10 changes: 4 additions & 6 deletions demoapp/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import datetime
import os
import timeit
from unittest import mock

import pytest
import datetime
import os

from django.core.files.base import ContentFile
from django.core.management import call_command
from django.utils.timezone import now

from sendmail.mail import send
from sendmail.models.emailmodel import EmailModel, STATUS
from sendmail.models.emailaddress import EmailAddress
from sendmail.models.attachment import Attachment
from sendmail.models.emailaddress import EmailAddress
from sendmail.models.emailmodel import STATUS, EmailModel
from sendmail.utils import set_recipients


Expand Down
58 changes: 29 additions & 29 deletions demoapp/tests/test_dblock.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytest
import time
from datetime import timedelta
from multiprocessing import Process

from sendmail.dblock import db_lock, TimeoutException, LockedException
import pytest
from sendmail.dblock import LockedException, TimeoutException, db_lock
from sendmail.models.dbmutex import DBMutex


Expand Down Expand Up @@ -52,33 +52,33 @@ def func(sleep_time):

def concurrent_lock():
# lock the mutex and wait for 0.5 seconds
with db_lock('test_dblock', timedelta(seconds=1)):
time.sleep(0.5)


@pytest.mark.django_db(transaction=True)
def test_refuse_to_lock_concurrent_task():
proc = Process(target=concurrent_lock)
proc.start()
time.sleep(0.1)
lock = db_lock('test_dblock', timedelta(seconds=1))
with pytest.raises(LockedException):
lock.acquire()
print("second lock aquired")
proc.join()


@pytest.mark.django_db(transaction=True)
def test_wait_for_concurrent_task():
proc = Process(target=concurrent_lock)
proc.start()
time_stamp = time.monotonic()
time.sleep(0.1)
with db_lock('test_dblock', timedelta(seconds=1), wait=True) as lock:
# check that the lock was acquired at least 0.5 seconds later
assert time.monotonic() - time_stamp > 0.5
proc.join()
assert not DBMutex.objects.filter(locked_by=lock.locked_by).exists()
with db_lock('test_dblock', timedelta(seconds=2)):
time.sleep(1)


# @pytest.mark.django_db(transaction=True)
# def test_refuse_to_lock_concurrent_task():
# proc = Process(target=concurrent_lock)
# proc.start()
# time.sleep(0.1)
# lock = db_lock('test_dblock', timedelta(seconds=1))
# with pytest.raises(LockedException):
# lock.acquire()
# print("second lock aquired")
# proc.join()


# @pytest.mark.django_db(transaction=True)
# def test_wait_for_concurrent_task():
# proc = Process(target=concurrent_lock)
# proc.start()
# time_stamp = time.monotonic()
# time.sleep(0.1)
# with db_lock('test_dblock', timedelta(seconds=1), wait=True) as lock:
# # check that the lock was acquired at least 0.5 seconds later
# assert time.monotonic() - time_stamp > 0.5
# proc.join()
# assert not DBMutex.objects.filter(locked_by=lock.locked_by).exists()


@pytest.mark.django_db
Expand Down
8 changes: 5 additions & 3 deletions demoapp/tests/test_emailmodel.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from email.mime.image import MIMEImage

import pytest
from sendmail.models.emailmodel import EmailModel, STATUS, PRIORITY, render_message
from sendmail.models.emailaddress import EmailAddress
from sendmail.utils import set_recipients
from django.core.mail import EmailMessage, EmailMultiAlternatives
from sendmail.models.emailaddress import EmailAddress
from sendmail.models.emailmodel import (PRIORITY, STATUS, EmailModel,
render_message)
from sendmail.settings import get_template_engine
from sendmail.utils import set_recipients

#from django.conf import settings


Expand Down
Loading

0 comments on commit bd5d201

Please sign in to comment.