SystemAccountAuthenticator implements PasswordAuthenticator { public void authenticate(final String userId, final String password) throws AuthenticationFailedException, UserIdLockedException, PasswordExpiredException { if (userId == null || password == null) { throw new AuthenticationFailedException(userId); } Date sysDate = new Timestamp(DateUtil.getDate(SystemTimeUtil.getDateString()).getTime()); final SystemAccount account; try { account = UniversalDao.findBySqlFile( SystemAccount.class, "FIND_SYSTEM_ACCOUNT", new Object[]{userId, sysDate}); } catch (NoDataException e) { throw new AuthenticationFailedException(userId); } authenticate(account, password, sysDate); } SystemAccountAuthenticator(); void setFailedCountToLock(int failedCountToLock); void setPasswordEncryptor(PasswordEncryptor passwordEncryptor); void setDbManager(SimpleDbTransactionManager dbManager); void authenticate(final String userId, final String password);  }
@Test public void testPasswordIsUnMatch() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130802"); try { authenticator.authenticate("active user", "password1"); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is("1")); } }
@Test public void testLoginSuccess() throws Exception { createPasswordAuthenticator("20130802").authenticate("active user2", "pass!!!"); PreparedStatement statement = con.prepareStatement("select * from system_account where user_id = ?"); statement.setString(1, "5"); ResultSet resultSet = statement.executeQuery(); assertThat(resultSet.next(), is(true)); assertThat("ユーザはロック中のまま", resultSet.getString("USER_ID_LOCKED"), is("FALSE")); assertThat("失敗回数は変わらない", resultSet.getInt("FAILED_COUNT"), is(0)); assertThat("最終ログイン日時が更新されること", resultSet.getTimestamp("LAST_LOGIN_DATE_TIME"), is(Timestamp.valueOf("2013-08-02 00:11:22.000"))); createPasswordAuthenticator("20130805").authenticate("active user2", "pass!!!"); }
@Test public void testUserIdIsNull() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130802"); try { authenticator.authenticate(null, "password"); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is(nullValue())); } }
@Test public void testUserIdIsEmptyString() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130802"); try { authenticator.authenticate("", "password"); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is("")); } }
@Test public void testUserIsNotFound() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130802"); try { authenticator.authenticate("no exists", "password"); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is("no exists")); } }
@Test public void testEffectiveDateIsNonArrival() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130801"); try { authenticator.authenticate("active user", "password"); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is("active user")); } }
@Test public void testEffectiveDateIsOver() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130806"); try { authenticator.authenticate("active user", "password"); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is("active user")); } }
@Test public void testPasswordIsNull() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130802"); try { authenticator.authenticate("active user", null); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is("active user")); } }
@Test public void testPasswordIsEmptyString() { SystemAccountAuthenticator authenticator = createPasswordAuthenticator("20130802"); try { authenticator.authenticate("active user", ""); fail("エラーが発生するので、ここは通過しない"); } catch (AuthenticationFailedException e) { assertThat(e.getUserId(), is("1")); } }