pyramid_cubicweb/tests/test_login.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 01 Jun 2016 17:27:29 +0200
changeset 11628 0f12ee84f30a
parent 11611 9d2bb6bdb5c8
permissions -rw-r--r--
Test and fix status code and content of the login form on authentication error The status code was 200 and is now fixed to 403, but the content part of the test will fail until cset 02328f8cbd5c is integrated in cubicweb.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
from pyramid_cubicweb.tests import PyramidCWTest
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     2
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
class LoginTest(PyramidCWTest):
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
    def test_login_form(self):
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
        res = self.webapp.get('/login')
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
        self.assertIn('__login', res.text)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
    def test_login_password_login(self):
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
        res = self.webapp.post('/login', {
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
            '__login': self.admlogin, '__password': self.admpassword})
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
        self.assertEqual(res.status_int, 303)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
        res = self.webapp.get('/login')
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
        self.assertEqual(res.status_int, 303)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
    def test_login_password_login_cookie_expires(self):
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
        res = self.webapp.post('/login', {
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
            '__login': self.admlogin, '__password': self.admpassword})
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
        self.assertEqual(res.status_int, 303)
11562
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    21
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    22
        cookies = self.webapp.cookiejar._cookies['localhost.local']['/']
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    23
        self.assertNotIn('pauth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    24
        self.assertIn('auth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    25
        self.assertIsNone(cookies['auth_tkt'].expires)
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
        res = self.webapp.get('/logout')
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
        self.assertEqual(res.status_int, 303)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
11562
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    30
        self.assertNotIn('auth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    31
        self.assertNotIn('pauth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    32
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
        res = self.webapp.post('/login', {
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
            '__login': self.admlogin, '__password': self.admpassword,
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
            '__setauthcookie': 1})
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
        self.assertEqual(res.status_int, 303)
11562
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    37
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    38
        cookies = self.webapp.cookiejar._cookies['localhost.local']['/']
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    39
        self.assertNotIn('auth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    40
        self.assertIn('pauth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    41
        self.assertIsNotNone(cookies['pauth_tkt'].expires)
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    42
11628
0f12ee84f30a Test and fix status code and content of the login form on authentication error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11611
diff changeset
    43
    def test_login_bad_password(self):
0f12ee84f30a Test and fix status code and content of the login form on authentication error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11611
diff changeset
    44
        res = self.webapp.post('/login', {
0f12ee84f30a Test and fix status code and content of the login form on authentication error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11611
diff changeset
    45
            '__login': self.admlogin, '__password': 'empty'}, status=403)
0f12ee84f30a Test and fix status code and content of the login form on authentication error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11611
diff changeset
    46
        self.assertIn('Authentication failed', res.text)
0f12ee84f30a Test and fix status code and content of the login form on authentication error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11611
diff changeset
    47
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    48
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    49
if __name__ == '__main__':
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    50
    from unittest import main
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    51
    main()