UserService { public static UserService create(String backingStore) { return new UserService(backingStore, ACCESS_TOKEN_TTL, ACCESS_TOKEN_TTL_UNIT); } @SuppressWarnings("unchecked") private  UserService(String backingStore, int accessTokenTtl,
            TimeUnit accessTokenTtlUnit); static UserService create(String backingStore); boolean authenticate(ByteBuffer username, ByteBuffer password); boolean can(ByteBuffer username, Permission permission,
            String environment); void create(ByteBuffer username, ByteBuffer password, Role role); void delete(ByteBuffer username); void disable(ByteBuffer username); void enable(ByteBuffer username); boolean exists(ByteBuffer username); void forEachUser(Consumer<ByteBuffer> consumer); Role getRole(ByteBuffer username); void grant(ByteBuffer username, Permission permission,
            String environment); boolean isEnabled(ByteBuffer username); void revoke(ByteBuffer username, String environment); void setPassword(ByteBuffer username, ByteBuffer password); void setRole(ByteBuffer username, Role role); final AccessTokenManager tokens; }
@Test public void testCanCreateAccessTokenForValidUser() { ByteBuffer username = getAcceptableUsername(); ByteBuffer password = getSecurePassword(); service.create(username, password, Role.ADMIN); AccessToken token = service.tokens.issue(username); Assert.assertTrue(service.tokens.isValid(token)); }
@Test public void testAccessTokenIsNotValidIfServerRestarts() { ByteBuffer username = getAcceptableUsername(); ByteBuffer password = getSecurePassword(); service.create(username, password, Role.ADMIN); AccessToken token = service.tokens.issue(username); UserService service2 = UserService.create(current); Assert.assertFalse(service2.tokens.isValid(token)); }
@Test public void testInvalidateAccessToken() { ByteBuffer username = getAcceptableUsername(); ByteBuffer password = getSecurePassword(); service.create(username, password, Role.ADMIN); AccessToken token = service.tokens.issue(username); service.tokens.expire(token); Assert.assertFalse(service.tokens.isValid(token)); }
@Test public void testTwoAccessTokensForSameUser() { ByteBuffer username = getAcceptableUsername(); ByteBuffer password = getSecurePassword(); service.create(username, password, Role.ADMIN); AccessToken token1 = service.tokens.issue(username); AccessToken token2 = service.tokens.issue(username); Assert.assertNotEquals(token1, token2); Assert.assertTrue(service.tokens.isValid(token1)); Assert.assertTrue(service.tokens.isValid(token2)); }
@Test public void testInvalidatingOneAccessTokenDoesNotAffectOther() { ByteBuffer username = getAcceptableUsername(); ByteBuffer password = getSecurePassword(); service.create(username, password, Role.ADMIN); AccessToken token1 = service.tokens.issue(username); AccessToken token2 = service.tokens.issue(username); service.tokens.expire(token2); Assert.assertTrue(service.tokens.isValid(token1)); }
@Test(expected = IllegalArgumentException.class) public void testCannotSetUserRoleToServiceOnCreation() { service.create(getAcceptableUsername(), getSecurePassword(), Role.SERVICE); }
@Test(expected = IllegalArgumentException.class) public void testCannotCreateUserWithServiceUsername() { ByteBuffer username = service.tokens .identify(service.tokens.serviceIssue()); service.create(username, getSecurePassword(), Role.ADMIN); }