<commit_before>"""Tests for pad.plugins.relay_country."""
import unittest

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

import pad.context
import pad.message

MSGTEST="""Delivered-To: <EMAIL>
Received: by 10.107.130.85 with SMTP id e82csp671450iod;
        Fri, 8 Jan 2016 10:27:27 -0800 (PST)
X-Received: by 10.194.24.226 with SMTP id x2mr119688960wjf.43.1452277647087;
        Fri, 08 Jan 2016 10:27:27 -0800 (PST)
Return-Path: <<EMAIL>>
Received: from mail45.mpmailserv.co.uk (mail45.mpmailserv.co.uk. [192.168.127.12])
        by mx.example.com with ESMTP id m80si439375wmd.112.2016.01.08.10.27.26
        for <<EMAIL>>;
        Fri, 08 Jan 2016 10:27:27 -0800 (PST)
Received-SPF: pass (example.com: domain of <EMAIL> designates 192.168.127.12 as permitted sender) client-ip=192.168.127.12;
Authentication-Results: mx.example.com;
       spf=pass (example.com: domain of <EMAIL> designates 192.168.127.12 as permitted sender) smtp.mailfrom=<EMAIL>;
       dkim=pass <EMAIL>
From: "=?utf-8?B?UENIIE1heW9yZW8=?=" <<EMAIL>>
To: <EMAIL>
Subject: =?utf-8?B?TGlxdWlkYWNpb24gZGVsIDIwMTUg?=
 =?utf-8?B?YSBDbGllbnRlc2==?=
Date: Fri, 08 Jan 2016 18:27:22 -0000
Message-ID: <20160108-18272230-3030-0@DSVR021489>
List-Unsubscribe: <mailto:<EMAIL>>, <http://web2.mpmailserv.co.uk/bg/unsubscribe.asp?sid=16147476&cid=52463>
Feedback-ID: 52463:16147476:Campaign:70955
DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple;
	d=mpmailserver.co.uk; s=dkim1;
	h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type;
	bh=OAQ8l70XSu555kegpIQDGx0twRMltOiKK9/pzRATUT0=;
	b=G4p5eIlu7dXOD9krH5BosMOJksrKgkBB85BgVZeyh0l0JfLhwve7WVMfR5jtDgae
		ET9Rh9rfmIEnXv4cb5Hy727huDWTLZionBaoJCx+gIIRBQTfJDn1Jxyq/XAZMuot
		BF2IOcAnjgkb8AXCz7zSIbw475llXxcwB5o44vxuHxE=
MIME-Version: 1.0
Content-Type: multipart/mixed;
	boundary="--=7879796F323D48C3BF41_0C5C_5373_A518"

"""

MSG_NORECEIVED="""Delivered-To: <EMAIL>
X-Received: by 10.194.24.226 with SMTP id x2mr119688960wjf.43.1452277647087;
        Fri, 08 Jan 2016 10:27:27 -0800 (PST)
Return-Path: <<EMAIL>>
Authentication-Results: mx.example.com;
       spf=pass (example.com: domain of <EMAIL> designates 192.168.127.12 as permitted sender) smtp.mailfrom=<EMAIL>;
       dkim=pass <EMAIL>
From: "=?utf-8?B?UENIIE1heW9yZW8=?=" <<EMAIL>>
To: <EMAIL>
Subject: =?utf-8?B?TGlxdWlkYWNpb24gZGVsIDIwMTUg?=
 =?utf-8?B?YSBDbGllbnRlc2==?=
Date: Fri, 08 Jan 2016 18:27:22 -0000
Message-ID: <20160108-18272230-3030-0@DSVR021489>
List-Unsubscribe: <mailto:<EMAIL>>, <http://web2.mpmailserv.co.uk/bg/unsubscribe.asp?sid=16147476&cid=52463>
Feedback-ID: 52463:16147476:Campaign:70955
DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple;
	d=mpmailserver.co.uk; s=dkim1;
	h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type;
	bh=OAQ8l70XSu555kegpIQDGx0twRMltOiKK9/pzRATUT0=;
	b=G4p5eIlu7dXOD9krH5BosMOJksrKgkBB85BgVZeyh0l0JfLhwve7WVMfR5jtDgae
		ET9Rh9rfmIEnXv4cb5Hy727huDWTLZionBaoJCx+gIIRBQTfJDn1Jxyq/XAZMuot
		BF2IOcAnjgkb8AXCz7zSIbw475llXxcwB5o44vxuHxE=
MIME-Version: 1.0
Content-Type: multipart/mixed;
	boundary="--=7879796F323D48C3BF41_0C5C_5373_A518"

"""




class MockGeoIP(object):
    """Mocking GeoIP class"""
    IPADDRESSES = {"10.107.130.85": "**",
               "192.168.127.12": "GB",
                  }
    def __init__(self, datfile):
        """Constructore, requires to call with the path of the .dat file although
        it is not currently used"""
        pass
    
    def country_code_by_addr(self, addr):
        """Mock country_code_by_addr the result is taken from the
        IPADDRESSES dictionary above"""
        return self.IPADDRESSES.get(addr, "")

class TestRelayCountry(unittest.TestCase):
    """Tests for the RelayCountryPlugin"""
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.options = {}
        self.global_data = {"geodb":"/innexistent/location/"}
        patch("pad.plugins.relay_country.RelayCountryPlugin.options",
              self.options).start()
        self.mock_ctxt = MagicMock(**{
            "get_plugin_data.side_effect": lambda p, k: self.global_data[k],
            "set_plugin_data.side_effect": lambda p, k, v: self.global_data.setdefault(k, v)}
                                  )
        patch("pad.plugins.relay_country.pygeoip.GeoIP",
                MockGeoIP).start()
        self.plugin = pad.plugins.relay_country.RelayCountryPlugin(self.mock_ctxt)

    def tearDown(self):
        unittest.TestCase.tearDown(self)<commit_msg>Refs #5 Typo error<commit_after>"""Tests for pad.plugins.relay_country."""
import unittest

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

import pad.context
import pad.message

MSGTEST="""Delivered-To: <EMAIL>
Received: by 10.107.130.85 with SMTP id e82csp671450iod;
        Fri, 8 Jan 2016 10:27:27 -0800 (PST)
X-Received: by 10.194.24.226 with SMTP id x2mr119688960wjf.43.1452277647087;
        Fri, 08 Jan 2016 10:27:27 -0800 (PST)
Return-Path: <<EMAIL>>
Received: from mail45.mpmailserv.co.uk (mail45.mpmailserv.co.uk. [172.16.31.102])
        by mx.example.com with ESMTP id m80si439375wmd.112.2016.01.08.10.27.26
        for <<EMAIL>>;
        Fri, 08 Jan 2016 10:27:27 -0800 (PST)
Received-SPF: pass (example.com: domain of <EMAIL> designates 192.168.127.12 as permitted sender) client-ip=192.168.127.12;
Authentication-Results: mx.example.com;
       spf=pass (example.com: domain of <EMAIL> designates 192.168.127.12 as permitted sender) smtp.mailfrom=<EMAIL>;
       dkim=pass <EMAIL>
From: "=?utf-8?B?UENIIE1heW9yZW8=?=" <<EMAIL>>
To: <EMAIL>
Subject: =?utf-8?B?TGlxdWlkYWNpb24gZGVsIDIwMTUg?=
 =?utf-8?B?YSBDbGllbnRlc2==?=
Date: Fri, 08 Jan 2016 18:27:22 -0000
Message-ID: <20160108-18272230-3030-0@DSVR021489>
List-Unsubscribe: <mailto:<EMAIL>>, <http://web2.mpmailserv.co.uk/bg/unsubscribe.asp?sid=16147476&cid=52463>
Feedback-ID: 52463:16147476:Campaign:70955
DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple;
	d=mpmailserver.co.uk; s=dkim1;
	h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type;
	bh=OAQ8l70XSu555kegpIQDGx0twRMltOiKK9/pzRATUT0=;
	b=G4p5eIlu7dXOD9krH5BosMOJksrKgkBB85BgVZeyh0l0JfLhwve7WVMfR5jtDgae
		ET9Rh9rfmIEnXv4cb5Hy727huDWTLZionBaoJCx+gIIRBQTfJDn1Jxyq/XAZMuot
		BF2IOcAnjgkb8AXCz7zSIbw475llXxcwB5o44vxuHxE=
MIME-Version: 1.0
Content-Type: multipart/mixed;
	boundary="--=7879796F323D48C3BF41_0C5C_5373_A518"

"""

MSG_NORECEIVED="""Delivered-To: <EMAIL>
X-Received: by 10.194.24.226 with SMTP id x2mr119688960wjf.43.1452277647087;
        Fri, 08 Jan 2016 10:27:27 -0800 (PST)
Return-Path: <<EMAIL>>
Authentication-Results: mx.example.com;
       spf=pass (example.com: domain of <EMAIL> designates 192.168.127.12 as permitted sender) smtp.mailfrom=<EMAIL>;
       dkim=pass <EMAIL>
From: "=?utf-8?B?UENIIE1heW9yZW8=?=" <<EMAIL>>
To: <EMAIL>
Subject: =?utf-8?B?TGlxdWlkYWNpb24gZGVsIDIwMTUg?=
 =?utf-8?B?YSBDbGllbnRlc2==?=
Date: Fri, 08 Jan 2016 18:27:22 -0000
Message-ID: <20160108-18272230-3030-0@DSVR021489>
List-Unsubscribe: <mailto:<EMAIL>>, <http://web2.mpmailserv.co.uk/bg/unsubscribe.asp?sid=16147476&cid=52463>
Feedback-ID: 52463:16147476:Campaign:70955
DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple;
	d=mpmailserver.co.uk; s=dkim1;
	h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type;
	bh=OAQ8l70XSu555kegpIQDGx0twRMltOiKK9/pzRATUT0=;
	b=G4p5eIlu7dXOD9krH5BosMOJksrKgkBB85BgVZeyh0l0JfLhwve7WVMfR5jtDgae
		ET9Rh9rfmIEnXv4cb5Hy727huDWTLZionBaoJCx+gIIRBQTfJDn1Jxyq/XAZMuot
		BF2IOcAnjgkb8AXCz7zSIbw475llXxcwB5o44vxuHxE=
MIME-Version: 1.0
Content-Type: multipart/mixed;
	boundary="--=7879796F323D48C3BF41_0C5C_5373_A518"

"""




class MockGeoIP(object):
    """Mocking GeoIP class"""
    IPADDRESSES = {"10.107.130.85": "**",
               "192.168.127.12": "GB",
                  }
    def __init__(self, datfile):
        """Initializer, requires to call with the path of the .dat file although
        it is not currently used"""
        pass
    
    def country_code_by_addr(self, addr):
        """Mock country_code_by_addr the result is taken from the
        IPADDRESSES dictionary above"""
        return self.IPADDRESSES.get(addr, "")

class TestRelayCountry(unittest.TestCase):
    """Tests for the RelayCountryPlugin"""
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.options = {}
        self.global_data = {"geodb":"/innexistent/location/"}
        patch("pad.plugins.relay_country.RelayCountryPlugin.options",
              self.options).start()
        self.mock_ctxt = MagicMock(**{
            "get_plugin_data.side_effect": lambda p, k: self.global_data[k],
            "set_plugin_data.side_effect": lambda p, k, v: self.global_data.setdefault(k, v)}
                                  )
        patch("pad.plugins.relay_country.pygeoip.GeoIP",
                MockGeoIP).start()
        self.plugin = pad.plugins.relay_country.RelayCountryPlugin(self.mock_ctxt)

    def tearDown(self):
        unittest.TestCase.tearDown(self)
{'fraction_non_alphanumeric': 0.13387401399844462, 'fraction_numerical': 0.1244306188201311, 'mean_word_length': 4.185483870967742, 'pattern_counts': {'":': 10, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 22, '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': '666199', 'n_tokens_mistral': 4532, 'n_tokens_neox': 3791, 'n_words': 584}