def test_user_account_deactivated_with_log_on_permanent_delivery_failure( self, user_role_supplier): notify_data = self.notify_data(to='*****@*****.**') user = User.query.filter( User.email_address == '*****@*****.**').first() assert user.active is True with logcapture.LogCapture(names=('app', ), level=logging.INFO) as logs: response = self.client.post('/callbacks/notify', data=json.dumps(notify_data), content_type='application/json') assert response.status_code == 200 user = User.query.filter( User.email_address == '*****@*****.**').first() assert user.active is False audit_events = AuditEvent.query.filter( AuditEvent.type == 'update_user').all() assert len(audit_events) == 1 assert audit_events[0].data['user'] == {'active': False} assert audit_events[0].data['notify_callback_data'] == notify_data assert logs.records[0].msg == "User account disabled for -htpNCrT2nn2dCuqWsXwBZDgkP-WQQCYAfPgSw7Rb4A= after " \ "Notify reported permanent delivery failure."
def assert_log_entry(modules, message, count=1): """A context manager that can be used to wrap a block of code under test to assert that a specific message is logged. Works well with the AnyStringMatching malleable above to assert a message meeting a given format. This is designed to most easily assert a single kind of message being logged one or more times, but if you assign the context manager to a variable you can access the `records` attribute to see all log records and make further inspections. Example: with assert_log_entry(message=AnyStringMatching('^My unknown string with a random word \w+$', count=2)) as logs: # noqa do_something_that_produces_a_log() assert any('some other log message' in record.msg for record in logs.records) """ log_catcher = logcapture.LogCapture(names=modules, install=False) log_catcher.install() try: yield log_catcher finally: log_catcher.uninstall_all() matching_records = [ True for record in log_catcher.records if message == record.msg ] assert len(matching_records) == count, f'{len(matching_records)} log records were seen that matched ' \ f'`{message}`: expected {count}'
def test_error_logged_on_technical_delivery_failure(self): notify_data = self.notify_data(status='technical-failure') with logcapture.LogCapture(names=('flask.app',), level=logging.WARNING) as logs: response = self.client.post('/callbacks/notify', data=json.dumps(notify_data), content_type='application/json') assert response.status_code == 200 assert len(logs.records) == 1 assert logs.records[0].msg == "Notify failed to deliver my-notification-reference to " \ "urpXHRZxYlyjcR9cAeiJkNpfSjZYuw-IOGMbo5x2HTM="
def test_callbacks_are_logged(self, status): notify_data = self.notify_data(status=status) with logcapture.LogCapture(names=('flask.app',), level=logging.INFO) as logs: self.client.post( '/callbacks/notify', data=json.dumps(notify_data), content_type='application/json' ) assert logs.records[0].msg \ == f"Notify callback: {status}: " \ "my-notification-reference to urpXHRZxYlyjcR9cAeiJkNpfSjZYuw-IOGMbo5x2HTM=" del notify_data["to"] assert logs.records[0].notify_delivery_receipt == notify_data