[server/test] fix random error in unittest_security
When entering a new Connection, we cache the user and its 'login'
attribute (with no permissions checking).
This test makes 'CWUser.login' unreadable by guests, and then proceeds
to make sure the 'anon' user can actually not read any 'login'
attribute. However, due to the above cnx initialization, anon's login
is actually cached, hence readable. This happens to make the test fail
sometimes depending on the order in which CWUser entities are returned,
because one of them has .complete() called, which as a side effect sets
the attribute cache to None for unreadable attributes.
Call .complete() on both entities to reset the login cache. While this
is still highly debatable, at least it's consistent.
import os
from os.path import join, dirname
from shutil import rmtree
import errno
import tempfile
from logilab.common.testlib import TestCase, unittest_main
from cubicweb.web.propertysheet import PropertySheet, lazystr
DATADIR = join(dirname(__file__), 'data')
try:
os.makedirs(join(DATADIR, 'uicache'))
except OSError as err:
if err.errno != errno.EEXIST:
raise
CACHEDIR = tempfile.mkdtemp(dir=join(DATADIR, 'uicache'))
class PropertySheetTC(TestCase):
def tearDown(self):
rmtree(CACHEDIR)
def test(self):
ps = PropertySheet(CACHEDIR, datadir_url='http://cwtest.com')
ps.load(join(DATADIR, 'sheet1.py'))
ps.load(join(DATADIR, 'sheet2.py'))
# defined by sheet1
self.assertEqual(ps['logo'], 'http://cwtest.com/logo.png')
# defined by sheet1, overriden by sheet2
self.assertEqual(ps['bgcolor'], '#FFFFFF')
# defined by sheet2
self.assertEqual(ps['fontcolor'], 'black')
# defined by sheet1, extended by sheet2
self.assertEqual(ps['stylesheets'], ['http://cwtest.com/cubicweb.css',
'http://cwtest.com/mycube.css'])
# lazy string defined by sheet1
self.assertIsInstance(ps['lazy'], lazystr)
self.assertEqual(str(ps['lazy']), '#FFFFFF')
# test compilation
self.assertEqual(ps.compile('a {bgcolor: %(bgcolor)s; size: 1%;}'),
'a {bgcolor: #FFFFFF; size: 1%;}')
self.assertEqual(ps.process_resource(DATADIR, 'pouet.css'),
CACHEDIR)
self.assertIn('pouet.css', ps._cache)
self.assertFalse(ps.need_reload())
os.utime(join(DATADIR, 'sheet1.py'), None)
self.assertIn('pouet.css', ps._cache)
self.assertTrue(ps.need_reload())
self.assertIn('pouet.css', ps._cache)
ps.reload()
self.assertNotIn('pouet.css', ps._cache)
self.assertFalse(ps.need_reload())
ps.process_resource(DATADIR, 'pouet.css') # put in cache
os.utime(join(DATADIR, 'pouet.css'), None)
self.assertFalse(ps.need_reload())
self.assertNotIn('pouet.css', ps._cache)
if __name__ == '__main__':
unittest_main()