Unify and refactor exception handling in framework and addons. The generic `except_osv` is now deprecated, and replaced by more specialized exception subtypes: - `UserError` (renamed from Warning, as it conflicts with the built-in `Warning`) raised when a non-technical error occurs during a business operation. It could be a missing information in the data provided by the user, or a misconfiguration. - `AccessError`: raised when any operation is denied because the user conducting it does not have the required access rights. - `AccessDenied`: raised when an operation that requires authenticated access is attempted via an unauthenticated request. - `MissingError`: raised when an operation is attempted on a record that does not exist. - `ValidationError`: raised when an operation violates a SQL or Python constraint. - All other exceptions are internal errors due to a system problem or bug, and raised untouched to the client-side, which should display a traceback. All exceptions take a single message argument. The `test_exceptions` module has been updated to showcase both new and old (deprecated) exceptions. A great many old `except_osv` had a useless title with "Error!" or "Warning", those have been removed, as this is handled by the client-side widget that displays the messages. This commit introduces a more consistent policy for logging errors and warnings: - All messages that do not require administrator attention should be logged at INFO level or lower. This includes all errors that are notified to the user in a friendly manner, even for access right problems or validation errors during business operations. - All messages that indicate a likely misconfiguration or malicious use by the users should be logged at WARNING level, as they typically require administrator attention. - All other unhandled internal errors cannot typically be handled by the user and should be logged at ERROR or higher level, as they require immediate administrator attention.
89 lines
4.0 KiB
Python
89 lines
4.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import datetime
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
from openerp.addons.event.tests.common import TestEventCommon
|
|
from openerp.exceptions import AccessError, ValidationError, UserError
|
|
from openerp.tools import mute_logger
|
|
|
|
|
|
class TestEventFlow(TestEventCommon):
|
|
|
|
@mute_logger('openerp.addons.base.ir.ir_model', 'openerp.models')
|
|
def test_00_basic_event_auto_confirm(self):
|
|
""" Basic event management with auto confirmation """
|
|
self.env['ir.values'].set_default('marketing.config.settings', 'auto_confirmation', True)
|
|
|
|
# EventUser creates a new event: ok
|
|
test_event = self.Event.sudo(self.user_eventmanager).create({
|
|
'name': 'TestEvent',
|
|
'date_begin': datetime.datetime.now() + relativedelta(days=-1),
|
|
'date_end': datetime.datetime.now() + relativedelta(days=1),
|
|
'seats_max': 2,
|
|
})
|
|
self.assertEqual(test_event.state, 'confirm', 'Event: auto_confirmation of event failed')
|
|
|
|
# EventUser create registrations for this event
|
|
test_reg1 = self.Registration.sudo(self.user_eventuser).create({
|
|
'name': 'TestReg1',
|
|
'event_id': test_event.id,
|
|
})
|
|
self.assertEqual(test_reg1.state, 'open', 'Event: auto_confirmation of registration failed')
|
|
self.assertEqual(test_event.seats_reserved, 1, 'Event: wrong number of reserved seats after confirmed registration')
|
|
test_reg2 = self.Registration.sudo(self.user_eventuser).create({
|
|
'name': 'TestReg2',
|
|
'event_id': test_event.id,
|
|
})
|
|
self.assertEqual(test_reg2.state, 'open', 'Event: auto_confirmation of registration failed')
|
|
self.assertEqual(test_event.seats_reserved, 2, 'Event: wrong number of reserved seats after confirmed registration')
|
|
|
|
# EventUser create registrations for this event: too much registrations
|
|
with self.assertRaises(ValidationError):
|
|
self.Registration.sudo(self.user_eventuser).create({
|
|
'name': 'TestReg3',
|
|
'event_id': test_event.id,
|
|
})
|
|
|
|
# EventUser validates registrations
|
|
test_reg1.button_reg_close()
|
|
self.assertEqual(test_reg1.state, 'done', 'Event: wrong state of attended registration')
|
|
self.assertEqual(test_event.seats_used, 1, 'Event: incorrect number of attendees after closing registration')
|
|
test_reg2.button_reg_close()
|
|
self.assertEqual(test_reg1.state, 'done', 'Event: wrong state of attended registration')
|
|
self.assertEqual(test_event.seats_used, 2, 'Event: incorrect number of attendees after closing registration')
|
|
|
|
# EventUser closes the event
|
|
test_event.button_done()
|
|
|
|
# EventUser cancels -> not possible when having attendees
|
|
with self.assertRaises(UserError):
|
|
test_event.button_cancel()
|
|
|
|
|
|
@mute_logger('openerp.addons.base.ir.ir_model', 'openerp.models')
|
|
def test_10_advanced_event_flow(self):
|
|
""" Avanced event flow: no auto confirmation, manage minimum / maximum
|
|
seats, ... """
|
|
self.env['ir.values'].set_default('marketing.config.settings', 'auto_confirmation', False)
|
|
|
|
# EventUser creates a new event: ok
|
|
test_event = self.Event.sudo(self.user_eventmanager).create({
|
|
'name': 'TestEvent',
|
|
'date_begin': datetime.datetime.now() + relativedelta(days=-1),
|
|
'date_end': datetime.datetime.now() + relativedelta(days=1),
|
|
'seats_max': 10,
|
|
})
|
|
self.assertEqual(
|
|
test_event.state, 'draft',
|
|
'Event: new event should be in draft state, no auto confirmation')
|
|
|
|
# EventUser create registrations for this event -> no auto confirmation
|
|
test_reg1 = self.Registration.sudo(self.user_eventuser).create({
|
|
'name': 'TestReg1',
|
|
'event_id': test_event.id,
|
|
})
|
|
self.assertEqual(
|
|
test_reg1.state, 'draft',
|
|
'Event: new registration should not be confirmed with auto_confirmation parameter being False')
|