@Test @WithMockUser(value = USERNAME, roles = ROLE_SU) void testUseRecoveryCode() { User molgenisUser = mock(User.class); RecoveryCode recoveryCode = mock(RecoveryCode.class); when(userService.getUser(USERNAME)).thenReturn(molgenisUser); when(molgenisUser.getId()).thenReturn("1234"); String recoveryCodeId = "lkfsdufash"; UserSecret userSecret = mock(UserSecret.class); @SuppressWarnings("unchecked") Query<RecoveryCode> recoveryCodeQuery = mock(Query.class, RETURNS_SELF); doReturn(recoveryCodeQuery).when(dataService).query(RECOVERY_CODE, RecoveryCode.class); when(recoveryCodeQuery .eq(USER_ID, molgenisUser.getId()) .and() .eq(CODE, recoveryCodeId) .findOne()) .thenReturn(recoveryCode); @SuppressWarnings("unchecked") Query<UserSecret> userSecretQuery = mock(Query.class, RETURNS_SELF); doReturn(userSecretQuery) .when(dataService) .query(UserSecretMetadata.USER_SECRET, UserSecret.class); when(userSecretQuery.eq(UserSecretMetadata.USER_ID, molgenisUser.getId()).findOne()) .thenReturn(userSecret); recoveryServiceImpl.useRecoveryCode(recoveryCodeId); verify(userSecret).setFailedLoginAttempts(0); verify(dataService).update(UserSecretMetadata.USER_SECRET, userSecret); }
@Override @Transactional public void useRecoveryCode(String recoveryCode) { String userId = getUser().getId(); RecoveryCode existingCode = runAsSystem( () -> dataService .query(RECOVERY_CODE, RecoveryCode.class) .eq(USER_ID, userId) .and() .eq(CODE, recoveryCode) .findOne()); if (existingCode != null) { runAsSystem(() -> dataService.delete(RECOVERY_CODE, existingCode)); UserSecret secret = runAsSystem( () -> dataService .query(USER_SECRET, UserSecret.class) .eq(UserSecretMetadata.USER_ID, userId) .findOne()); secret.setFailedLoginAttempts(0); runAsSystem(() -> dataService.update(USER_SECRET, secret)); } else { throw new BadCredentialsException("Invalid recovery code or code already used"); } }
RecoveryServiceImpl implements RecoveryService { @Override @Transactional public void useRecoveryCode(String recoveryCode) { String userId = getUser().getId(); RecoveryCode existingCode = runAsSystem( () -> dataService .query(RECOVERY_CODE, RecoveryCode.class) .eq(USER_ID, userId) .and() .eq(CODE, recoveryCode) .findOne()); if (existingCode != null) { runAsSystem(() -> dataService.delete(RECOVERY_CODE, existingCode)); UserSecret secret = runAsSystem( () -> dataService .query(USER_SECRET, UserSecret.class) .eq(UserSecretMetadata.USER_ID, userId) .findOne()); secret.setFailedLoginAttempts(0); runAsSystem(() -> dataService.update(USER_SECRET, secret)); } else { throw new BadCredentialsException("Invalid recovery code or code already used"); } } }
RecoveryServiceImpl implements RecoveryService { @Override @Transactional public void useRecoveryCode(String recoveryCode) { String userId = getUser().getId(); RecoveryCode existingCode = runAsSystem( () -> dataService .query(RECOVERY_CODE, RecoveryCode.class) .eq(USER_ID, userId) .and() .eq(CODE, recoveryCode) .findOne()); if (existingCode != null) { runAsSystem(() -> dataService.delete(RECOVERY_CODE, existingCode)); UserSecret secret = runAsSystem( () -> dataService .query(USER_SECRET, UserSecret.class) .eq(UserSecretMetadata.USER_ID, userId) .findOne()); secret.setFailedLoginAttempts(0); runAsSystem(() -> dataService.update(USER_SECRET, secret)); } else { throw new BadCredentialsException("Invalid recovery code or code already used"); } } RecoveryServiceImpl(
      DataService dataService,
      UserService userService,
      RecoveryCodeFactory recoveryCodeFactory,
      IdGenerator idGenerator); }
RecoveryServiceImpl implements RecoveryService { @Override @Transactional public void useRecoveryCode(String recoveryCode) { String userId = getUser().getId(); RecoveryCode existingCode = runAsSystem( () -> dataService .query(RECOVERY_CODE, RecoveryCode.class) .eq(USER_ID, userId) .and() .eq(CODE, recoveryCode) .findOne()); if (existingCode != null) { runAsSystem(() -> dataService.delete(RECOVERY_CODE, existingCode)); UserSecret secret = runAsSystem( () -> dataService .query(USER_SECRET, UserSecret.class) .eq(UserSecretMetadata.USER_ID, userId) .findOne()); secret.setFailedLoginAttempts(0); runAsSystem(() -> dataService.update(USER_SECRET, secret)); } else { throw new BadCredentialsException("Invalid recovery code or code already used"); } } RecoveryServiceImpl(
      DataService dataService,
      UserService userService,
      RecoveryCodeFactory recoveryCodeFactory,
      IdGenerator idGenerator); @Override @Transactional Stream<RecoveryCode> generateRecoveryCodes(); @Override @Transactional void useRecoveryCode(String recoveryCode); @Override Stream<RecoveryCode> getRecoveryCodes(); }
RecoveryServiceImpl implements RecoveryService { @Override @Transactional public void useRecoveryCode(String recoveryCode) { String userId = getUser().getId(); RecoveryCode existingCode = runAsSystem( () -> dataService .query(RECOVERY_CODE, RecoveryCode.class) .eq(USER_ID, userId) .and() .eq(CODE, recoveryCode) .findOne()); if (existingCode != null) { runAsSystem(() -> dataService.delete(RECOVERY_CODE, existingCode)); UserSecret secret = runAsSystem( () -> dataService .query(USER_SECRET, UserSecret.class) .eq(UserSecretMetadata.USER_ID, userId) .findOne()); secret.setFailedLoginAttempts(0); runAsSystem(() -> dataService.update(USER_SECRET, secret)); } else { throw new BadCredentialsException("Invalid recovery code or code already used"); } } RecoveryServiceImpl(
      DataService dataService,
      UserService userService,
      RecoveryCodeFactory recoveryCodeFactory,
      IdGenerator idGenerator); @Override @Transactional Stream<RecoveryCode> generateRecoveryCodes(); @Override @Transactional void useRecoveryCode(String recoveryCode); @Override Stream<RecoveryCode> getRecoveryCodes();  }