From ff82dfbf68cca5d085b7b08946ebd4fe7ace2fa3 Mon Sep 17 00:00:00 2001 From: L1ghtn1ng Date: Wed, 15 Jan 2020 22:29:42 +0000 Subject: [PATCH] Tidy up tests and remove duplicated code --- tests/discovery/test_certspotter.py | 5 ++--- tests/discovery/test_githubcode.py | 12 +++--------- tests/discovery/test_linkedin_links.py | 7 +++---- tests/discovery/test_otx.py | 5 ++--- theHarvester/discovery/otxsearch.py | 4 ++-- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/tests/discovery/test_certspotter.py b/tests/discovery/test_certspotter.py index 80736b45..0a39306b 100644 --- a/tests/discovery/test_certspotter.py +++ b/tests/discovery/test_certspotter.py @@ -5,26 +5,25 @@ from theHarvester.discovery import certspottersearch import requests import pytest +pytestmark = pytest.mark.asyncio + class TestCertspotter(object): @staticmethod def domain() -> str: return 'metasploit.com' - @pytest.mark.asyncio async def test_api(self): base_url = f'https://api.certspotter.com/v1/issuances?domain={TestCertspotter.domain()}&expand=dns_names' headers = {'User-Agent': Core.get_user_agent()} request = requests.get(base_url, headers=headers) assert request.status_code == 200 - @pytest.mark.asyncio async def test_search(self): search = certspottersearch.SearchCertspoter(TestCertspotter.domain()) await search.process() assert isinstance(await search.get_hostnames(), set) - @pytest.mark.asyncio async def test_search_no_results(self): search = certspottersearch.SearchCertspoter('radiant.eu') await search.process() diff --git a/tests/discovery/test_githubcode.py b/tests/discovery/test_githubcode.py index 78ad60d0..b8f358f9 100644 --- a/tests/discovery/test_githubcode.py +++ b/tests/discovery/test_githubcode.py @@ -6,6 +6,8 @@ from unittest.mock import MagicMock from requests import Response import pytest +pytestmark = pytest.mark.asyncio + class TestSearchGithubCode: @@ -64,55 +66,47 @@ class TestSearchGithubCode: response.json = MagicMock(return_value=json) response.status_code = 200 - @pytest.mark.asyncio async def test_missing_key(self): with pytest.raises(MissingKey): Core.github_key = MagicMock(return_value=None) githubcode.SearchGithubCode(word="test", limit=500) - @pytest.mark.asyncio async def test_fragments_from_response(self): Core.github_key = MagicMock(return_value="lol") test_class_instance = githubcode.SearchGithubCode(word="test", limit=500) - test_result = test_class_instance.fragments_from_response(self.OkResponse.response) + test_result = await test_class_instance.fragments_from_response(self.OkResponse.response) assert test_result == ["test1", "test2"] - @pytest.mark.asyncio async def test_invalid_fragments_from_response(self): Core.github_key = MagicMock(return_value="lol") test_class_instance = githubcode.SearchGithubCode(word="test", limit=500) test_result = test_class_instance.fragments_from_response(self.MalformedResponse.response) assert test_result == [] - @pytest.mark.asyncio async def test_handle_response_ok(self): Core.github_key = MagicMock(return_value="lol") test_class_instance = githubcode.SearchGithubCode(word="test", limit=500) test_result = test_class_instance.handle_response(self.OkResponse.response) assert isinstance(test_result, SuccessResult) - @pytest.mark.asyncio async def test_handle_response_retry(self): Core.github_key = MagicMock(return_value="lol") test_class_instance = githubcode.SearchGithubCode(word="test", limit=500) test_result = test_class_instance.handle_response(self.RetryResponse.response) assert isinstance(test_result, RetryResult) - @pytest.mark.asyncio async def test_handle_response_fail(self): Core.github_key = MagicMock(return_value="lol") test_class_instance = githubcode.SearchGithubCode(word="test", limit=500) test_result = test_class_instance.handle_response(self.FailureResponse.response) assert isinstance(test_result, ErrorResult) - @pytest.mark.asyncio async def test_next_page(self): Core.github_key = MagicMock(return_value="lol") test_class_instance = githubcode.SearchGithubCode(word="test", limit=500) test_result = githubcode.SuccessResult(list(), next_page=2, last_page=4) assert(2 == test_class_instance.next_page_or_end(test_result)) - @pytest.mark.asyncio async def test_last_page(self): Core.github_key = MagicMock(return_value="lol") test_class_instance = githubcode.SearchGithubCode(word="test", limit=500) diff --git a/tests/discovery/test_linkedin_links.py b/tests/discovery/test_linkedin_links.py index cdc2ab33..6c4f5327 100644 --- a/tests/discovery/test_linkedin_links.py +++ b/tests/discovery/test_linkedin_links.py @@ -6,10 +6,11 @@ import os import re import pytest +pytestmark = pytest.mark.asyncio + class TestGetLinks(object): - @pytest.mark.asyncio async def test_splitter(self): results = [ 'https://www.linkedin.com/in/don-draper-b1045618', @@ -21,14 +22,12 @@ class TestGetLinks(object): filtered_results = await splitter(results) assert len(filtered_results) == 1 - @pytest.mark.asyncio async def test_get_links(self): search = linkedinsearch.SearchLinkedin("facebook.com", '100') await search.process() links = await search.get_links() assert isinstance(links, list) - @pytest.mark.asyncio async def test_links_linkedin(self): dir_path = os.path.dirname(os.path.realpath(__file__)) mock_response = open(dir_path + "/test_linkedin_links.txt") @@ -44,4 +43,4 @@ class TestGetLinks(object): if __name__ == '__main__': - pytest.main() \ No newline at end of file + pytest.main() diff --git a/tests/discovery/test_otx.py b/tests/discovery/test_otx.py index 5f720d4d..81ff3395 100644 --- a/tests/discovery/test_otx.py +++ b/tests/discovery/test_otx.py @@ -5,26 +5,25 @@ from theHarvester.discovery import otxsearch import requests import pytest +pytestmark = pytest.mark.asyncio + class TestOtx(object): @staticmethod def domain() -> str: return 'metasploit.com' - @pytest.mark.asyncio async def test_api(self): base_url = f'https://otx.alienvault.com/api/v1/indicators/domain/{TestOtx.domain()}/passive_dns' headers = {'User-Agent': Core.get_user_agent()} request = requests.get(base_url, headers=headers) assert request.status_code == 200 - @pytest.mark.asyncio async def test_search(self): search = otxsearch.SearchOtx(TestOtx.domain()) await search.process() assert isinstance(await search.get_hostnames(), set) - @pytest.mark.asyncio async def test_search_no_results(self): search = otxsearch.SearchOtx('radiant.eu') await search.process() diff --git a/theHarvester/discovery/otxsearch.py b/theHarvester/discovery/otxsearch.py index 94aa153d..c9e00fbb 100644 --- a/theHarvester/discovery/otxsearch.py +++ b/theHarvester/discovery/otxsearch.py @@ -1,6 +1,6 @@ from theHarvester.lib.core import * -import json -#import grequests +# import json +# import grequests import re