Skip to content

Commit

Permalink
Fix dojo import
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorNelson committed Nov 10, 2023
1 parent e9e65a4 commit ef8bfd0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dojo_plugin/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def __init__(self, *args, **kwargs):
if kwargs.get("challenge") is not None:
raise AttributeError("Import requires challenge to be None")

for field in ["id", "name", "description", "challenge", "image"]:
for field in ["id", "name", "description", "challenge"]:
kwargs[field] = kwargs[field] if kwargs.get(field) is not None else getattr(default, field, None)

# TODO: maybe we should track the entire import
Expand Down
34 changes: 23 additions & 11 deletions test/test_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,33 @@ def test_login():
login("admin", "admin")


@pytest.mark.dependency()
def test_create_dojo(admin_session):
create_dojo_json = dict(repository="pwncollege/example-dojo", public_key="", private_key="")
def create_dojo(repository, *, official=True):
create_dojo_json = dict(repository=repository, public_key="", private_key="")
response = admin_session.post(f"{PROTO}://{HOST}/pwncollege_api/v1/dojo/create", json=create_dojo_json)
assert response.status_code == 200, f"Expected status code 200, but got {response.status_code}"
dojo_reference_id = response.json()["dojo"]

# TODO: add an official endpoint for making dojos official
id, dojo_id = dojo_reference_id.split("~", 1)
dojo_id = int.from_bytes(bytes.fromhex(dojo_id.rjust(8, "0")), "big", signed=True)
sql = f"UPDATE dojos SET official = 1 WHERE id = '{id}' and dojo_id = {dojo_id}"
dojo_run("db", input=sql)
sql = f"SELECT official FROM dojos WHERE id = '{id}' and dojo_id = {dojo_id}"
db_result = dojo_run("db", input=sql)
assert db_result.stdout == "official\n1\n", f"Failed to make dojo official: {db_result.stdout}"
if official:
# TODO: add an official endpoint for making dojos official
id, dojo_id = dojo_reference_id.split("~", 1)
dojo_id = int.from_bytes(bytes.fromhex(dojo_id.rjust(8, "0")), "big", signed=True)
sql = f"UPDATE dojos SET official = 1 WHERE id = '{id}' and dojo_id = {dojo_id}"
dojo_run("db", input=sql)
sql = f"SELECT official FROM dojos WHERE id = '{id}' and dojo_id = {dojo_id}"
db_result = dojo_run("db", input=sql)
assert db_result.stdout == "official\n1\n", f"Failed to make dojo official: {db_result.stdout}"

return dojo_reference_id


@pytest.mark.dependency()
def test_create_dojo(admin_session):
create_dojo("pwncollege/example-dojo")


@pytest.mark.dependency(depends=["test_create_dojo"])
def test_create_import_dojo(admin_session):
create_dojo("pwncollege/example-import-dojo")


@pytest.mark.dependency(depends=["test_create_dojo"])
Expand Down

0 comments on commit ef8bfd0

Please sign in to comment.