patient.py 4.2 KB
Newer Older
Marc Duez's avatar
Marc Duez committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python

import unittest
from gluon.globals import Request, Session, Storage, Response
from gluon.contrib.test_helpers import form_postvars
from gluon import current

class PatientController(unittest.TestCase):
        
    def __init__(self, p):
        global auth, session, request
        unittest.TestCase.__init__(self, p)
        
    def setUp(self):
        # Load the to-be-tested file
        execfile("applications/vidjil/controllers/patient.py", globals())
        # set up default session/request/auth/...
        global response, session, request, auth
        session = Session()
        request = Request({})
21
        auth = VidjilAuth(globals(), db)
Marc Duez's avatar
Marc Duez committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        auth.login_bare("test@vidjil.org", "1234")
        
        # rewrite info / error functions 
        # for some reasons we lost them between the testRunner and the testCase but we need them to avoid error so ...
        def f(a):
            pass
        log.info = f
        log.error = f
        log.debug = f
        
        # for defs
        current.db = db
        current.auth = auth
        
Marc Duez's avatar
Marc Duez committed
36
37
38
        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)
39
    
Marc Duez's avatar
Marc Duez committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        
    def testIndex(self):
        resp = index()
        self.assertTrue(resp.has_key('query'), "index() has returned an incomplete response")
        
        
    def testAdd(self):
        resp = add()
        self.assertTrue(resp.has_key('message'), "add() has returned an incomplete response")
        
        
    def test1AddForm(self):
        request.vars["first_name"] = "bob"
        request.vars["last_name"] = "bob"
        request.vars["birth"] = "2011-11-11"
        request.vars["info"] = "test patient kZtYnOipmAzZ"
        request.vars["id_label"] = "bob"
57
        request.vars["patient_group"] = fake_group_id
Marc Duez's avatar
Marc Duez committed
58
59
        
        resp = add_form()
Marc Duez's avatar
Marc Duez committed
60
        print db(db.auth_permission.id>0).select()
Marc Duez's avatar
Marc Duez committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        self.assertNotEqual(resp.find('patient added'), -1, "add patient failled")
        
        
    def testEdit(self):
        request.vars["id"] = fake_patient_id
        
        resp = edit()
        self.assertTrue(resp.has_key('message'), "edit() has returned an incomplete response")
        
        
    def testEditForm(self):
        request.vars["id"] = fake_patient_id
        request.vars["first_name"] = "bab"
        request.vars["last_name"] = "bab"
        request.vars["birth"] = "2010-10-10"
        request.vars["info"] = "bab"
        request.vars["id_label"] = "bab"
        
        resp = edit_form()
Marc Duez's avatar
Marc Duez committed
80
        self.assertNotEqual(resp.find('bab bab (1): patient edited"'), -1, "edit patient failled")
Marc Duez's avatar
Marc Duez committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
        
        
    def testConfirm(self):
        request.vars["id"] = fake_patient_id
        
        resp = confirm()
        self.assertTrue(resp.has_key('message'), "confirm() has returned an incomplete response")
        
        
    def test4Delete(self):
        patient_id = db( db.patient.info == "test patient kZtYnOipmAzZ").select()[0].id
        request.vars["id"] = patient_id
        
        resp = delete()
        self.assertNotEqual(resp.find('patient ('+str(patient_id)+') deleted'), -1, "delete patient failled")
        
        
    def test2Permission(self):
        patient_id = db( db.patient.info == "test patient kZtYnOipmAzZ").select()[0].id
        request.vars["id"] = patient_id
        
        resp = permission()
        self.assertTrue(resp.has_key('query'), "permission() has returned an incomplete response")
        
        
    def test3ChangePermission(self):
        patient_id = db( db.patient.info == "test patient kZtYnOipmAzZ").select()[0].id
        request.vars["patient_id"] = patient_id
109
        request.vars["group_id"] = fake_group_id
Marc Duez's avatar
Marc Duez committed
110
111
        
        resp = change_permission()
112
        self.assertFalse(auth.get_group_access('patient', patient_id, fake_group_id), "fail to remove permission")
Marc Duez's avatar
Marc Duez committed
113
114
        
        resp = change_permission()
115
        self.assertTrue(auth.get_group_access('patient', patient_id, fake_group_id), "fail to add permission")
Marc Duez's avatar
Marc Duez committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133