# Path: gargantua/utils/encryt.py
# def generate_passwd(passwd):
#     return bcrypt.hashpw(passwd.encode('utf8'), bcrypt.gensalt(len(passwd))).decode('utf8')
# 
# def validate_passwd(passwd, hashed):
#     return bcrypt.hashpw(passwd.encode('utf8'), hashed.encode('utf8')) == hashed.encode('utf8')
# 
# def generate_token(json_, secret=SECRET_KEY):
#     return jwt.encode(json_, secret, algorithm='HS512').decode()
# 
# def validate_token(token, secret=SECRET_KEY):
#     return decode_token(token, secret=secret) is not None
# 
# Path: gargantua/utils/markdown.py
# def render_md_to_html(content, is_extract_menu=False):
#     html = markdown2.markdown(content, extras=['fenced-code-blocks', 'footnotes', 'tables'])
#     html = '<div>{}</div>'.format(html)
#     html = TITLE_REG.sub(r'<\2 id="\3">\3</\2>', html)
#     html = html.replace('class="codehilite"', 'class="codehilite highlight"')
#     h2_count = h3_count = 1
#     if is_extract_menu:
#         title_menu = TitleMenu()
#         for cont, level, title in TITLE_REG.findall(html):
#             title_menu.add_title(level, title)
#             if level == 'h2':  # 给 h2 添加中文序号
#                 serial = convert2chn_serial(h2_count)
#                 h2_count += 1
#                 h3_count = 1
#             elif level == 'h3':  # 给 h3 添加序号
#                 serial = '{}'.format(h3_count)
#                 h3_count += 1
# 
#             new_title = '<{level} id="{title}">{serial}、{title}</{level}>'\
#                 .format(level=level, title=title, serial=serial)
#             html = html.replace(cont, new_title)
# 
#         return html, title_menu.render()
#     else:
#         return html


import random
import jwt
from string import ascii_lowercase
from unittest import TestCase
from gargantua.utils import render_md_to_html, \
    generate_passwd, validate_passwd, generate_token, validate_token
class TestMarkdown(TestCase):

    def test_render_md_to_html(self):
        scenarios = (
            {'md': 'title\n===', 'expect': '<div class="highlight"><h1>title</h1>\n</div>'},
            {'md': '#1\n##2', 'expect': '<div class="highlight"><h1>1</h1>\n\n<h2 id="2">2</h2>\n</div>'},
        )

        for scena in scenarios:
            self.assertEqual(render_md_to_html(scena['md']), scena['expect'])


class TestEncrypt(TestCase):

    def generate_random(self):
        return ''.join([random.choice(ascii_lowercase) for _ in range(15)])

    def test_bcrypt(self):
        passwd = self.generate_random()
        wrong = passwd[:-1] + '0'
        hashed = generate_passwd(passwd)
        self.assertTrue(validate_passwd(passwd, hashed))
        self.assertFalse(validate_passwd(wrong, hashed))

    def test_jwt(self):
        passwd = self.generate_random()
        wrong = passwd[:-1] + '0'
        j = {'username': 'laisky'}
        token = generate_token(j, passwd)
        self.assertTrue(validate_token(token, passwd))

# Path: gargantua/utils/encryt.py
# def generate_passwd(passwd):
#     return bcrypt.hashpw(passwd.encode('utf8'), bcrypt.gensalt(len(passwd))).decode('utf8')
# 
# def validate_passwd(passwd, hashed):
#     return bcrypt.hashpw(passwd.encode('utf8'), hashed.encode('utf8')) == hashed.encode('utf8')
# 
# def generate_token(json_, secret=SECRET_KEY):
#     return jwt.encode(json_, secret, algorithm='HS512').decode()
# 
# def validate_token(token, secret=SECRET_KEY):
#     return decode_token(token, secret=secret) is not None
# 
# Path: gargantua/utils/markdown.py
# def render_md_to_html(content, is_extract_menu=False):
#     html = markdown2.markdown(content, extras=['fenced-code-blocks', 'footnotes', 'tables'])
#     html = '<div>{}</div>'.format(html)
#     html = TITLE_REG.sub(r'<\2 id="\3">\3</\2>', html)
#     html = html.replace('class="codehilite"', 'class="codehilite highlight"')
#     h2_count = h3_count = 1
#     if is_extract_menu:
#         title_menu = TitleMenu()
#         for cont, level, title in TITLE_REG.findall(html):
#             title_menu.add_title(level, title)
#             if level == 'h2':  # 给 h2 添加中文序号
#                 serial = convert2chn_serial(h2_count)
#                 h2_count += 1
#                 h3_count = 1
#             elif level == 'h3':  # 给 h3 添加序号
#                 serial = '{}'.format(h3_count)
#                 h3_count += 1
# 
#             new_title = '<{level} id="{title}">{serial}、{title}</{level}>'\
#                 .format(level=level, title=title, serial=serial)
#             html = html.replace(cont, new_title)
# 
#         return html, title_menu.render()
#     else:
#         return html


# Path: tests/test_utils.py
import random
import jwt
from string import ascii_lowercase
from unittest import TestCase
from gargantua.utils import render_md_to_html, \
    generate_passwd, validate_passwd, generate_token, validate_token

class TestMarkdown(TestCase):

    def test_render_md_to_html(self):
        scenarios = (
            {'md': 'title\n===', 'expect': '<div class="highlight"><h1>title</h1>\n</div>'},
            {'md': '#1\n##2', 'expect': '<div class="highlight"><h1>1</h1>\n\n<h2 id="2">2</h2>\n</div>'},
        )

        for scena in scenarios:
            self.assertEqual(render_md_to_html(scena['md']), scena['expect'])


class TestEncrypt(TestCase):

    def generate_random(self):
        return ''.join([random.choice(ascii_lowercase) for _ in range(15)])

    def test_bcrypt(self):
        passwd = self.generate_random()
        wrong = passwd[:-1] + '0'
        hashed = generate_passwd(passwd)
        self.assertTrue(validate_passwd(passwd, hashed))
        self.assertFalse(validate_passwd(wrong, hashed))

    def test_jwt(self):
        passwd = self.generate_random()
        wrong = passwd[:-1] + '0'
        j = {'username': 'laisky'}
        token = generate_token(j, passwd)
        self.assertTrue(validate_token(token, passwd))

        self.assertRaises(jwt.DecodeError, validate_token, token, wrong)