feat: Add email settings management and templates functionality
- Implemented email settings configuration in the admin panel, allowing for SMTP settings and notification preferences. - Created a new template for email settings with fields for SMTP host, port, username, password, sender address, and recipients. - Added JavaScript functionality to handle loading, saving, and validating email settings. - Introduced email templates management, enabling the listing, editing, and saving of email templates. - Updated navigation to include links to email settings and templates. - Added tests for email settings and templates to ensure proper functionality and validation.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
|
||||
server_app_module = importlib.import_module("server.app")
|
||||
@@ -11,33 +12,65 @@ def client():
|
||||
yield client
|
||||
|
||||
|
||||
def test_get_settings_returns_dict(client):
|
||||
# Login as admin first
|
||||
client.post('/auth/login', data={'username': 'admin', 'password': 'admin'})
|
||||
resp = client.get('/admin/api/settings')
|
||||
def login(client):
|
||||
return client.post('/auth/login', data={'username': 'admin', 'password': 'admin'})
|
||||
|
||||
|
||||
def test_email_template_list_requires_auth(client):
|
||||
resp = client.get('/admin/api/email-templates')
|
||||
assert resp.status_code == 302
|
||||
assert resp.headers['Location'] == '/auth/login'
|
||||
|
||||
|
||||
def test_list_email_templates_returns_metadata(client):
|
||||
login(client)
|
||||
resp = client.get('/admin/api/email-templates')
|
||||
assert resp.status_code == 200
|
||||
body = resp.get_json()
|
||||
assert body['status'] == 'ok'
|
||||
assert isinstance(body.get('settings'), dict)
|
||||
|
||||
payload = resp.get_json()
|
||||
assert payload['status'] == 'ok'
|
||||
assert isinstance(payload['templates'], list)
|
||||
assert payload['templates'][0]['id'] == 'newsletter_confirmation'
|
||||
|
||||
|
||||
def test_update_and_get_newsletter_template(client):
|
||||
key = 'newsletter_confirmation_template'
|
||||
sample = '<p>Thanks for subscribing, {{email}}</p>'
|
||||
|
||||
# Update via PUT
|
||||
# Login as admin first
|
||||
client.post('/auth/login', data={'username': 'admin', 'password': 'admin'})
|
||||
|
||||
resp = client.put(f'/admin/api/settings/{key}', json={'value': sample})
|
||||
def test_get_email_template_returns_content(client):
|
||||
login(client)
|
||||
resp = client.get('/admin/api/email-templates/newsletter_confirmation')
|
||||
assert resp.status_code == 200
|
||||
body = resp.get_json()
|
||||
assert body['status'] == 'ok'
|
||||
|
||||
# Retrieve via GET and ensure the value is present
|
||||
resp = client.get('/admin/api/settings')
|
||||
payload = resp.get_json()
|
||||
assert payload['status'] == 'ok'
|
||||
template = payload['template']
|
||||
assert template['id'] == 'newsletter_confirmation'
|
||||
assert 'content' in template
|
||||
|
||||
|
||||
def test_update_email_template_persists_content(client):
|
||||
login(client)
|
||||
new_content = '<p>Updated template {{email}}</p>'
|
||||
|
||||
resp = client.put(
|
||||
'/admin/api/email-templates/newsletter_confirmation',
|
||||
json={'content': new_content},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.get_json()
|
||||
assert body['status'] == 'ok'
|
||||
settings = body.get('settings') or {}
|
||||
assert settings.get(key) == sample
|
||||
payload = resp.get_json()
|
||||
assert payload['status'] == 'ok'
|
||||
assert payload['template']['content'] == new_content
|
||||
|
||||
# Fetch again to ensure persistence
|
||||
resp_get = client.get('/admin/api/email-templates/newsletter_confirmation')
|
||||
assert resp_get.status_code == 200
|
||||
template = resp_get.get_json()['template']
|
||||
assert template['content'] == new_content
|
||||
|
||||
|
||||
def test_update_email_template_requires_content(client):
|
||||
login(client)
|
||||
resp = client.put(
|
||||
'/admin/api/email-templates/newsletter_confirmation',
|
||||
json={'content': ' '},
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
payload = resp.get_json()
|
||||
assert payload['status'] == 'error'
|
||||
|
||||
Reference in New Issue
Block a user