# Path: virtool/users/utils.py
# def hash_password(password: str) -> str:
#     """
#     Salt and hash a unicode password. Uses bcrypt.
# 
#     :param password: a password string to salt and hash
#     :return: a salt and hashed password
# 
#     """
#     return bcrypt.hashpw(password.encode(), bcrypt.gensalt(12))


import hashlib
import pytest
import virtool.errors
from aiohttp.test_utils import make_mocked_coro
from virtool.db.transforms import apply_transforms
from virtool.users.db import (
    AttachUserTransform,
    B2CUserAttributes,
    compose_force_reset_update,
    compose_groups_update,
    compose_password_update,
    compose_primary_group_update,
    create,
    edit,
    find_or_create_b2c_user,
    generate_handle,
    update_sessions_and_keys,
    validate_credentials,
)
from virtool.users.utils import hash_password
from virtool.utils import random_alphanumeric

@pytest.mark.parametrize(
    "user_id,password,result",
    [
        ("test", "foobar", True),
        ("baz", "foobar", False),
        ("test", "baz", False),
        ("baz", "baz", False),
    ],
)
@pytest.mark.parametrize("legacy", [True, False])
async def test_validate_credentials(legacy, user_id, password, result, dbi):
    """
    Test that valid, bcrypt-based credentials work.

    """
    document = {"_id": "test"}

    if legacy:
        salt = random_alphanumeric(24)

        document.update(
            {
                "salt": salt,
                "password": hashlib.sha512(
                    salt.encode("utf-8") + "foobar".encode("utf-8")
                ).hexdigest(),
            }
        )
    else:

# Path: virtool/users/utils.py
# def hash_password(password: str) -> str:
#     """
#     Salt and hash a unicode password. Uses bcrypt.
# 
#     :param password: a password string to salt and hash
#     :return: a salt and hashed password
# 
#     """
#     return bcrypt.hashpw(password.encode(), bcrypt.gensalt(12))


# Path: tests/users/test_db.py
import hashlib
import pytest
import virtool.errors
from aiohttp.test_utils import make_mocked_coro
from virtool.db.transforms import apply_transforms
from virtool.users.db import (
    AttachUserTransform,
    B2CUserAttributes,
    compose_force_reset_update,
    compose_groups_update,
    compose_password_update,
    compose_primary_group_update,
    create,
    edit,
    find_or_create_b2c_user,
    generate_handle,
    update_sessions_and_keys,
    validate_credentials,
)
from virtool.users.utils import hash_password
from virtool.utils import random_alphanumeric


@pytest.mark.parametrize(
    "user_id,password,result",
    [
        ("test", "foobar", True),
        ("baz", "foobar", False),
        ("test", "baz", False),
        ("baz", "baz", False),
    ],
)
@pytest.mark.parametrize("legacy", [True, False])
async def test_validate_credentials(legacy, user_id, password, result, dbi):
    """
    Test that valid, bcrypt-based credentials work.

    """
    document = {"_id": "test"}

    if legacy:
        salt = random_alphanumeric(24)

        document.update(
            {
                "salt": salt,
                "password": hashlib.sha512(
                    salt.encode("utf-8") + "foobar".encode("utf-8")
                ).hexdigest(),
            }
        )
    else:

        document["password"] = hash_password("foobar")