Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

notification.py 3.85 KB
Newer Older
1 2 3 4 5
#!/usr/bin/python

import unittest
import tempfile
import gluon.contrib.simplejson
6
from datetime import date
7 8 9 10 11 12 13 14
from gluon.globals import Request, Session, Storage, Response
from gluon.contrib.test_helpers import form_postvars
from gluon import current

class NotificationController(unittest.TestCase):
    
    def __init__(self, p):
        global auth, session, request
15
        unittest.TestCase.__init__(self, p)
16 17 18 19 20 21 22 23 24

    def setUp(self):
        # Load the to-be-tested file
        execfile("applications/vidjil/controllers/notification.py", globals())
        # set up default session/request/auth/...
        global response, session, request, auth
        session = Session()
        request = Request({})
        auth = VidjilAuth(globals(), db)
25
        auth.login_bare("test@vidjil.org", "123456")
26 27 28
        
        # rewrite info / error functions 
        # for some reasons we lost them between the testRunner and the testCase but we need them to avoid error so ...
29
        def f(a, **kwargs):
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
            pass
        log.info = f
        log.error = f
        log.debug = f
        
        # for defs
        current.db = db
        current.auth = auth
        
        auth.add_permission(group_id, 'admin', db.patient, 0)
        auth.add_permission(group_id, 'read', db.patient, 0)
        auth.add_permission(group_id, 'create', db.patient, 0)

    def testIndex(self):
        res = index()
45
        self.assertTrue(res.has_key('message'), "index has returned an incomplete response")
46 47 48

    def testAdd(self):
        res = add()
49
        self.assertNotEqual(res.has_key('message'), "add has returned an incomplete response")
50 51 52 53 54 55 56 57 58

    def test1AddForm(self):
        request.vars['title'] = "test title"
        request.vars['message_content'] = "test content"
        request.vars['message_type'] = "type"
        request.vars['priority'] = 'header'
        request.vars['expiration'] = '2100-10-30'

        res = add_form()
59
        self.assertNotEqual(res.find('notification added'), -1, "add notification failed")
60 61 62 63 64 65
        query = db(db.notification.id > 0).select()
        self.assertTrue(query, "add_form was unable to create a notification")
        
    def testEdit(self):
        request.vars['id'] = fake_notification_id
        res = edit()
66
        self.assertTrue(res.has_key('message'), "edit returned an incomplete response")
67

68 69 70
    def test2EditForm(self):
        curdate = date.today()
        curdate.replace(year = curdate.year+1)
71 72 73 74 75
        request.vars['id'] = fake_notification_id
        request.vars['title'] = "test title"
        request.vars['message_content'] = "test content"
        request.vars['message_type'] = "type"
        request.vars['priority'] = "header"
76
        request.vars['expiration'] = str(curdate)
77 78
        res = edit_form()
        note = db.notification[fake_notification_id]
79 80
        preferences = db((db.user_preference.preference=='mail')
                        &(db.user_preference.val==fake_notification_id)).select()
81
        self.assertNotEquals(res.find('notification updated'), -1, "edit_form returned an incomplete response")
82 83
        self.assertTrue(note.title == "test title", "edit_form was unable to update the title")
        self.assertTrue(note.message_content == "test content", "edit_form was unable to update the message content")
84
        self.assertEqual(len(preferences), 0, "edit_form was unable to clear the associated preferences")
85

86
    def test4Delete(self):
87
        notification_id = db(db.notification.title=="test title").select()[0].id
88
        request.vars['id'] = str(notification_id)
89
        res = delete()
90
        self.assertNotEqual(res.find("notification " + str(notification_id) + " deleted"), -1, "delete returned an incomplete response")
91 92
        

93
    def test3GetActiveNotifications(self):
94 95
        #TODO Improve this test
        res = get_active_notifications()
96
        self.assertNotEqual(res, "[]", "get active notifications returned no notifications")