### Question:
KeyService { public void deleteKeyAndIgnoreWarnings(String keyId) throws KeyNotFoundException, ActionNotPossibleException, GlobalConfOutdatedException { try { deleteKey(keyId, true); } catch (UnhandledWarningsException e) { throw new RuntimeException(e); } } @Autowired  KeyService(TokenService tokenService, SignerProxyFacade signerProxyFacade,
            PossibleActionsRuleEngine possibleActionsRuleEngine,
            ManagementRequestSenderService managementRequestSenderService,
            SecurityHelper securityHelper,
            AuditDataHelper auditDataHelper,
            AuditEventHelper auditEventHelper,
            AuditEventLoggingFacade auditEventLoggingFacade); KeyInfo getKey(String keyId); KeyInfo getKey(TokenInfo tokenInfo, String keyId); KeyInfo updateKeyFriendlyName(String id, String friendlyName); KeyInfo addKey(String tokenId, String keyLabel); void deleteKeyAndIgnoreWarnings(String keyId); void deleteKey(String keyId, Boolean ignoreWarnings); EnumSet<PossibleActionEnum> getPossibleActionsForKey(String keyId); static final String WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED; }### Answer:
@Test(expected = AccessDeniedException.class) @WithMockUser(authorities = { "DELETE_AUTH_KEY", "DELETE_SIGN_KEY", "DELETE_KEY" }) public void deleteKeyUnregisterRequiresSpecificPermission() throws Exception { keyService.deleteKeyAndIgnoreWarnings(AUTH_KEY_ID); }
@Test @WithMockUser(authorities = { "DELETE_AUTH_KEY", "SEND_AUTH_CERT_DEL_REQ" }) public void deleteAuthKeyPermissionCheck() throws Exception { try { keyService.deleteKeyAndIgnoreWarnings(SIGN_KEY_ID); fail("should not be allowed"); } catch (AccessDeniedException expected) { } try { keyService.deleteKeyAndIgnoreWarnings(TYPELESS_KEY_ID); fail("should not be allowed"); } catch (AccessDeniedException expected) { } keyService.deleteKeyAndIgnoreWarnings(AUTH_KEY_ID); }
@Test @WithMockUser(authorities = { "DELETE_SIGN_KEY" }) public void deleteSignKeyPermissionCheck() throws Exception { try { keyService.deleteKeyAndIgnoreWarnings(AUTH_KEY_ID); fail("should not be allowed"); } catch (AccessDeniedException expected) { } try { keyService.deleteKeyAndIgnoreWarnings(TYPELESS_KEY_ID); fail("should not be allowed"); } catch (AccessDeniedException expected) { } keyService.deleteKeyAndIgnoreWarnings(SIGN_KEY_ID); }
@Test @WithMockUser(authorities = { "DELETE_KEY" }) public void deleteTypelessKeyPermissionCheck() throws Exception { try { keyService.deleteKeyAndIgnoreWarnings(AUTH_KEY_ID); fail("should not be allowed"); } catch (AccessDeniedException expected) { } try { keyService.deleteKeyAndIgnoreWarnings(SIGN_KEY_ID); fail("should not be allowed"); } catch (AccessDeniedException expected) { } keyService.deleteKeyAndIgnoreWarnings(TYPELESS_KEY_ID); }
@Test @WithMockUser(authorities = { "DELETE_AUTH_KEY", "DELETE_SIGN_KEY", "DELETE_KEY" }) public void deleteChecksPossibleActions() throws Exception { mockPossibleActionsRuleEngineDenyAll(); try { keyService.deleteKeyAndIgnoreWarnings(AUTH_KEY_ID); fail("should not be possible"); } catch (ActionNotPossibleException expected) { } }