OpenIDAuthenticationToken token = new OpenIDAuthenticationToken(null, "url", "msg", attributes);
context.setAuthentication(token);
SecurityContextHolder.setContext(context);
BusinessObjectService boSvc = mock(BusinessObjectService.class);
successHandler.setBusinessObjectService(boSvc);
assertEquals("no email attribute has been set in the token", noEmailOrTokenError, successHandler.getActivationMessage());
// add email attribute
OpenIDAttribute emailAttr = new OpenIDAttribute("email", "email_type", values);
attributes.add(emailAttr);
// simulate not finding an entity with the email provided
when(boSvc.findMatching(same(EntityEmailBo.class), anyMapOf(String.class, String.class))).thenReturn(
Collections.<EntityEmailBo> emptyList());
// mock entity info svc to return the mocked entity
EntityInfoService entityInfoSvc = mock(org.martinlaw.auth.OpenIDSuccessAuthenticationSuccessHandler.EntityInfoService.class);
when(entityInfoSvc.getEntityByEmail(emailFromOpenId)).thenReturn(null);
successHandler.setEntityInfoService(entityInfoSvc);
String expected = "Email address '" + emailFromOpenId + "' is not associated with an existing user :(";
assertEquals("entity should not be found", expected, successHandler.getActivationMessage());
// provide a mock entity object
EntityBo entity = new EntityBo();
EntityNameBo defaultName = new EntityNameBo();
defaultName.setActive(true);
defaultName.setDefaultValue(true);
final String firstName = "Karani";
defaultName.setFirstName(firstName);
entity.getNames().add(defaultName);
when(entityInfoSvc.getEntityByEmail(emailFromOpenId)).thenReturn(entity);
// prepare the entity email to be found and so that the mock entity can be returned
EntityEmailBo emailBo = new EntityEmailBo();
final String entityId = "entity1";
emailBo.setEntityId(entityId);
List<EntityEmailBo> result = new ArrayList<EntityEmailBo>();
result.add(emailBo);
when(boSvc.findMatching(same(EntityEmailBo.class), anyMapOf(String.class, String.class))).thenReturn(result);
// test that only active users with staff affiliation can log in
// entity.setActive(true); - does not matter
List<EntityAffiliationBo> affils = new ArrayList<EntityAffiliationBo>();
entity.setAffiliations(affils);