|
| 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 | + |
0 commit comments