config.py 4.48 KB
Newer Older
Marc Duez's avatar
Marc Duez committed
1 2
#!/usr/bin/python

3 4
from __future__ import print_function

Marc Duez's avatar
Marc Duez committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import unittest
from gluon.globals import Request, Session, Storage, Response
from gluon.contrib.test_helpers import form_postvars
from gluon import current


test_config_name = "test_config_plapipou"
#tmplog = log

class ConfigController(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/config.py", globals())
Marc Duez's avatar
Marc Duez committed
24
        execfile("applications/vidjil/modules/defs.py", globals())
Marc Duez's avatar
Marc Duez committed
25 26 27 28
        # set up default session/request/auth/...
        global response, session, request, auth
        session = Session()
        request = Request({})
29
        auth = VidjilAuth(globals(), db)
30
        auth.login_bare("test@vidjil.org", "123456")
Marc Duez's avatar
Marc Duez committed
31
        
Marc Duez's avatar
Marc Duez committed
32 33 34 35 36
        auth.add_permission(group_id, 'admin', db.patient, 0)
        auth.add_permission(group_id, 'admin', db.config, 0)
        auth.add_permission(group_id, 'read', db.config, 0)
        
        
Marc Duez's avatar
Marc Duez committed
37 38
        # rewrite info / error functions 
        # for some reasons we lost them between the testRunner and the testCase but we need them to avoid error so ...
39
        def f(a, **kwargs):
Marc Duez's avatar
Marc Duez committed
40 41 42
            pass
        log.info = f
        log.error = f
Marc Duez's avatar
Marc Duez committed
43
        log.debug = f
Marc Duez's avatar
Marc Duez committed
44 45 46 47 48 49 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 75
        
        
    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 test1AddForm1(self):
        #test incomplete request
        request.vars['config_command'] = ""
        resp = add_form()
        self.assertNotEqual(resp.find('config_command needed,'), -1, "addForm doesn't return a valid error message")
    
    
    def test2AddForm1(self):
        #test valid request
        request.vars['config_name'] = test_config_name
        request.vars['config_info'] = " plop"
        request.vars['config_command'] = " -plop"
        request.vars['config_fuse_command'] = " -plop"
        request.vars['config_program'] = " plop.cpp"
        
        resp = add_form()
        self.assertTrue( len(db( db.config.name == test_config_name ).select()) == 1 , "fail to insert a new config") 
        
        
    def test3Edit(self):
76
        request.vars["id"] = fake_config_id
Marc Duez's avatar
Marc Duez committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        resp = edit()
        self.assertTrue(resp.has_key('message'), "edit() has returned an incomplete response")
        
        
    def test4EditForm(self):
        id_config = db( db.config.name == test_config_name).select()[0].id
        
        request.vars["id"] = id_config
        request.vars['config_name'] = test_config_name
        request.vars['config_info'] = "plup"
        
        resp = edit_form()
        self.assertTrue( db.config[id_config].info == "plup" , "fail to edit config info")
        
        
    def testConfirm(self):
93
        request.vars["id"] = fake_config_id
Marc Duez's avatar
Marc Duez committed
94 95 96 97 98 99 100 101 102 103
        resp = confirm()
        self.assertTrue(resp.has_key('message'), "confirm() has returned an incomplete response")
        
        
    def test7Delete(self):
        id_config = db( db.config.name == test_config_name).select()[0].id
        
        request.vars["id"] = id_config
        
        resp = delete()
104
        print(resp)
Marc Duez's avatar
Marc Duez committed
105
        self.assertEqual(resp.find('config deleted'), -1, "delete doesn't return a valid message")
Marc Duez's avatar
Marc Duez committed
106 107 108 109 110 111 112 113
        
        
    def test5Permission(self):
        id_config = db( db.config.name == test_config_name).select()[0].id
        
        request.vars["id"] = id_config
        
        resp = permission()
Marc Duez's avatar
Marc Duez committed
114
        self.assertTrue(resp.has_key('query'), "permission() has returned an incomplete response")
Marc Duez's avatar
Marc Duez committed
115 116 117 118 119 120 121 122
        
        
    def test6change_permission(self):
        id_config = db( db.config.name == test_config_name).select()[0].id
        id_group = auth.user_group()
        
        request.vars["group_id"] = id_group
        request.vars["config_id"] = id_config
123 124

        #add access to a config
Marc Duez's avatar
Marc Duez committed
125
        resp = change_permission()
126
        self.assertTrue( auth.get_group_access('config', id_config, id_group), "fail to add a permission")
Marc Duez's avatar
Marc Duez committed
127 128 129 130
        
        
        #remove it
        resp = change_permission()
131
        self.assertFalse( auth.get_group_access('config', id_config, id_group), "fail to remove a permission")
Marc Duez's avatar
Marc Duez committed
132 133 134 135 136 137 138 139