test_models.py 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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/>.

from django.contrib.auth.models import User

from django.test import TestCase

from ..models import Profile
from ..views import search_user

Szeberényi Imre committed
25 26 27 28 29
try:
    # Python 2: "unicode" is built-in
    unicode
except NameError:
    unicode = str
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

class NotificationTestCase(TestCase):

    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        Profile.objects.get_or_create(user=self.u1)
        self.u2 = User.objects.create(username='user2')
        Profile.objects.get_or_create(user=self.u2)

    def test_notification_send(self):
        c1 = self.u1.notification_set.count()
        c2 = self.u2.notification_set.count()
        profile = self.u1.profile
        msg = profile.notify('subj',
                             '%(var)s %(user)s',
                             {'var': 'testme'})
        assert self.u1.notification_set.count() == c1 + 1
        assert self.u2.notification_set.count() == c2
Szeberényi Imre committed
48 49
        assert 'user1' in unicode(msg.message)
        assert 'testme' in unicode(msg.message)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        assert msg in self.u1.notification_set.all()


class ProfileTestCase(TestCase):

    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        Profile.objects.get_or_create(user=self.u1)
        self.u2 = User.objects.create(username='user2',
                                      email='john@example.org')
        Profile.objects.get_or_create(user=self.u2, org_id='apple')

    def test_search_user_by_name(self):
        self.assertEqual(search_user('user1'), self.u1)
        self.assertEqual(search_user('user2'), self.u2)

    def test_search_user_by_mail(self):
        self.assertEqual(search_user('john@example.org'), self.u2)

    def test_search_user_by_orgid(self):
        self.assertEqual(search_user('apple'), self.u2)

    def test_search_user_nonexist(self):
        with self.assertRaises(User.DoesNotExist):
            search_user('no-such-user')