Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dojo_plugin/utils/dojo.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,12 @@ def assert_import_one(query, error_message):
for name, value in dojo_kwargs.items():
setattr(dojo, name, value)

existing_challenges = {(challenge.module.id, challenge.id): challenge.challenge for challenge in dojo.challenges}
# Category/name mismatches also occur after transfers; path_override is the only persisted import marker.
existing_challenges = {
(challenge.module.id, challenge.id): challenge.challenge
for challenge in dojo.challenges
if not challenge.path_override
}
def challenge(module_id, challenge_id, transfer=None):
if (module_id, challenge_id) in existing_challenges:
return existing_challenges[(module_id, challenge_id)]
Expand Down
35 changes: 35 additions & 0 deletions test/test_dojos.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,41 @@ def test_create_dojo_pulls_image(admin_session):
def test_import(import_dojo, admin_session):
assert admin_session.get(f"{DOJO_URL}/{import_dojo}/hello").status_code == 200


def test_convert_imported_challenge_without_delete(admin_session, example_dojo):
suffix = "".join(random.choices(string.ascii_lowercase, k=8))
dojo_id = f"convert-import-{suffix}"
spec = {
"id": dojo_id,
"modules": [{
"id": "test",
"resources": [{
"type": "challenge",
"id": "test",
"name": "Test",
"import": {"dojo": example_dojo, "module": "hello", "challenge": "apple"},
}],
}],
}
dojo = create_dojo_yml(yaml.safe_dump(spec), session=admin_session)
source_id = get_dojo_challenge_id("example", "hello", "apple")
assert get_dojo_challenge_id(dojo_id, "test", "test") == source_id

del spec["modules"][0]["resources"][0]["import"]
response = admin_session.post(f"{DOJO_URL}/pwncollege_api/v1/dojos/{dojo}/update", json=spec)
assert response.status_code == 200

converted_id = get_dojo_challenge_id(dojo_id, "test", "test")
assert converted_id != source_id
assert db_sql(f"SELECT category || ':' || name FROM challenges WHERE id = {converted_id}").strip() == f"{dojo.rsplit('~', 1)[1]}:test:test"
assert db_sql(f"SELECT COALESCE(data->>'path_override', '') FROM dojo_challenges WHERE challenge_id = {converted_id}").strip() == ""
assert int(db_sql(f"SELECT count(*) FROM flags WHERE challenge_id = {converted_id}")) == 1
assert get_dojo_challenge_id("example", "hello", "apple") == source_id

response = admin_session.post(f"{DOJO_URL}/pwncollege_api/v1/dojos/{dojo}/update", json=spec)
assert response.status_code == 200
assert get_dojo_challenge_id(dojo_id, "test", "test") == converted_id

# this exists despite test_import because it doesn't re-run on re-test, but we still want to make sure our public example-import dojo passes
def test_create_import_dojo(example_import_dojo, admin_session):
assert admin_session.get(f"{DOJO_URL}/{example_import_dojo}/").status_code == 200
Expand Down
Loading