<commit_before>import inspect
import os
import shutil
import sys
import unittest
import gnupg
try:
    from unittest import mock
except ImportError:
    import mock

__file__ = os.path.abspath(inspect.getsourcefile(lambda _: None))

TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pysswords.db import Database
from pysswords.crypt import create_key_input
from pysswords.utils import touch, which
from pysswords.credential import Credential
from pysswords import __main__, crypt, db


def mock_create_gpg(binary, database_path, passphrase):
    gnupg_path = os.path.join(database_path, ".gnupg")
    gpg = gnupg.GPG(which(binary), homedir=gnupg_path)
    with open(TEST_DIR + "/testkey.pub") as f:
        gpg.import_keys(f.read())
    with open(TEST_DIR + "/testkey.sec") as f:
        gpg.import_keys(f.read())
    return gpg


class PysswordsTests(unittest.TestCase):

    def setUp(self):
        self.patcher_gpg = mock.patch(
            "pysswords.db.create_gpg", new=mock_create_gpg)
        self.patcher_gpg.start()

        self.database_path = os.path.join(TEST_DIR, ".pysswords")
        self.gnupg_path = os.path.join(self.database_path, ".gnupg")
        self.database = Database.create(
            path=self.database_path,
            passphrase="<PASSWORD>"
        )

    def tearDown(self):
        self.patcher_gpg.stop()
        shutil.rmtree(self.database_path)

    def test(self):
        pass

    def some_credential(self, **kwargs):
        credential = Credential(
            name=kwargs.get("name", "example"),
            login=kwargs.get("login", "john"),
            password=kwargs.get("password", "<PASSWORD>"),
            comments=kwargs.get("comments",
                                "This is login credentials for example")
        )
        return credential

    def test_init_database_creates_gnupg_hidden_directory(self):
        self.assertTrue(os.path.exists(self.database_path))
        self.assertTrue(os.path.exists(self.gnupg_path))

    def test_credentials_are_a_list_of_credential_instances(self):
        self.database.add(self.some_credential(name="John1"))
        self.database.add(self.some_credential(name="John2"))
        self.database.add(self.some_credential(name="John3"))

        for credential in self.database.credentials:
            self.assertIsInstance(credential, Credential)

    def test_add_credential_creates_directory_with_credential_name(self):
        credential = self.some_credential()
        self.database.add(credential)
        credentials = self.database.credentials
        self.assertIn(credential.name, (c.name for c in credentials))

    def test_save_credential_creates_dir_with_cred_name_on_given_path(self):
        credential = self.some_credential()
        credential.save(database_path=self.database_path)
        credential_path = os.path.join(self.database_path, credential.name)
        self.assertTrue(os.path.exists(credential_path))
        self.assertTrue(os.path.isdir(credential_path))

    def test_credential_method_returns_expected_credential_object(self):
        name = "email"
        login = "<EMAIL>"
        password = "<PASSWORD>"
        comments = "email"
        credential = Credential(
            name=name,<commit_msg>Test database gpg encrypt uses AES256 encryption algorithm<commit_after>import inspect
import os
import shutil
import sys
import unittest
import gnupg
try:
    from unittest import mock
except ImportError:
    import mock

__file__ = os.path.abspath(inspect.getsourcefile(lambda _: None))

TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pysswords.db import Database
from pysswords.crypt import create_key_input
from pysswords.utils import touch, which
from pysswords.credential import Credential
from pysswords import __main__, crypt, db


def mock_create_gpg(binary, database_path, passphrase):
    gnupg_path = os.path.join(database_path, ".gnupg")
    gpg = gnupg.GPG(which(binary), homedir=gnupg_path)
    with open(TEST_DIR + "/testkey.pub") as f:
        gpg.import_keys(f.read())
    with open(TEST_DIR + "/testkey.sec") as f:
        gpg.import_keys(f.read())
    return gpg


class PysswordsTests(unittest.TestCase):

    def setUp(self):
        self.patcher_gpg = mock.patch(
            "pysswords.db.create_gpg", new=mock_create_gpg)
        self.patcher_gpg.start()

        self.database_path = os.path.join(TEST_DIR, ".pysswords")
        self.gnupg_path = os.path.join(self.database_path, ".gnupg")
        self.database = Database.create(
            path=self.database_path,
            passphrase="<PASSWORD>"
        )

    def tearDown(self):
        self.patcher_gpg.stop()
        shutil.rmtree(self.database_path)

    def test(self):
        pass

    def some_credential(self, **kwargs):
        credential = Credential(
            name=kwargs.get("name", "example"),
            login=kwargs.get("login", "john"),
            password=kwargs.get("password", "<PASSWORD>"),
            comments=kwargs.get("comments",
                                "This is login credentials for example")
        )
        return credential

    def test_init_database_creates_gnupg_hidden_directory(self):
        self.assertTrue(os.path.exists(self.database_path))
        self.assertTrue(os.path.exists(self.gnupg_path))

    def test_credentials_are_a_list_of_credential_instances(self):
        self.database.add(self.some_credential(name="John1"))
        self.database.add(self.some_credential(name="John2"))
        self.database.add(self.some_credential(name="John3"))

        for credential in self.database.credentials:
            self.assertIsInstance(credential, Credential)

    def test_add_credential_creates_directory_with_credential_name(self):
        credential = self.some_credential()
        self.database.add(credential)
        credentials = self.database.credentials
        self.assertIn(credential.name, (c.name for c in credentials))

    def test_add_credential_uses_cipher_algo_aes256_to_encrypt(self):
        fingerprint = "fingerprint"
        mock_gpg = mock.Mock()
        mock_gpg.list_keys.return_value = [{"fingerprint": fingerprint}]
        path = "path"
        database = Database(path, mock_gpg)
        credential = mock.Mock()
        database.add(credential)
        self.assertTrue(database.gpg.encrypt.called)
        args, kwargs = database.gpg.encrypt.call_args
        self.assertEqual(kwargs.get("cipher_algo"), "AES256")

    def test_save_credential_creates_dir_with_cred_name_on_given_path(self):
        credential = self.some_credential()
        credential.save(database_path=self.database_path)
        credential_path = os.path.join(self.database_path, credential.name)
        self.assertTrue(os.path.exists(credential_path))
        self.assertTrue(os.path.isdir(credential_path))

    def test_credential_method_returns_expected_credential_object(self):
        name = "email"
        login = "<EMAIL>"
        password = "<PASSWORD>"
        comments = "email"
        credential = Credential(
            name=name,
{'fraction_non_alphanumeric': 0.10677334804800662, 'fraction_numerical': 0.0023451510553179748, 'mean_word_length': 3.2323409223584356, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 11, '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': '1299899', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2278, 'n_words': 373}