# Path: virtool/users/utils.py
# PERMISSIONS = [
#     "cancel_job",
#     "create_ref",
#     "create_sample",
#     "modify_hmm",
#     "modify_subtraction",
#     "remove_file",
#     "remove_job",
#     "upload_file",
# ]
# 
# 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 pytest
from virtool.users.utils import PERMISSIONS, hash_password

@pytest.mark.parametrize("value", ["valid_email", "invalid_email"])
async def test_is_valid_email(value, spawn_client, resp_is):
    """
    Tests that when an invalid email is used, validators.is_valid_email raises a 422 error.
    """
    client = await spawn_client(authorize=True)

    data = {
        "email": "valid@email.ca" if value == "valid_email" else "-foo-bar-@baz!.ca",
        "old_password": "old_password",
        "password": "password",
    }

    resp = await client.patch("/account", data=data)

    if value == "valid_email":
        await resp_is.bad_request(resp, "Invalid credentials")
    else:
        await resp_is.invalid_input(resp, {"email": ["Not a valid email"]})


@pytest.mark.parametrize("error", [None, "wrong_handle", "wrong_password"])
async def test_login(spawn_client, create_user, resp_is, error, mocker):
    client = await spawn_client()

    await client.db.users.insert_one(
        {
            "user_id": "abc123",
            "handle": "foobar",

# Path: virtool/users/utils.py
# PERMISSIONS = [
#     "cancel_job",
#     "create_ref",
#     "create_sample",
#     "modify_hmm",
#     "modify_subtraction",
#     "remove_file",
#     "remove_job",
#     "upload_file",
# ]
# 
# 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/account/test_api.py
import pytest
from virtool.users.utils import PERMISSIONS, hash_password


@pytest.mark.parametrize("value", ["valid_email", "invalid_email"])
async def test_is_valid_email(value, spawn_client, resp_is):
    """
    Tests that when an invalid email is used, validators.is_valid_email raises a 422 error.
    """
    client = await spawn_client(authorize=True)

    data = {
        "email": "valid@email.ca" if value == "valid_email" else "-foo-bar-@baz!.ca",
        "old_password": "old_password",
        "password": "password",
    }

    resp = await client.patch("/account", data=data)

    if value == "valid_email":
        await resp_is.bad_request(resp, "Invalid credentials")
    else:
        await resp_is.invalid_input(resp, {"email": ["Not a valid email"]})


@pytest.mark.parametrize("error", [None, "wrong_handle", "wrong_password"])
async def test_login(spawn_client, create_user, resp_is, error, mocker):
    client = await spawn_client()

    await client.db.users.insert_one(
        {
            "user_id": "abc123",
            "handle": "foobar",

            "password": hash_password("p@ssword123"),