22
33import static org .easymock .EasyMock .expect ;
44import static org .easymock .EasyMock .expectLastCall ;
5+ import static org .easymock .EasyMock .isA ;
56import static org .easymock .EasyMock .replay ;
67import static org .easymock .EasyMock .verify ;
78
89import java .util .ArrayList ;
910import java .util .List ;
11+ import java .util .Set ;
12+ import java .util .TreeSet ;
1013
1114import javax .servlet .http .HttpServletResponse ;
15+ import javax .servlet .http .HttpSession ;
1216import javax .servlet .http .HttpServletRequest ;
1317
1418import org .easymock .EasyMockExtension ;
1923import org .junit .jupiter .api .extension .ExtendWith ;
2024import org .springframework .security .core .GrantedAuthority ;
2125import org .springframework .security .core .authority .SimpleGrantedAuthority ;
26+ import org .springframework .security .authentication .AuthenticationManager ;
2227import org .springframework .security .authentication .TestingAuthenticationToken ;
2328import org .springframework .security .core .context .SecurityContext ;
2429import org .springframework .security .core .context .SecurityContextHolder ;
2530import org .springframework .security .core .context .SecurityContextImpl ;
31+ import org .wise .portal .domain .authentication .impl .StudentUserDetails ;
32+ import org .wise .portal .domain .group .Group ;
33+ import org .wise .portal .domain .group .impl .PersistentGroup ;
34+ import org .wise .portal .domain .project .impl .Projectcode ;
2635import org .wise .portal .domain .run .Run ;
2736import org .wise .portal .domain .run .impl .RunImpl ;
37+ import org .wise .portal .domain .user .User ;
38+ import org .wise .portal .domain .user .impl .UserImpl ;
2839import org .wise .portal .domain .workgroup .Workgroup ;
2940import org .wise .portal .domain .workgroup .impl .WorkgroupImpl ;
41+ import org .wise .portal .service .authentication .UserDetailsService ;
3042import org .wise .portal .service .run .RunService ;
43+ import org .wise .portal .service .student .StudentService ;
44+ import org .wise .portal .service .user .UserService ;
3145import org .wise .portal .service .workgroup .WorkgroupService ;
3246
3347@ ExtendWith (EasyMockExtension .class )
3448public class SurveyAPIControllerTest {
3549 private Run run ;
3650 private TestingAuthenticationToken authority ;
3751 private SecurityContext securityContext ;
52+ private UserImpl studentUser = new UserImpl ();
53+ private StudentUserDetails studentUserDetails = new StudentUserDetails ();
54+ List <Workgroup > workgroups ;
3855
3956 @ TestSubject
4057 SurveyAPIController surveyAPIController = new SurveyAPIController ();
4158
59+ @ Mock
60+ AuthenticationManager authenticationManager ;
61+
4262 @ Mock
4363 HttpServletResponse httpServletResponse ;
4464
4565 @ Mock
4666 HttpServletRequest httpServletRequest ;
4767
68+ @ Mock
69+ HttpSession httpSession ;
70+
4871 @ Mock
4972 RunService runService ;
5073
74+ @ Mock
75+ StudentService studentService ;
76+
77+ @ Mock
78+ UserService userService ;
79+
80+ @ Mock
81+ UserDetailsService userDetailsService ;
82+
5183 @ Mock
5284 WorkgroupService workgroupService ;
5385
86+ private static final String [] periodnames = { "1" , "2" , "3" , "6" , "9" , "10" , "sunflower" };
87+
5488 @ BeforeEach
5589 public void setUp () {
5690 run = new RunImpl ();
5791 run .setId (1L );
92+ run .setIsSurvey (true );
93+ Set <Group > periods = new TreeSet <Group >();
94+ for (String periodname : periodnames ) {
95+ Group period = new PersistentGroup ();
96+ period .setName (periodname );
97+ if (periodname .equals ("1" )) {
98+ period .addMember (studentUser );
99+ }
100+ periods .add (period );
101+ }
102+ run .setPeriods (periods );
103+ workgroups = new ArrayList <Workgroup >();
58104 }
59105
60- SecurityContext getSecurityContext (String role , String authorityName ) {
61- authority = new TestingAuthenticationToken (role ,
106+ SecurityContext getSecurityContext (Object principal , String authorityName ) {
107+ authority = new TestingAuthenticationToken (principal ,
62108 new GrantedAuthority [] { new SimpleGrantedAuthority (authorityName ) });
63109 authority .setAuthenticated (true );
110+ authority .setDetails (new StudentUserDetails ());
64111 securityContext = new SecurityContextImpl ();
65112 securityContext .setAuthentication (authority );
66113 return securityContext ;
@@ -80,54 +127,58 @@ public void launchSurveyRun_NotASurvey_RedirectHomePage() throws Exception {
80127 }
81128
82129 @ Test
83- public void launchSurveyRun_AlreadySignedIn_RedirectLogOutPage () throws Exception {
84- httpServletResponse .sendRedirect ("/survey/logout " );
130+ public void launchSurveyRun_AlreadyAssociatedWithRun_RedirectUnit () throws Exception {
131+ httpServletResponse .sendRedirect ("/student/unit/1 " );
85132 expectLastCall ();
86133 replay (httpServletResponse );
87- run .setIsSurvey (true );
88134 expect (runService .retrieveRunByRuncode ("dog1234" )).andReturn (run );
89135 replay (runService );
90- SecurityContextHolder .setContext (getSecurityContext ("student" , "ROLE_STUDENT" ));
136+ expect (userService .retrieveStudentById (null )).andReturn (studentUser );
137+ replay (userService );
138+ SecurityContextHolder .setContext (getSecurityContext (studentUserDetails , "ROLE_STUDENT" ));
91139 surveyAPIController .launchSurveyRun ("dog1234-1" , httpServletResponse , httpServletRequest );
92- verify (httpServletResponse );
93- verify (runService );
140+ verify (httpServletResponse , runService , userService );
94141 }
95142
96143 @ Test
97144 public void launchSurveyRun_OverWorkgroupLimit_RedirectWorkgroupLimitPage () throws Exception {
98145 httpServletResponse .sendRedirect ("/survey/workgroupLimitReached" );
99146 expectLastCall ();
100147 replay (httpServletResponse );
101- run . setIsSurvey ( false );
148+ expect ( userService . retrieveStudentById ( 1L )). andReturn ( new UserImpl () );
102149 expect (runService .retrieveRunByRuncode ("dog1234" )).andReturn (run );
103150 replay (runService );
104151 SecurityContextHolder .setContext (getSecurityContext ("anonymousUser" , "ROLE_ANONYMOUS" ));
105- List <Workgroup > workgroups = new ArrayList <Workgroup >();
106152 for (int i = 0 ; i < 1005 ; i ++) {
107153 workgroups .add (new WorkgroupImpl ());
108154 }
109155 expect (workgroupService .getWorkgroupsForRun (run )).andReturn (workgroups );
110156 replay (workgroupService );
111157 surveyAPIController .launchSurveyRun ("dog1234-1" , httpServletResponse , httpServletRequest );
112- verify (httpServletResponse );
113- verify (runService );
114- verify (workgroupService );
158+ verify (httpServletResponse , runService , workgroupService );
115159 }
116160
117161 @ Test
118162 public void launchSurveyRun_NoIssues_RedirectUnit () throws Exception {
119163 httpServletResponse .sendRedirect ("/student/unit/1" );
120164 expectLastCall ();
121165 replay (httpServletResponse );
122- run .setIsSurvey (false );
123166 expect (runService .retrieveRunByRuncode ("dog1234" )).andReturn (run );
124167 replay (runService );
168+ studentUser .setUserDetails (studentUserDetails );
169+ expect (userService .createUser (studentUserDetails )).andReturn (studentUser );
170+ replay (userService );
125171 SecurityContextHolder .setContext (getSecurityContext ("anonymousUser" , "ROLE_ANONYMOUS" ));
126172 expect (workgroupService .getWorkgroupsForRun (run )).andReturn (new ArrayList <Workgroup >());
127173 replay (workgroupService );
174+ expect (userDetailsService .loadAuthorityByName ("ROLE_SURVEY_STUDENT" )).andReturn (null );
175+ replay (userDetailsService );
176+ expect (httpServletRequest .getSession (true )).andReturn (httpSession );
177+ replay (httpServletRequest );
178+ studentService .addStudentToRun (isA (User .class ), isA (Projectcode .class ));
179+ expectLastCall ();
180+ replay (studentService );
128181 surveyAPIController .launchSurveyRun ("dog1234-1" , httpServletResponse , httpServletRequest );
129- verify (httpServletResponse );
130- verify (runService );
131- verify (workgroupService );
182+ verify (httpServletResponse , runService , workgroupService , studentService );
132183 }
133184}
0 commit comments