Skip to content

Commit ec1f263

Browse files
committed
Fix mocking of anonymous users in IsaacTest tests
I finally worked out the "right" way to do the mocking consistent with the existing code. Also, the existing mocks were fragile since not all endpoints must use "getCurrentRegisteredUser", so this and "getCurrentUser" are updated to return the right values but also be optional now.
1 parent fd03879 commit ec1f263

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/test/java/uk/ac/cam/cl/dtg/isaac/IsaacTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import uk.ac.cam.cl.dtg.isaac.dto.ResultsWrapper;
3939
import uk.ac.cam.cl.dtg.isaac.dto.UserGroupDTO;
4040
import uk.ac.cam.cl.dtg.isaac.dto.content.QuizSummaryDTO;
41+
import uk.ac.cam.cl.dtg.isaac.dto.users.AnonymousUserDTO;
4142
import uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO;
4243
import uk.ac.cam.cl.dtg.isaac.dto.users.UserSummaryWithEmailAddressDTO;
4344
import uk.ac.cam.cl.dtg.segue.api.managers.GroupManager;
@@ -86,6 +87,7 @@ public class IsaacTest {
8687
protected RegisteredUserDTO secondTeacher;
8788
protected RegisteredUserDTO otherTeacher;
8889
protected RegisteredUserDTO noone;
90+
protected AnonymousUserDTO anonUser;
8991
protected RegisteredUserDTO secondStudent;
9092
protected RegisteredUserDTO otherStudent;
9193
protected RegisteredUserDTO adminUser;
@@ -190,6 +192,7 @@ protected void initializeIsaacObjects() {
190192
otherTeacher.setId(++id);
191193

192194
noone = null;
195+
anonUser = new AnonymousUserDTO("fake-session-id");
193196

194197
secondStudent = new RegisteredUserDTO("Second", "Student", "[email protected]", EmailVerificationStatus.VERIFIED, somePastDate, Gender.FEMALE, somePastDate, "", null, false);
195198
secondStudent.setRole(Role.STUDENT);

src/test/java/uk/ac/cam/cl/dtg/isaac/api/AbstractFacadeTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import jakarta.annotation.Nullable;
3333
import jakarta.servlet.http.HttpServletRequest;
34-
import jakarta.servlet.http.HttpSession;
3534
import jakarta.ws.rs.core.Request;
3635
import jakarta.ws.rs.core.Response;
3736
import jakarta.ws.rs.core.Response.Status;
@@ -48,7 +47,6 @@
4847
import java.util.stream.Collectors;
4948

5049
import static org.easymock.EasyMock.anyObject;
51-
import static org.easymock.EasyMock.anyString;
5250
import static org.easymock.EasyMock.createNiceMock;
5351
import static org.easymock.EasyMock.expect;
5452
import static org.easymock.EasyMock.getCurrentArguments;
@@ -102,17 +100,12 @@ abstract public class AbstractFacadeTest extends IsaacTest {
102100

103101
@Before
104102
public void abstractFacadeTestSetup() {
105-
HttpSession session = createMock(HttpSession.class);
106-
expect(session.getAttribute(anyString())).andReturn(null).anyTimes();
107-
expect(session.getId()).andReturn("fake-session-id").anyTimes();
108-
replay(session);
109103
httpServletRequest = createMock(HttpServletRequest.class);
110-
expect(httpServletRequest.getSession()).andReturn(session).anyTimes();
111104
replay(httpServletRequest);
112105
request = createNiceMock(Request.class); // We don't particularly care about what gets called on this.
113106
replay(request);
114107

115-
userManager = createPartialMock(UserAccountManager.class, "getCurrentRegisteredUser", "convertToUserSummaryObject", "getUserDTOById");
108+
userManager = createPartialMock(UserAccountManager.class, "getCurrentUser", "getCurrentRegisteredUser", "convertToUserSummaryObject", "getUserDTOById");
116109

117110
registerDefaultsFor(userManager, m -> {
118111
expect(m.convertToUserSummaryObject(anyObject(RegisteredUserDTO.class))).andStubAnswer(() -> {
@@ -435,9 +428,11 @@ private void runSteps(Endpoint endpoint) {
435428
private void runStepsAs(@Nullable RegisteredUserDTO user, Endpoint endpoint) {
436429
withMock(userManager, m -> {
437430
if (user == null) {
438-
expect(m.getCurrentRegisteredUser(httpServletRequest)).andThrow(new NoUserLoggedInException());
431+
expect(m.getCurrentRegisteredUser(httpServletRequest)).andThrow(new NoUserLoggedInException()).anyTimes();
432+
expect(m.getCurrentUser(httpServletRequest)).andReturn(anonUser).anyTimes();
439433
} else {
440-
expect(m.getCurrentRegisteredUser(httpServletRequest)).andReturn(user);
434+
expect(m.getCurrentRegisteredUser(httpServletRequest)).andReturn(user).anyTimes();
435+
expect(m.getCurrentUser(httpServletRequest)).andReturn(user).anyTimes();
441436
}
442437
});
443438
currentUser = user;

0 commit comments

Comments
 (0)