Skip to content

Unit tests coverage for plugins and classes in main #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f96faa1
basic setup of env, vscode
May 14, 2024
54ce625
Merge pull request #1 from stolen-smile/basic-setting
stolen-smile May 14, 2024
dd0bdfc
Changed name of plugin from 'googles.py' to 'google.py'. Changed miss…
stolen-smile May 14, 2024
5768cfc
Merge pull request #2 from stolen-smile/fixing-googles-plugin
stolen-smile May 14, 2024
6dddca2
Resolve merge conflicts
stolen-smile May 15, 2024
834c1a4
Add files via upload
stolen-smile May 15, 2024
59842e2
Merge pull request #3 from stolen-smile/creating-structure-for-testing
stolen-smile May 15, 2024
91d1bb9
fixing imports in test files
stolen-smile May 22, 2024
4e9f1cc
Unit tests for google plugin in plugins/google.py
stolen-smile May 24, 2024
3b585b3
Merge pull request #5 from stolen-smile/google-unit-tests
stolen-smile May 24, 2024
2d7fa54
Unit tests for youtube plugin in plugins/youtube.py
stolen-smile May 25, 2024
2bb95ff
Merge pull request #6 from stolen-smile/youtube-unit-tests
stolen-smile May 25, 2024
3f2cc4d
Unit tests for yahoo plugin in plugins/yahoo.py
stolen-smile May 25, 2024
85907ee
Merge pull request #7 from stolen-smile/yahoo-unit-tests
stolen-smile May 25, 2024
58ac971
Unit tests for twitter plugin in plugins/twitter.py
stolen-smile May 25, 2024
9ecb204
Merge pull request #8 from stolen-smile/twitter-unit-tests
stolen-smile May 25, 2024
be9577f
Unit tests for reddit plugin in plugins/reddit.py
stolen-smile May 26, 2024
6970f34
Merge pull request #9 from stolen-smile/reddit-unit-tests
stolen-smile May 26, 2024
515af86
Fixing mistypes in test_twitter.py and test_youtube.py:
stolen-smile May 26, 2024
aad3234
Merge pull request #10 from stolen-smile/small-fixes-in-test_twitter-…
stolen-smile May 26, 2024
c8e17c6
Unit tests for linkedin plugin in plugins/linkedin.py
stolen-smile May 27, 2024
f276834
Merge pull request #11 from stolen-smile/linkedin-unit-tests
stolen-smile May 27, 2024
66183e5
Unit tests for instagram plugin in plugins/instagram.py
stolen-smile May 27, 2024
0e043f1
Unit tests for googleplus plugin in plugins/googleplus.py
stolen-smile May 27, 2024
78b61a5
Unit tests for github plugin in plugins/github.py
stolen-smile May 27, 2024
2b4b051
Merge pull request #12 from stolen-smile/instagram-unit-tests
stolen-smile May 27, 2024
7917015
Merge pull request #13 from stolen-smile/googleplus-unit-tests
stolen-smile May 27, 2024
b21be8d
Merge pull request #14 from stolen-smile/github-unit-tests
stolen-smile May 27, 2024
e8773bb
Unit tests for exalead plugin in plugins/exalead.py
stolen-smile May 27, 2024
99509bc
Merge pull request #15 from stolen-smile/exalead-unit-tests
stolen-smile May 27, 2024
d970715
Unit tests for dogpile plugin in plugins/dogpile.py
stolen-smile May 27, 2024
0c57c01
Merge pull request #16 from stolen-smile/dogpile-unit-tests
stolen-smile May 27, 2024
5f08270
Unit tests for bing plugin in plugins/bing.py
stolen-smile May 29, 2024
bd772c0
Merge pull request #17 from stolen-smile/bing-unit-tests
stolen-smile May 29, 2024
6be20c6
Unit tests for baidu plugin in plugins/baidu.py
stolen-smile May 29, 2024
ddfb4e3
Merge pull request #18 from stolen-smile/baidu-unit-tests
stolen-smile May 29, 2024
202dcef
Unit tests for ask plugin in plugins/ask.py
stolen-smile May 31, 2024
f9c7816
Merge pull request #19 from stolen-smile/ask-unit-tests
stolen-smile May 31, 2024
a65fcec
Unit tests for class myparser in EmailHarvester.py
stolen-smile Jun 8, 2024
7ebd9bb
Merge pull request #20 from stolen-smile/myparser-class-unit-tests
stolen-smile Jun 8, 2024
dfc15fd
Unit tests for class EmailHarvester in EmailHarvester.py
stolen-smile Jun 8, 2024
2084bd7
Merge pull request #21 from stolen-smile/emailharvester-class-unit-tests
stolen-smile Jun 8, 2024
5d6eda7
Made some cosmetic edits
stolen-smile Jun 12, 2024
e997a0d
Merge pull request #22 from stolen-smile/cosmetic-edits-to-tests
stolen-smile Jun 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ RemoteSystemsTempFiles
.metadata
.settings
*.prefs
.vscode/
2 changes: 1 addition & 1 deletion plugins/googles.py → plugins/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ class Plugin:
def __init__(self, app, conf):#
global app_emailharvester, config
#config = conf
app.register_plugin('googles', {'search': search})
app.register_plugin('google', {'search': search})
app_emailharvester = app

Empty file added tests/__init__.py
Empty file.
103 changes: 103 additions & 0 deletions tests/test_emailharvester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import unittest
from unittest.mock import MagicMock, patch, Mock
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

import EmailHarvester

class TestEmailHarvester(unittest.TestCase):
def setUp(self):
self.userAgent = 'test-agent'
self.proxy = None
self.emailHarvester = EmailHarvester.EmailHarvester(self.userAgent, self.proxy)

def test_register_plugin(self):
mock_function = MagicMock()

self.emailHarvester.register_plugin('test_method', {'function': mock_function})

self.assertIn('test_method', self.emailHarvester.plugins)
self.assertEqual({'function': mock_function}, self.emailHarvester.plugins['test_method'])

def test_get_plugins(self):
mock_function1 = MagicMock()
self.emailHarvester.register_plugin('test_method1', {'function1': mock_function1})

mock_function2 = MagicMock()
self.emailHarvester.register_plugin('test_method2', {'function2': mock_function2})

self.assertEqual(self.emailHarvester.get_plugins(), self.emailHarvester.plugins)

@patch('builtins.print')
def test_show_message(self, mock_print):
test_message = "Test message"
self.emailHarvester.show_message(test_message)
mock_print.assert_called_once_with(test_message)

def test_init_search(self):
url = 'http://example.com/search?q={word}&start={counter}'
word = 'test'
limit = 100
counterInit = 0
counterStep = 10
engineName = 'Test-Engine'

self.emailHarvester.init_search(url, word, limit, counterInit, counterStep, engineName)

self.assertEqual(url, self.emailHarvester.url)
self.assertEqual(word, self.emailHarvester.word)
self.assertEqual(limit, self.emailHarvester.limit)
self.assertEqual(counterInit, self.emailHarvester.counter)
self.assertEqual(counterStep, self.emailHarvester.step)
self.assertEqual(engineName, self.emailHarvester.activeEngine)
self.assertEqual("",self.emailHarvester.results)
self.assertEqual("", self.emailHarvester.totalresults)

@patch('requests.get')
def test_do_search(self, mock_get):
url = "http://example.com/search?q={word}&start={counter}"
word = "test"
response_content = "Search results content"
mock_response = Mock()
mock_response.content = response_content.encode('utf-8')
mock_response.encoding = 'utf-8'
mock_get.return_value = mock_response

self.emailHarvester.init_search(url, word, 1, 0, 1, "ExampleEngine")
self.emailHarvester.do_search()

expected_url = url.format(counter = '0', word = word)
mock_get.assert_called_once_with(expected_url, headers={'User-Agent': self.userAgent})
self.assertEqual(response_content, self.emailHarvester.results)
self.assertEqual(response_content, self.emailHarvester.totalresults)

@patch('time.sleep', return_value = None)
@patch('requests.get')
@patch('builtins.print')
def test_process(self, mock_print, mock_get, mock_sleep):
url = "http://example.com/search?q={word}&start={counter}"
word = "test"
response_content = "Search results content"
mock_response = Mock()
mock_response.content = response_content.encode('utf-8')
mock_response.encoding = 'utf-8'
mock_get.return_value = mock_response

self.emailHarvester.init_search(url, word, 20, 0, 10, "ExampleEngine")
self.emailHarvester.process()

self.assertEqual(2, mock_get.call_count)
self.assertEqual(20, self.emailHarvester.counter)
self.assertEqual(response_content * 2, self.emailHarvester.totalresults)

def test_get_emails(self):
self.emailHarvester.totalresults = "Contact us at [email protected] and [email protected]"
self.emailHarvester.word = "example.com"
emails = self.emailHarvester.get_emails()
expected_emails = ["[email protected]", "[email protected]"]
self.assertEqual(set(expected_emails), set(emails))

if __name__ == '__main__':
unittest.main()
40 changes: 40 additions & 0 deletions tests/test_myparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

import EmailHarvester

class TestMyParser(unittest.TestCase):
def setUp(self):
self.testParser = EmailHarvester.myparser()

def test_extract(self):
self.testParser.extract("some results", "example.com")
self.assertEqual("some results", self.testParser.results)
self.assertEqual("example.com", self.testParser.word)


def test_genericClean(self):
self.testParser.extract("<KW>test</KW>%2f<em>email</em>", "example.com")
self.testParser.genericClean()
expected_result = "test email"
self.assertEqual(expected_result, self.testParser.results)

def test_emails(self):
self.testParser.extract("Contact us at [email protected] and [email protected]", "example.com")
emails = self.testParser.emails()
expected_emails = ["[email protected]", "[email protected]"]
self.assertEqual(set(expected_emails), set(emails))

def test_unique(self):
self.testParser.temp = ["[email protected]", "[email protected]", "[email protected]"]
unique_emails = self.testParser.unique()
expected_unique_emails = ["[email protected]", "[email protected]"]
self.assertEqual(set(expected_unique_emails), set(unique_emails))


if __name__ == '__main__':
unittest.main()
Empty file added tests/test_plugins/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions tests/test_plugins/test_ask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

from plugins.ask import Plugin, search, AskSearch

class TestAskPlugin(unittest.TestCase):
def setUp(self):
self.app_mock = MagicMock()
self.config = {'useragent':'test-agent', 'proxy': None}
self.plugin = Plugin(self.app_mock, self.config)

def test_plugin_init(self):
self.app_mock.register_plugin.assert_called_once_with('ask', {'search': search})

@patch('plugins.ask.config', new_callable=dict)
@patch('plugins.ask.requests.get')
@patch('plugins.ask.app_emailharvester')
def test_search(self, mock_app_emailharvester, mock_requests_get, mock_config):
# Setup mock configuration
mock_config['proxy'] = None
mock_config['useragent'] = 'test-agent'

# Mock parser methods
mock_app_emailharvester.parser = MagicMock()
mock_app_emailharvester.parser.extract = MagicMock()
mock_app_emailharvester.parser.emails = MagicMock(return_value=['[email protected]'])

# Mock the HTTP response from requests.get
mock_response = MagicMock()
mock_response.content.decode.return_value = "response content"
mock_response.encoding = 'utf-8'
mock_requests_get.return_value = mock_response

domain = "email.com"
limit = 10

# Execute the search function
result = search(domain, limit)

#---

expected_url_template = 'http://www.ask.com/web?q=%40{word}&page={page}'

# Extract called URLs from requests.get
urls_called = [call[0][0] for call in mock_requests_get.call_args_list]

# Generate expected URLs
expected_urls = [expected_url_template.format(word=domain, page=i+1) for i in range(limit // 10)]

# Verify that each expected URL was called
for expected_url in expected_urls:
self.assertIn(expected_url, urls_called, f"{expected_url} was not called")

#---

# Verify that the parser methods were called correctly
mock_app_emailharvester.parser.extract.assert_called_once_with("response content", domain)
mock_app_emailharvester.parser.emails.assert_called_once()

# Check the result of the search function
self.assertEqual(['[email protected]'], result)

if __name__ == '__main__':
unittest.main()
41 changes: 41 additions & 0 deletions tests/test_plugins/test_baidu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

from plugins.baidu import Plugin, search

class TestBaiduPlugin(unittest.TestCase):
def setUp(self):
self.app_mock = MagicMock()
self.config = {'useragent':'test-agent', 'proxy': None}
self.plugin = Plugin(self.app_mock, self.config)

def test_plugin_init(self):
self.app_mock.register_plugin.assert_called_once_with('baidu', {'search': search})

@patch('plugins.baidu.app_emailharvester')
def test_search(self, mock_app_emailharvester):
mock_app_emailharvester.init_search = MagicMock()
mock_app_emailharvester.process = MagicMock()
mock_app_emailharvester.get_emails = MagicMock(return_value = ['[email protected]'])

domain = "email.com"
limit = 10
result = search(domain,limit)

expected_url = 'http://www.baidu.com/search/s?wd="%40{word}"&pn={counter}'

mock_app_emailharvester.init_search(expected_url, domain, limit, 0, 10, 'Baidu')
mock_app_emailharvester.process.assert_called_once()
mock_app_emailharvester.get_emails.assert_called_once()

self.assertEqual(['[email protected]'], result)




if __name__ == '__main__':
unittest.main()
38 changes: 38 additions & 0 deletions tests/test_plugins/test_bing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

from plugins.bing import Plugin, search

class TestBingPlugin(unittest.TestCase):
def setUp(self):
self.app_mock = MagicMock()
self.config = {'useragent':'test-agent', 'proxy': None}
self.plugin = Plugin(self.app_mock, self.config)

def test_plugin_init(self):
self.app_mock.register_plugin.assert_called_once_with('bing', {'search': search})

@patch('plugins.bing.app_emailharvester')
def test_search(self, mock_app_emailharvester):
mock_app_emailharvester.init_search = MagicMock()
mock_app_emailharvester.process = MagicMock()
mock_app_emailharvester.get_emails = MagicMock(return_value = ['[email protected]'])

domain = "email.com"
limit = 10
result = search(domain,limit)

expected_url = 'http://www.bing.com/search?q=%40{word}&count=50&first={counter}'

mock_app_emailharvester.init_search(expected_url, domain, limit, 0, 50, 'Bing')
mock_app_emailharvester.process.assert_called_once()
mock_app_emailharvester.get_emails.assert_called_once()

self.assertEqual(['[email protected]'], result)

if __name__ == '__main__':
unittest.main()
38 changes: 38 additions & 0 deletions tests/test_plugins/test_dogpile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

from plugins.dogpile import Plugin, search

class TestDogpilePlugin(unittest.TestCase):
def setUp(self):
self.app_mock = MagicMock()
self.config = {'useragent':'test-agent', 'proxy': None}
self.plugin = Plugin(self.app_mock, self.config)

def test_plugin_init(self):
self.app_mock.register_plugin.assert_called_once_with('dogpile', {'search': search})

@patch('plugins.dogpile.app_emailharvester')
def test_search(self, mock_app_emailharvester):
mock_app_emailharvester.init_search = MagicMock()
mock_app_emailharvester.process = MagicMock()
mock_app_emailharvester.get_emails = MagicMock(return_value = ['[email protected]'])

domain = "email.com"
limit = 10
result = search(domain,limit)

expected_url = 'http://www.dogpile.com/search/web?qsi={counter}&q="%40{word}"'

mock_app_emailharvester.init_search(expected_url, domain, limit, 1, 10, 'Dogpile')
mock_app_emailharvester.process.assert_called_once()
mock_app_emailharvester.get_emails.assert_called_once()

self.assertEqual(['[email protected]'], result)

if __name__ == '__main__':
unittest.main()
43 changes: 43 additions & 0 deletions tests/test_plugins/test_exalead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

from plugins.exalead import Plugin, search


class TestExaleadPlugin(unittest.TestCase):
def setUp(self):
self.app_mock = MagicMock()
self.config = {'useragent':'test-agent', 'proxy': None}
self.plugin = Plugin(self.app_mock, self.config)

def test_plugin_init(self):
self.app_mock.register_plugin.assert_called_once_with('exalead', {'search': search})

@patch('plugins.exalead.app_emailharvester')
def test_search(self, mock_app_emailharvester):
mock_app_emailharvester.init_search = MagicMock()
mock_app_emailharvester.process = MagicMock()
mock_app_emailharvester.get_emails = MagicMock(return_value = ['[email protected]'])

domain = "email.com"
limit = 10
result = search(domain,limit)

expected_url = 'http://www.exalead.com/search/web/results/?q=%40{word}&elements_per_page=10&start_index={counter}'

mock_app_emailharvester.init_search(expected_url, domain, limit, 0, 50, 'Exalead')
mock_app_emailharvester.process.assert_called_once()
mock_app_emailharvester.get_emails.assert_called_once()

self.assertEqual(['[email protected]'], result)





if __name__ == '__main__':
unittest.main()
Loading