### Question:
WebAuthn4jAuthenticator implements Authenticator { public void authenticate(AuthenticationFlowContext context) { LoginFormsProvider form = context.form(); Map<String, String> params = generateParameters(context.getRealm(), context.getUriInfo().getBaseUri()); context.getAuthenticationSession().setAuthNote(WebAuthnConstants.AUTH_CHALLENGE_NOTE, params.get(WebAuthnConstants.CHALLENGE)); UserModel user = context.getUser(); boolean isUserIdentified = false; if (user != null) { isUserIdentified = true; form.setAttribute("authenticators", new WebAuthnAuthenticatorsBean(user)); } else { } params.put("isUserIdentified", Boolean.toString(isUserIdentified)); params.forEach(form::setAttribute); context.challenge(form.createForm("webauthn.ftl")); } WebAuthn4jAuthenticator(KeycloakSession session); void authenticate(AuthenticationFlowContext context); void action(AuthenticationFlowContext context); boolean requiresUser(); boolean configuredFor(KeycloakSession session, RealmModel realm, UserModel user); void setRequiredActions(KeycloakSession session, RealmModel realm, UserModel user); void close();  }### Answer:
@Test public void test_authenticate_2factor() throws Exception { List<String> publicKeyCredentialIds = new ArrayList<>(); publicKeyCredentialIds.add(getRandomString(32)); when(context.getUser().getAttribute(WebAuthnConstants.PUBKEY_CRED_ID_ATTR)).thenReturn(publicKeyCredentialIds); try { authenticator.authenticate(context); } catch (Exception e) { Assert.fail(e.getMessage()); } verify(context).challenge(any(Response.class)); }
@Test public void test_authenticate_2factor_publickey_not_registered() throws Exception { when(context.getUser().getAttribute(WebAuthnConstants.PUBKEY_CRED_ID_ATTR)).thenReturn(null); try { authenticator.authenticate(context); Assert.fail(); } catch (AuthenticationFlowException e) { } when(context.getUser().getAttribute(WebAuthnConstants.PUBKEY_CRED_ID_ATTR)).thenReturn(new ArrayList<String>()); try { authenticator.authenticate(context); Assert.fail(); } catch (AuthenticationFlowException e) { } }
@Test public void test_authenticate_2factor_multiple_publicKey_registered() throws Exception { List<String> publicKeyCredentialIds = new ArrayList<>(); publicKeyCredentialIds.add(getRandomString(32)); publicKeyCredentialIds.add(getRandomString(32)); when(context.getUser().getAttribute(WebAuthnConstants.PUBKEY_CRED_ID_ATTR)).thenReturn(publicKeyCredentialIds); try { authenticator.authenticate(context); Assert.fail(); } catch (AuthenticationFlowException e) { } }
@Test public void test_authenticate_passwordless() throws Exception { when(context.getUser()).thenReturn(null); try { authenticator.authenticate(context); } catch (Exception e) { Assert.fail(e.getMessage()); } verify(context).challenge(any(Response.class)); }