Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(other): add mailpit for email testing (local and e2e) #949

Merged
merged 11 commits into from
Jul 2, 2024

Conversation

mahula
Copy link
Contributor

@mahula mahula commented May 30, 2024

🍰 Pullrequest

Mailpit is added to test Authentik e-mails locally and in end-to-end tests.

mailpit_inbox

mailpit_test_email

mailpit_html_check

How to test

cd authentik
# get hostname and write e-mail environment variables to .env
echo -e "\n\nAUTHENTIK_EMAIL__HOST=$(hostname)
AUTHENTIK_EMAIL__PORT=1025
AUTHENTIK_EMAIL__USE_TLS=false
AUTHENTIK_EMAIL__USE_SSL=false
AUTHENTIK_EMAIL__TIMEOUT=10
[email protected]" >> .env

# start authentik and mailserver
docker-compose -f docker-compose.yml -f docker-compose.test.yml up -d

# execute the email test
docker compose exec worker ak test_email [email protected]

# in the browser see the Mailpit inbox
<your_machine_name>:8025
# or
localhost:8025

Issues

Todo

  • None

@mahula mahula self-assigned this May 30, 2024
@mahula mahula linked an issue May 30, 2024 that may be closed by this pull request
2 tasks
@mahula mahula changed the title feat(other): add mailserver for local email testing feat(other): [WIP] add mailserver for local email testing May 30, 2024
@mahula mahula marked this pull request as draft May 30, 2024 16:39
@mahula mahula changed the title feat(other): [WIP] add mailserver for local email testing feat(other): add mailserver for local email testing Jul 2, 2024
@mahula mahula changed the title feat(other): add mailserver for local email testing feat(other): add mailserver for email testing (local and e2e) Jul 2, 2024
@mahula mahula marked this pull request as ready for review July 2, 2024 10:21
@roschaefer roschaefer changed the title feat(other): add mailserver for email testing (local and e2e) feat(other): add mailpit for email testing (local and e2e) Jul 2, 2024
Elweyn
Elweyn previously approved these changes Jul 2, 2024
@mahula mahula requested a review from roschaefer July 2, 2024 15:41
@roschaefer roschaefer force-pushed the 555-devops-maildev branch from 3f1d4cf to 1f172f3 Compare July 2, 2024 16:52
Copy link
Contributor

@roschaefer roschaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mail

I see errors when I follow the README 🙁

Comment on lines 61 to 66
echo -e "\n\nAUTHENTIK_EMAIL__HOST=$(hostname)
AUTHENTIK_EMAIL__PORT=1025
AUTHENTIK_EMAIL__USE_TLS=false
AUTHENTIK_EMAIL__USE_SSL=false
AUTHENTIK_EMAIL__TIMEOUT=10
[email protected]" >> .env
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authentic/.env is not .gitignored. So I would suggest to check this directly into .env.. Or specify it in docker-compose.test.yml.

```bash
# in the authentik directory
# get hostname and write e-mail environment variables to .env
echo -e "\n\nAUTHENTIK_EMAIL__HOST=$(hostname)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the motivation for the echo [...] >> was to call hostname I think it's fine to assume localhost or to add a hint to change the hostname.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The need to set the host machine's name instead of localhost is required since both Authentik and Mailpit run in separate Docker containers (see goauthentik/authentik#9921 (comment))


```bash
# in the authentik directory execute the email test
docker compose exec worker ak test_email [email protected]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get:

{"app_name": "authentik.enterprise.providers.microsoft_entra", "domain_url": null, "event": "Imported related module", "level": "info", "logger": "authentik.blueprints.apps", "module": "authentik.enterprise.providers.microsoft_entra.signals", "pid": 245, "schema_name": "public", "timestamp": "2024-07-02T17:02:24.870470"}
{"app_name": "authentik.enterprise.providers.rac", "domain_url": null, "event": "Imported related module", "level": "info", "logger": "authentik.blueprints.apps", "module": "authentik.enterprise.providers.rac.signals", "pid": 245, "schema_name": "public", "timestamp": "2024-07-02T17:02:24.874639"}
{"app_name": "authentik.events", "domain_url": null, "event": "Imported related module", "level": "info", "logger": "authentik.blueprints.apps", "module": "authentik.events.tasks", "pid": 245, "schema_name": "public", "timestamp": "2024-07-02T17:02:24.874890"}
{"app_name": "authentik.events", "domain_url": null, "event": "Imported related module", "level": "info", "logger": "authentik.blueprints.apps", "module": "authentik.events.signals", "pid": 245, "schema_name": "public", "timestamp": "2024-07-02T17:02:24.874966"}
Switching to schema 'public'
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/manage.py", line 52, in <module>
    execute_from_command_line(sys.argv)
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/tenants/management/__init__.py", line 38, in handle
    self.handle_per_tenant(*args, **options)
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/stages/email/management/commands/test_email.py", line 38, in handle_per_tenant
    send_mail(message.__dict__, stage.pk)
  File "/ak-root/venv/lib/python3.12/site-packages/celery/local.py", line 182, in __call__
    return self._get_current_object()(*a, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/celery/app/task.py", line 411, in __call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/celery/app/autoretry.py", line 60, in run
    ret = task.retry(exc=exc, **retry_kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/celery/app/task.py", line 720, in retry
    raise_with_context(exc or Retry('Task can be retried', None))
  File "/ak-root/venv/lib/python3.12/site-packages/celery/app/autoretry.py", line 38, in run
    return task._orig_run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/stages/email/tasks.py", line 105, in send_mail
    raise exc
  File "/authentik/stages/email/tasks.py", line 73, in send_mail
    backend.open()
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/mail/backends/smtp.py", line 86, in open
    self.connection = self.connection_class(
                      ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/socket.py", line 852, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.12/socket.py", line 837, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

Copy link
Contributor Author

@mahula mahula Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you use localhost or your machine's name for the environment variable?
There is a reason the host machine's name is required.

Comment on lines 80 to 83
# in the browser see the Mailpit inbox
<your_machine_name>:8025
# or
localhost:8025
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# in the browser see the Mailpit inbox
<your_machine_name>:8025
# or
localhost:8025
\`\`\`
See Mailpit Inbox at <http://localhost:8025>.

@@ -0,0 +1,8 @@
services:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about calling it docker-compose.override.yml? This can be used by default in development, no?

@roschaefer roschaefer force-pushed the 555-devops-maildev branch from 6bbe081 to f1cfabf Compare July 2, 2024 18:41
Copy link
Contributor

@roschaefer roschaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

email

@mahula mahula enabled auto-merge (squash) July 2, 2024 18:50
@mahula mahula merged commit 3002d71 into master Jul 2, 2024
76 checks passed
@mahula mahula deleted the 555-devops-maildev branch July 2, 2024 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

💥 [DevOps] Mailpit
3 participants