<commit_before>import os
import json
import pwd
from typtop.dbaccess import (
    UserTypoDB, DB_NAME, get_time,
    on_wrong_password, on_correct_password, logT, logT_cols, auxT,
    find_one, FREQ_COUNTS, INDEX_J, WAITLIST_SIZE, WAIT_LIST,
    compute_id, WARM_UP_CACHE, pkdecrypt, pkencrypt,
    NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN
)
import typtop.config as config
import typtop.dbaccess as dbaccess
import yaml
import pytest
import re
WARM_UP_CACHE = False
NN = 5
secretAuxSysT = "SecretAuxData"
ORIG_PW_ID = 'OrgPwID'

NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = 30
dbaccess.WARM_UP_CACHE = 0

def get_username():
    return pwd.getpwuid(os.getuid()).pw_name

def DB_path():
    # TODO _ for some reason it does't work
    user = get_username()
    db = UserTypoDB(user, debug_mode=True)
    return db.get_db_path()
    #return "/home/{}/{}.db".format(get_username(), DB_NAME)

def remove_DB():
    os.remove(DB_path())

def start_DB():
    remove_DB()
    db = UserTypoDB(get_username(), debug_mode=True)
    db.init_typtop(get_pw(), allow_typo_login=True)
    return db

def test_warmed_cache():
    t1, dbaccess.WARM_UP_CACHE = dbaccess.WARM_UP_CACHE, 1
    t2, dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN, 0
    typoDB = start_DB()
    assert typoDB.check(pws[1]), pws[1]
    assert typoDB.check(pws[0]), pws[0]
    dbaccess.WARM_UP_CACHE, dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = t1, t2

def count_real_typos_in_cache(t_db, PW_CHANGE=False):
    flist_ctx = t_db.get_from_auxtdb(FREQ_COUNTS, yaml.load)
    f_list_all = json.loads(pkdecrypt(t_db._sk, flist_ctx))
    f_list = [f for f in f_list_all if f>0]
    return len(f_list), sum(f_list)

def test_login_settings():
    typoDB = start_DB()
    #db = typoDB.getdb()
    assert typoDB.is_allowed_login()
    typoDB.allow_login(allow=False)
    assert not typoDB.is_allowed_login()
    typoDB.allow_login()
    assert typoDB.is_allowed_login()

def test_waitlist(isStandAlone=True):
    typoDB = start_DB()
    pwset = set(pws[:4])
    for i in range(4):
        typoDB.check(pws[i])
    typos_in_waitlist = set()
    for typo_ctx in typoDB.get_from_auxtdb(WAIT_LIST):
        typo_txt = pkdecrypt(typoDB._sk, typo_ctx)
        if re.match(r'\[".*", ".*"\]', typo_txt):
            typo, ts = yaml.safe_load(typo_txt)
            typos_in_waitlist.add(typo)
    assert not (typos_in_waitlist - pwset) and not (pwset - typos_in_waitlist)

def test_add_to_cache(isStandAlone=True):
    typoDB = start_DB()
    indexj = typoDB.get_from_auxtdb(INDEX_J, int)
    typoDB.check(pws[0])
    typoDB.check(pws[0])
    typoDB.check(pws[1])
    typoDB.check(pws[5])
    typoDB.check(pws[2])
    assert (typoDB.get_from_auxtdb(INDEX_J, int) - indexj) % WAITLIST_SIZE == 5
    typoDB.check(get_pw())
    # ntypo, fcount = count_real_typos_in_cache(typoDB)
    # assert ntypo == 3
    # assert fcount > 5

    # No idea what the followig is doing.
    # sk_dict1, isIn_t1 = typoDB.fetch_from_cache(pws[0], False, False)
    # t1_h,_ = sk_dict1.popitem()
    # assert isIn_t1
    # assert hash_t.count(H_typo=t1_h) == 1
    # assert
    # assert hash_t.count(H_typo=t5_h) == 1
    if isStandAlone:<commit_msg>Fixed the broken tests<commit_after>import os
import json
import pwd
from typtop.dbaccess import (
    UserTypoDB, DB_NAME, get_time,
    on_wrong_password, on_correct_password, logT, logT_cols, auxT,
    find_one, FREQ_COUNTS, INDEX_J, WAITLIST_SIZE, WAIT_LIST,
    compute_id, WARM_UP_CACHE, pkdecrypt, pkencrypt,
    NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN
)
import typtop.config as config
import typtop.dbaccess as dbaccess
import yaml
import pytest
import re
WARM_UP_CACHE = False
NN = 5
secretAuxSysT = "SecretAuxData"
ORIG_PW_ID = 'OrgPwID'

NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = 30
dbaccess.WARM_UP_CACHE = 0

def get_username():
    user='rahul'
    # return pwd.getpwuid(os.getuid()).pw_name
    return user

def DB_path():
    # TODO _ for some reason it does't work
    user = get_username()
    db = UserTypoDB(user, debug_mode=True)
    return db.get_db_path()
    #return "/home/{}/{}.db".format(get_username(), DB_NAME)

def remove_DB():
    os.remove(DB_path())

def start_DB():
    remove_DB()
    db = UserTypoDB(get_username(), debug_mode=True)
    db.init_typtop(get_pw(), allow_typo_login=True)
    return db

def test_warmed_cache():
    t1, dbaccess.WARM_UP_CACHE = dbaccess.WARM_UP_CACHE, 1
    t2, dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN, 0
    typoDB = start_DB()
    assert typoDB.check(pws[1]), pws[1]
    assert typoDB.check(pws[0]), pws[0]
    dbaccess.WARM_UP_CACHE, dbaccess.NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN = t1, t2

def count_real_typos_in_cache(t_db, PW_CHANGE=False):
    flist_ctx = t_db.get_from_auxtdb(FREQ_COUNTS, yaml.load)
    f_list_all = json.loads(pkdecrypt(t_db._sk, flist_ctx))
    f_list = [f for f in f_list_all if f>0]
    return len(f_list), sum(f_list)

def test_login_settings():
    typoDB = start_DB()
    #db = typoDB.getdb()
    assert typoDB.is_allowed_login()
    typoDB.allow_login(allow=False)
    assert not typoDB.is_allowed_login()
    typoDB.allow_login()
    assert typoDB.is_allowed_login()

def test_waitlist(isStandAlone=True):
    typoDB = start_DB()
    pwset = set(pws[:4])
    for i in range(4):
        typoDB.check(pws[i])
    typos_in_waitlist = set()
    install_id = typoDB.get_installation_id()
    for typo_ctx in typoDB.get_from_auxtdb(WAIT_LIST):
        typo_txt = pkdecrypt(typoDB._sk, typo_ctx)
        typo, ts = yaml.safe_load(typo_txt)
        if not typo.startswith(install_id):
            typos_in_waitlist.add(typo)
    assert not (typos_in_waitlist - pwset) and not (pwset - typos_in_waitlist)

def test_add_to_cache(isStandAlone=True):
    typoDB = start_DB()
    indexj = typoDB.get_from_auxtdb(INDEX_J, int)
    typoDB.check(pws[0])
    typoDB.check(pws[0])
    typoDB.check(pws[1])
    typoDB.check(pws[5])
    typoDB.check(pws[2])
    assert (typoDB.get_from_auxtdb(INDEX_J, int) - indexj) % WAITLIST_SIZE == 5
    typoDB.check(get_pw())
    # ntypo, fcount = count_real_typos_in_cache(typoDB)
    # assert ntypo == 3
    # assert fcount > 5

    # No idea what the followig is doing.
    # sk_dict1, isIn_t1 = typoDB.fetch_from_cache(pws[0], False, False)
    # t1_h,_ = sk_dict1.popitem()
    # assert isIn_t1
    # assert hash_t.count(H_typo=t1_h) == 1
    # assert
    # assert hash_t.count(H_typo=t5_h) == 1
    if isStandAlone:
{'fraction_non_alphanumeric': 0.10902483343428225, 'fraction_numerical': 0.010599636583888553, 'mean_word_length': 4.0846805234796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2813221', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2758, 'n_words': 477}