cubicweb/pyramid/test/test_login.py
author Samuel Trégouët <samuel.tregouet@logilab.fr>
Fri, 13 Jan 2017 11:36:51 +0100
branch3.24
changeset 11924 4c949c28ce59
parent 11816 6392f34fcdad
child 11929 fcbd6b251d81
permissions -rw-r--r--
[pyramid] fix login route with language-mode = url-prefix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11816
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
     1
from os.path import join
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
     2
from shutil import rmtree
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
     3
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11628
diff changeset
     4
from cubicweb.pyramid.test import PyramidCWTest
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
11924
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
     6
class LoginTestLangUrlPrefix(PyramidCWTest):
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
     7
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
     8
    @classmethod
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
     9
    def setUpClass(cls):
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    10
        super(LoginTestLangUrlPrefix, cls).setUpClass()
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    11
        cls.config.global_set_option('language-mode', 'url-prefix')
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    12
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    13
    def test_login_password_login_lang_prefix(self):
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    14
        res = self.webapp.post('/fr/login', {
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    15
            '__login': self.admlogin, '__password': self.admpassword})
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    16
        self.assertEqual(res.status_int, 303)
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    17
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    18
        res = self.webapp.get('/fr/login')
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    19
        self.assertEqual(res.status_int, 303)
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    20
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    21
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
class LoginTest(PyramidCWTest):
11924
4c949c28ce59 [pyramid] fix login route with language-mode = url-prefix
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 11816
diff changeset
    24
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
    def test_login_form(self):
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
        res = self.webapp.get('/login')
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
        self.assertIn('__login', res.text)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
    def test_login_password_login(self):
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
        res = self.webapp.post('/login', {
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
            '__login': self.admlogin, '__password': self.admpassword})
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
        self.assertEqual(res.status_int, 303)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
        res = self.webapp.get('/login')
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
        self.assertEqual(res.status_int, 303)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
    def test_login_password_login_cookie_expires(self):
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
        res = self.webapp.post('/login', {
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
            '__login': self.admlogin, '__password': self.admpassword})
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
        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
    41
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    42
        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
    43
        self.assertNotIn('pauth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    44
        self.assertIn('auth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    45
        self.assertIsNone(cookies['auth_tkt'].expires)
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
        res = self.webapp.get('/logout')
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
        self.assertEqual(res.status_int, 303)
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
11562
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    50
        self.assertNotIn('auth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    51
        self.assertNotIn('pauth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    52
11559
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
        res = self.webapp.post('/login', {
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
            '__login': self.admlogin, '__password': self.admpassword,
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
            '__setauthcookie': 1})
136dd873dca2 [login] Test the login views
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
        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
    57
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    58
        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
    59
        self.assertNotIn('auth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    60
        self.assertIn('pauth_tkt', cookies)
a49f08423f02 [auth] Use a second authtkt policy for 'rememberme'
Christophe de Vienne <christophe@unlish.com>
parents: 11559
diff changeset
    61
        self.assertIsNotNone(cookies['pauth_tkt'].expires)
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    62
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
    63
    def test_login_bad_password(self):
11816
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    64
        self.config.i18ncompile(['en', 'fr'])
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    65
        try:
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    66
            self.config._gettext_init()
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    67
            res = self.webapp.post(
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    68
                '/login',
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    69
                {'__login': self.admlogin, '__password': 'empty'},
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    70
                headers={'Accept-Language': 'fr'},
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    71
                status=403)
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    72
        finally:
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    73
            rmtree(join(self.config.apphome, 'i18n'))
6392f34fcdad [pyramid] Fix login error message language
Florent Cayré <florent.cayre@logilab.fr>
parents: 11631
diff changeset
    74
        self.assertIn(u"\xc9chec de l'authentification", res.text)
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
    75
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    76
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    77
if __name__ == '__main__':
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    78
    from unittest import main
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11562
diff changeset
    79
    main()