KeycloakUserManager extends PersonalAccountUserManager { public User getOrCreateUser(String id, String email, String username) throws ServerException, ConflictException { Optional<User> userById = getUserById(id); if (!userById.isPresent()) { synchronized (this) { userById = getUserById(id); if (!userById.isPresent()) { Optional<User> userByEmail = getUserByEmail(email); if (userByEmail.isPresent()) { remove(userByEmail.get().getId()); } final UserImpl cheUser = new UserImpl(id, email, username, generate("", 12), emptyList()); try { return create(cheUser, false); } catch (ConflictException ex) { cheUser.setName(generate(cheUser.getName(), 4)); return create(cheUser, false); } } } } return actualizeUserEmail(userById.get(), email); } @Inject  KeycloakUserManager(
      UserDao userDao,
      ProfileDao profileDao,
      PreferenceDao preferencesDao,
      AccountManager accountManager,
      EventService eventService,
      @Named("che.auth.reserved_user_names") String[] reservedNames); User getOrCreateUser(String id, String email, String username);  }
@Test public void shouldReturnExistingUser() throws Exception { UserImpl userImpl = new UserImpl("id", "user@mail.com", "name"); when(userDao.getById(eq("id"))).thenReturn(userImpl); User user = keycloakUserManager.getOrCreateUser("id", "user@mail.com", "name"); verify(userDao).getById("id"); assertEquals("id", user.getId()); assertEquals("user@mail.com", user.getEmail()); assertEquals("name", user.getName()); }
@Test public void shouldReturnUserAndUpdateHisEmail() throws Exception { ArgumentCaptor<UserImpl> captor = ArgumentCaptor.forClass(UserImpl.class); UserImpl userImpl = new UserImpl("id", "user@mail.com", "name"); when(userDao.getById(eq("id"))).thenReturn(userImpl); User user = keycloakUserManager.getOrCreateUser("id", "new@mail.com", "name"); verify(userDao, times(2)).getById("id"); verify(userDao).update(captor.capture()); assertEquals("id", captor.getValue().getId()); assertEquals("new@mail.com", captor.getValue().getEmail()); assertEquals("name", captor.getValue().getName()); assertEquals("id", user.getId()); assertEquals("new@mail.com", user.getEmail()); assertEquals("name", user.getName()); }
@Test public void shoudCreateNewUserIfHeIsNotFoundByIdOrEmail() throws Exception { ArgumentCaptor<UserImpl> captor = ArgumentCaptor.forClass(UserImpl.class); when(userDao.getById(eq("id"))).thenThrow(NotFoundException.class); when(userDao.getByEmail(eq("user@mail.com"))).thenThrow(NotFoundException.class); keycloakUserManager.getOrCreateUser("id", "user@mail.com", "name"); verify(userDao, times(2)).getById(eq("id")); verify(userDao).getByEmail(eq("user@mail.com")); verify(userDao).create((captor.capture())); assertEquals("id", captor.getValue().getId()); assertEquals("user@mail.com", captor.getValue().getEmail()); assertEquals("name", captor.getValue().getName()); }
@Test public void shoudRecreateUserIfHeIsntFoundByIdButFoundByEmail() throws Exception { UserImpl newUserImpl = new UserImpl("id", "user@mail.com", "name"); UserImpl oldUserImpl = new UserImpl("oldId", "user@mail.com", "name"); ArgumentCaptor<UserImpl> captor = ArgumentCaptor.forClass(UserImpl.class); when(userDao.getById(eq("id"))).thenThrow(NotFoundException.class); when(userDao.getByEmail(eq("user@mail.com"))).thenReturn(oldUserImpl); keycloakUserManager.getOrCreateUser("id", "user@mail.com", "name"); verify(userDao, times(2)).getById(eq("id")); verify(userDao).getByEmail(eq("user@mail.com")); verify(userDao).remove(eq(oldUserImpl.getId())); verify(userDao).create((captor.capture())); assertEquals(newUserImpl.getId(), captor.getValue().getId()); assertEquals(newUserImpl.getEmail(), captor.getValue().getEmail()); assertEquals(newUserImpl.getName(), captor.getValue().getName()); }
@Test public void shoudRecreateUserWithDifferentNameIfConflictOccures() throws Exception { UserImpl newUserImpl = new UserImpl("id", "user@mail.com", "name"); ArgumentCaptor<UserImpl> captor = ArgumentCaptor.forClass(UserImpl.class); ArgumentCaptor<UserImpl> captor2 = ArgumentCaptor.forClass(UserImpl.class); when(userDao.getById(eq("id"))).thenThrow(NotFoundException.class); when(userDao.getByEmail(eq("user@mail.com"))).thenThrow(NotFoundException.class); doAnswer( invocation -> { if (((UserImpl) invocation.getArgument(0)).getName().equals("name")) { throw new ConflictException(""); } return newUserImpl; }) .when(userDao) .create(any()); keycloakUserManager.getOrCreateUser("id", "user@mail.com", "name"); verify(userDao, times(2)).getById(eq("id")); verify(userDao).getByEmail(eq("user@mail.com")); verify(userDao, atLeastOnce()).create((captor.capture())); assertEquals(newUserImpl.getId(), captor.getValue().getId()); assertEquals(newUserImpl.getEmail(), captor.getValue().getEmail()); assertNotEquals(newUserImpl.getName(), captor.getValue().getName()); }