Skip to content

Commit d43a28d

Browse files
committed
Working on tests
1 parent e6d358c commit d43a28d

3 files changed

Lines changed: 139 additions & 12 deletions

File tree

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void launchSurveyRun(@PathVariable String code, HttpServletResponse respo
6666
Run run = runService.retrieveRunByRuncode(projectCode.getRuncode());
6767
if (run.getIsSurvey()) {
6868
handleSurveyLaunched(response, request, run, projectCode);
69-
} else {
69+
} else {
7070
response.sendRedirect("/");
7171
}
7272
}
@@ -75,15 +75,15 @@ private void handleSurveyLaunched(HttpServletResponse response, HttpServletReque
7575
throws AuthorityNotFoundException, IOException, DuplicateUsernameException, ObjectNotFoundException,
7676
PeriodNotFoundException, StudentUserAlreadyAssociatedWithRunException, RunHasEndedException {
7777
if (userAlreadySignedIn()) {
78-
response.sendRedirect("/survey/logout");
79-
} else if (underWorkgroupLimit(run)) {
80-
User user = this.createNewStudentAccount();
81-
loginStudent(request, user);
82-
studentService.addStudentToRun(user, projectCode);
83-
response.sendRedirect("/student/unit/" + run.getId());
84-
} else {
85-
response.sendRedirect("/survey/workgroupLimitReached");
86-
}
78+
response.sendRedirect("/survey/logout");
79+
} else if (underWorkgroupLimit(run)) {
80+
User user = this.createNewStudentAccount();
81+
loginStudent(request, user);
82+
studentService.addStudentToRun(user, projectCode);
83+
response.sendRedirect("/student/unit/" + run.getId());
84+
} else {
85+
response.sendRedirect("/survey/workgroupLimitReached");
86+
}
8787
}
8888

8989
private Boolean userAlreadySignedIn() {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.wise.portal.presentation.web.controllers.survey;
2+
3+
import static org.easymock.EasyMock.expect;
4+
import static org.easymock.EasyMock.expectLastCall;
5+
import static org.easymock.EasyMock.replay;
6+
import static org.easymock.EasyMock.verify;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import javax.servlet.http.HttpServletResponse;
12+
import javax.servlet.http.HttpServletRequest;
13+
14+
import org.easymock.EasyMockExtension;
15+
import org.easymock.Mock;
16+
import org.easymock.TestSubject;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
import org.springframework.security.core.GrantedAuthority;
21+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
22+
import org.springframework.security.authentication.TestingAuthenticationToken;
23+
import org.springframework.security.core.context.SecurityContext;
24+
import org.springframework.security.core.context.SecurityContextHolder;
25+
import org.wise.portal.domain.run.Run;
26+
import org.wise.portal.domain.run.impl.RunImpl;
27+
import org.wise.portal.domain.workgroup.Workgroup;
28+
import org.wise.portal.domain.workgroup.impl.WorkgroupImpl;
29+
import org.wise.portal.service.run.RunService;
30+
import org.wise.portal.service.workgroup.WorkgroupService;
31+
32+
@ExtendWith(EasyMockExtension.class)
33+
public class SurveyAPIControllerTest {
34+
private Run run;
35+
private TestingAuthenticationToken authority;
36+
private SecurityContext securityContext;
37+
38+
@TestSubject SurveyAPIController surveyAPIController = new SurveyAPIController();
39+
40+
@Mock HttpServletResponse httpServletResponse;
41+
42+
@Mock HttpServletRequest httpServletRequest;
43+
44+
@Mock RunService runService;
45+
46+
@Mock WorkgroupService workgroupService;
47+
48+
@BeforeEach
49+
public void setUp() {
50+
run = new RunImpl();
51+
run.setId(1);
52+
}
53+
54+
SecurityContext getSecurityContext(String role, String authorityName) {
55+
authority = new TestingAuthenticationToken(role,
56+
new GrantedAuthority[] { new SimpleGrantedAuthority(authorityName) });
57+
authority.setAuthenticated(true);
58+
securityContext = new SecurityContextImpl();
59+
securityContext.setAuthentication(authority);
60+
}
61+
62+
@Test
63+
public void launchSurveyRun_NotASurvey_RedirectHomePage() throws Exception {
64+
httpServletResponse.sendRedirect("/");
65+
expectLastCall();
66+
replay(httpServletResponse);
67+
run.setIsSurvey(false);
68+
expect(runService.retrieveRunByRuncode("dog1234")).andReturn(run);
69+
replay(runService);
70+
surveyAPIController.launchSurveyRun("dog1234-1", httpServletResponse, httpServletRequest);
71+
verify(httpServletResponse);
72+
verify(runService);
73+
}
74+
75+
@Test
76+
public void launchSurveyRun_AlreadySignedIn_RedirectLogOutPage() throws Exception {
77+
httpServletResponse.sendRedirect("/survey/logout");
78+
expectLastCall();
79+
replay(httpServletResponse);
80+
run.setIsSurvey(true);
81+
expect(runService.retrieveRunByRuncode("dog1234")).andReturn(run);
82+
replay(runService);
83+
SecurityContextHolder.setContext(getSecurityContext("student", "ROLE_STUDENT"));
84+
surveyAPIController.launchSurveyRun("dog1234-1", httpServletResponse, httpServletRequest);
85+
verify(httpServletResponse);
86+
verify(runService);
87+
}
88+
89+
@Test
90+
public void launchSurveyRun_OverWorkgroupLimit_RedirectWorkgroupLimitPage() throws Exception {
91+
httpServletResponse.sendRedirect("/survey/workgroupLimitReached");
92+
expectLastCall();
93+
replay(httpServletResponse);
94+
run.setIsSurvey(false);
95+
expect(runService.retrieveRunByRuncode("dog1234")).andReturn(run);
96+
replay(runService);
97+
SecurityContextHolder.setContext(getSecurityContext("anonymousUser", "ROLE_ANONYMOUS"));
98+
List<Workgroup> workgroups = new ArrayList<Workgroup>();
99+
for (int i = 0; i < 1005; i++) {
100+
workgroups.add(new WorkgroupImpl());
101+
}
102+
expect(workgroupService.getWorkgroupsForRun(run)).andReturn(workgroups);
103+
replay(workgroupService);
104+
surveyAPIController.launchSurveyRun("dog1234-1", httpServletResponse, httpServletRequest);
105+
verify(httpServletResponse);
106+
verify(runService);
107+
verify(workgroupService);
108+
}
109+
110+
@Test
111+
public void launchSurveyRun_NoIssues_RedirectUnit() throws Exception {
112+
httpServletResponse.sendRedirect("/student/unit/1");
113+
expectLastCall();
114+
replay(httpServletResponse);
115+
run.setIsSurvey(false);
116+
expect(runService.retrieveRunByRuncode("dog1234")).andReturn(run);
117+
replay(runService);
118+
SecurityContextHolder.setContext(getSecurityContext("anonymousUser", "ROLE_ANONYMOUS"));
119+
expect(workgroupService.getWorkgroupsForRun(run)).andReturn(new ArrayList<Workgroup>());
120+
replay(workgroupService);
121+
surveyAPIController.launchSurveyRun("dog1234-1", httpServletResponse, httpServletRequest);
122+
verify(httpServletResponse);
123+
verify(runService);
124+
verify(workgroupService);
125+
}
126+
}
127+

src/test/java/org/wise/portal/presentation/web/controllers/teacher/TeacherAPIControllerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,10 @@ public void createRun_ThreePeriods_CreateRun() throws Exception {
364364
periodNamesSet.add("1");
365365
periodNamesSet.add("2");
366366
periodNamesSet.add("free");
367-
expect(runService.createRun(projectId, teacher1, periodNamesSet, maxStudentsPerTeam, startDate,
367+
expect(runService.createRun(projectId, teacher1, periodNamesSet, false, maxStudentsPerTeam, startDate,
368368
endDate, isLockedAfterEndDate, Locale.US)).andReturn(run1);
369369
replay(runService);
370-
teacherAPIController.createRun(teacherAuth, request, projectId, periods, maxStudentsPerTeam,
370+
teacherAPIController.createRun(teacherAuth, request, projectId, periods, false, maxStudentsPerTeam,
371371
startDate, endDate, isLockedAfterEndDate);
372372
verify(userService);
373373
verify(request);

0 commit comments

Comments
 (0)