Skip to content

Commit

Permalink
SAK-46968 Assignments: canSubmit now checks getUserSubmission (sakaip…
Browse files Browse the repository at this point in the history
…roject#10311)

* SAK-46968 Assignments: canSubmit now checks getUserSubmission

https://sakaiproject.atlassian.net/browse/SAK-46968

* Update assignment/impl/src/test/org/sakaiproject/assignment/impl/AssignmentServiceTest.java
  • Loading branch information
adrianfish authored Mar 4, 2022
1 parent d1e9c1f commit af3df11
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ public void updateSubmission(AssignmentSubmission submission) throws PermissionE

// TODO these permissions checks should coincide with the changes that are being made for the submission
if (!(allowUpdateSubmission(reference) || allowGradeSubmission(assignmentReference))) {
throw new PermissionException(sessionManager.getCurrentSessionUserId(), SECURE_UPDATE_ASSIGNMENT_SUBMISSION, null);
throw new PermissionException(sessionManager.getCurrentSessionUserId(), SECURE_UPDATE_ASSIGNMENT_SUBMISSION, reference);
}
eventTrackingService.post(eventTrackingService.newEvent(AssignmentConstants.EVENT_UPDATE_ASSIGNMENT_SUBMISSION, reference, true));

Expand Down Expand Up @@ -2350,8 +2350,13 @@ public boolean canSubmit(Assignment assignment, String userId) {
if (submission != null) {

// check for allow resubmission or not
//if an Extension exists for the user, we switch out the assignment's overall Close date for the extension deadline. We do this if the grade has been actually released, or if the submission object has not actually been submitted yet. Additionally, we make sure that a Resubmission date is not set [make sure it's null], so that this date-switching happens ONLY under Extension-related circumstances.
if (submission.getProperties().get(AssignmentConstants.ALLOW_EXTENSION_CLOSETIME) != null && (BooleanUtils.toBoolean(submission.getGradeReleased()) || !BooleanUtils.toBoolean(submission.getSubmitted()))){
// If an Extension exists for the user, we switch out the assignment's overall
// Close date for the extension deadline. We do this if the grade has been actually
// released, or if the submission object has not actually been submitted yet.
// Additionally, we make sure that a Resubmission date is not set [make sure it's null],
// so that this date-switching happens ONLY under Extension-related circumstances.
if (submission.getProperties().get(AssignmentConstants.ALLOW_EXTENSION_CLOSETIME) != null
&& (BooleanUtils.toBoolean(submission.getGradeReleased()) || !BooleanUtils.toBoolean(submission.getUserSubmission()))) {
Instant extensionCloseTime = Instant.ofEpochMilli(Long.parseLong(submission.getProperties().get(AssignmentConstants.ALLOW_EXTENSION_CLOSETIME)));
isBeforeAssignmentCloseDate = !currentTime.isAfter(extensionCloseTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void addAndGetSubmission() {
String context = UUID.randomUUID().toString();
String submitterId = UUID.randomUUID().toString();
try {
AssignmentSubmission savedSubmission = createNewSubmission(context, submitterId);
AssignmentSubmission savedSubmission = createNewSubmission(context, submitterId, null);
Assert.assertNotNull(savedSubmission);
Assert.assertNotNull(savedSubmission.getId());

Expand Down Expand Up @@ -423,7 +423,7 @@ public void removeSubmission() {

AssignmentSubmission submission = null;
try {
submission = createNewSubmission(context, submitterId);
submission = createNewSubmission(context, submitterId, null);
} catch (Exception e) {
Assert.fail("Could not create submission\n" + e.toString());
}
Expand Down Expand Up @@ -454,7 +454,7 @@ public void findSubmissionForUser() {
AssignmentSubmission submission = null;

try {
submission = createNewSubmission(context, submitterId);
submission = createNewSubmission(context, submitterId, null);
} catch (Exception e) {
Assert.fail("Could not create submission\n" + e.toString());
}
Expand All @@ -477,7 +477,7 @@ public void duplicateSubmissionsViaService() {
AssignmentSubmission submission = null;

try {
submission = createNewSubmission(context, submitterId);
submission = createNewSubmission(context, submitterId, null);
} catch (Exception e) {
Assert.fail("Could not create submission\n" + e.toString());
}
Expand Down Expand Up @@ -511,7 +511,7 @@ public void duplicateSubmissionRemoval() {
AssignmentSubmission submission = null;

try {
submission = createNewSubmission(context, submitterId);
submission = createNewSubmission(context, submitterId, null);
} catch (Exception e) {
Assert.fail("Could not create submission\n" + e.toString());
}
Expand Down Expand Up @@ -998,7 +998,7 @@ public void submissionStatus() {
Assert.assertEquals("Not Started", status);

try {
AssignmentSubmission submission = createNewSubmission(context, submitterId);
AssignmentSubmission submission = createNewSubmission(context, submitterId, null);
when(securityService.unlock(AssignmentServiceConstants.SECURE_ACCESS_ASSIGNMENT_SUBMISSION,
AssignmentReferenceReckoner.reckoner().submission(submission).reckon().getReference())).thenReturn(true);
status = assignmentService.getSubmissionStatus(submission.getId());
Expand Down Expand Up @@ -1255,7 +1255,7 @@ public void gradeUpdateFromAssignmentEventObeserver() {
String instructorId = UUID.randomUUID().toString();
Long itemId = new Random().nextLong();
try {
AssignmentSubmission newSubmission = createNewSubmission(context, submitterId);
AssignmentSubmission newSubmission = createNewSubmission(context, submitterId, null);
Assignment assignment = newSubmission.getAssignment();
assignment.getProperties().put(AssignmentConstants.PROP_ASSIGNMENT_ASSOCIATE_GRADEBOOK_ASSIGNMENT, itemId.toString());
assignment.setTypeOfGrade(Assignment.GradeType.SCORE_GRADE_TYPE);
Expand Down Expand Up @@ -1330,8 +1330,74 @@ public void mergeAssignmentFromXML() {
}
}

private AssignmentSubmission createNewSubmission(String context, String submitterId) throws UserNotDefinedException, IdUnusedException {
@Test
public void canSubmit() {

String context = UUID.randomUUID().toString();
Assignment assignment = createNewAssignment(context);

String userId = "user1";
when(sessionManager.getCurrentSessionUserId()).thenReturn(userId);

Assert.assertFalse(assignmentService.canSubmit(assignment));

String siteRef = "/site/" + context;

when(securityService.unlock(userId, AssignmentServiceConstants.SECURE_ADD_ASSIGNMENT_SUBMISSION, siteRef)).thenReturn(true);

when(siteService.siteReference(context)).thenReturn(siteRef);

assignment.setOpenDate(Instant.now().plus(Period.ofDays(1)));
assignment.setCloseDate(Instant.now().plus(Duration.ofDays(3)));

Assert.assertFalse(assignmentService.canSubmit(assignment));

assignment.setOpenDate(Instant.now().minus(Period.ofDays(1)));
Assert.assertTrue(assignmentService.canSubmit(assignment));

assignment.setOpenDate(Instant.now().minus(Period.ofDays(2)));
assignment.setCloseDate(Instant.now().minus(Duration.ofDays(1)));
Assert.assertFalse(assignmentService.canSubmit(assignment));

assignment.setOpenDate(Instant.now().minus(Period.ofDays(1)));
assignment.setCloseDate(Instant.now().plus(Duration.ofDays(3)));

try {
AssignmentSubmission submission = createNewSubmission(context, userId, assignment);
Assert.assertFalse(assignmentService.canSubmit(assignment));
String reference = AssignmentReferenceReckoner.reckoner().submission(submission).reckon().getReference();
when(securityService.unlock(AssignmentServiceConstants.SECURE_ACCESS_ASSIGNMENT_SUBMISSION, reference)).thenReturn(true);
Assert.assertTrue(assignmentService.canSubmit(assignment));

String user2 = "user2";
when(sessionManager.getCurrentSessionUserId()).thenReturn(user2);
String addSubmissionRef = AssignmentReferenceReckoner.reckoner().context(context).subtype("s").reckon().getReference();
when(securityService.unlock(user2, AssignmentServiceConstants.SECURE_ADD_ASSIGNMENT_SUBMISSION, null)).thenReturn(true);

assignment.setOpenDate(Instant.now().minus(Period.ofDays(3)));
assignment.setCloseDate(Instant.now().minus(Duration.ofDays(1)));
Assert.assertFalse(assignmentService.canSubmit(assignment));

submission = createNewSubmission(context, user2, assignment);
submission.setSubmitted(false);
submission.setUserSubmission(false);
Map<String, String> props = submission.getProperties();
props.put(AssignmentConstants.ALLOW_EXTENSION_CLOSETIME, Long.toString(Instant.now().plus(Duration.ofDays(5)).toEpochMilli()));

reference = AssignmentReferenceReckoner.reckoner().submission(submission).reckon().getReference();
when(securityService.unlock(AssignmentServiceConstants.SECURE_ADD_ASSIGNMENT_SUBMISSION, reference)).thenReturn(true);
assignmentService.updateSubmission(submission);
} catch (Exception e) {
Assert.fail("Could not create submission\n" + e.toString());
}
}

private AssignmentSubmission createNewSubmission(String context, String submitterId, Assignment assignment) throws UserNotDefinedException, IdUnusedException {

if (assignment == null) {
assignment = createNewAssignment(context);
}

String addSubmissionRef = AssignmentReferenceReckoner.reckoner().context(context).subtype("s").reckon().getReference();
Site site = mock(Site.class);
when(site.getGroup(submitterId)).thenReturn(mock(Group.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,10 @@ public ActionReturn setGrade(Map<String, Object> params) {
options.put(WITH_GRADES, true);
options.put(ALLOW_RESUBMIT_NUMBER, resubmitNumber);

if (!StringUtils.isBlank(resubmitDate)) {
if (StringUtils.isNotBlank(resubmitDate)) {
options.put(ALLOW_RESUBMIT_CLOSE_EPOCH_MILLIS, resubmitDate);
}
if(!StringUtils.isBlank(extensionDate)){
if (StringUtils.isNotBlank(extensionDate)){
options.put(ALLOW_EXTENSION_CLOSE_EPOCH_MILLIS, extensionDate);
}
Set<String> attachmentKeys
Expand Down Expand Up @@ -1024,9 +1024,8 @@ public Map<String, String> getProperties(String reference) {
}

boolean canUserAccessWizardPageAndLinkedArtifcact = false;
if (!"".equals(decSiteId) && !"".equals(decPageId)
&& !"null".equals(submissionId)) {
Map<String, Object> params = new HashMap<String, Object>();
if (StringUtils.isNotEmpty(decSiteId) && StringUtils.isNotEmpty(decPageId) && !"null".equals(submissionId)) {
Map<String, Object> params = new HashMap<>();
params.put("siteId", decSiteId);
params.put("pageId", decPageId);
params.put("linkedArtifactId", submissionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void gradeSubmission(AssignmentSubmission submission, String gradeOption,
properties.remove(ALLOW_RESUBMIT_NUMBER);
}

if(options.get(ALLOW_EXTENSION_CLOSETIME) != null){ //put State's info about extension into the Submission properties.
if (options.get(ALLOW_EXTENSION_CLOSETIME) != null){ //put State's info about extension into the Submission properties.
Instant extensionDeadline = getTimeFromOptions(options, ALLOW_EXTENSION_CLOSEMONTH, ALLOW_EXTENSION_CLOSEDAY, ALLOW_EXTENSION_CLOSEYEAR, ALLOW_EXTENSION_CLOSEHOUR, ALLOW_EXTENSION_CLOSEMIN);
properties.put(ALLOW_EXTENSION_CLOSETIME, String.valueOf(extensionDeadline.toEpochMilli()));
} else if (options.get(ALLOW_EXTENSION_CLOSE_EPOCH_MILLIS) != null) {
Expand Down

0 comments on commit af3df11

Please sign in to comment.