Skip to content

Commit

Permalink
Add some very rough unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trickeydan committed Feb 26, 2024
1 parent 9fa4030 commit f067cc0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
CMD:=poetry run
PYMODULE:=ctff
TESTS:=tests
EXTRACODE:=
EXTRACODE:=example

all: type test format lint

Expand Down
9 changes: 3 additions & 6 deletions ctff/challenge_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from logging import Logger
from typing import TypeVar

from flask import current_app, flash, redirect
from flask import current_app, flash
from flask.templating import render_template
from flask.views import MethodView
from werkzeug.wrappers import Response

from ctff.challenge import Challenge

Expand Down Expand Up @@ -35,7 +34,7 @@ def get(self) -> str:
ctff=current_app,
)

def post(self) -> Response:
def post(self) -> str:
"""Verify a submission."""
challenge = self.challenge()
if challenge.verify_submission():
Expand All @@ -47,6 +46,4 @@ def post(self) -> Response:
if self.challenge.group is None:
raise RuntimeError
else:
return redirect(
f"/{self.challenge.group.url_slug}/{challenge.get_url_slug()}",
)
return self.get()
3 changes: 3 additions & 0 deletions example/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .main import app

__all__ = ["app"]
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Generator

import pytest
from flask.testing import FlaskClient, FlaskCliRunner

from ctff import CTFF
from example import app as example_app_instance


@pytest.fixture
def example_app() -> Generator[CTFF, None, None]:
yield example_app_instance


@pytest.fixture
def client(example_app: CTFF) -> FlaskClient:
return example_app.test_client()


@pytest.fixture
def runner(example_app: CTFF) -> FlaskCliRunner:
return example_app.test_cli_runner()
63 changes: 63 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from flask.testing import FlaskClient

from ctff import CTFF


class TestCTFFIndex:
def test_get(self, example_app: CTFF, client: FlaskClient) -> None:
resp = client.get("/")

assert resp.status_code == 200
assert example_app.introduction_html in resp.text
assert "This CTF contains 1 categories of challenge." in resp.text
assert '<a href="/basic-challenges">Basic Challenges</a>' in resp.text


class TestCTFFChallengeGroup:
def test_get(self, example_app: CTFF, client: FlaskClient) -> None:
resp = client.get("/basic-challenges")

challenge_group = example_app._challenge_groups[0]

assert resp.status_code == 200
assert challenge_group.introduction_html in resp.text
assert f"<h1>{challenge_group.name}</h1>" in resp.text
assert '<a href="/basic-challenges/super-easy">Super Easy</a>' in resp.text


class TestCTFFChallenge:
def test_get(self, example_app: CTFF, client: FlaskClient) -> None:
resp = client.get("/basic-challenges/super-easy")

challenge_group = example_app._challenge_groups[0]
challenge = challenge_group._challenges[0]

assert resp.status_code == 200
assert f"<h1>{challenge.title}</h1>" in resp.text # type: ignore[attr-defined]
for part in challenge.parts: # type: ignore[attr-defined]
assert part.render() in resp.text

def test_post(self, example_app: CTFF, client: FlaskClient) -> None:
resp = client.post("/basic-challenges/super-easy", data={"example": "bees"})

challenge_group = example_app._challenge_groups[0]
challenge = challenge_group._challenges[0]

assert resp.status_code == 200
assert f"<h1>{challenge.title}</h1>" in resp.text # type: ignore[attr-defined]
assert "You completed the challenge." in resp.text
assert "flag{exampleFlag}" in resp.text
for part in challenge.parts: # type: ignore[attr-defined]
assert part.render() in resp.text

def test_post_incorrect(self, example_app: CTFF, client: FlaskClient) -> None:
resp = client.post("/basic-challenges/super-easy", data={"example": "wasps"})

challenge_group = example_app._challenge_groups[0]
challenge = challenge_group._challenges[0]

assert resp.status_code == 200
assert f"<h1>{challenge.title}</h1>" in resp.text # type: ignore[attr-defined]
assert "Incorrect." in resp.text
for part in challenge.parts: # type: ignore[attr-defined]
assert part.render() in resp.text

0 comments on commit f067cc0

Please sign in to comment.