MachineTokenProviderImpl implements MachineTokenProvider { @Override public String getToken(String workspaceId) throws MachineTokenException { final Subject subject = EnvironmentContext.getCurrent().getSubject(); if (subject.isAnonymous()) { throw new IllegalStateException( format( "Unable to get machine token of the workspace '%s' " + "because it does not exist for an anonymous user.", workspaceId)); } return getToken(subject.getUserId(), workspaceId); } @Inject  MachineTokenProviderImpl(
      PermissionChecker permissionChecker, MachineTokenRegistry tokenRegistry); @Override String getToken(String workspaceId); @Override String getToken(String userId, String workspaceId);  }
@Test public void shouldReturnMachineTokenForCurrentSubject() throws Exception { doReturn("user123").when(currentSubject).getUserId(); doReturn("secret").when(tokenRegistry).getOrCreateToken(any(), any()); doReturn(true) .when(permissionChecker) .hasPermission("user123", WorkspaceDomain.DOMAIN_ID, "workspace123", WorkspaceDomain.USE); String token = tokenProvider.getToken("workspace123"); assertEquals(token, "secret"); verify(tokenRegistry).getOrCreateToken("user123", "workspace123"); verify(permissionChecker) .hasPermission("user123", WorkspaceDomain.DOMAIN_ID, "workspace123", WorkspaceDomain.USE); }
@Test( expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "Unable to get machine token of the workspace " + "'workspace123' because it does not exist for an anonymous user\\.") public void shouldThrowExceptionIfCurrentSubjectIsAnonymous() throws Exception { doReturn(true).when(currentSubject).isAnonymous(); tokenProvider.getToken("workspace123"); }
@Test( expectedExceptions = MachineAccessForbidden.class, expectedExceptionsMessageRegExp = "The user `user123` doesn't have the required `use` permission for workspace `workspace123`") public void shouldThrowExceptionIfCurrentSubjectDoesNotHavePermissionToRetrieveToken() throws Exception { doReturn(false) .when(permissionChecker) .hasPermission("user123", WorkspaceDomain.DOMAIN_ID, "workspace123", WorkspaceDomain.USE); doReturn("user123").when(currentSubject).getUserId(); doReturn("secret").when(tokenRegistry).getOrCreateToken(any(), any()); tokenProvider.getToken("workspace123"); }
@Test public void shouldReturnMachineTokenForTheSpecifiedUser() throws Exception { doReturn("secret").when(tokenRegistry).getOrCreateToken(any(), any()); doReturn(true) .when(permissionChecker) .hasPermission("user123", WorkspaceDomain.DOMAIN_ID, "workspace123", WorkspaceDomain.USE); String token = tokenProvider.getToken("user123", "workspace123"); assertEquals(token, "secret"); verify(tokenRegistry).getOrCreateToken("user123", "workspace123"); verify(permissionChecker) .hasPermission("user123", WorkspaceDomain.DOMAIN_ID, "workspace123", WorkspaceDomain.USE); }
@Test( expectedExceptions = MachineAccessForbidden.class, expectedExceptionsMessageRegExp = "The user `user123` doesn't have the required `use` permission for workspace `workspace123`") public void shouldThrowExceptionIfTheSpecifiedUserDoesNotHavePermissionToRetrieveToken() throws Exception { doReturn(false) .when(permissionChecker) .hasPermission("user123", WorkspaceDomain.DOMAIN_ID, "workspace123", WorkspaceDomain.USE); doReturn("secret").when(tokenRegistry).getOrCreateToken(any(), any()); tokenProvider.getToken("user123", "workspace123"); }