Skip to content

Commit

Permalink
Add black and isort tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piatrashkakanstantinas committed Oct 2, 2021
1 parent 2bf89ba commit b543a41
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 114 deletions.
6 changes: 4 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def lint(session: Session) -> None:
def tests(session: Session) -> None:
"""Run the test suite."""
session.run("poetry", "install", "--no-dev", external=True)
install_with_constraints(session, "pytest")
install_with_constraints(session, "pytest", "pytest-black", "pytest-isort")
session.run("pytest")


Expand All @@ -61,7 +61,9 @@ def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
args = session.posargs or ["-m", "not e2e"]
session.run("poetry", "install", "--no-dev", external=True)
install_with_constraints(session, "pytest", "pytest-mock", "typeguard")
install_with_constraints(
session, "pytest", "pytest-mock", "typeguard", "pytest-black", "pytest-isort"
)
session.run("pytest", f"--typeguard-packages={package}", *args)


Expand Down
219 changes: 131 additions & 88 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ black = {version = "^21.6b0", python = "^3.6.2"}
isort = {version = "^5.9.2", python = "^3.6.1"}
flake8 = "3.8.4"
requests = "^2.26.0"
pytest-black = "^0.3.12"
pytest-isort = "^2.0.0"

[tool.poetry.extras]
optimize = ["orjson"]
Expand All @@ -30,3 +32,15 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
pywhat = "pywhat.what:main"
what = "pywhat.what:main"

[tool.isort]
profile = "black"
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
line_length = 88

[tool.pytest.ini_options]
addopts = "--black --isort"
3 changes: 1 addition & 2 deletions pywhat/filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from collections.abc import Mapping
from typing import Optional

from pywhat.helper import (AvailableTags, CaseInsensitiveSet, InvalidTag,
load_regexes)
from pywhat.helper import AvailableTags, CaseInsensitiveSet, InvalidTag, load_regexes


class Filter(Mapping):
Expand Down
49 changes: 33 additions & 16 deletions pywhat/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def pretty_print(self, text: dict, text_input):
if self._check_if_directory(text_input):
# if input is a folder, add a filename column
table.add_column("File", overflow="fold")

# Check if there are any bug bounties with exploits
# in the regex
self._check_if_exploit_in_json(text)
Expand Down Expand Up @@ -63,14 +63,17 @@ def pretty_print(self, text: dict, text_input):
)
else:
description = i["Regex Pattern"]["Description"]

if "Exploit" in i["Regex Pattern"] and i["Regex Pattern"]["Exploit"]:

if (
"Exploit" in i["Regex Pattern"]
and i["Regex Pattern"]["Exploit"]
):
exploit = i["Regex Pattern"]["Exploit"]

if not description:
description = "None"

#FIXME this is quite messy
# FIXME this is quite messy
if self.bug_bounty_mode:
if self._check_if_directory(text_input):
table.add_row(
Expand Down Expand Up @@ -109,11 +112,12 @@ def pretty_print(self, text: dict, text_input):

def print_json(self, text: dict):
self.console.print(json.dumps(text))

"""
Does not create a table, prints it as raw text
Returns the printable object
"""

def print_raw(self, text: dict, text_input) -> str:
output_str = ""

Expand All @@ -124,7 +128,6 @@ def print_raw(self, text: dict, text_input) -> str:
output_str += f"[bold #D7Afff]File Identified[/bold #D7Afff]: [bold]{key}[/bold] with Magic Numbers {value['ISO 8859-1']}."
output_str += f"\n[bold #D7Afff]File Description:[/bold #D7Afff] {value['Description']}."
output_str += "\n"


if text["Regexes"]:
for key, value in text["Regexes"].items():
Expand All @@ -133,8 +136,13 @@ def print_raw(self, text: dict, text_input) -> str:
matched = i["Matched"]
if self._check_if_directory(text_input):
output_str += f"[bold #D7Afff]File: {key}[/bold #D7Afff]\n"
output_str += "[bold #D7Afff]Matched on: [/bold #D7Afff]" + i["Matched"]
output_str += "\n[bold #D7Afff]Name: [/bold #D7Afff]" + i["Regex Pattern"]["Name"]
output_str += (
"[bold #D7Afff]Matched on: [/bold #D7Afff]" + i["Matched"]
)
output_str += (
"\n[bold #D7Afff]Name: [/bold #D7Afff]"
+ i["Regex Pattern"]["Name"]
)

link = None
if "URL" in i["Regex Pattern"] and i["Regex Pattern"]["URL"]:
Expand All @@ -143,25 +151,34 @@ def print_raw(self, text: dict, text_input) -> str:
+ i["Regex Pattern"]["URL"]
+ matched.replace(" ", "")
)

if link:
output_str += link

if i["Regex Pattern"]["Description"]:
description = "\n[bold #D7Afff]Description: [/bold #D7Afff]" + i["Regex Pattern"]["Description"]

description = (
"\n[bold #D7Afff]Description: [/bold #D7Afff]"
+ i["Regex Pattern"]["Description"]
)

if description:
output_str += description

if "Exploit" in i["Regex Pattern"] and i["Regex Pattern"]["Exploit"]:
output_str += "\n[bold #D7Afff]Exploit: [/bold #D7Afff]" + i["Regex Pattern"]["Exploit"]

if (
"Exploit" in i["Regex Pattern"]
and i["Regex Pattern"]["Exploit"]
):
output_str += (
"\n[bold #D7Afff]Exploit: [/bold #D7Afff]"
+ i["Regex Pattern"]["Exploit"]
)
output_str += "\n\n"

if output_str == "":
self.console.print("Nothing found!")

self.console.print(output_str)

def _check_if_exploit_in_json(self, text: dict) -> bool:
if "File Signatures" in text.keys() and text["File Signatures"]:
# loops files
Expand All @@ -175,4 +192,4 @@ def _check_if_exploit_in_json(self, text: dict) -> bool:
self.bug_bounty_mode = True

def _check_if_directory(self, text_input):
return os.path.isdir(text_input)
return os.path.isdir(text_input)
4 changes: 3 additions & 1 deletion pywhat/regex_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def check(

if reg.get("Exploit") is not None and "curl" in reg["Exploit"]:
# Replace anything like XXXXX_XXXXXX_HERE with the match
reg["Exploit"] = re.sub(r'[A-Z_]+_HERE', matched, reg["Exploit"])
reg["Exploit"] = re.sub(
r"[A-Z_]+_HERE", matched, reg["Exploit"]
)

children = reg.get("Children")
if children is not None:
Expand Down
6 changes: 5 additions & 1 deletion pywhat/what.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ def create_filter(rarity, include, exclude):
is_flag=True,
help="Search filenames for possible matches.",
)
@click.option("--format", required = False, help="--format json for json output. --format pretty for a pretty table output.")
@click.option(
"--format",
required=False,
help="--format json for json output. --format pretty for a pretty table output.",
)
def main(**kwargs):
"""
pyWhat - Identify what something is.
Expand Down
18 changes: 14 additions & 4 deletions tests/test_regex_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def _assert_match_in_items(name, res):
assert i["Regex Pattern"]["Name"] == name


@pytest.mark.skip(reason="Not all regex have tests now, check https://github.com/bee-san/pyWhat/pull/146#issuecomment-927087231 for info.")
@pytest.mark.skip(
reason="Not all regex have tests now, check https://github.com/bee-san/pyWhat/pull/146#issuecomment-927087231 for info."
)
def test_if_all_tests_exist():
with open("tests/test_regex_identifier.py", "r", encoding="utf-8") as file:
tests = file.read()
Expand Down Expand Up @@ -662,15 +664,23 @@ def test_github_access_token():


def test_slack_api_key():
res = r.check(["xoxp-514654431830-843187921057-792480346180-d44d2r9b71f954o8z2k5llt41ovpip6v"])
res = r.check(
["xoxp-514654431830-843187921057-792480346180-d44d2r9b71f954o8z2k5llt41ovpip6v"]
)
_assert_match_first_item("Slack API Key", res)
_assert_match_exploit_first_item("https://slack.com/api/auth.test?token=xoxp-514654431830-843187921057-792480346180-d44d2r9b71f954o8z2k5llt41ovpip6v", res)
_assert_match_exploit_first_item(
"https://slack.com/api/auth.test?token=xoxp-514654431830-843187921057-792480346180-d44d2r9b71f954o8z2k5llt41ovpip6v",
res,
)


def test_slack_token():
res = r.check(["xoxb-51465443183-hgvhXVd2ISC2x7gaoRWBOUdQ"])
_assert_match_first_item("Slack Token", res)
_assert_match_exploit_first_item("https://slack.com/api/auth.test?token=xoxb-51465443183-hgvhXVd2ISC2x7gaoRWBOUdQ", res)
_assert_match_exploit_first_item(
"https://slack.com/api/auth.test?token=xoxb-51465443183-hgvhXVd2ISC2x7gaoRWBOUdQ",
res,
)


def test_pgp_public_key():
Expand Down

0 comments on commit b543a41

Please # to comment.