Skip to content

Add username parameter to dojo solves #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
11 changes: 7 additions & 4 deletions dojo_plugin/api/v1/dojos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask import request
from flask_restx import Namespace, Resource
from sqlalchemy.sql import and_
from CTFd.models import db, Solves
from CTFd.models import db, Solves, Users
from CTFd.cache import cache
from CTFd.plugins.challenges import get_chal_class
from CTFd.utils.decorators import authed_only, admins_only, ratelimit
Expand Down Expand Up @@ -119,10 +119,13 @@ def get(self, dojo):

@dojos_namespace.route("/<dojo>/solves")
class DojoSolveList(Resource):
@authed_only
@dojo_route
def get(self, dojo):
user = get_current_user()
username = request.args.get("username")
user = Users.query.filter_by(name=username, hidden=False).first() if username else get_current_user()
if not user:
return {"error": "User not found"}, 400

solves_query = dojo.solves(user=user, ignore_visibility=True, ignore_admins=False)

if after := request.args.get("after"):
Expand Down Expand Up @@ -301,4 +304,4 @@ def get(self, dojo, module, challenge_id):
return {
"success": True,
"description": render_markdown(dojo_challenge.description)
}
}
27 changes: 26 additions & 1 deletion test/test_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,29 @@ def test_surveys(surveys_dojo, random_user):

post_survey_response(surveys_dojo, "surveys-module-1", "challenge-level", "Test response", session=session)
post_survey_response(surveys_dojo, "surveys-module-1", "module-level", "up", session=session)
post_survey_response(surveys_dojo, "surveys-module-2", "dojo-level", 1, session=session)
post_survey_response(surveys_dojo, "surveys-module-2", "dojo-level", 1, session=session)

@pytest.mark.dependency(depends=["test_start_challenge"])
def test_dojo_solves_api(example_dojo, random_user):
user_name, session = random_user
dojo = example_dojo

random_id = "".join(random.choices(string.ascii_lowercase, k=16))
other_session = login(random_id, random_id, register=True)

start_challenge(dojo, "hello", "apple", session=session)
solve_challenge(dojo, "hello", "apple", session=session, user=user_name)

response = session.get(f"{DOJO_URL}/pwncollege_api/v1/dojos/{dojo}/solves")
assert response.status_code == 200
data = response.json()
assert data["success"]
assert len(data["solves"]) == 1
assert data["solves"][0]["challenge_id"] == "apple"

response = other_session.get(f"{DOJO_URL}/pwncollege_api/v1/dojos/{dojo}/solves", params={"username": user_name})
assert response.status_code == 200
data = response.json()
assert data["success"]
assert len(data["solves"]) == 1
assert data["solves"][0]["challenge_id"] == "apple"