jburns
Aug 24th, 2004, 01:52 PM
what's the best way to get a SecureContext in unit tests? I'm using auth.getPrincipal()).getUsername() to do some queries based on the username in a controller, and would like to test it by a mock, or another method. I have httpunit tests working just fine as an outside view.
// from acegi sample app
Authentication auth = ((SecureContext) ContextHolder.getContext()).getAuthentication();
String owner = auth.getPrincipal().toString();
if (auth.getPrincipal() instanceof UserDetails) {
owner = ((UserDetails) auth.getPrincipal()).getUsername();
}
// end
(...)
User currentuser = usermgr.getUserByUsername(owner);
project.setOwner(currentuser);
mgr.saveProject(project);
// from acegi sample app
Authentication auth = ((SecureContext) ContextHolder.getContext()).getAuthentication();
String owner = auth.getPrincipal().toString();
if (auth.getPrincipal() instanceof UserDetails) {
owner = ((UserDetails) auth.getPrincipal()).getUsername();
}
// end
(...)
User currentuser = usermgr.getUserByUsername(owner);
project.setOwner(currentuser);
mgr.saveProject(project);