<commit_before># coding: utf-8
import os
import tempfile

from mock import patch
from nose.tools import eq_

from acmd import OK
from acmd import tool_repo, Server
from acmd.tools.config_tool import encrypt_str, decrypt_str

CONFIG = """[settings]
default_server = local

[server localhost]
host = http://localhost:4502
username = admin
password = <PASSWORD>"""

ENCRYPTED_CONFIG = """[settings]
default_server = local

[server localhost]
host = http://localhost:4502
username = admin
password = [3aTbniprRw==]
password_iv = MDEyMzQ1Njc4OWFiY2RlZg==

"""

@patch('getpass.getpass')
@patch('acmd.tools.config_tool.get_iv')
def test_encrypt_password(get_iv, getpass):
    getpass.return_value = "foobarpass"
    get_iv.return_value = b'0123456789abcdef'

    _, tmp_filepath = tempfile.mkstemp(".rc")
    with open(tmp_filepath, 'w') as f:
        f.write(CONFIG)

    tool = tool_repo.get_tool('config')
    server = Server('localhost')
    ret = tool.execute(server, ['config', 'encrypt', tmp_filepath])
    eq_(OK, ret)

    with open(tmp_filepath, 'r') as f:
        data = f.read()

    eq_(ENCRYPTED_CONFIG, data)

    os.remove(tmp_filepath)


def test_encrypt_decrypt_str():
    IV = b'This is an IV456'
    msg = "Hello Wörld"
    key = b'Some kind of key'
    data = encrypt_str(IV, key, msg)
    eq_("SapMLwk/NqcHuy4y", data)
    new_msg = decrypt_str(IV, key, data)
    eq_(msg, new_msg)
<commit_msg>Added encrypt decrypt test for config tool<commit_after># coding: utf-8
import os
import tempfile

from mock import patch
from nose.tools import eq_

from acmd import OK
from acmd import tool_repo, Server
from acmd.tools.config_tool import encrypt_str, decrypt_str

PLAINTEXT_CONFIG = """[settings]
default_server = local

[server localhost]
host = http://localhost:4502
username = admin
password = <PASSWORD>

"""

ENCRYPTED_CONFIG = """[settings]
default_server = local

[server localhost]
host = http://localhost:4502
username = admin
password = [3aTbniprRw==]
password_iv = MDEyMzQ1Njc4OWFiY2RlZg==

"""


@patch('getpass.getpass')
@patch('acmd.tools.config_tool.get_iv')
def test_encrypt_password(get_iv, getpass):
    getpass.return_value = "<PASSWORD>"
    get_iv.return_value = b'0123456789abcdef'
    _, tmp_filepath = tempfile.mkstemp(".rc")
    with open(tmp_filepath, 'w') as f:
        f.write(PLAINTEXT_CONFIG)
    tool = tool_repo.get_tool('config')
    server = Server('localhost')
    ret = tool.execute(server, ['config', 'encrypt', tmp_filepath])
    eq_(OK, ret)
    with open(tmp_filepath, 'r') as f:
        data = f.read()
    eq_(ENCRYPTED_CONFIG, data)
    os.remove(tmp_filepath)


@patch('getpass.getpass')
@patch('acmd.tools.config_tool.get_iv')
def test_encrypt_decrypt(get_iv, getpass):
    getpass.return_value = "somepassword"
    get_iv.return_value = b'fedcba0987654321'
    _, tmp_filepath = tempfile.mkstemp(".rc")
    with open(tmp_filepath, 'w') as f:
        f.write(PLAINTEXT_CONFIG)
    tool = tool_repo.get_tool('config')
    server = Server('localhost')
    ret = tool.execute(server, ['config', 'encrypt', tmp_filepath])
    eq_(OK, ret)
    ret = tool.execute(server, ['config', 'decrypt', tmp_filepath])
    eq_(OK, ret)
    with open(tmp_filepath, 'r') as f:
        data = f.read()
    eq_(PLAINTEXT_CONFIG, data)
    os.remove(tmp_filepath)


def test_encrypt_decrypt_str():
    iv = b'This is an IV456'
    msg = "Hello Wörld"
    key = b'Some kind of key'
    data = encrypt_str(iv, key, msg)
    eq_("SapMLwk/NqcHuy4y", data)
    new_msg = decrypt_str(iv, key, data)
    eq_(msg, new_msg)

{'fraction_non_alphanumeric': 0.12785130949028442, 'fraction_numerical': 0.01802309208673613, 'mean_word_length': 4.200585651537335, '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': '7368512', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1294, 'n_words': 291}