@Test public void activate() throws Exception { Account account = service.createViaEmail("xyz", "xyz@gmail.com", "595959", lc); AccountOnceToken token = accountDao.listOnceTokens().get(0); assertTrue(service.activate(token.getToken())); Account loaded = accountDao.findById(account.getAccountId()).get(); assertTrue(loaded.isActivated()); assertTrue(loaded.getAuthorities().contains(Authority.CITIZEN)); assertFalse("activate twice should invalid", service.activate(token.getToken())); assertFalse("not exist token should invalid", service.activate("not exist")); }
@Override public boolean activate(String inputOnceToken) { return accountDao.findOnceToken(inputOnceToken, AccountOnceToken.Type.ACTIVATION) .filter(token -> token.isValid(Instant.now(clock))) .map(onceToken -> { Account account = accountDao.findById(onceToken.getAccountId()).get(); EnumSet<Authority> newAuth = EnumSet.copyOf(account.getAuthorities()); newAuth.add(Authority.CITIZEN); accountDao.updateAuthorities(account, newAuth); accountDao.completeOnceToken(onceToken); return true; }) .orElse(false); }
AccountServiceImpl implements AccountService { @Override public boolean activate(String inputOnceToken) { return accountDao.findOnceToken(inputOnceToken, AccountOnceToken.Type.ACTIVATION) .filter(token -> token.isValid(Instant.now(clock))) .map(onceToken -> { Account account = accountDao.findById(onceToken.getAccountId()).get(); EnumSet<Authority> newAuth = EnumSet.copyOf(account.getAuthorities()); newAuth.add(Authority.CITIZEN); accountDao.updateAuthorities(account, newAuth); accountDao.completeOnceToken(onceToken); return true; }) .orElse(false); } }
AccountServiceImpl implements AccountService { @Override public boolean activate(String inputOnceToken) { return accountDao.findOnceToken(inputOnceToken, AccountOnceToken.Type.ACTIVATION) .filter(token -> token.isValid(Instant.now(clock))) .map(onceToken -> { Account account = accountDao.findById(onceToken.getAccountId()).get(); EnumSet<Authority> newAuth = EnumSet.copyOf(account.getAuthorities()); newAuth.add(Authority.CITIZEN); accountDao.updateAuthorities(account, newAuth); accountDao.completeOnceToken(onceToken); return true; }) .orElse(false); }  }
AccountServiceImpl implements AccountService { @Override public boolean activate(String inputOnceToken) { return accountDao.findOnceToken(inputOnceToken, AccountOnceToken.Type.ACTIVATION) .filter(token -> token.isValid(Instant.now(clock))) .map(onceToken -> { Account account = accountDao.findById(onceToken.getAccountId()).get(); EnumSet<Authority> newAuth = EnumSet.copyOf(account.getAuthorities()); newAuth.add(Authority.CITIZEN); accountDao.updateAuthorities(account, newAuth); accountDao.completeOnceToken(onceToken); return true; }) .orElse(false); }  @Override Account createViaEmail(String username, String email, String password, Locale locale); @Override Optional<Account> findMe(Authorization authorization); @Override Optional<AccountAuth> authenticate(String username, String password); @Override Optional<AccountAccessToken> strongVerifyAccessToken(String rawAccessToken); @Override AccountAuth extendsAccessToken(AccountAccessToken accessToken); @Override boolean isUsernameAvailable(String username); @Override boolean isEmailAvailable(String email); @Override void updateAuthorities(Authorization authorization, EnumSet<Authority> authorities); @Override boolean activate(String inputOnceToken); @Override void resendActivation(Authorization authorization, Locale locale); @Override void sendResetPassword(String username, String email, Locale locale); @Override Optional<AccountOnceToken> findValidResetPasswordToken(String inputOnceToken); @Override void updatePasswordWithOnceToken(String accountOnceToken, String password, Locale locale); @Override AccountAuth updateNewPassword(Authorization authorization,
      String oldPassword,
      String newPassword,
      Locale locale); @Override Optional<AccountAccessToken> tryDecodeAccessToken(String rawAccountAccessToken); @Override AccountStats loadAccountStats(String username); @Override Account loadAccount(String username); @Override String updateDescription(Authorization authorization, String description); @Override String loadEditableDescription(Authorization authorization); @Override void complaintEmail(List<String> emails); @Override void muteEmail(List<String> emails); @Override AccountOnceToken createOauthDirectAuthorizeToken(Authorization authorization); @Override Optional<Account> oauthDirectAuthorize(String inputOnceToken); }
AccountServiceImpl implements AccountService { @Override public boolean activate(String inputOnceToken) { return accountDao.findOnceToken(inputOnceToken, AccountOnceToken.Type.ACTIVATION) .filter(token -> token.isValid(Instant.now(clock))) .map(onceToken -> { Account account = accountDao.findById(onceToken.getAccountId()).get(); EnumSet<Authority> newAuth = EnumSet.copyOf(account.getAuthorities()); newAuth.add(Authority.CITIZEN); accountDao.updateAuthorities(account, newAuth); accountDao.completeOnceToken(onceToken); return true; }) .orElse(false); }  @Override Account createViaEmail(String username, String email, String password, Locale locale); @Override Optional<Account> findMe(Authorization authorization); @Override Optional<AccountAuth> authenticate(String username, String password); @Override Optional<AccountAccessToken> strongVerifyAccessToken(String rawAccessToken); @Override AccountAuth extendsAccessToken(AccountAccessToken accessToken); @Override boolean isUsernameAvailable(String username); @Override boolean isEmailAvailable(String email); @Override void updateAuthorities(Authorization authorization, EnumSet<Authority> authorities); @Override boolean activate(String inputOnceToken); @Override void resendActivation(Authorization authorization, Locale locale); @Override void sendResetPassword(String username, String email, Locale locale); @Override Optional<AccountOnceToken> findValidResetPasswordToken(String inputOnceToken); @Override void updatePasswordWithOnceToken(String accountOnceToken, String password, Locale locale); @Override AccountAuth updateNewPassword(Authorization authorization,
      String oldPassword,
      String newPassword,
      Locale locale); @Override Optional<AccountAccessToken> tryDecodeAccessToken(String rawAccountAccessToken); @Override AccountStats loadAccountStats(String username); @Override Account loadAccount(String username); @Override String updateDescription(Authorization authorization, String description); @Override String loadEditableDescription(Authorization authorization); @Override void complaintEmail(List<String> emails); @Override void muteEmail(List<String> emails); @Override AccountOnceToken createOauthDirectAuthorizeToken(Authorization authorization); @Override Optional<Account> oauthDirectAuthorize(String inputOnceToken);  }