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.
72 lines
3.8 KiB
Python
72 lines
3.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
##############################################################################
|
|
#
|
|
# OpenERP, Open Source Business Applications
|
|
# Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
##############################################################################
|
|
|
|
from openerp.addons.mail.tests.common import TestMail
|
|
from openerp.exceptions import AccessError
|
|
from openerp.tools import mute_logger
|
|
|
|
|
|
class TestMailGroup(TestMail):
|
|
|
|
@mute_logger('openerp.addons.base.ir.ir_model', 'openerp.models')
|
|
def test_00_mail_group_access_rights(self):
|
|
""" Testing mail_group access rights and basic mail_thread features """
|
|
cr, uid, user_noone_id, user_employee_id = self.cr, self.uid, self.user_noone_id, self.user_employee_id
|
|
|
|
# Do: Bert reads Jobs -> ok, public
|
|
self.mail_group.read(cr, user_noone_id, [self.group_jobs_id])
|
|
# Do: Bert read Pigs -> ko, restricted to employees
|
|
with self.assertRaises(AccessError):
|
|
self.mail_group.read(cr, user_noone_id, [self.group_pigs_id])
|
|
# Do: Raoul read Pigs -> ok, belong to employees
|
|
self.mail_group.read(cr, user_employee_id, [self.group_pigs_id])
|
|
|
|
# Do: Bert creates a group -> ko, no access rights
|
|
with self.assertRaises(AccessError):
|
|
self.mail_group.create(cr, user_noone_id, {'name': 'Test'})
|
|
# Do: Raoul creates a restricted group -> ok
|
|
new_group_id = self.mail_group.create(cr, user_employee_id, {'name': 'Test'})
|
|
# Do: Bert added in followers, read -> ok, in followers
|
|
self.mail_group.message_subscribe_users(cr, uid, [new_group_id], [user_noone_id])
|
|
self.mail_group.read(cr, user_noone_id, [new_group_id])
|
|
|
|
# Do: Raoul reads Priv -> ko, private
|
|
with self.assertRaises(AccessError):
|
|
self.mail_group.read(cr, user_employee_id, [self.group_priv_id])
|
|
# Do: Raoul added in follower, read -> ok, in followers
|
|
self.mail_group.message_subscribe_users(cr, uid, [self.group_priv_id], [user_employee_id])
|
|
self.mail_group.read(cr, user_employee_id, [self.group_priv_id])
|
|
|
|
# Do: Raoul write on Jobs -> ok
|
|
self.mail_group.write(cr, user_employee_id, [self.group_priv_id], {'name': 'modified'})
|
|
# Do: Bert cannot write on Private -> ko (read but no write)
|
|
with self.assertRaises(AccessError):
|
|
self.mail_group.write(cr, user_noone_id, [self.group_priv_id], {'name': 're-modified'})
|
|
# Test: Bert cannot unlink the group
|
|
with self.assertRaises(AccessError):
|
|
self.mail_group.unlink(cr, user_noone_id, [self.group_priv_id])
|
|
# Do: Raoul unlinks the group, there are no followers and messages left
|
|
self.mail_group.unlink(cr, user_employee_id, [self.group_priv_id])
|
|
fol_ids = self.mail_followers.search(cr, uid, [('res_model', '=', 'mail.group'), ('res_id', '=', self.group_priv_id)])
|
|
self.assertFalse(fol_ids, 'unlinked document should not have any followers left')
|
|
msg_ids = self.mail_message.search(cr, uid, [('model', '=', 'mail.group'), ('res_id', '=', self.group_priv_id)])
|
|
self.assertFalse(msg_ids, 'unlinked document should not have any followers left')
|