assets_test.py 1.65 KB
Newer Older
Romain Bignon's avatar
Romain Bignon committed
1 2
from assnet.storage import Storage
from assnet.server import Server
3 4 5 6 7 8 9 10

from unittest import TestCase
from webtest import TestApp

from tempfile import mkdtemp
import os
import shutil

Laurent Bachelier's avatar
Laurent Bachelier committed
11

Laurent Bachelier's avatar
Laurent Bachelier committed
12
class AssetsTest(TestCase):
13
    def setUp(self):
Romain Bignon's avatar
Romain Bignon committed
14
        self.root = mkdtemp(prefix='assnet_test_root')
Laurent Bachelier's avatar
Laurent Bachelier committed
15
        Storage.create(self.root)
16
        server = Server(self.root)
17
        self.app = TestApp(server)
18 19 20 21 22 23
        datapath = os.path.join(self.root, 'test_data')
        os.mkdir(datapath)
        os.mkdir(os.path.join(datapath, 'assets'))
        with open(os.path.join(datapath, 'assets', 'main.css'), 'w') as f:
            f.write('body { background-color: pink; }')
        # monkeypatching
24 25 26
        self.data_paths = Storage.DATA_PATHS
        Storage.DATA_PATHS.insert(0, datapath)
        Storage.DATA_PATHS.insert(0, os.path.join(self.root, 'doesnotexist'))
27 28 29 30 31

    def tearDown(self):
        if self.root:
            shutil.rmtree(self.root)
        # demonkeypatching
32
        Storage.DATA_PATHS = self.data_paths
33 34 35 36 37 38 39

    def test_getAsset(self):
        res = self.app.get('/?action=asset&file=main.css', status=200)
        assert 'body { background-color: pink; }' == res.body

        self.app.get('/?action=asset&file=none.css', status=404)

Romain Bignon's avatar
Romain Bignon committed
40
        self.app.get('/?action=asset&file=../../.assnet/config', status=412)
41 42

    def test_stylesheets(self):
43
        res = self.app.get('/?view=list', status=200)
44
        assert '<link rel="stylesheet" type="text/css" href="/?action=asset&amp;file=main.css" />' in res.body
45

46
        res = self.app.get('/test_data/?view=medialist', status=200)
47
        assert '<link rel="stylesheet" type="text/css" href="/?action=asset&amp;file=main.css" />' in res.body