devtools/webtest.py
author Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
Mon, 19 Oct 2015 23:39:30 +0200
changeset 10948 3ffacbdf7e9c
parent 9930 d20c2b262f55
child 11016 d04703f54510
permissions -rw-r--r--
[skel] remove (what looks like) a spurious copy/paste
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
from __future__ import absolute_import
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
import webtest
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
from cubicweb.wsgi import handler
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
from cubicweb.devtools.testlib import CubicWebTC
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
class CubicWebTestTC(CubicWebTC):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
    @classmethod
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
    def init_config(cls, config):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
        super(CubicWebTestTC, cls).init_config(config)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
        config.global_set_option('base-url', 'http://localhost.local/')
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
    def setUp(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
        super(CubicWebTestTC, self).setUp()
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
        webapp = handler.CubicWebWSGIApplication(self.config)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
        self.webapp = webtest.TestApp(webapp)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
    def tearDown(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
        del self.webapp
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
        super(CubicWebTestTC, self).tearDown()
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
    def login(self, user=None, password=None, **args):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
        if user is None:
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
            user = self.admlogin
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
        if password is None:
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
            password = self.admpassword if user == self.admlogin else user
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
        args.update({
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
            '__login': user,
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
            '__password': password
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
        return self.webapp.get('/login', args)
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
    def logout(self):
d20c2b262f55 [tests] Add a webtest based TestCase class
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
        return self.webapp.get('/logout')