devtools/test/unittest_webtest.py
author Christophe de Vienne <christophe@unlish.com>
Tue, 17 Jun 2014 11:14:20 +0200
changeset 9930 d20c2b262f55
child 10032 fd1dafb0ab10
permissions -rw-r--r--
[tests] Add a webtest based TestCase class Closes #4002134
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9930
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
import httplib
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     2
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
from logilab.common.testlib import Tags
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
from cubicweb.devtools.webtest import CubicWebTestTC
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
class CWTTC(CubicWebTestTC):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
    def test_response(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
        response = self.webapp.get('/')
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
        self.assertEqual(200, response.status_int)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
    def test_base_url(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
        if self.config['base-url'] not in self.webapp.get('/').text:
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
            self.fail('no mention of base url in retrieved page')
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
class CWTIdentTC(CubicWebTestTC):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
    anonymous_allowed = False
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
    tags = CubicWebTestTC.tags | Tags(('auth',))
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
    def test_reponse_denied(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
        res = self.webapp.get('/', expect_errors=True)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
        self.assertEqual(httplib.FORBIDDEN, res.status_int)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
    def test_login(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
        res = self.webapp.get('/', expect_errors=True)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
        self.assertEqual(httplib.FORBIDDEN, res.status_int)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
        self.login(self.admlogin, self.admpassword)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
        res = self.webapp.get('/')
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
        self.assertEqual(httplib.OK, res.status_int)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
        self.logout()
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
        res = self.webapp.get('/', expect_errors=True)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
        self.assertEqual(httplib.FORBIDDEN, res.status_int)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
if __name__ == '__main__':
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
    from logilab.common.testlib import unittest_main
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
    unittest_main()