| 1 |
# CherryPy |
| 2 |
# Copyright (C) 2026 IKUS Software |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import tempfile |
| 18 |
from collections import namedtuple |
| 19 |
from urllib.parse import urlencode |
| 20 |
|
| 21 |
import cherrypy |
| 22 |
from cherrypy.lib.sessions import FileSession |
| 23 |
from cherrypy.test import helper |
| 24 |
|
| 25 |
from .. import auth # noqa |
| 26 |
|
| 27 |
User = namedtuple('User', 'id,username') |
| 28 |
|
| 29 |
|
| 30 |
def checkpassword(username, password): |
| 31 |
return username == 'myuser' and password == 'changeme' |
| 32 |
|
| 33 |
|
| 34 |
def user_lookup_func(login, user_info): |
| 35 |
if login == 'myuser': |
| 36 |
return login, User(2, login) |
| 37 |
return None |
| 38 |
|
| 39 |
|
| 40 |
def user_from_key_func(userkey): |
| 41 |
if userkey == 'myuser': |
| 42 |
return User(2, 'myuser') |
| 43 |
return None |
| 44 |
|
| 45 |
|
| 46 |
@cherrypy.tools.sessions(locking='explicit', storage_class=FileSession) |
| 47 |
@cherrypy.tools.auth( |
| 48 |
user_lookup_func=user_lookup_func, |
| 49 |
user_from_key_func=user_from_key_func, |
| 50 |
checkpassword=checkpassword, |
| 51 |
) |
| 52 |
class Root: |
| 53 |
|
| 54 |
@cherrypy.expose |
| 55 |
def index(self): |
| 56 |
return "OK" |
| 57 |
|
| 58 |
@cherrypy.expose() |
| 59 |
def login(self, username=None, password=None): |
| 60 |
if cherrypy.serving.request.method == 'POST' and username and password: |
| 61 |
userobj = cherrypy.tools.auth.login_with_credentials(username, password) |
| 62 |
if userobj: |
| 63 |
raise cherrypy.tools.auth.redirect_to_original_url() |
| 64 |
else: |
| 65 |
return "invalid credentials" |
| 66 |
return "login" |
| 67 |
|
| 68 |
|
| 69 |
class AuthManagerTest(helper.CPWebCase): |
| 70 |
interactive = False |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def setup_class(cls): |
| 74 |
cls.tempdir = tempfile.TemporaryDirectory(prefix='cherrypy-foundation-', suffix='-session') |
| 75 |
cls.session_dir = cls.tempdir.name |
| 76 |
super().setup_class() |
| 77 |
|
| 78 |
@classmethod |
| 79 |
def teardown_class(cls): |
| 80 |
cls.tempdir.cleanup() |
| 81 |
super().teardown_class() |
| 82 |
|
| 83 |
@classmethod |
| 84 |
def setup_server(cls): |
| 85 |
cherrypy.config.update( |
| 86 |
{ |
| 87 |
'tools.sessions.storage_path': cls.session_dir, |
| 88 |
} |
| 89 |
) |
| 90 |
cherrypy.tree.mount(Root(), '/') |
| 91 |
|
| 92 |
def test_auth_redirect(self): |
| 93 |
# Given unauthenticated user |
| 94 |
# When requesting protected page |
| 95 |
self.getPage('/') |
| 96 |
# Then user is redirected to login page |
| 97 |
self.assertStatus(303) |
| 98 |
self.assertHeaderItemValue('Location', f'http://{self.HOST}:{self.PORT}/login/') |
| 99 |
|
| 100 |
def test_auth_login(self): |
| 101 |
# Given unauthenticated user |
| 102 |
# When posting valid login |
| 103 |
self.getPage('/login/', method='POST', body=urlencode({'username': 'myuser', 'password': 'changeme'})) |
| 104 |
# Then user is redirect to index. |
| 105 |
self.assertStatus(303) |
| 106 |
self.assertHeaderItemValue('Location', f'http://{self.HOST}:{self.PORT}/') |
| 107 |
# Then page is accessible |
| 108 |
self.getPage('/', headers=self.cookies) |
| 109 |
self.assertStatus(200) |
| 110 |
self.assertInBody('OK') |