baseweb_test.py 5.39 KB
Newer Older
Romain Bignon's avatar
Romain Bignon committed
1 2
from assnet.storage import Storage
from assnet.server import Server
Laurent Bachelier's avatar
Laurent Bachelier committed
3 4 5 6 7

from unittest import TestCase
from webtest import TestApp

from tempfile import mkdtemp
8
import os
Laurent Bachelier's avatar
Laurent Bachelier committed
9 10
import shutil

Laurent Bachelier's avatar
Laurent Bachelier committed
11

Laurent Bachelier's avatar
Laurent Bachelier committed
12 13
class BaseWebTest(TestCase):
    def setUp(self):
Romain Bignon's avatar
Romain Bignon committed
14
        self.root = mkdtemp(prefix='assnet_test_root')
15
        Storage.create(self.root)
Laurent Bachelier's avatar
Laurent Bachelier committed
16
        server = Server(self.root)
17
        self.app = TestApp(server)
Laurent Bachelier's avatar
Laurent Bachelier committed
18 19 20 21 22 23 24

    def tearDown(self):
        if self.root:
            shutil.rmtree(self.root)

    def test_listAndDownload(self):
        res = self.app.get("/")
25
        assert "<title>Index of /</title>" in res.body
Laurent Bachelier's avatar
Laurent Bachelier committed
26

27
        with open(os.path.join(self.root, "penguins_are_cute"), 'w') as f:
Laurent Bachelier's avatar
Laurent Bachelier committed
28 29 30 31 32 33 34 35
            f.write("HELLO")

        res = self.app.get("/")
        assert "penguins_are_cute" in res.body

        res = self.app.get("/penguins_are_cute")
        assert "HELLO" == res.body

36 37
    def test_pathNormalization(self):
        os.mkdir(os.path.join(self.root, "penguins"))
38
        with open(os.path.join(self.root, "penguins", "gentoo"), 'w') as f:
39 40 41 42 43 44 45 46 47
            f.write("The best penguin.")

        # trailing slash for directories
        res = self.app.get("/penguins/", status=200)

        res = self.app.get("/penguins", status=302)
        assert res.location == "http://localhost/penguins/"
        res.follow(status=200)

48 49
        res = self.app.get("/penguins?view=list", status=302)
        assert res.location == "http://localhost/penguins/?view=list"
50 51 52 53 54 55 56 57 58
        res.follow(status=200)

        # no trailing slash for files
        res = self.app.get("/penguins/gentoo", status=200)

        res = self.app.get("/penguins/gentoo/", status=302)
        assert res.location == "http://localhost/penguins/gentoo"
        res.follow(status=200)

59 60
        res = self.app.get("/penguins/gentoo/?view=raw", status=302)
        assert res.location == "http://localhost/penguins/gentoo?view=raw"
61 62 63 64 65 66 67 68 69
        res.follow(status=200)

        # limit cases
        res = self.app.get("/", status=200)

        res = self.app.get("//", status=302)
        assert res.location == "http://localhost/"
        res.follow(status=200)

70 71 72 73 74 75 76 77 78
        # it fails without the http://localhost
        res = self.app.get("http://localhost///", status=302)
        assert res.location == "http://localhost/"
        res.follow(status=200)

        res = self.app.get("http://localhost////", status=302)
        assert res.location == "http://localhost/"
        res.follow(status=200)

Laurent Bachelier's avatar
Laurent Bachelier committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        # more sanitization
        res = self.app.get("/penguins///", status=302)
        assert res.location == "http://localhost/penguins/"
        res.follow(status=200)
        res = self.app.get("/penguins/..//..", status=302)
        assert res.location == "http://localhost/"
        res.follow(status=200)
        res = self.app.get("/../", status=302)
        assert res.location == "http://localhost/"
        res.follow(status=200)
        res = self.app.get(r"/penguins\..\penguins/..", status=302)
        assert res.location == "http://localhost/"
        res.follow(status=200)
        res = self.app.get(r"/penguins\\\gentoo\\", status=302)
        assert res.location == "http://localhost/penguins/gentoo"
        res.follow(status=200)

96
    def test_notFound(self):
97 98
        self.app.get("/penguins/", status=404)
        self.app.get("/penguins", status=404)
99 100 101 102 103 104 105 106

    def test_listWithParent(self):
        os.mkdir(os.path.join(self.root, "penguins"))
        res = self.app.get("/", status=200)
        assert "Parent directory" not in res.body
        res = self.app.get("/penguins/", status=200)
        assert "Parent directory" in res.body
        res = res.click("Parent directory")
107
        assert "<title>Index of /</title>" in res.body
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    def test_actionsInRoot(self):
        os.mkdir(os.path.join(self.root, 'penguins'))
        with open(os.path.join(self.root, 'penguins', 'gentoo'), 'w') as f:
            f.write('HELLO')
        res = self.app.get('/?action=login', status=200)
        assert 'penguins' not in res.body
        res = self.app.get('/?foo=bar&action=login', status=200)
        assert 'penguins' not in res.body
        self.app.get('/doesnotexist?action=login', status=404)
        self.app.get('/doesnotexist/?action=login', status=404)
        res = self.app.get('/penguins/?action=login', status=200)
        assert 'gentoo' in res.body
        res = self.app.get('/penguins/gentoo?action=login', status=200)
        assert 'HELLO' == res.body
123 124

    def test_methods(self):
125
        self.app._gen_request('HEAD', '/?view=text_list', status=200)
126 127 128 129 130 131 132 133 134 135
        self.app.get('/?view=text_list', status=200)
        self.app.post('/?view=text_list', status=405)
        self.app.put('/?view=text_list', status=405)
        self.app.delete('/?view=text_list', status=405)

        self.app.get('/?action=logout', status=302)
        self.app.post('/?action=logout', status=405)

        self.app.get('/?action=login', status=200)
        self.app.post('/?action=login', status=200)
136 137 138
        self.app.post('/?action=login', {'_method': 'DELETE'}, status=405)

        self.app.post('/?_method=GET', status=405)
139
        self.app.post('/?action=login', {'_method': 'HEAD'}, status=405)
140 141 142

    def test_initConfig(self):
        os.mkdir(os.path.join(self.root, "penguins"))
Romain Bignon's avatar
Romain Bignon committed
143
        self.app.get('http://assnet.test/penguins/', status=200)
144 145

        storage = Storage.lookup(self.root)
Romain Bignon's avatar
Romain Bignon committed
146
        assert storage.get_config().data['web']['root_url'] == 'http://assnet.test/'
147
        assert storage.get_config().data['web']['cookie_secret']