blob: ab3918217856c08fc27593fd826841419dafb440 [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 logic."""
import unittest
from google.appengine.ext import ndb
from melange.logic import email as email_logic
from melange.models import email as email_model
from tests import test_utils
_TEST_EMAIL = 'test@example.com'
_TEST_CONTEXT = {
'foo': 'foo_value',
'bar': 'bar_value'
}
class CreateRecipientTest(unittest.TestCase):
"""Unit tests for createRecipient function."""
def testRecipientCreated(self):
"""Tests that recipient entity is created properly."""
recipient = email_logic.createRecipient(_TEST_EMAIL, _TEST_CONTEXT)
self.assertEqual(_TEST_EMAIL, recipient.email)
self.assertDictEqual(_TEST_CONTEXT, recipient.context)
self.assertEqual(email_model.RecipientStatus.PENDING, recipient.status)
_OTHER_EMAIL = 'other@example.com'
_OTHER_CONTEXT = {
'foo': 'foo_other',
'bar': 'bar_other'
}
_TEST_TEMPLATE_PATH = 'test/template/path'
_TEST_TEMPLATE_STRING = 'Test template string'
_TEST_SUBJECT = 'Test subject'
_TEST_PARENT = ndb.Key('Model name', 'unused id')
class CreateEmailTemplateTest(unittest.TestCase):
"""Unit tests for createEmailTemplate function."""
def testPathTypeEmailTemplateCreated(self):
"""Tests that path type email template entity is created properly."""
recipient = email_logic.createRecipient(_TEST_EMAIL, _TEST_CONTEXT)
other_recipient = email_logic.createRecipient(_OTHER_EMAIL, _OTHER_CONTEXT)
email_template = email_logic.createEmailTemplate(
email_model.TemplateType.PATH, [recipient, other_recipient],
_TEST_SUBJECT, template_path=_TEST_TEMPLATE_PATH, parent=_TEST_PARENT)
# check properties of the template
self.assertEqual(
email_model.TemplateType.PATH, email_template.template_type)
self.assertEqual(_TEST_TEMPLATE_PATH, email_template.template_path)
self.assertIsNone(email_template.template_string)
self.assertEqual(_TEST_SUBJECT, email_template.subject)
self.assertEqual(_TEST_PARENT, email_template.key.parent())
# check recipients of the template
self.assertEqual(2, len(email_template.recipients))
self.assertEqual(recipient.email, email_template.recipients[0].email)
self.assertDictEqual(
recipient.context, email_template.recipients[0].context)
self.assertEqual(other_recipient.email, email_template.recipients[1].email)
self.assertDictEqual(
other_recipient.context, email_template.recipients[1].context)
def testStringTypeEmailTemplateCreated(self):
"""Tests that string type email template entity is created properly."""
recipient = email_logic.createRecipient(_TEST_EMAIL, _TEST_CONTEXT)
other_recipient = email_logic.createRecipient(_OTHER_EMAIL, _OTHER_CONTEXT)
email_template = email_logic.createEmailTemplate(
email_model.TemplateType.STRING, [recipient, other_recipient],
_TEST_SUBJECT, template_string=_TEST_TEMPLATE_STRING,
parent=_TEST_PARENT)
# check properties of the template
self.assertEqual(
email_model.TemplateType.STRING, email_template.template_type)
self.assertIsNone(email_template.template_path)
self.assertEqual(_TEST_TEMPLATE_STRING, email_template.template_string)
self.assertEqual(_TEST_SUBJECT, email_template.subject)
self.assertEqual(_TEST_PARENT, email_template.key.parent())
# check recipients of the template
self.assertEqual(2, len(email_template.recipients))
self.assertEqual(recipient.email, email_template.recipients[0].email)
self.assertDictEqual(
recipient.context, email_template.recipients[0].context)
self.assertEqual(other_recipient.email, email_template.recipients[1].email)
self.assertDictEqual(
other_recipient.context, email_template.recipients[1].context)
def testInvalidArguments(self):
"""Tests that exception is raised for invalid arguments."""
# template_string is also provided for TemplateType.PATH type
with self.assertRaises(ValueError):
email_logic.createEmailTemplate(
email_model.TemplateType.PATH, [], _TEST_SUBJECT,
template_path=_TEST_TEMPLATE_PATH,
template_string=_TEST_TEMPLATE_STRING)
# only template_string is provided for TemplateType.PATH type
with self.assertRaises(ValueError):
email_logic.createEmailTemplate(
email_model.TemplateType.PATH, [], _TEST_SUBJECT,
template_string=_TEST_TEMPLATE_STRING)
# template_path is also provided for TemplateType.STRING type
with self.assertRaises(ValueError):
email_logic.createEmailTemplate(
email_model.TemplateType.STRING, [], _TEST_SUBJECT,
template_path=_TEST_TEMPLATE_PATH,
template_string=_TEST_TEMPLATE_STRING)
# only template_path is provided for TemplateType.STRING type
with self.assertRaises(ValueError):
email_logic.createEmailTemplate(
email_model.TemplateType.STRING, [], _TEST_SUBJECT,
template_path=_TEST_TEMPLATE_PATH)
class SendEmailTemplateTest(test_utils.SoCTestCase):
"""Unit tests for sendEmailTemplate function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testTaskInQueue(self):
"""Tests that AppEngine task is enqueued."""
email_template = email_logic.createEmailTemplate(
email_model.TemplateType.PATH, [], _TEST_SUBJECT,
template_path=_TEST_TEMPLATE_PATH)
@ndb.transactional
def txn():
email_logic.sendEmailTemplate(email_template.key)
txn()
# check that appropriate task is added to the queue
url = '/tasks/email/schedule/%s' % email_template.key.urlsafe()
self.assertTasksInQueue(n=1, url=url)