def test_custom_condition_success(self): def custom(req): return True addCommitCondition("custom-condition", custom=custom) checker = CommitChecker(self.request, ('custom-condition',)) self.assertTrue(checker.can_commit())
def test_custom_condition_fail(self): def custom(req): return False addCommitCondition("custom-condition", custom=custom) checker = CommitChecker(self.request, ('custom-condition', )) self.assertTrue(not checker.can_commit())
def test_custom_condition_fail(self): def custom(req): return False addCommitCondition("custom-condition", custom=custom) checker = CommitChecker(self.request, ('custom-condition',)) self.assertTrue(not checker.can_commit())
def test_custom_condition_success(self): def custom(req): return True addCommitCondition("custom-condition", custom=custom) checker = CommitChecker(self.request, ('custom-condition', )) self.assertTrue(checker.can_commit())
def test_multiple_conditions_all_fail(self): addCommitCondition("path", path="/foo/bar") addCommitCondition("method", request_method="POST", host="www.foobar.com") self.request.set('ACTUAL_URL', self.portal_url + '/foo') self.request.set('REQUEST_METHOD', 'GET') self.request.set('BASE1', 'http://www.foboar.com') checker = CommitChecker(self.request, ('path', 'method')) self.assertTrue(not checker.can_commit())
def test_all_criteria_not_met(self): addCommitCondition("condition", path="/foo/bar", request_method="POST", host="www.foobar.com") self.request.set('ACTUAL_URL', self.portal_url + '/foo') self.request.set('REQUEST_METHOD', 'GET') self.request.set('BASE1', 'http://www.foboar.com') checker = CommitChecker(self.request, ('condition',)) self.assertTrue(not checker.can_commit())
def test_all_criteria_not_met(self): addCommitCondition("condition", path="/foo/bar", request_method="POST", host="www.foobar.com") self.request.set('ACTUAL_URL', self.portal_url + '/foo') self.request.set('REQUEST_METHOD', 'GET') self.request.set('BASE1', 'http://www.foboar.com') checker = CommitChecker(self.request, ('condition', )) self.assertTrue(not checker.can_commit())
def doomIt(event): """Doom the transaction if needed. The transaction will be doomed if lockdown is enabled and none of the activated commit conditions are met. See: https://zodb.readthedocs.io/en/latest/transactions.html#dooming-a-transaction """ request = event.request published = request.PARENTS[0] mt = getattr( getattr(published, 'aq_base', None), 'meta_type', getattr(published, 'meta_type', None)) if (mt not in _blacklisted_meta_types) and ILayer.providedBy(request): if not _get_setting('enabled', False): # skip out of here first return status_message = _get_setting('status_message', None) or u'' status_message = status_message.strip() if status_message and (not api.user.is_anonymous()): api.portal.show_message( status_message, request=request, type='warn') # let's check if this is valid now. activated = _get_setting('activated', set()) try: checker = CommitChecker(request, activated) if checker.can_commit(): return except Exception: # if there is any error, ignore and doom. better to be safe... logger.warn( 'Error checking conditions, dooming the ' 'transaction: {}'.format(traceback.format_exc())) transaction.doom()
def doomIt(event): request = event.request published = request.PARENTS[0] mt = getattr( getattr(published, 'aq_base', None), 'meta_type', getattr(published, 'meta_type', None)) if mt not in _blacklisted_meta_types and ILayer.providedBy(request): # print 'checking', repr(published), # print getattr(published, 'meta_type', None) try: registry = getUtility(IRegistry) except KeyError: logger.warn("settings not installed") return # settings not installed try: settings = registry.forInterface(ISettings) except KeyError: logger.warn("settings not installed") return # settings not registered if not settings.enabled: # skip out of here first return # let's check if this is valid now. try: checker = CommitChecker(request, settings.activated) if checker.can_commit(): return except Exception: # if there is any error, ignore and doom # better to be safe.. logger.warn("Error checking conditions, " "dooming the tranaction: %s" % traceback.format_exc()) transaction.doom()
def doomIt(event): """Doom the transaction if needed. The transaction will be doomed if lockdown is enabled and none of the activated commit conditions are met. See: https://zodb.readthedocs.io/en/latest/transactions.html#dooming-a-transaction """ request = event.request published = request.PARENTS[0] mt = getattr(getattr(published, 'aq_base', None), 'meta_type', getattr(published, 'meta_type', None)) if (mt not in _blacklisted_meta_types) and ILayer.providedBy(request): if not _get_setting('enabled', False): # skip out of here first return status_message = _get_setting('status_message', None) or u'' status_message = status_message.strip() if status_message and (not api.user.is_anonymous()): api.portal.show_message(status_message, request=request, type='warn') # let's check if this is valid now. activated = _get_setting('activated', set()) try: checker = CommitChecker(request, activated) if checker.can_commit(): return except Exception: # if there is any error, ignore and doom. better to be safe... logger.warn('Error checking conditions, dooming the ' 'transaction: {}'.format(traceback.format_exc())) transaction.doom()
def test_portal_type_condition_fail(self): addCommitCondition("pt-condition", portal_type="foobar") self.request.set('PARENTS', [FakePT('foboar')]) checker = CommitChecker(self.request, ('pt-condition', )) self.assertTrue(not checker.can_commit())
def test_host_condition_plus_port_success(self): addCommitCondition("host-condition", host="www.foobar.com:8080") self.request.set('BASE1', 'https://www.foobar.com:8080') checker = CommitChecker(self.request, ('host-condition',)) self.assertTrue(checker.can_commit())
def test_host_condition_wildcard_fail(self): addCommitCondition("host-condition", host="*.bar.com") self.request.set('BASE1', 'http://fob.oar.com') checker = CommitChecker(self.request, ('host-condition',)) self.assertTrue(not checker.can_commit())
def test_request_condition_success_case_insensitive(self): addCommitCondition("request-method-condition", request_method="put") self.request.set('REQUEST_METHOD', 'PUT') checker = CommitChecker(self.request, ('request-method-condition',)) self.assertTrue(checker.can_commit())
def test_request_condition_fail(self): addCommitCondition("request-method-condition", request_method="POST") self.request.set('REQUEST_METHOD', 'GET') checker = CommitChecker(self.request, ('request-method-condition',)) self.assertTrue(not checker.can_commit())
def test_path_condition_success(self): addCommitCondition("path-condition", path="/foo/bar") self.request.set('ACTUAL_URL', self.portal_url + '/foo/bar') checker = CommitChecker(self.request, ('path-condition', )) self.assertTrue(checker.can_commit())
def test_path_condition_fail_with_wildcard(self): addCommitCondition("path-condition", path="/foo*") self.request.set('ACTUAL_URL', self.portal_url + '/fob/oar') checker = CommitChecker(self.request, ('path-condition',)) self.assertTrue(not checker.can_commit())
def test_path_condition_fail_with_wildcard(self): addCommitCondition("path-condition", path="/foo*") self.request.set('ACTUAL_URL', self.portal_url + '/fob/oar') checker = CommitChecker(self.request, ('path-condition', )) self.assertTrue(not checker.can_commit())
def test_path_condition_success(self): addCommitCondition("path-condition", path="/foo/bar") self.request.set('ACTUAL_URL', self.portal_url + '/foo/bar') checker = CommitChecker(self.request, ('path-condition',)) self.assertTrue(checker.can_commit())
def test_request_condition_success_case_insensitive(self): addCommitCondition("request-method-condition", request_method="put") self.request.set('REQUEST_METHOD', 'PUT') checker = CommitChecker(self.request, ('request-method-condition', )) self.assertTrue(checker.can_commit())
def test_request_condition_fail(self): addCommitCondition("request-method-condition", request_method="POST") self.request.set('REQUEST_METHOD', 'GET') checker = CommitChecker(self.request, ('request-method-condition', )) self.assertTrue(not checker.can_commit())
def test_portal_type_condition_fail(self): addCommitCondition("pt-condition", portal_type="foobar") self.request.set('PARENTS', [FakePT('foboar')]) checker = CommitChecker(self.request, ('pt-condition',)) self.assertTrue(not checker.can_commit())
def test_host_condition_plus_port_success(self): addCommitCondition("host-condition", host="www.foobar.com:8080") self.request.set('BASE1', 'https://www.foobar.com:8080') checker = CommitChecker(self.request, ('host-condition', )) self.assertTrue(checker.can_commit())
def test_host_condition_wildcard_fail(self): addCommitCondition("host-condition", host="*.bar.com") self.request.set('BASE1', 'http://fob.oar.com') checker = CommitChecker(self.request, ('host-condition', )) self.assertTrue(not checker.can_commit())