group.py 4.29 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 GroupController(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/group.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
        auth.login_bare("test@vidjil.org", "1234")
        
Marc Duez's avatar
Marc Duez committed
24 25 26 27 28
        
        auth.add_permission(group_id, 'admin', db.auth_group, 0)
        auth.add_permission(group_id, 'read', db.auth_group, 0)
        auth.add_permission(group_id, 'create', db.auth_group, 0)
        
Marc Duez's avatar
Marc Duez committed
29 30
        # rewrite info / error functions 
        # for some reasons we lost them between the testRunner and the testCase but we need them to avoid error so ...
31
        def f(a, **kwargs):
Marc Duez's avatar
Marc Duez committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            pass
        log.info = f
        log.error = f
        log.debug = f
        
        # for defs
        current.db = db
        current.auth = auth

        
    def testIndex(self):      
        resp = index()
        self.assertTrue(resp.has_key('message'), "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["group_name"] = "test_group_1"
        request.vars["info"] = "test_group_1"
55
        request.vars["group_parent"] = fake_group_id
Marc Duez's avatar
Marc Duez committed
56 57 58
        
        resp = add_form()
        self.assertTrue(auth.has_membership("test_group_1"), "group creation failed")
59
        new_group_id = db(db.auth_group.role == "test_group_1").select(db.auth_group.id).first().id
Marc Duez's avatar
Marc Duez committed
60 61 62
            
            
    def testConfirm(self):      
63
        request.vars['id'] = fake_group_id
Marc Duez's avatar
Marc Duez committed
64 65 66 67 68 69 70 71 72
        resp = confirm()
        self.assertTrue(resp.has_key('message'), "confirm() has returned an incomplete response")
        
        
    def test9Delete(self):
        group_id = db( db.auth_group.role == "test_group_1").select()[0].id
        request.vars["id"] = group_id
        
        resp = delete()
Marc Duez's avatar
Marc Duez committed
73
        self.assertEqual(resp.find('"message":"group deleted"'), -1, "group have been deleted")
Marc Duez's avatar
Marc Duez committed
74 75 76
        
        
    def testInfo(self):      
77
        request.vars['id'] = fake_group_id
Marc Duez's avatar
Marc Duez committed
78 79 80 81
        resp = info()
        self.assertTrue(resp.has_key('message'), "info() has returned an incomplete response")
        
        
82 83
    def testPermission(self):
        request.vars['id'] = fake_group_id
Marc Duez's avatar
Marc Duez committed
84 85 86 87 88 89 90 91 92 93
        resp = permission()
        self.assertTrue(resp.has_key('message'), "permission() has returned an incomplete response")
        
        
    def test2RemovePermission(self):
        group_id = db( db.auth_group.role == "test_group_1").select()[0].id
        request.vars["user_id"] = user_id
        request.vars["group_id"] = group_id
        
        resp = remove_permission()
94
        auth.get_permission('admin', 'auth_group', group_id)
Marc Duez's avatar
Marc Duez committed
95 96 97 98 99 100 101 102
        
        
    def test3ChangePermission(self):
        group_id = db( db.auth_group.role == "test_group_1").select()[0].id
        request.vars["user_id"] = user_id
        request.vars["group_id"] = group_id
        
        resp = change_permission()
103
        auth.get_permission('admin', 'auth_group', group_id)
Marc Duez's avatar
Marc Duez committed
104 105
        
        
106 107 108 109 110 111 112 113 114 115 116 117 118
    def test4Rights(self):
        group_id = db( db.auth_group.role == "test_group_1").select()[0].id
        request.vars["id"] = group_id
        request.vars["name"] = "patient"        #table name on which the right will aply
        request.vars["right"] = "plouf"         #right name

        request.vars["value"] = "true"          #add right
        resp = rights()
        self.assertNotEqual(resp.find("add 'plouf' permission on 'patient' for group test_group_1"), -1, "add permission failled")

        request.vars["value"] = "false"         #remove right
        resp = rights()
        self.assertNotEqual(resp.find("remove 'plouf' permission on 'patient' for group test_group_1"), -1, "remove permission failled")
Marc Duez's avatar
Marc Duez committed
119 120 121