Skip to content

Commit 5020256

Browse files
Redirect to homepage if run is not active.
1 parent b9ef218 commit 5020256

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/main/java/org/wise/portal/presentation/web/controllers/survey/SurveyAPIController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,21 @@ public void launchSurveyRun(@PathVariable String code, HttpServletResponse respo
6262
HttpServletRequest request) throws AuthorityNotFoundException, IOException,
6363
DuplicateUsernameException, ObjectNotFoundException, PeriodNotFoundException,
6464
StudentUserAlreadyAssociatedWithRunException, RunHasEndedException {
65-
6665
Projectcode projectCode = new Projectcode(code);
6766
Run run = runService.retrieveRunByRuncode(projectCode.getRuncode());
68-
if (run.isSurvey()) {
67+
if (run.isSurvey() && isActive(run)) {
6968
handleSurveyLaunched(response, request, run, projectCode);
7069
} else {
7170
sendRedirect(response, "/");
7271
}
7372
}
7473

74+
private boolean isActive(Run run) {
75+
Date now = new Date();
76+
Date endTime = run.getEndtime();
77+
return run.getStarttime().before(now) && (endTime == null || endTime.after(now));
78+
}
79+
7580
private void handleSurveyLaunched(HttpServletResponse response, HttpServletRequest request,
7681
Run run, Projectcode projectCode) throws AuthorityNotFoundException, IOException,
7782
DuplicateUsernameException, ObjectNotFoundException, PeriodNotFoundException,
@@ -103,11 +108,11 @@ private Object getSecurityContextHolderPrincipal() {
103108
return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
104109
}
105110

106-
private Boolean isStudentAssociatedWithRun(Run run, StudentUserDetails principal) {
111+
private boolean isStudentAssociatedWithRun(Run run, StudentUserDetails principal) {
107112
return run.isStudentAssociatedToThisRun(userService.retrieveStudentById((principal).getId()));
108113
}
109114

110-
private Boolean underWorkgroupLimit(Run run) {
115+
private boolean underWorkgroupLimit(Run run) {
111116
return workgroupService.getWorkgroupsForRun(run).size() <= 1000;
112117
}
113118

src/test/java/org/wise/portal/presentation/web/controllers/survey/SurveyAPIControllerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.easymock.EasyMock.verify;
88

99
import java.util.ArrayList;
10+
import java.util.Date;
1011
import java.util.List;
1112
import java.util.Set;
1213
import java.util.TreeSet;
@@ -90,6 +91,7 @@ public void setUp() {
9091
run = new RunImpl();
9192
run.setId(1L);
9293
run.setIsSurvey(true);
94+
run.setStarttime(new Date(System.currentTimeMillis() - 3600 * 1000));
9395
Set<Group> periods = new TreeSet<Group>();
9496
for (String periodname : periodnames) {
9597
Group period = new PersistentGroup();
@@ -126,6 +128,32 @@ public void launchSurveyRun_NotASurvey_RedirectHomePage() throws Exception {
126128
verify(runService);
127129
}
128130

131+
@Test
132+
public void launchSurveyRun_RunInFuture_RedirectHomePage() throws Exception {
133+
httpServletResponse.sendRedirect("/");
134+
expectLastCall();
135+
replay(httpServletResponse);
136+
run.setStarttime(new Date(System.currentTimeMillis() + 3600 * 1000)); // Future start time
137+
expect(runService.retrieveRunByRuncode("dog1234")).andReturn(run);
138+
replay(runService);
139+
surveyAPIController.launchSurveyRun("dog1234-1", httpServletResponse, httpServletRequest);
140+
verify(httpServletResponse);
141+
verify(runService);
142+
}
143+
144+
@Test
145+
public void launchSurveyRun_RunInPast_RedirectHomePage() throws Exception {
146+
httpServletResponse.sendRedirect("/");
147+
expectLastCall();
148+
replay(httpServletResponse);
149+
run.setEndtime(new Date(System.currentTimeMillis() - 3600 * 1000)); // Past end time
150+
expect(runService.retrieveRunByRuncode("dog1234")).andReturn(run);
151+
replay(runService);
152+
surveyAPIController.launchSurveyRun("dog1234-1", httpServletResponse, httpServletRequest);
153+
verify(httpServletResponse);
154+
verify(runService);
155+
}
156+
129157
@Test
130158
public void launchSurveyRun_AlreadyAssociatedWithRun_RedirectUnit() throws Exception {
131159
httpServletResponse.sendRedirect("/student/unit/1");

0 commit comments

Comments
 (0)