blob: 29162522980671a018605573a236f1c6c3dff5ec [file] [log] [blame]
# Copyright 2014 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for email related AppEngine tasks handlers."""
from melange.models import email as email_model
from seeder import email as email_seeder
from tests import test_utils
def _getEmailSchedulerUrl(entity_key):
"""Returns URL to 'Schedule Email' handler.
Args:
entity_key: ndb.Key of email_model.EmailTemplate entity that should be
processed.
Returns:
The URL to 'Schedule Email' handler.
"""
return '/tasks/email/schedule/%s' % entity_key.urlsafe()
class EmailTemplateSchedulerTest(test_utils.DjangoTestCase):
"""Unit tests for EmailTemplateScheduler class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testEmailsScheduledProperlyForTemplatePath(self):
"""Tests that emails are handled properly if defined by template path."""
email_template = email_seeder.seedEmailTemplate(
email_model.TemplateType.PATH)
url = _getEmailSchedulerUrl(email_template.key)
response = self.post(url)
self.assertResponseOK(response)
# check that email template is deleted
self.assertIsNone(email_template.key.get())
# check that a task is scheduled for each recipient
tasks = self.taskqueue_stub.get_filtered_tasks()
self.assertEqual(len(email_template.recipients), len(tasks))
# check that email is sent to all recipients and the content is personalized
for recipient in email_template.recipients:
self.assertEmailSent(
to=recipient.email, subject=email_template.subject,
html='Hello %s' % recipient.context['recipient_name'])
def testEmailsScheduledProperlyForTemplateString(self):
"""Tests that emails are handled properly if defined by template string."""
email_template = email_seeder.seedEmailTemplate(
email_model.TemplateType.STRING)
url = _getEmailSchedulerUrl(email_template.key)
response = self.post(url)
self.assertResponseOK(response)
# check that email template is deleted
self.assertIsNone(email_template.key.get())
# check that a task is scheduled for each recipient
tasks = self.taskqueue_stub.get_filtered_tasks()
self.assertEqual(len(email_template.recipients), len(tasks))
# check that email is sent to all recipients and the content is personalized
for recipient in email_template.recipients:
self.assertEmailSent(
to=recipient.email, subject=email_template.subject,
html='Hello %s' % recipient.context['recipient_name'])