Rafał Pitoń 6 лет назад
Родитель
Сommit
4862b1192d

+ 5 - 4
misago/core/mail.py

@@ -2,17 +2,20 @@ from django.core import mail as djmail
 from django.template.loader import render_to_string
 from django.utils.translation import get_language
 
-from misago.conf import settings
+from misago.conf import db_settings, settings
+
+from .utils import get_host_from_address
 
 
 def build_mail(recipient, subject, template, sender=None, context=None):
     context = context.copy() if context else {}
     context.update({
         'SITE_ADDRESS': settings.MISAGO_ADDRESS,
+        'SITE_HOST': get_host_from_address(settings.MISAGO_ADDRESS),
         'LANGUAGE_CODE': get_language()[:2],
         'LOGIN_URL': settings.LOGIN_URL,
 
-        'misago_settings': settings,
+        'misago_settings': db_settings,
 
         'user': recipient,
         'sender': sender,
@@ -28,13 +31,11 @@ def build_mail(recipient, subject, template, sender=None, context=None):
     return message
 
 
-# fixme: rename this function to email_user
 def mail_user(recipient, subject, template, sender=None, context=None):
     message = build_mail(recipient, subject, template, sender, context)
     message.send()
 
 
-# fixme: rename this function to email_users
 def mail_users(recipients, subject, template, sender=None, context=None):
     messages = []
 

+ 44 - 0
misago/core/tests/test_mail.py

@@ -0,0 +1,44 @@
+from django.contrib.auth import get_user_model
+from django.core import mail
+from django.test import TestCase
+from django.urls import reverse
+
+from misago.core.mail import mail_user, mail_users
+
+
+UserModel = get_user_model()
+
+
+class MailTests(TestCase):
+    def test_mail_user(self):
+        """mail_user sets message in backend"""
+        user = UserModel.objects.create_user('Bob', 'bob@bob.com', 'pass123')
+
+        mail_user(user, "Misago Test Mail", "misago/emails/base")
+
+        self.assertEqual(mail.outbox[0].subject, "Misago Test Mail")
+
+        # assert that url to user's avatar is valid
+        html_body = mail.outbox[0].alternatives[0][0]
+        user_avatar_url = reverse('misago:user-avatar', kwargs={'pk': user.pk, 'size': 32})
+
+        self.assertIn(user_avatar_url, html_body)
+
+    def test_mail_users(self):
+        """mail_users sets messages in backend"""
+        test_users = [
+            UserModel.objects.create_user('Alpha', 'alpha@test.com', 'pass123'),
+            UserModel.objects.create_user('Beta', 'beta@test.com', 'pass123'),
+            UserModel.objects.create_user('Niner', 'niner@test.com', 'pass123'),
+            UserModel.objects.create_user('Foxtrot', 'foxtrot@test.com', 'pass123'),
+            UserModel.objects.create_user('Uniform', 'uniform@test.com', 'pass123'),
+        ]
+
+        mail_users(test_users, "Misago Test Spam", "misago/emails/base")
+
+        spams_sent = 0
+        for message in mail.outbox:
+            if message.subject == 'Misago Test Spam':
+                spams_sent += 1
+
+        self.assertEqual(spams_sent, len(test_users))

+ 33 - 1
misago/core/tests/test_utils.py

@@ -9,7 +9,7 @@ from django.utils import six
 
 from misago.core.utils import (
     clean_return_path, format_plaintext_for_html, is_referer_local, is_request_to_misago,
-    parse_iso8601_string, slugify, get_exception_message, clean_ids_list)
+    parse_iso8601_string, slugify, get_exception_message, clean_ids_list, get_host_from_address)
 
 
 class IsRequestToMisagoTests(TestCase):
@@ -298,3 +298,35 @@ class CleanIdsListTests(TestCase):
         for invalid_input in INVALID_INPUTS:
             with self.assertRaisesMessage(PermissionDenied, "Test error message!"):
                 clean_ids_list(invalid_input, "Test error message!")
+
+
+class GetHostFromAddressTests(TestCase):
+    def test_none(self):
+        """get_host_from_address returns None for None"""
+        result = get_host_from_address(None)
+        self.assertIsNone(result)
+
+    def test_empty_string(self):
+        """get_host_from_address returns None for empty string"""
+        result = get_host_from_address('')
+        self.assertIsNone(result)
+
+    def test_hostname(self):
+        """get_host_from_address returns hostname unchanged"""
+        result = get_host_from_address('hostname')
+        self.assertEqual(result, 'hostname')
+        
+    def test_hostname_with_trailing_slash(self):
+        """get_host_from_address returns hostname for hostname with trailing slash"""
+        result = get_host_from_address('//hostname')
+        self.assertEqual(result, 'hostname')
+
+    def test_hostname_with_port(self):
+        """get_host_from_address returns hostname for hostname with port"""
+        result = get_host_from_address('hostname:8888')
+        self.assertEqual(result, 'hostname')
+        
+    def test_hostname_with_port_path_and_protocol(self):
+        """get_host_from_address returns hostname for hostname with port and path"""
+        result = get_host_from_address('https://hostname:8888/somewhere/else/')
+        self.assertEqual(result, 'hostname')

+ 19 - 0
misago/core/utils.py

@@ -154,3 +154,22 @@ def clean_ids_list(ids_list, error_message):
         return list(map(int, ids_list))
     except (ValueError, TypeError):
         raise PermissionDenied(error_message)
+
+
+def get_host_from_address(address):
+    if not address:
+        return None
+
+    if address.lower().startswith('https://'):
+        address = address[8:]
+    if address.lower().startswith('http://'):
+        address = address[7:]
+    if address[0] == '/':
+        address = address.lstrip('/')
+    if '/' in address:
+        address = address.split('/')[0] or address
+    if ':' in address:
+        address = address.split(':')[0] or address
+
+    return address
+    

+ 4 - 2
misago/project_template/project_name/settings.py

@@ -365,8 +365,10 @@ REST_FRAMEWORK = {
 # Misago specific settings
 # https://misago.readthedocs.io/en/latest/developers/settings.html
 
-# Complete HTTP address of your Misago installation
-# If you are unsure what to enter here, refer to your site's admin control panel for help
+# Complete HTTP address to your Misago site homepage. Misago relies on this address to create
+# links in e-mails that are sent to site users.
+# On Misago admin panel home page you will find a message telling you if you have entered the
+# correct value, or what value is correct in case you've didn't.
 
 MISAGO_ADDRESS = 'http://my-misago-site.com/'