<filename>tests/tests_block_bad_ip.py
from unittest.mock import patch
from unittest import TestCase

from block_bad_auth import read_process_file, log_ip_addresses, \
    PAST_IP_LOG, \
    run_process, WHITE_IPS


class BlockBadAuth(TestCase):
    TEST_MAIL_LOG = 'fixtures/mail.log'
    TEST_PAST_IP_LOG = 'fixtures/past_blocked_ips.log'

    @patch("block_bad_auth.MAIL_LOG", TEST_MAIL_LOG)
    @patch("block_bad_auth.PAST_IP_LOG", TEST_PAST_IP_LOG)
    def setUp(self):
        self.ip_set = read_process_file(self.TEST_MAIL_LOG)
        log_ip_addresses(self.ip_set)

    def tearDown(self):
        with open(self.TEST_PAST_IP_LOG, 'w') as f:
            f.write('')

    def test_ips_are_read_in_correctly(self):
        assert self.ip_set.intersection(WHITE_IPS) == set()
        assert self.ip_set == set(
            ['172.16.17.32', '172.16.17.32', '172.16.58.3',
             '172.16.58.3', '172.16.17.32', '192.168.3.11',
             '172.16.31.10', '172.16.58.3', '172.16.58.3',
             '172.16.17.32', '172.16.58.3',
             '192.168.3.11', '172.16.17.32', '172.16.17.32',
             '192.168.127.12', '172.16.17.32', '172.16.17.32',
             '172.16.31.10', '192.168.3.11', '192.168.127.12',
             '192.168.127.12', '172.16.31.10',
             '172.16.17.32', '172.16.31.10', '192.168.127.12', '192.168.3.11',
             '172.16.31.10', '172.16.58.3',
             '172.16.31.10', '172.16.17.32', '172.16.31.10',
             '172.16.31.10', '172.16.31.10', '192.168.127.12',
             '172.16.31.10', '172.16.58.3', '192.168.3.11',
             '192.168.127.12', '172.16.31.10', '172.16.58.3',
             '172.16.17.32', '192.168.3.11', '192.168.127.12',
             '172.16.31.10', '192.168.127.12', '192.168.127.12',
             '192.168.127.12', '172.16.31.10'])


class MainRun(TestCase):
    TEST_MAIL_LOG = 'fixtures/mail.log'
    TEST_PAST_IP_LOG = 'fixtures/past_blocked_ips.log'
    ip_set = {}

    class MyPopen:
        @staticmethod
        def communicate():
            return ['response', 'error']

    @patch("block_bad_auth.MAIL_LOG", TEST_MAIL_LOG)
    @patch("block_bad_auth.PAST_IP_LOG", TEST_PAST_IP_LOG)
    @patch("subprocess.Popen", return_value=MyPopen)
    def setUp(self,
              Popen):
        with open(self.TEST_PAST_IP_LOG, 'w') as f:
            f.write("172.16.31.10\n"
                    "12172.16.58.35\n"
                    "should be gone")
        self.ip_set = read_process_file(self.TEST_MAIL_LOG)
        run_process()
        self.Popen = Popen

    def tearDown(self):
        with open(self.TEST_PAST_IP_LOG, 'w') as f:
            f.write('')

    def test_write_past_blocked_ips(self):
        with open(self.TEST_PAST_IP_LOG, 'r') as f:
            m = f.readlines()
            m = [i.strip() for i in m]
        check_value = 'should be gone' in m
        assert check_value is False

    def test_bash_cmd(self):
        list_of_calls = self.Popen.mock_calls
        one_ufw_cmd = list_of_calls[0][1][0]
        assert 'bash' in one_ufw_cmd
        assert '-c' in one_ufw_cmd
        assert one_ufw_cmd[2].find('iptables -I INPUT 50') == 0

    def test_main(self):
        ips_blocked = read_process_file(self.TEST_MAIL_LOG)
        assert self.ip_set.intersection(WHITE_IPS) == set()
        assert ips_blocked == set(
            ['192.168.3.11', '172.16.31.10', '192.168.3.11',
             '192.168.127.12', '172.16.31.10', '192.168.127.12',
             '172.16.17.32', '172.16.17.32', '192.168.127.12',
             '192.168.3.11', '172.16.58.3', '172.16.17.32',
             '192.168.3.11', '172.16.31.10', '192.168.127.12', '172.16.31.10',
             '192.168.127.12', '172.16.17.32',
             '172.16.58.3', '172.16.31.10', '172.16.58.3',
             '172.16.31.10', '172.16.17.32', '192.168.3.11',
             '192.168.3.11', '172.16.58.3', '172.16.17.32',
             '172.16.31.10', '172.16.58.3', '192.168.127.12',
             '172.16.31.10', '192.168.127.12', '192.168.127.12',
             '172.16.17.32', '172.16.31.10', '172.16.58.3',
             '172.16.31.10', '172.16.17.32', '192.168.127.12',
             '172.16.31.10', '172.16.58.3',
             '172.16.58.3', '172.16.17.32', '172.16.31.10', '172.16.17.32',
             '172.16.31.10', '192.168.127.12',
             '172.16.17.32'])
