Coverage for backend / app / emails / routers / templates.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-17 21:34 +0000

1"""Router to get email templates""" 

2 

3from pathlib import Path 

4 

5from fastapi import APIRouter, Depends, HTTPException 

6from fastapi.responses import HTMLResponse 

7from fastapi.templating import Jinja2Templates 

8from starlette import status 

9 

10from app import models 

11from app.config import settings 

12from app.core import oauth2 

13from app.routers.utility import assert_admin 

14 

15email_template_router = APIRouter(prefix="/email-templates", tags=["email-templates"]) 

16 

17_templates = Jinja2Templates(directory=str(Path(__file__).parent.parent / "templates")) 

18 

19_SAMPLE_DATA: dict[str, dict] = { 

20 "email_confirmation": { 

21 "name": "Jane Smith", 

22 "confirmation_url": "https://example.com/verify-email/?token=abc123xyz", 

23 "token_expiry_min": settings.verification_token_expiration_minutes, 

24 }, 

25 "password_reset": { 

26 "name": "Jane Smith", 

27 "reset_url": "https://example.com/reset-password/?token=abc123xyz", 

28 "token_expiry_min": settings.password_reset_token_expiration_minutes, 

29 }, 

30 "email_change": { 

31 "name": "Jane Smith", 

32 "confirmation_url": "https://example.com/verify-new-email/?token=abc123xyz", 

33 "token_expiry_min": settings.email_change_token_expiration_minutes, 

34 }, 

35 "password_changed": { 

36 "change_date": "February 18, 2026 at 02:30 PM UTC", 

37 "support_email": settings.support_email, 

38 }, 

39 "email_changed": { 

40 "change_date": "February 18, 2026 at 02:30 PM UTC", 

41 "support_email": settings.support_email, 

42 "old_email": "jane.old@example.com", 

43 "new_email": "jane.new@example.com", 

44 }, 

45 "trial_end_reminder": { 

46 "upgrade_url": settings.frontend_url + "/settings/premium", 

47 "end_date": "March 1, 2026", 

48 "support_email": settings.support_email, 

49 }, 

50 "new_version": { 

51 "version": settings.app_version, 

52 "features": [ 

53 { 

54 "title": "Email Template Previewer", 

55 "description": "Admins can now preview all email templates directly in the app.", 

56 }, 

57 { 

58 "title": "Improved Job Alerts", 

59 "description": "Job alerts now support more filtering options for better matches.", 

60 }, 

61 { 

62 "title": "Dark Mode Enhancements", 

63 "description": "Refined colour palette for better readability in dark mode.", 

64 }, 

65 ], 

66 "app_url": settings.frontend_url, 

67 }, 

68} 

69 

70 

71@email_template_router.get("/preview/{template_name}", response_class=HTMLResponse) 

72def preview_email_template( 

73 template_name: str, 

74 current_user: models.User = Depends(oauth2.get_current_user), 

75) -> str: 

76 """Render an email template with sample data and return it as HTML. Admin only. 

77 :param template_name: The template name (without .html extension). 

78 :param current_user: The current authenticated admin user. 

79 :returns: Rendered HTML content of the email template.""" 

80 

81 assert_admin(current_user) 

82 

83 if template_name not in _SAMPLE_DATA: 

84 raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Template '{template_name}' not found") 

85 

86 template = _templates.env.get_template(f"{template_name}.html") 

87 return template.render(**_SAMPLE_DATA[template_name])