test_periodic_task.py 2.96 KB
Newer Older
Bach Dániel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18 19 20
import unittest
from mock import patch, MagicMock

Őry Máté committed
21
from django.contrib.auth.models import User
22 23 24 25 26 27 28 29

from ..models import Notification
from ..tasks import local_periodic_tasks


@patch.object(local_periodic_tasks, 'send_mail')
@patch.object(Notification, 'objects')
class EmailNotificationTestCase(unittest.TestCase):
30 31 32

    nextpk = 0

Őry Máté committed
33
    def get_fake_notification(self, user=None, **kwargs):
34
        self.nextpk += 1
Őry Máté committed
35
        if user is None:
36
            user = MagicMock(spec=User, pk=self.nextpk)
Őry Máté committed
37 38 39 40 41 42 43 44 45
            user.profile.__unicode__.return_value = "user"
            user.email = "mail"
            user.profile.email_notifications = True
            user.profile.preferred_language = "en"
        params = {"to": user, "subject": "subj", "message": "msg",
                  "status": Notification.STATUS.new}
        params.update(kwargs)
        return MagicMock(spec=Notification, **params)

46
    def test_not_sending(self, no, sm):
Őry Máté committed
47 48 49
        fake = [self.get_fake_notification()]
        fake[0].to.profile.email_notifications = False
        no.filter.return_value = fake
50 51 52 53
        local_periodic_tasks.send_email_notifications()
        assert not sm.called

    def test_sending(self, no, sm):
Őry Máté committed
54 55
        fake = [self.get_fake_notification()]
        no.filter.return_value = fake
56 57
        local_periodic_tasks.send_email_notifications()
        assert sm.called
Őry Máté committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        assert all(i.status == i.STATUS.delivered for i in fake)

    def test_sending_more(self, no, sm):
        fake = [self.get_fake_notification(), self.get_fake_notification()]
        fake.append(self.get_fake_notification(fake[0].to))
        no.filter.return_value = fake
        local_periodic_tasks.send_email_notifications()
        self.assertEquals(sm.call_count, 2)
        assert all(i.status == i.STATUS.delivered for i in fake)

    def test_sending_some(self, no, sm):
        fake = [self.get_fake_notification(), self.get_fake_notification()]
        fake.append(self.get_fake_notification(fake[0].to))
        fake[1].to.profile.email_notifications = False
        no.filter.return_value = fake
        local_periodic_tasks.send_email_notifications()
        self.assertEquals(
            [i.status == i.STATUS.delivered for i in fake],
            [True, False, True])
        self.assertEquals(sm.call_count, 1)