MachineSsoServerClient extends HttpSsoServerClient { @Override public Subject getSubject(String token, String clientUrl) { if (!token.startsWith("machine")) { return super.getSubject(token, clientUrl); } try { final User user = userManager.getById(tokenRegistry.getUserId(token)); return new SubjectImpl(user.getName(), user.getId(), token, false); } catch (ApiException x) { LOG.warn(x.getLocalizedMessage(), x); } return null; } @Inject  MachineSsoServerClient(
      @Named("che.api") String apiEndpoint,
      HttpJsonRequestFactory requestFactory,
      MachineTokenRegistry tokenRegistry,
      UserManager userManager); @Override Subject getSubject(String token, String clientUrl);  }
@Test public void getUserMustDelegateCallToTheSuperWhenTokenIsNotPrefixedWithMachine() throws NotFoundException { ssoClient.getSubject("123456789", "client"); verify(registrySpy, never()).getUserId(any()); }
@Test public void getUserMustReturnNullWhenTokenIsMachinePrefixedButUserForSuchTokenDoesNotExist() throws NotFoundException { final Subject user = ssoClient.getSubject("machine123456789", "client"); assertNull(user); verify(registrySpy).getUserId("machine123456789"); }
@Test public void getSubjectMustReturnTheSubjectRetrievedFromTheApiRequestWithMachineToken() throws Exception { final String token = registrySpy.generateToken("user123", "workspace1234"); final User user = new UserImpl("user123", "mail", "name"); when(userManagerMock.getById(any())).thenReturn(user); final Subject sessionUser = ssoClient.getSubject(token, "client"); assertNotNull(sessionUser); assertEquals(sessionUser.getUserName(), user.getName()); assertEquals(sessionUser.getUserId(), user.getId()); assertFalse(sessionUser.isTemporary()); assertEquals(sessionUser.getToken(), token); }