diff --git a/src/postorius/models.py b/src/postorius/models.py index cb79995..73b7f21 100644 --- a/src/postorius/models.py +++ b/src/postorius/models.py @@ -262,16 +262,6 @@ self.created.replace(tzinfo=None) return age > expiration_delta - def _create_host_url(self, request): - # Create the host url - protocol = 'https' - if not request.is_secure(): - protocol = 'http' - server_name = request.META['SERVER_NAME'] - if server_name[-1] == '/': - server_name = server_name[:len(server_name) - 1] - return '{0}://{1}'.format(protocol, server_name) - def send_confirmation_link(self, request, template_context=None, template_path=None): """ @@ -289,12 +279,10 @@ Falls back to host url and activation link. :type template_context: django.template.Context """ - # create the host url and the activation link need for the template - host_url = self._create_host_url(request) # Get the url string from url conf. url = reverse('address_activation_link', kwargs={'activation_key': self.activation_key}) - activation_link = '{0}{1}'.format(host_url, url) + activation_link = request.build_absolute_uri(url) # Detect the right template path, either from the param, # the setting or the default if not template_path: @@ -305,7 +293,8 @@ # the activation_link and the host_url. if not template_context: template_context = Context( - {'activation_link': activation_link, 'host_url': host_url}) + {'activation_link': activation_link, + 'host_url': request.build_absolute_uri("/")}) email_subject = getattr( settings, 'EMAIL_CONFIRMATION_SUBJECT', u'Confirmation needed') try: diff --git a/src/postorius/tests/test_address_activation.py b/src/postorius/tests/test_address_activation.py index b09ee44..8c46f77 100644 --- a/src/postorius/tests/test_address_activation.py +++ b/src/postorius/tests/test_address_activation.py @@ -122,6 +122,7 @@ def tearDown(self): self.profile.delete() self.user.delete() + mail.outbox = [] def test_profile_creation(self): # Profile is created and has all necessary properties. @@ -174,10 +175,14 @@ # set the activation key to a fixed string for testing self.profile.activation_key = \ '6323fba0097781fdb887cfc37a1122ee7c8bb0b0' + # Simulate a VirtualHost with a different name + self.request.META["HTTP_HOST"] = "another-virtualhost" + # Now send the email self.profile.send_confirmation_link(self.request) self.assertEqual(mail.outbox[0].to[0], u'les@example.org') self.assertEqual(mail.outbox[0].subject, u'Confirmation needed') - self.assertTrue(self.profile.activation_key in mail.outbox[0].body) + self.assertIn(self.profile.activation_key, mail.outbox[0].body) + self.assertIn("another-virtualhost", mail.outbox[0].body) class TestAddressActivationLinkSuccess(unittest.TestCase): diff --git a/src/postorius/utils.py b/src/postorius/utils.py index a48b448..7be80b1 100644 --- a/src/postorius/utils.py +++ b/src/postorius/utils.py @@ -26,14 +26,6 @@ logger = logging.getLogger(__name__) -def get_domain_name(request): - """Extracts a domain name from the request object. - """ - if "HTTP_HOST" in request.META.keys(): - return request.META["HTTP_HOST"].split(":")[0] - return None - - def get_client(): return Client('{0}/3.0'.format(settings.MAILMAN_API_URL), settings.MAILMAN_USER,