4

I have a Java Spring method I want to test out that look something like this:

@RunWithPermission(values={Permission.Manage, Permissions.ManageAll}, any=true)
@RequestMapping(method = RequestMethod.POST)
public Object createReport(@RequestBody ReportParam definition) throws Exception {
    ...
    return reportManager.createReport(definition);
}

Now my function will only run if the user has Permission.Manage or Permission.ManageAll but will not run if the user has permission like Permission.View. I want to write a unit test that will test this out so I tried something like this:

@Autowired
@InjectMocks
private RestReportController controller;
...
@Test()
@RunWithPermission(values={Permissions.View}, any=true)
public void testCreateReportPermissions() throws Exception {
    ReportParam param = getReportParam("report_param.json");
    param.setLabelIds(Arrays.asList(labelService.getHomeId()));
    controller.createReport(param);
}

EDIT: Attempt 2: I've also tried to create some sort of fake user session to see if I can "set" the permissions during the test but that doesn't seem to work either.

@Test()
public void testCreateReportPermissions() throws Exception{
    ReportParam param = getReportParam("report_param.json");
    param.setLabelIds(Arrays.asList(labelService.getHomeId()));
    sessionManager.clearUserSession();
    userSession = TestUtils.createFakeSession("testuser001", "1000", Permissions.View);
    sessionManager.setUserSession(userSession);
    controller.createReport(param);
}

However, as seen by my test, it creates the Report and the unit test finishes. However, is there a way to have it throw some sort of error or exception since I annotated the method with a permission with Permission.View? If anyone could help, that would be great. Thanks!

user1871869
  • 3,317
  • 13
  • 56
  • 106

0 Answers0