devtools/test/unittest_webtest.py
author Rémi Cardona <remi.cardona@logilab.fr>
Tue, 06 Oct 2015 15:06:45 +0200
changeset 10794 0b4d6beec132
parent 10604 d4bf85db41f2
permissions -rw-r--r--
[dataimport] Update doc to reflect new API relate() looks deprecated.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10032
diff changeset
     1
from six.moves import http_client
9930
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):
10032
fd1dafb0ab10 [devtools] use a specific test_db_id when disabling anon
Julien Cristau <julien.cristau@logilab.fr>
parents: 9930
diff changeset
    18
    test_db_id = 'webtest-ident'
9930
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
    anonymous_allowed = False
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
    tags = CubicWebTestTC.tags | Tags(('auth',))
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
    def test_reponse_denied(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
        res = self.webapp.get('/', expect_errors=True)
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10032
diff changeset
    24
        self.assertEqual(http_client.FORBIDDEN, res.status_int)
9930
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
    def test_login(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
        res = self.webapp.get('/', expect_errors=True)
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10032
diff changeset
    28
        self.assertEqual(http_client.FORBIDDEN, res.status_int)
9930
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
        self.login(self.admlogin, self.admpassword)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
        res = self.webapp.get('/')
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10032
diff changeset
    32
        self.assertEqual(http_client.OK, res.status_int)
9930
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
        self.logout()
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
        res = self.webapp.get('/', expect_errors=True)
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10032
diff changeset
    36
        self.assertEqual(http_client.FORBIDDEN, res.status_int)
9930
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
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
if __name__ == '__main__':
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
    from logilab.common.testlib import unittest_main
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
    unittest_main()