def testInvalidStateChangeError(self, mock_enable): host_key = test_utils.CreateBit9Host().key exm_key = test_utils.CreateExemption( host_key.id(), initial_state=_STATE.DENIED) with self.assertRaises(api.InvalidStateChangeError): api.Cancel(exm_key) mock_enable.assert_not_called() self.assertEqual(_STATE.DENIED, exm_key.get().state) self.mock_send.assert_not_called()
def testSuccess_Santa(self): host_key = test_utils.CreateSantaHost(primary_user='******').key exm_key = test_utils.CreateExemption( host_key.id(), initial_state=_STATE.APPROVED) api.Cancel(exm_key) exm = exm_key.get() self.assertEqual(_STATE.CANCELLED, exm.state) self.assertBigQueryInsertions([ constants.BIGQUERY_TABLE.HOST, constants.BIGQUERY_TABLE.EXEMPTION]) self.DrainTaskQueue(constants.TASK_QUEUE.EXEMPTIONS) self.mock_send.assert_called_once()
def post(self, host_id): if not self.exm: self.abort(httplib.NOT_FOUND, explanation='Exemption not found') # Verify that the current user is associated with the Host. # NOTE: Admins don't get a pass here, they can (and should) use the # above 'revoke' handler instead. if not model_utils.IsHostAssociatedWithUser(self.host, self.user): logging.error('Host %s not associated with user %s', host_id, self.user.nickname) self.abort(httplib.FORBIDDEN, explanation='Host not associated with requesting user') try: exemption_api.Cancel(self.exm.key) except Exception: # pylint: disable=broad-except logging.exception( 'Error encountered while cancelling Exemption for host %s', host_id) self.abort(httplib.INTERNAL_SERVER_ERROR, explanation='Failed to cancel exemption')