Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

test: add tests for sf.parseLinks

Amr Awad194f18e2d3b1
diff --git a/sflib.py b/sflib.py
index 323d0f85..520e104b 100644
--- a/sflib.py
+++ b/sflib.py
@@ -1196,16 +1196,13 @@ class SpiderFoot:
for lnk in BeautifulSoup(data, "lxml",
parse_only=SoupStrainer(t)).find_all(t):
if lnk.has_attr(tags[t]):
- urlsRel.append([None, lnk[tags[t]]])
+ urlsRel.append(lnk[tags[t]])
except BaseException as e:
self.error("Error parsing with BeautifulSoup: " + str(e), False)
return None
# Loop through all the URLs/links found
- for linkTuple in urlsRel:
- # Remember the regex will return two vars (two groups captured)
- junk = linkTuple[0]
- link = linkTuple[1]
+ for link in urlsRel:
if type(link) != str:
link = str(link)
linkl = link.lower()
diff --git a/tests/test_sflib.py b/tests/test_sflib.py
index 221147f7..0b0f8165 100644
--- a/tests/test_sflib.py
+++ b/tests/test_sflib.py
@@ -4,7 +4,7 @@ from sflib import SpiderFoot
class TestSflib:
@pytest.mark.parametrize(
- "test_string,parsed_emails",
+ "test_string,expected_emails",
[
("[email protected]", ["[email protected]"]), # valid email
("b@f.", []), # too short (less than 5 charachters), should be ignored
@@ -12,7 +12,43 @@ class TestSflib:
("amr@wh...", []), # truncated, should be ignored
],
)
- def test_parse_emails(self, test_string, parsed_emails):
+ def test_parse_emails(self, test_string, expected_emails):
sf = SpiderFoot(options={"_debug": False})
- assert sf.parseEmails(test_string) == parsed_emails
+ assert sf.parseEmails(test_string) == expected_emails
+
+ @pytest.mark.parametrize(
+ "test_string,expected_links",
+ [
+ ( # valid full link
+ "<a href='https://www.google.com/'></a>",
+ {
+ "https://www.google.com/": {
+ "original": "https://www.google.com/",
+ "source": "https://www.example.com/",
+ }
+ },
+ ),
+ ( # for a partial link, it's added to the source url
+ "<a href='/partial'></a>",
+ {
+ "https://www.example.com/partial": {
+ "original": "/partial",
+ "source": "https://www.example.com/",
+ }
+ },
+ ),
+ ("<a href='#in-page-id'></a>", {},), # In page ids are ignored
+ ("<a href='mailto:[email protected]'></a>", {},), # mailto: are ignored
+ ],
+ )
+ def test_parse_links(self, test_string, expected_links):
+ sf = SpiderFoot(options={"_debug": False})
+
+ parsed_links = sf.parseLinks(
+ url="https://www.example.com/",
+ data=test_string,
+ domains=["testdomain.org"],
+ )
+
+ assert parsed_links == expected_links