feat(testing): add email testing utilities

Changes:
- util/sendmail now populates email files in the 'test-emails' directory.
    - util/sendmail does this in a serialized fashion based off of
      the test suite and name retrieved from PYTEST_CURRENT_TEST
      in the format: `<test_suite>_<test_function>.n.txt` where n
      is increased by one every time sendmail is run.
- pytest conftest fixtures have been added for test email setup;
  it wipes out old emails for the particular test function being run.
- New aurweb.testing.email.Email class allows developers to test
  against emails stored by util/sendmail. Simple pass the serial
  you want to test against, starting at serial = 1; e.g. Email(serial).

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-22 15:00:22 -08:00
parent b72bd38f76
commit 9fb1fbe32c
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 162 additions and 1 deletions

View file

@ -1,2 +1,25 @@
#!/bin/bash
# Send email to temporary filesystem for tests.
dir='test-emails'
filename='email.txt'
if [ ! -z ${PYTEST_CURRENT_TEST+x} ]; then
filename="$(echo $PYTEST_CURRENT_TEST | cut -d ' ' -f 1 | sed -r 's/(\/|\.|,|:)/_/g')"
fi
mkdir -p "$dir"
path="${dir}/${filename}"
serial_file="${path}.serial"
if [ ! -f $serial_file ]; then
echo 0 > $serial_file
fi
# Increment and update $serial_file.
serial=$(($(cat $serial_file) + 1))
echo $serial > $serial_file
# Use the serial we're on to mark the email file.
# Emails have the format: PYTEST_CURRENT_TEST.s.txt
# where s is the current serial for PYTEST_CURRENT_TEST.
cat > "${path}.${serial}.txt"
exit 0