<commit_before>"""
Testing Module
"""

import pytest
import api.user
import api.common

from common import clear_collections

@pytest.mark.usefixtures("db")
class TestUsers(object):
    """
    API Tests for User.py
    """
    @clear_collections("user")
    def test_create_batch_normal_users(self, users=100):
        """
        Tests user creation.

        Covers:
            user.create_user
            user.get_all_users
            user.get_user
        """

        uids = []
        for i in range(users):
            name = "fred" + str(i)
            uids.append(api.user.create_user(
                name, name + <EMAIL>", "HASH"
            ))

        assert len(set(uids)) == len(uids), "UIDs are not unique."

        assert len(api.user.get_all_users()) == users and \
            users == len(uids), "Not all users were created."

        for i, uid in enumerate(uids):
            name = "fred" + str(i)

            user_from_uid = api.user.get_user(uid=uid)
            user_from_name = api.user.get_user(name=name)

            assert user_from_uid == user_from_name, "User lookup from uid and name are not the same."

    def test_register_user(self, db):
        """
        Tests the registration and form validation functionality.
        """

        pass
<commit_msg>Added update_password test.<commit_after>"""
Testing Module
"""

import pytest
import api.user
import api.common
import bcrypt

from api.common import APIException
from common import clear_collections

@pytest.mark.usefixtures("db")
class TestUsers(object):
    """
    API Tests for User.py
    """

    @clear_collections("user")
    def test_create_batch_normal_users(self, users=100):
        """
        Tests user creation.

        Covers:
            user.create_user
            user.get_all_users
            user.get_user
        """

        uids = []
        for i in range(users):
            name = "fred" + str(i)
            uids.append(api.user.create_user(
                name, name + <EMAIL>", name
            ))

        assert len(set(uids)) == len(uids), "UIDs are not unique."

        assert len(api.user.get_all_users()) == users and \
            users == len(uids), "Not all users were created."

        for i, uid in enumerate(uids):
            name = "fred" + str(i)

            user_from_uid = api.user.get_user(uid=uid)
            user_from_name = api.user.get_user(name=name)

            assert user_from_uid == user_from_name, "User lookup from uid and name are not the same."

    #TODO: Team should be tested first
    @clear_collections("user", "team")
    def test_register_user(self, db):
        """
        Tests the registration and form validation functionality.
        """
        pass

    @clear_collections("user")
    def test_change_pw_user(self):
        """
        Tests password change functionality.

        Covers:
            user.update_password
            user.hash_password
        Depends:
            user.get_user
        """
        uid = api.user.create_user("fred", "<EMAIL>", "HASH")

        old_hash = api.user.get_user(uid=uid)["pwhash"]
        assert old_hash == "HASH", "Was unable to confirm password was stored correctly."

        api.user.update_password(uid, "HACK")

        new_hash = api.user.get_user(uid=uid)["pwhash"]

        assert bcrypt.hashpw("HACK", new_hash) == new_hash, \
            "Password does not match hashed plaintext after changing it."

        with pytest.raises(APIException):
            api.user.update_password(uid, "")
            assert False, "Should not be able to update password to nothing."



{'fraction_non_alphanumeric': 0.10954356846473029, 'fraction_numerical': 0.0016597510373443983, 'mean_word_length': 2.1092003439380913, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '4082381', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1060, 'n_words': 272}