<commit_before>from cherrypy.test import test
test.prefer_parent_path()

import md5, sha

import cherrypy
from cherrypy.lib import httpauth

def setup_server():
    class Root:
        def index(self):
            return "This is public."
        index.exposed = True

    class DigestProtected:
        def index(self):
            return "Hello %s, you've been authorized." % cherrypy.request.login
        index.exposed = True

    class BasicProtected:
        def index(self):
            return "Hello %s, you've been authorized." % cherrypy.request.login
        index.exposed = True

    class BasicProtected2:
        def index(self):
            return "Hello %s, you've been authorized." % cherrypy.request.login
        index.exposed = True

    def fetch_users():
        return {'test': 'test'}

    def sha_password_encrypter(password):
        return sha.new(password).hexdigest()
    
    def fetch_password(username):
        return sha.new('test').hexdigest()

    conf = {'/digest': {'tools.digest_auth.on': True,
                        'tools.digest_auth.realm': 'localhost',
                        'tools.digest_auth.users': fetch_users},
            '/basic': {'tools.basic_auth.on': True,
                       'tools.basic_auth.realm': 'localhost',
                       'tools.basic_auth.users': {'test': md5.new('test').hexdigest()}},
            '/basic2': {'tools.basic_auth.on': True,
                        'tools.basic_auth.realm': 'localhost',
                        'tools.basic_auth.users': fetch_password,
                        'tools.basic_auth.encrypt': sha_password_encrypter}}
            
    root = Root()
    root.digest = DigestProtected()
    root.basic = BasicProtected()
    root.basic2 = BasicProtected2()
    cherrypy.tree.mount(root, config=conf)

from cherrypy.test import helper

class HTTPAuthTest(helper.CPWebCase):

    def testPublic(self):
        self.getPage("/")
        self.assertStatus('200 OK')
        self.assertHeader('Content-Type', 'text/html;charset=utf-8')
        self.assertBody('This is public.')

    def testBasic(self):
        self.getPage("/basic/")<commit_msg>Upgraded test_httpauth to hashlib in Python 2.5+.<commit_after>from cherrypy.test import test
test.prefer_parent_path()

try:
    # Python 2.5+
    from hashlib import md5, sha1 as sha
except ImportError:
    from md5 import new as md5
    from sha import new as sha

import cherrypy
from cherrypy.lib import httpauth

def setup_server():
    class Root:
        def index(self):
            return "This is public."
        index.exposed = True

    class DigestProtected:
        def index(self):
            return "Hello %s, you've been authorized." % cherrypy.request.login
        index.exposed = True

    class BasicProtected:
        def index(self):
            return "Hello %s, you've been authorized." % cherrypy.request.login
        index.exposed = True

    class BasicProtected2:
        def index(self):
            return "Hello %s, you've been authorized." % cherrypy.request.login
        index.exposed = True

    def fetch_users():
        return {'test': 'test'}

    def sha_password_encrypter(password):
        return sha(password).hexdigest()
    
    def fetch_password(username):
        return sha('test').hexdigest()

    conf = {'/digest': {'tools.digest_auth.on': True,
                        'tools.digest_auth.realm': 'localhost',
                        'tools.digest_auth.users': fetch_users},
            '/basic': {'tools.basic_auth.on': True,
                       'tools.basic_auth.realm': 'localhost',
                       'tools.basic_auth.users': {'test': md5('test').hexdigest()}},
            '/basic2': {'tools.basic_auth.on': True,
                        'tools.basic_auth.realm': 'localhost',
                        'tools.basic_auth.users': fetch_password,
                        'tools.basic_auth.encrypt': sha_password_encrypter}}
            
    root = Root()
    root.digest = DigestProtected()
    root.basic = BasicProtected()
    root.basic2 = BasicProtected2()
    cherrypy.tree.mount(root, config=conf)

from cherrypy.test import helper

class HTTPAuthTest(helper.CPWebCase):

    def testPublic(self):
        self.getPage("/")
        self.assertStatus('200 OK')
        self.assertHeader('Content-Type', 'text/html;charset=utf-8')
        self.assertBody('This is public.')

    def testBasic(self):
        self.getPage("/basic/")
{'fraction_non_alphanumeric': 0.1197752808988764, 'fraction_numerical': 0.006067415730337079, 'mean_word_length': 2.237090909090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7195829', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1253, 'n_words': 290}