<commit_before>import argparse
import json
import os
import runpy
import sys
from tempfile import NamedTemporaryFile
import unittest

try:
    from unittest.mock import patch, MagicMock
except ImportError:
    from mock import patch, MagicMock

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pysswords.db import Database, Credential
from pysswords.crypt import CryptOptions
import pysswords.__main__

try:
    FileNotFoundError
except NameError:
    FileNotFoundError = EnvironmentError

TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))


class PysswordsTests(unittest.TestCase):
    def setUp(self):
        self.db_file = NamedTemporaryFile(mode='w', delete=False)
        self.db_path = self.db_file.name
        self.testing_path = os.path.join(TEST_DIR,
                                         "data",
                                         "testing_database.db")
        self.db_file.close()
        self.password = <PASSWORD>"
        self.default_iterations = 1000

        self.crypt_options = CryptOptions(
            password=self.password,
            salt=None,
            iterations=1000
        )

        self.db = Database.create(
            path=self.db_path,
            crypt_options=self.crypt_options
        )

    def tearDown(self):
        try:
            os.remove(self.db_path)
        except FileNotFoundError:
            pass

        try:
            os.remove(self.testing_path)
        except FileNotFoundError:
            pass

    def some_credential(self, **kwargs):
        return Credential(
            name=kwargs.get("name", "example"),
            login=kwargs.get("login", "john"),
            password=kwargs.get("password", "<PASSWORD>"),
            login_url=kwargs.get("login_url", "http://example.org/login"),
            description=kwargs.get("description",
                                   "This is login credentials for example"),
        )

    def test_database_default_content(self):
        expected_content = json.dumps([{}])
        self.assertEqual(Database.DEFAULT_CONTENT, expected_content)

    def test_create_credential_database(self):
        database = Database.create(
            path=self.db_path,
            crypt_options=self.crypt_options
        )
        self.assertIsInstance(database, Database)
        self.assertEqual(len(database.credentials), 0)

    def test_add_new_credential(self):
        self.assertEqual(len(self.db.credentials), 0)
        self.db.add_credential(self.some_credential())
        self.assertEqual(len(self.db.credentials), 1)

    def test_delete_credential_by_name(self):
        self.db.add_credential(self.some_credential(name="example"))
        self.db.delete_credential(name="example")
        self.assertEqual(len(self.db.credentials), 0)

    def test_delete_credential_by_login(self):
        self.db.add_credential(self.some_credential(login="john"))
        self.db.delete_credential(login="john")
        self.assertEqual(len(self.db.credentials), 0)

    def test_delete_credential_by_name_and_login(self):
        credential = self.some_credential(name="github", login="example")
        credential2 = self.some_credential(name="github", login="john")

        self.db.add_credential(credential)
        self.db.add_credential(credential2)

        self.db.delete_credential(name=credential2.name,
                                  login=credential2.login)

        self.assertIn(credential, self.db.credentials)
        self.assertNotIn(credential2, self.db.credentials)

    def test_find_credentials_by_name(self):
        credential = self.some_credential(name="Amazing Name")
        self.db.add_credential(credential)
        self.db.add_credential(credential)
        self.db.find_credentials(name=credential.name)

        self.assertIn(credential, self.db.credentials)
        self.assertEqual(len(self.db.credentials), 2)

    def test_find_credentials_by_login(self):
        credential = self.some_credential(login="john")
        self.db.add_credential(credential)
        self.db.find_credentials(login=credential.login)

        self.assertIn(credential, self.db.credentials)
        self.assertEqual(len(self.db.credentials), 1)

    def test_find_credentials_by_name_and_login(self):
        credential = self.some_credential(name="github", login="example")
        credential2 = self.some_credential(name="github", login="john")

        self.db.add_credential(credential)
        self.db.add_credential(credential2)
        credentials = self.db.find_credentials(name=credential.name,
                                               login=credential.login)

        self.assertIn(credential, self.db.credentials)
        self.assertEqual(len(credentials), 1)

    def test_verify_creates_valid_database_with_password(self):
        database_path = self.testing_path
        database = Database.create(
            path=database_path,
            crypt_options=self.crypt_options
        )

        verify_result = Database.verify(database.path, self.password)
        self.assertTrue(verify_result)
        bad_password = "<PASSWORD>"
        verify_bad_result = Database.verify(database.path, bad_password)
        self.assertFalse(verify_bad_result)


class ConsoleInterfaceTests(unittest.TestCase):

    def setUp(self):
        self.path = os.path.join(TEST_DIR,
                                 "data",
                                 "testing_database.db")
        self.args = argparse.Namespace(
            path=self.path,
            create=False,
            add=False,
            password="password"
        )
        self.PatcherDatabase = patch(
            "pysswords.__main__.Database"
        )
        self.PatcherCryptOptions = patch(
            "pysswords.__main__.CryptOptions",
            return_value=CryptOptions(
                password=self.args.password,
                salt=None,
                iterations=1000
            )
        )
        self.MockCryptOptions = self.PatcherCryptOptions.start()
        self.MockDatabase = self.PatcherDatabase.start()

    def tearDown(self):
        try:
            os.remove(self.path)
        except FileNotFoundError:
            pass

        self.PatcherCryptOptions.stop()
        self.PatcherDatabase.stop()

    def test_get_args_throw_errors_when_no_argument_is_set(self):
        with open(os.devnull, 'w') as devnull:<commit_msg>Add salt and iterations to args from console interface tests<commit_after>import argparse
import json
import os
import runpy
import sys
from tempfile import NamedTemporaryFile
import unittest

try:
    from unittest.mock import patch, MagicMock
except ImportError:
    from mock import patch, MagicMock

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pysswords.db import Database, Credential
from pysswords.crypt import CryptOptions
import pysswords.__main__

try:
    FileNotFoundError
except NameError:
    FileNotFoundError = EnvironmentError

TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))


class PysswordsTests(unittest.TestCase):
    def setUp(self):
        self.db_file = NamedTemporaryFile(mode='w', delete=False)
        self.db_path = self.db_file.name
        self.testing_path = os.path.join(TEST_DIR,
                                         "data",
                                         "testing_database.db")
        self.db_file.close()
        self.password = <PASSWORD>"
        self.default_iterations = 1000

        self.crypt_options = CryptOptions(
            password=self.password,
            salt=None,
            iterations=1000
        )

        self.db = Database.create(
            path=self.db_path,
            crypt_options=self.crypt_options
        )

    def tearDown(self):
        try:
            os.remove(self.db_path)
        except FileNotFoundError:
            pass

        try:
            os.remove(self.testing_path)
        except FileNotFoundError:
            pass

    def some_credential(self, **kwargs):
        return Credential(
            name=kwargs.get("name", "example"),
            login=kwargs.get("login", "john"),
            password=kwargs.get("password", "<PASSWORD>"),
            login_url=kwargs.get("login_url", "http://example.org/login"),
            description=kwargs.get("description",
                                   "This is login credentials for example"),
        )

    def test_database_default_content(self):
        expected_content = json.dumps([{}])
        self.assertEqual(Database.DEFAULT_CONTENT, expected_content)

    def test_create_credential_database(self):
        database = Database.create(
            path=self.db_path,
            crypt_options=self.crypt_options
        )
        self.assertIsInstance(database, Database)
        self.assertEqual(len(database.credentials), 0)

    def test_add_new_credential(self):
        self.assertEqual(len(self.db.credentials), 0)
        self.db.add_credential(self.some_credential())
        self.assertEqual(len(self.db.credentials), 1)

    def test_delete_credential_by_name(self):
        self.db.add_credential(self.some_credential(name="example"))
        self.db.delete_credential(name="example")
        self.assertEqual(len(self.db.credentials), 0)

    def test_delete_credential_by_login(self):
        self.db.add_credential(self.some_credential(login="john"))
        self.db.delete_credential(login="john")
        self.assertEqual(len(self.db.credentials), 0)

    def test_delete_credential_by_name_and_login(self):
        credential = self.some_credential(name="github", login="example")
        credential2 = self.some_credential(name="github", login="john")

        self.db.add_credential(credential)
        self.db.add_credential(credential2)

        self.db.delete_credential(name=credential2.name,
                                  login=credential2.login)

        self.assertIn(credential, self.db.credentials)
        self.assertNotIn(credential2, self.db.credentials)

    def test_find_credentials_by_name(self):
        credential = self.some_credential(name="Amazing Name")
        self.db.add_credential(credential)
        self.db.add_credential(credential)
        self.db.find_credentials(name=credential.name)

        self.assertIn(credential, self.db.credentials)
        self.assertEqual(len(self.db.credentials), 2)

    def test_find_credentials_by_login(self):
        credential = self.some_credential(login="john")
        self.db.add_credential(credential)
        self.db.find_credentials(login=credential.login)

        self.assertIn(credential, self.db.credentials)
        self.assertEqual(len(self.db.credentials), 1)

    def test_find_credentials_by_name_and_login(self):
        credential = self.some_credential(name="github", login="example")
        credential2 = self.some_credential(name="github", login="john")

        self.db.add_credential(credential)
        self.db.add_credential(credential2)
        credentials = self.db.find_credentials(name=credential.name,
                                               login=credential.login)

        self.assertIn(credential, self.db.credentials)
        self.assertEqual(len(credentials), 1)

    def test_verify_creates_valid_database_with_password(self):
        database_path = self.testing_path
        database = Database.create(
            path=database_path,
            crypt_options=self.crypt_options
        )

        verify_result = Database.verify(database.path, self.password)
        self.assertTrue(verify_result)
        bad_password = "<PASSWORD>"
        verify_bad_result = Database.verify(database.path, bad_password)
        self.assertFalse(verify_bad_result)


class ConsoleInterfaceTests(unittest.TestCase):

    def setUp(self):
        self.path = os.path.join(TEST_DIR,
                                 "data",
                                 "testing_database.db")
        self.args = argparse.Namespace(
            path=self.path,
            create=False,
            add=False,
            password="password",
            iterations=1000,
            salt=None
        )
        self.PatcherDatabase = patch(
            "pysswords.__main__.Database"
        )
        self.PatcherCryptOptions = patch(
            "pysswords.__main__.CryptOptions",
            return_value=CryptOptions(
                password=self.args.password,
                salt=None,
                iterations=1000
            )
        )
        self.MockCryptOptions = self.PatcherCryptOptions.start()
        self.MockDatabase = self.PatcherDatabase.start()

    def tearDown(self):
        try:
            os.remove(self.path)
        except FileNotFoundError:
            pass

        self.PatcherCryptOptions.stop()
        self.PatcherDatabase.stop()

    def test_get_args_throw_errors_when_no_argument_is_set(self):
        with open(os.devnull, 'w') as devnull:
{'fraction_non_alphanumeric': 0.09753458536208362, 'fraction_numerical': 0.004637143519591932, 'mean_word_length': 2.4378320935175344, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7268642', 'n_tokens_mistral': 3788, 'n_tokens_neox': 3653, 'n_words': 532}