<commit_before>from unittest.mock import Mock, patch
from werkzeug.security import check_password_hash
from project.bl import UserBL
from project.tests.utils import ProjectTestCase
from project.models import User, Vacancy
import re


celery_send_mail = Mock(return_value=None)


class TestUserBL(ProjectTestCase):

    def test_set_password(self):
        """Check password setter correctness"""
        raw_pass = '<PASSWORD>'
        instance = Mock(password=None)
        bl = UserBL(instance)
        bl.set_password(raw_pass)
        self.assertIsNotNone(instance.password)
        self.assertTrue(check_password_hash(instance.password, raw_pass))

    @patch('project.tasks.mail.celery_send_mail.delay')
    def test_reset_password(self, send_mail):
        orig_password = '<PASSWORD>'
        email = '<EMAIL>'
        login = 'nightmaremoon'
        data = {
            'login': login,
            'password': <PASSWORD>,
            'email': email,
            'name': 'Luna',
            'surname': 'Princess',
        }
        User.bl.create(data)
        User.bl.forgot_password(email)
        self.assertEqual(send_mail.call_count, 1)
        mail, *_ = send_mail.call_args[0]
        del _
        self.assertIsNotNone(
            mail,
            msg='Mail was not sent',
        )
        self.assertEqual(
            mail.subject,
            "Cброс пароля на HR портале",
            msg="Incorrect mail subject",
        )
        self.assertEqual(
            mail.recipients,
            [email],
            msg="Incorrect recipients",
        )
        match = re.search(
            r'/auth/reset/(?P<token>\w{20})',
            mail.body,
        )
        del mail
        token = match.groupdict()['token'] if match else None
        is_success = User.bl.reset_password(token)
        self.assertTrue(
            is_success,
            msg='Resetting password failed',
        )
        self.assertEqual(send_mail.call_count, 2)
        mail, *_ = send_mail.call_args[0]
        del _
        self.assertIsNotNone(
            mail,
            msg='Mail was not sent',
        )
        self.assertEqual(
            mail.recipients,
            [email],
            msg="Incorrect recipients",
        )
        usr = User.bl.authenticate(login, orig_password)
        self.assertIsNone(
            usr,
            msg="User authenticated w/ old password",
        )
        del usr
        match = re.search(
            r'Новый пароль: (?P<new_password>\w{8})',
            mail.body,
        )
        del mail
        new_password = match.groupdict()['new_password'] if match else None
        usr = User.bl.authenticate(login, new_password)
        self.assertIsNotNone(
            usr,
            msg="User was not authenticated with new password"
        )

    @patch('project.tasks.mail.celery_send_mail.delay')
    def test_reset_password_bad_token(self, send_mail):
        fake_token = '-1!~ -2'
        is_success = UserBL.reset_password(fake_token)
        self.assertFalse(
            is_success,
            msg='resetToken succeeded on bad token',
        )
        self.assertFalse(
            send_mail.called,
            msg='send_mail was called, while it shouldn\'t'
        )

    def test_create_with_password(self):
        password = '<PASSWORD>'
        email = '<EMAIL>'
        login = 'cadence'
        name = 'Cadence'
        surname = 'Princess'
        data = {
            'login': login,
            'password': password,
            'email': email,
            'name': name,
            'surname': surname,
        }
        User.bl.create(data)
        usr = User.bl.authenticate(login, password)
        self.assertIsNotNone(
            usr,
            msg="Cannot authenticate created user",
        )
        self.assertEqual(
            usr.name,
            name,
            msg='Names do not match',
        )
        self.assertEqual(
            usr.surname,
            surname,
            msg='Surnames do not match',
        )

    @patch('project.tasks.mail.celery_send_mail.delay')
    def test_create_without_password(self, send_mail):
        email = '<EMAIL>'
        login = 'celestia'
        name = 'Celestia'
        surname = 'Princess'
        data = {
            'login': login,
            'email': email,
            'name': name,
            'surname': surname,
        }
        User.bl.create(data)
        self.assertEqual(send_mail.call_count, 1)
        mail, *_ = send_mail.call_args[0]
        del _
        self.assertIsNotNone(
            mail,
            msg='Mail was not sent',
        )
        self.assertEqual(
            mail.subject,
            'Вам была создана учетная запись на HR портале!',
            msg='Incorrect mail subject',
        )
        self.assertEqual(
            mail.recipients,
            [email],
            msg='Incorrect recipients',
        )
        match = re.search(
            r'login: (?P<_login>\w+)\npassword:(?P<_password>\w{8})',
            mail.body,
        )
        _login = match.groupdict()['_login'] if match else None
        _password = match.groupdict()['_password'] if match else None
        self.assertEqual(
            _login,
            login,
            msg='Logins do not match!',
        )
        usr = User.bl.authenticate(login, _password)
        self.assertIsNotNone(
            usr,
            msg="Cannot authenticate created user",
        )
        self.assertEqual(
            usr.name,
            name,
            msg='Names do not match',
        )
        self.assertEqual(
            usr.surname,
            surname,
            msg='Surnames do not match',
        )


class TestVacancyBL(ProjectTestCase):
<commit_msg>create_superuser test<commit_after>from unittest.mock import Mock, patch
from werkzeug.security import check_password_hash
from project.bl import UserBL
from project.tests.utils import ProjectTestCase
from project.models import User, Vacancy
import re


celery_send_mail = Mock(return_value=None)


class TestUserBL(ProjectTestCase):

    def test_set_password(self):
        """Check password setter correctness"""
        raw_pass = 'celestia'
        instance = Mock(password=None)
        bl = UserBL(instance)
        bl.set_password(raw_pass)
        self.assertIsNotNone(instance.password)
        self.assertTrue(check_password_hash(instance.password, raw_pass))

    @patch('project.tasks.mail.celery_send_mail.delay')
    def test_reset_password(self, send_mail):
        orig_password = '<PASSWORD>'
        email = '<EMAIL>'
        login = 'nightmaremoon'
        data = {
            'login': login,
            'password': orig_password,
            'email': email,
            'name': 'Luna',
            'surname': 'Princess',
        }
        User.bl.create(data)
        User.bl.forgot_password(email)
        self.assertEqual(send_mail.call_count, 1)
        mail, *_ = send_mail.call_args[0]
        del _
        self.assertIsNotNone(
            mail,
            msg='Mail was not sent',
        )
        self.assertEqual(
            mail.subject,
            "Cброс пароля на HR портале",
            msg="Incorrect mail subject",
        )
        self.assertEqual(
            mail.recipients,
            [email],
            msg="Incorrect recipients",
        )
        match = re.search(
            r'/auth/reset/(?P<token>\w{20})',
            mail.body,
        )
        del mail
        token = match.groupdict()['token'] if match else None
        is_success = User.bl.reset_password(token)
        self.assertTrue(
            is_success,
            msg='Resetting password failed',
        )
        self.assertEqual(send_mail.call_count, 2)
        mail, *_ = send_mail.call_args[0]
        del _
        self.assertIsNotNone(
            mail,
            msg='Mail was not sent',
        )
        self.assertEqual(
            mail.recipients,
            [email],
            msg="Incorrect recipients",
        )
        usr = User.bl.authenticate(login, orig_password)
        self.assertIsNone(
            usr,
            msg="User authenticated w/ old password",
        )
        del usr
        match = re.search(
            r'Новый пароль: (?P<new_password>\w{8})',
            mail.body,
        )
        del mail
        new_password = match.groupdict()['new_password'] if match else None
        usr = User.bl.authenticate(login, new_password)
        self.assertIsNotNone(
            usr,
            msg="User was not authenticated with new password"
        )

    @patch('project.tasks.mail.celery_send_mail.delay')
    def test_reset_password_bad_token(self, send_mail):
        fake_token = <PASSWORD>'
        is_success = UserBL.reset_password(fake_token)
        self.assertFalse(
            is_success,
            msg='resetToken succeeded on bad token',
        )
        self.assertFalse(
            send_mail.called,
            msg='send_mail was called, while it shouldn\'t'
        )

    def test_create_with_password(self):
        password = '<PASSWORD>'
        email = '<EMAIL>'
        login = 'cadence'
        name = 'Cadence'
        surname = 'Princess'
        data = {
            'login': login,
            'password': password,
            'email': email,
            'name': name,
            'surname': surname,
        }
        User.bl.create(data)
        usr = User.bl.authenticate(login, password)
        self.assertIsNotNone(
            usr,
            msg="Cannot authenticate created user",
        )
        self.assertEqual(
            usr.name,
            name,
            msg='Names do not match',
        )
        self.assertEqual(
            usr.surname,
            surname,
            msg='Surnames do not match',
        )

    @patch('project.tasks.mail.celery_send_mail.delay')
    def test_create_without_password(self, send_mail):
        email = '<EMAIL>'
        login = 'celestia'
        name = 'Celestia'
        surname = 'Princess'
        data = {
            'login': login,
            'email': email,
            'name': name,
            'surname': surname,
        }
        User.bl.create(data)
        self.assertEqual(send_mail.call_count, 1)
        mail, *_ = send_mail.call_args[0]
        del _
        self.assertIsNotNone(
            mail,
            msg='Mail was not sent',
        )
        self.assertEqual(
            mail.subject,
            'Вам была создана учетная запись на HR портале!',
            msg='Incorrect mail subject',
        )
        self.assertEqual(
            mail.recipients,
            [email],
            msg='Incorrect recipients',
        )
        match = re.search(
            r'login: (?P<_login>\w+)\npassword:(?P<_password>\w{8})',
            mail.body,
        )
        _login = match.groupdict()['_login'] if match else None
        _password = match.groupdict()['_password'] if match else None
        self.assertEqual(
            _login,
            login,
            msg='Logins do not match!',
        )
        usr = User.bl.authenticate(login, _password)
        self.assertIsNotNone(
            usr,
            msg="Cannot authenticate created user",
        )
        self.assertEqual(
            usr.name,
            name,
            msg='Names do not match',
        )
        self.assertEqual(
            usr.surname,
            surname,
            msg='Surnames do not match',
        )

    def test_create_superuser(self):
        login = 'tirek'
        password = '<PASSWORD>'
        u = User.bl.create_superuser(login, password)
        self.assertIsNotNone(
            u,
            msg='created superuser is None',
        )
        self.assertEqual(
            u.role,
            User.ROLE.superuser,
            msg='created superuser is not actually a superuser',
        )
        del u
        u = User.bl.authenticate(login, password)
        self.assertIsNotNone(
            u,
            msg='Cannot authenticate created superuser',
        )
        self.assertEqual(
            u.role,
            User.ROLE.superuser,
            msg='authenticated superuser is not actually a superuser',
        )
        del u


class TestVacancyBL(ProjectTestCase):

{'fraction_non_alphanumeric': 0.10180570308031701, 'fraction_numerical': 0.0017975324781436392, 'mean_word_length': 1.6254826254826256, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '5415286', 'n_tokens_mistral': 3554, 'n_tokens_neox': 3443, 'n_words': 722}