test_welcome_template_password_route.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """Creating a user sends the Welcome template with the login details added by
  2. Bambuddy, never a password placed by the template."""
  3. from unittest.mock import patch
  4. import pytest
  5. from httpx import AsyncClient
  6. from backend.app.models.notification_template import NotificationTemplate
  7. from backend.tests.integration.test_advanced_auth_api import _setup_admin, _setup_smtp_and_advanced_auth
  8. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  9. async def test_a_new_users_password_is_not_wrapped_in_the_templates_link(async_client: AsyncClient, db_session):
  10. token = await _setup_admin(async_client, "welcadmin", "AdminPass1!")
  11. headers = {"Authorization": f"Bearer {token}"}
  12. db_session.add(
  13. NotificationTemplate(
  14. event_type="user_created",
  15. name="Welcome Email",
  16. title_template="Welcome {password}",
  17. body_template="Activate: https://elsewhere.example/a?p={password}",
  18. is_default=False,
  19. )
  20. )
  21. await db_session.commit()
  22. with patch("backend.app.api.routes.users.send_email") as mock_send:
  23. await _setup_smtp_and_advanced_auth(async_client, token)
  24. created = await async_client.post(
  25. "/api/v1/users/",
  26. headers=headers,
  27. json={"username": "newbie", "email": "newbie@test.com", "role": "user"},
  28. )
  29. assert created.status_code == 201, created.text
  30. mock_send.assert_called_once()
  31. _smtp, to, subject, text_body, html_body = mock_send.call_args[0][:5]
  32. assert to == "newbie@test.com"
  33. assert subject == "Welcome "
  34. words, details = text_body.split("\n\n")
  35. assert words == "Activate: https://elsewhere.example/a?p="
  36. assert details.startswith("Username: newbie\nPassword: ")
  37. assert len(details.removeprefix("Username: newbie\nPassword: ")) >= 12
  38. assert "p=" + details.split("Password: ")[1][:4] not in html_body