From ca0d6efad95e9ee0a487572b04dded02d4b30129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Luiz=20Lorencetti?= Date: Tue, 20 Dec 2016 22:23:43 -0200 Subject: [PATCH] Adds the sites framework to be able to get the full url in the management command --- pets/meupet/services.py | 11 ++++-- .../templates/meupet/request_action_email.txt | 2 +- pets/meupet/tests/test_model_pet.py | 29 +++++----------- pets/meupet/tests/test_services.py | 34 +++++++++++++++++++ pets/pets/settings/prod.py | 3 ++ 5 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 pets/meupet/tests/test_services.py diff --git a/pets/meupet/services.py b/pets/meupet/services.py index 843980c..4ec0b69 100644 --- a/pets/meupet/services.py +++ b/pets/meupet/services.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.contrib.sites.models import Site from django.core import mail from django.core.urlresolvers import reverse from django.template.loader import render_to_string @@ -16,14 +17,20 @@ def send_request_action_email(pet): subject = _('Update pet registration') to = pet.owner.email template_name = 'meupet/request_action_email.txt' + current_site = Site.objects.get_current() + + full_url = 'https://{domain}{path}'.format( + domain=current_site.domain, + path=reverse('meupet:update_register', args=[pet.request_key]) + ) context = { 'username': pet.owner.first_name, 'pet': pet.name, 'days': settings.DAYS_TO_STALE_REGISTER, 'status': pet.get_status_display(), - # ToDO: use full url - 'link': reverse('meupet:update_register', args=[pet.request_key]), + 'link': full_url, + 'site_name': current_site.name, } return send_email(subject, to, template_name, context) diff --git a/pets/meupet/templates/meupet/request_action_email.txt b/pets/meupet/templates/meupet/request_action_email.txt index dc858a6..49140be 100644 --- a/pets/meupet/templates/meupet/request_action_email.txt +++ b/pets/meupet/templates/meupet/request_action_email.txt @@ -12,4 +12,4 @@ days or update the status of the pet.{% endblocktrans %} {% blocktrans %}If no action is taken the registration will be disabled in {{ days }} days.{% endblocktrans %} {% trans "Best regards," %} -CadĂȘ meu Bicho \ No newline at end of file +{{ site_name }} \ No newline at end of file diff --git a/pets/meupet/tests/test_model_pet.py b/pets/meupet/tests/test_model_pet.py index 570df69..701bf7a 100644 --- a/pets/meupet/tests/test_model_pet.py +++ b/pets/meupet/tests/test_model_pet.py @@ -1,10 +1,7 @@ -from unittest import mock +from unittest.mock import patch -from django.core import mail -from django.conf import settings from django.test import TestCase from django.utils import timezone -from django.core.urlresolvers import reverse from model_mommy import mommy @@ -56,23 +53,13 @@ def test_expired_pets(self): self.assertNotIn(new_pet, pets) self.assertNotIn(staled_pet, pets) - def test_request_action_from_user(self): - """Should send email requesting action from the user to keep the register active""" + @patch('meupet.services.send_request_action_email') + def test_request_action_from_user(self, mock_send_email): + """Should call send_request_action_email method from request_action""" pet = mommy.make(Pet) pet.request_action() - email = mail.outbox[0] - contents = [ - pet.name, - pet.owner.first_name, - str(settings.DAYS_TO_STALE_REGISTER), - reverse('meupet:update_register', args=[pet.request_key]), - pet.get_status_display() - ] - - for expected in contents: - with self.subTest(): - self.assertIn(expected, email.body) + mock_send_email.assert_called_once_with(pet) def test_request_sent_saved(self): """Should set the request_sent date in the pet and keep modified date""" @@ -87,7 +74,7 @@ def test_request_sent_saved(self): self.assertAlmostEqual(pet.request_sent, timezone.now(), delta=timezone.timedelta(seconds=1)) def test_request_action_email_set_activation_success(self): - """Should set the request_key if the request_action_email succeed""" + """Should set the request_key if the send_request_action_email succeed""" pet = mommy.make(Pet, request_key='') pet.request_action() @@ -96,10 +83,10 @@ def test_request_action_email_set_activation_success(self): self.assertNotEqual('', pet.request_key) def test_request_action_email_not_set_activation_fail(self): - """Shouldn't set the request_key if the request_action_email fail""" + """Shouldn't set the request_key if the send_request_action_email fail""" pet = mommy.make(Pet, request_key='') - with mock.patch('meupet.services.send_request_action_email') as mock_method: + with patch('meupet.services.send_request_action_email') as mock_method: mock_method.return_value = False pet.request_action() diff --git a/pets/meupet/tests/test_services.py b/pets/meupet/tests/test_services.py new file mode 100644 index 0000000..bffb542 --- /dev/null +++ b/pets/meupet/tests/test_services.py @@ -0,0 +1,34 @@ +from django.conf import settings +from django.contrib.sites.models import Site +from django.core import mail +from django.core.urlresolvers import reverse +from django.test import TestCase + +from model_mommy import mommy + +from meupet.models import Pet + + +class ServicesTest(TestCase): + def test_request_action_from_user(self): + """Validate the information sent in the email""" + pet = mommy.make(Pet) + pet.request_action() + email = mail.outbox[0] + current_site = Site.objects.get_current() + + pet_update_url = reverse('meupet:update_register', args=[pet.request_key]) + full_url = '%s%s' % (current_site.domain, pet_update_url) + + contents = [ + pet.name, + pet.owner.first_name, + str(settings.DAYS_TO_STALE_REGISTER), + full_url, + pet.get_status_display(), + current_site.name, + ] + + for expected in contents: + with self.subTest(): + self.assertIn(expected, email.body) diff --git a/pets/pets/settings/prod.py b/pets/pets/settings/prod.py index 28d9504..b0f3675 100644 --- a/pets/pets/settings/prod.py +++ b/pets/pets/settings/prod.py @@ -30,6 +30,7 @@ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', + 'django.contrib.sites', 'django.contrib.staticfiles', ) @@ -53,6 +54,8 @@ INSTALLED_APPS = DJANGO_APPS + PROJECT_APPS + THIRD_PARTS_APPS +SITE_ID = 1 + MIDDLEWARE_CLASSES = ( 'opbeat.contrib.django.middleware.OpbeatAPMMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',