### Question:
Security implements SecurityContext { public LoginResult login(final String email, final String password) { final SecurityUser candidate = dao.findUserByEmail(userClass, email); if (candidate == null) { return LoginResult.NOT_FOUND; } if (candidate.getPasswordHash() == null) { return LoginResult.INVALID; } if (!BCrypt.checkpw(password, candidate.getPasswordHash())) { return LoginResult.INCORRECT; } return new LoginResult(loginAs(candidate)); } @Inject @SuppressWarnings("unchecked")  Security(
            final SecurityDao dao,
            @Context final Configuration configuration,
            @HeaderParam(AUTHORIZATION) final String authorization,
            @CookieParam(COOKIE_NAME) final String cookie); Class<SecurityUser> getUserClass(); @Override @SuppressWarnings("unchecked") T getUserPrincipal(); boolean isLoggedIn(); void requireLogin(); String getSessionToken(); LoginResult login(final String email, final String password); NewCookie loginAs(final SecurityUser candidate); NewCookie logout(); ChangePasswordResult changePassword(final String oldPassword, final String newPassword, final String confirmNewPassword); String forgotPassword(final SecurityUser user); ResetPasswordResult resetPassword(final String resetId, final String newPassword, final String confirmNewPassword); @Override boolean isUserInRole(final String role); @Override boolean isSecure(); @Override String getAuthenticationScheme(); static final int MINIMUM_PASSWORD_LENGTH; static final String COOKIE_NAME; }### Answer:
@Test public void testLogin() { final User user = new User(); user.setPassword("testtest"); final SecurityDao dao = mock(SecurityDao.class); when(dao.findUserByEmail(eq(User.class), eq("user@example.com"))).thenReturn(user); final Configuration config = mock(Configuration.class); when(config.getProperty(eq(MinijaxProperties.SECURITY_USER_CLASS))).thenReturn(User.class); final Security<User> security = new Security<>(dao, config, null, null); final LoginResult result = security.login("user@example.com", "testtest"); final NewCookie cookie = result.getCookie(); assertNotNull(cookie); }
@Test public void testLoginUserNotFound() { final SecurityDao dao = mock(SecurityDao.class); when(dao.findUserByEmail(any(), any())).thenReturn(null); final Configuration config = mock(Configuration.class); when(config.getProperty(eq(MinijaxProperties.SECURITY_USER_CLASS))).thenReturn(User.class); final Security<User> security = new Security<>(dao, config, null, null); final LoginResult result = security.login("user@example.com", "testtest"); assertEquals(LoginResult.Status.NOT_FOUND, result.getStatus()); }
@Test public void testLoginPasswordNotSet() { final User user = new User(); final SecurityDao dao = mock(SecurityDao.class); when(dao.findUserByEmail(eq(User.class), eq("user@example.com"))).thenReturn(user); final Configuration config = mock(Configuration.class); when(config.getProperty(eq(MinijaxProperties.SECURITY_USER_CLASS))).thenReturn(User.class); final Security<User> security = new Security<>(dao, config, null, null); final LoginResult result = security.login("user@example.com", "testtest"); assertEquals(LoginResult.Status.INVALID, result.getStatus()); }
@Test public void testLoginIncorrectPassword() { final User user = new User(); user.setPassword("testtest"); final SecurityDao dao = mock(SecurityDao.class); when(dao.findUserByEmail(eq(User.class), eq("user@example.com"))).thenReturn(user); final Configuration config = mock(Configuration.class); when(config.getProperty(eq(MinijaxProperties.SECURITY_USER_CLASS))).thenReturn(User.class); final Security<User> security = new Security<>(dao, config, null, null); final LoginResult result = security.login("user@example.com", "wrong-password"); assertEquals(LoginResult.Status.INCORRECT, result.getStatus()); }