import unittest

from email_scraper.scrape import extract_emails, deobfuscate_html, scrape_emails


class TestExtractor(unittest.TestCase):
    def test_basic(self):
        self.assertEqual(extract_emails('hello world'), [])
        self.assertEqual(extract_emails('hello <EMAIL> world'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL> <EMAIL>'), ['<EMAIL>', '<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL> <EMAIL>'), ['<EMAIL>', '<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL>,<EMAIL>'), ['<EMAIL>', '<EMAIL>'])
        self.assertEqual(extract_emails('hello <EMAIL>. i have been waiting for you.'), ['<EMAIL>'])

    def test_basic_html(self):
        self.assertEqual(extract_emails('<a href="mailto:<EMAIL>">boo</a>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<a href=\'mailto:<EMAIL>\'>boo</a>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<a href="mailto:<EMAIL>?subject=meh">boo</a>'), ['<EMAIL>'])

    def test_tlds(self):
        self.assertEqual(extract_emails('<EMAIL>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL>'), [])


class TestDeobfuscate(unittest.TestCase):
    def test_entities(self):
        self.assertEqual(deobfuscate_html('&#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;'
                                          '&#097;&#105;&#110;&#046;&#099;&#111;&#109;'), '<EMAIL>')

    def test_atob(self):
        atob = 'atob(\'bWFpbHRvOmVtYWlsQGV4YW1wbGUuY29t\')'
        self.assertEqual(deobfuscate_html(atob), 'mailto:<EMAIL>')

class TestHidden(unittest.TestCase):
    def test_hidden(self):
        self.assertEqual(
            extract_emails("foo johnsmith (at) yahoo (dot) com bar"),
            ["<EMAIL>"]
        )

class TestScraping(unittest.TestCase):
    def test_basic(self):
        html = """<html>
        <body>
        <a href="mailto:<EMAIL>"><EMAIL></a>
        </body>
        </html>"""
        self.assertEqual(scrape_emails(html), {'<EMAIL>', '<EMAIL>'})

    def test_atob(self):
        atob = '<a href="javascript:window.location.href=atob(\'bWFpbHRvOmVtYWlsQGV4YW1wbGUuY29t\')">E-Mail</a>'
        self.assertEqual(scrape_emails(atob), {'<EMAIL>'})

    def test_entities(self):
        html = """<p>For more information, send email to <A HREF="mailto:
                  &#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;&#097;&#105;&#110;&#046;&#099;&#111;&#109;">
                  &#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;&#097;&#105;&#110;&#046;&#099;&#111;&#109;
                  </A></p>"""
        self.assertEqual(scrape_emails(html), {'<EMAIL>'})

import unittest

from email_scraper.scrape import extract_emails, deobfuscate_html, scrape_emails


class TestExtractor(unittest.TestCase):
    def test_basic(self):
        self.assertEqual(extract_emails('hello world'), [])
        self.assertEqual(extract_emails('hello <EMAIL> world'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL> <EMAIL>'), ['<EMAIL>', '<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL> <EMAIL>'), ['<EMAIL>', '<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL>,<EMAIL>'), ['<EMAIL>', '<EMAIL>'])
        self.assertEqual(extract_emails('hello <EMAIL>. i have been waiting for you.'), ['<EMAIL>'])

    def test_basic_html(self):
        self.assertEqual(extract_emails('<a href="mailto:<EMAIL>">boo</a>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<a href=\'mailto:<EMAIL>\'>boo</a>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<a href="mailto:<EMAIL>?subject=meh">boo</a>'), ['<EMAIL>'])

    def test_tlds(self):
        self.assertEqual(extract_emails('<EMAIL>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL>'), ['<EMAIL>'])
        self.assertEqual(extract_emails('<EMAIL>'), [])


class TestDeobfuscate(unittest.TestCase):
    def test_entities(self):
        self.assertEqual(deobfuscate_html('&#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;'
                                          '&#097;&#105;&#110;&#046;&#099;&#111;&#109;'), '<EMAIL>')

    def test_atob(self):
        atob = 'atob(\'bWFpbHRvOmVtYWlsQGV4YW1wbGUuY29t\')'
        self.assertEqual(deobfuscate_html(atob), 'mailto:<EMAIL>')

class TestHidden(unittest.TestCase):
    def test_hidden(self):
        self.assertEqual(
            extract_emails("foo johnsmith (at) yahoo (dot) com bar"),
            ["<EMAIL>"]
        )

class TestScraping(unittest.TestCase):
    def test_basic(self):
        html = """<html>
        <body>
        <a href="mailto:<EMAIL>"><EMAIL></a>
        </body>
        </html>"""
        self.assertEqual(scrape_emails(html), {'<EMAIL>', '<EMAIL>'})

    def test_atob(self):
        atob = '<a href="javascript:window.location.href=atob(\'bWFpbHRvOmVtYWlsQGV4YW1wbGUuY29t\')">E-Mail</a>'
        self.assertEqual(scrape_emails(atob), {'<EMAIL>'})

    def test_entities(self):
        html = """<p>For more information, send email to <A HREF="mailto:
                  &#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;&#097;&#105;&#110;&#046;&#099;&#111;&#109;">
                  &#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;&#097;&#105;&#110;&#046;&#099;&#111;&#109;
                  </A></p>"""
        self.assertEqual(scrape_emails(html), {'<EMAIL>'})

#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;' #097;&#105;&#110;&#046;&#099;&#111;&#109;'), '<EMAIL>') <html>         <body>         <a href="mailto:<EMAIL>"><EMAIL></a>         </body>         </html> <p>For more information, send email to <A HREF="mailto:                   &#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;&#097;&#105;&#110;&#046;&#099;&#111;&#109;">                   &#121;&#111;&#117;&#114;&#110;&#097;&#109;&#101;&#064;&#100;&#111;&#109;&#097;&#105;&#110;&#046;&#099;&#111;&#109;                   </A></p>