Skip to content

Commit

Permalink
Merge pull request #202 from Normal-OJ/feat/allow-view-CE-stderr
Browse files Browse the repository at this point in the history
feat(submission_api): allow students to view output of CE submissions
  • Loading branch information
Bogay authored Jun 7, 2023
2 parents 884ed6f + 6e78249 commit ba8815e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 32 deletions.
22 changes: 3 additions & 19 deletions model/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,35 +278,19 @@ def get_submission(user, submission: Submission):
return HTTPResponse(data=ret)


@submission_api.route(
'/<submission>/output/<int:task_no>/<int:case_no>',
methods=['GET'],
)
@Request.args('text')
@submission_api.get('/<submission>/output/<int:task_no>/<int:case_no>')
@login_required
@Request.doc('submission', Submission)
def get_submission_output(
user,
submission: Submission,
task_no: int,
case_no: int,
text: Optional[str],
):
if not submission.permission(user, Submission.Permission.FEEDBACK):
if not submission.permission(user, Submission.Permission.VIEW_OUTPUT):
return HTTPError('permission denied', 403)
if text is None:
text = True
else:
try:
text = {'true': True, 'false': False}[text]
except KeyError:
return HTTPError('Invalid `text` value.', 400)
try:
output = submission.get_single_output(
task_no,
case_no,
text=text,
)
output = submission.get_single_output(task_no, case_no)
except FileNotFoundError as e:
return HTTPError(str(e), 400)
except AttributeError as e:
Expand Down
23 changes: 18 additions & 5 deletions mongo/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ class Permission(enum.IntFlag):
COMMENT = enum.auto() # teacher or TAs can give comment
REJUDGE = enum.auto() # teacher or TAs can rejudge submission
GRADE = enum.auto() # teacher or TAs can grade homework
VIEW_OUTPUT = enum.auto()
OTHER = VIEW
STUDENT = OTHER | UPLOAD | FEEDBACK
MANAGER = STUDENT | COMMENT | REJUDGE | GRADE
MANAGER = STUDENT | COMMENT | REJUDGE | GRADE | VIEW_OUTPUT

_config = None

Expand Down Expand Up @@ -166,7 +167,12 @@ def config(cls):
cls._config.save()
return cls._config.reload()

def get_single_output(self, task_no, case_no, text=True):
def get_single_output(
self,
task_no: int,
case_no: int,
text: bool = True,
):
try:
case = self.tasks[task_no].cases[case_no]
except IndexError:
Expand All @@ -177,7 +183,7 @@ def get_single_output(self, task_no, case_no, text=True):
ret = {k: zf.read(k) for k in ('stdout', 'stderr')}
if text:
ret = {k: v.decode('utf-8') for k, v in ret.items()}
except AttributeError as e:
except AttributeError:
raise AttributeError('The submission is still in pending')
return ret

Expand Down Expand Up @@ -741,12 +747,19 @@ def own_permission(self, user) -> Permission:
cap = self.Permission.MANAGER
elif user.username == self.user.username:
cap = self.Permission.STUDENT
elif Problem(self.problem).permission(user=user,
req=Problem.Permission.VIEW):
elif Problem(self.problem).permission(
user=user,
req=Problem.Permission.VIEW,
):
cap = self.Permission.OTHER
else:
cap = self.Permission(0)

# students can view outputs of their CE submissions
CE = 2
if cap & self.Permission.STUDENT and self.status == CE:
cap |= self.Permission.VIEW_OUTPUT

cache.set(key, cap.value, 60)
return cap

Expand Down
43 changes: 43 additions & 0 deletions tests/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mongo import engine
from .base_tester import BaseTester
from .utils import *
from tests import utils

A_NAMES = [
'teacher',
Expand Down Expand Up @@ -1008,3 +1009,45 @@ def test_edit_config(self, client_admin):
'token': 'AAAAA',
}]
}


def test_student_cannot_view_WA_submission_output(forge_client, app):
student = utils.user.create_user()
problem = utils.problem.create_problem(
test_case_info=utils.problem.create_test_case_info(
language=0,
task_len=1,
))
WA = 1
with app.app_context():
submission = utils.submission.create_submission(
user=student,
problem=problem,
status=WA,
)
utils.submission.add_fake_output(submission)
client = forge_client(student.username)
rv = client.get(f'/submission/{submission.id}/output/0/0')
assert rv.status_code == 403, rv.get_json()


def test_student_can_view_CE_submission_output(forge_client, app):
student = utils.user.create_user()
problem = utils.problem.create_problem(
test_case_info=utils.problem.create_test_case_info(
language=0,
task_len=1,
))
CE = 2
with app.app_context():
submission = utils.submission.create_submission(
user=student,
problem=problem,
status=CE,
)
utils.submission.add_fake_output(submission)
client = forge_client(student.username)
rv = client.get(f'/submission/{submission.id}/output/0/0')
assert rv.status_code == 200, rv.get_json()
expected = submission.get_single_output(0, 0)
assert expected == rv.get_json()['data']
4 changes: 2 additions & 2 deletions tests/utils/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def create_problem(


def cmp_copied_problem(original: Problem, copy: Problem):
# It shouold be a new problem
# It should be a new problem
assert original.problem_id != copy.problem_id
# But some fields are identical
fields = (
Expand All @@ -175,6 +175,6 @@ def cmp_copied_problem(original: Problem, copy: Problem):
old = getattr(original, field)
new = getattr(copy, field)
assert old == new, (field, old, new)
# And some fields shuold be default
# And some fields should be default
assert len(copy.homeworks) == 0
assert len(copy.high_scores) == 0
26 changes: 20 additions & 6 deletions tests/utils/submission.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import logging
import tempfile
import warnings
import zipfile
import pytest
from datetime import date, datetime
from copy import copy
from datetime import datetime
from random import randint, choice
from typing import Any, Optional, Union
from typing import Optional, Union
from mongo import *
from mongo.utils import drop_none

Expand Down Expand Up @@ -38,8 +37,7 @@ def create_submission(
'lang': lang,
'timestamp': timestamp,
}
sid = Submission.add(**params)
submission = Submission(sid)
submission = Submission.add(**params)
if status is None:
if score is not None:
status = 0 if score == 100 else choice([1, 3, 4, 5])
Expand Down Expand Up @@ -74,3 +72,19 @@ def create_submission(
}))
submission.reload()
return submission


def add_fake_output(submission: Submission):
status_str = next(s for s, c in submission.status2code.items()
if c == submission.status)
single_result = {
'exitCode': 0,
'status': status_str,
'stdout': 'out',
'stderr': 'err',
'execTime': submission.exec_time,
'memoryUsage': submission.memory_usage,
}
result = [[copy(single_result) for _ in range(t.case_count)]
for t in submission.problem.test_case.tasks]
submission.process_result(result)

0 comments on commit ba8815e

Please sign in to comment.