# -*- coding: utf-8 -*- # Copyright 2017 LasLabs Inc. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). import mock from odoo import _ from odoo.exceptions import AccessError from odoo.tests.common import HttpCase, TransactionCase class TestExport(TransactionCase): def setUp(self): super(TestExport, self).setUp() export_group = self.env.ref('base_export_security.export_group') self.authorized_user = self.env['res.users'].create({ 'login': 'exporttestuser', 'partner_id': self.env['res.partner'].create({ 'name': "Export Test User" }).id, 'groups_id': [(4, export_group.id, 0)], }) self.unauthorized_user = self.env['res.users'].create({ 'login': 'unauthorizedexporttestuser', 'partner_id': self.env['res.partner'].create({ 'name': "Unauthorized Export Test User" }).id, }) self.model_name = 'ir.module.module' self.recordset = self.env[self.model_name].search([]) self.field_names = ['name', 'id'] self.model = self.env['ir.model'].search([ ('model', '=', self.model_name), ]) self.records = self.env['ir.model.data'].search([ ('model', '=', self.model_name), ('res_id', 'in', self.recordset.ids), ]) self.fields = self.env['ir.model.fields'].search([ ('model', '=', self.model_name), ('name', 'in', self.field_names), ]) def test_log_export(self): """It should create a new Export record with correct data""" log = self.env['export.event'].sudo(self.authorized_user).log_export( self.recordset, self.field_names, ) self.assertEqual( [log.model_id, log.field_ids, log.record_ids, log.user_id], [self.model, self.fields, self.records, self.authorized_user], 'Log not created properly', ) def test_log_export_posts_notification(self): """It should call post_notification method""" post_notification_mock = mock.MagicMock() self.env['export.event']._patch_method( 'post_notification', post_notification_mock, ) self.env['export.event'].sudo(self.authorized_user).log_export( self.recordset, self.field_names, ) post_notification_mock.assert_called_once_with() self.env['export.event']._revert_method('post_notification') def test_post_notification(self): """It should post a notification with appropriate data to the #data export channel""" export = self.env['export.event'].create({ 'model_id': self.model.id, 'field_ids': [(4, self.fields.ids)], 'record_ids': [(4, self.records.ids)], 'user_id': self.authorized_user.id, }) message = export.sudo().post_notification() field_labels = ', '.join( self.fields.sorted().mapped('field_description'), ) message_data = { 'records': len(self.records.ids), 'model': self.model.name, 'user': self.authorized_user.name, 'fields': field_labels, } message_body = _( '
%(records)d %(model)s records exported by %(user)s'
'.
Fields exported: %(fields)s