97 lines
4.7 KiB
Python
97 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import base64
|
|
from odoo import Command
|
|
from odoo.exceptions import UserError
|
|
from odoo.tests import tagged
|
|
from odoo.tests.common import TransactionCase
|
|
from odoo.tools import file_open
|
|
|
|
TEXT = base64.b64encode(bytes("workflow bridge project", 'utf-8'))
|
|
|
|
|
|
@tagged('post_install', '-at_install')
|
|
class TestCaseDocumentsBridgeExpense(TransactionCase):
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
super().setUpClass()
|
|
|
|
cls.folder_internal = cls.env.ref('documents.document_internal_folder')
|
|
cls.folder_internal.action_update_access_rights(access_internal='edit')
|
|
|
|
cls.documents_user = cls.env['res.users'].create({
|
|
'name': "aaadocuments test basic user",
|
|
'login': "aadtbu",
|
|
'email': "aadtbu@yourcompany.com",
|
|
'group_ids': [Command.set([cls.env.ref('documents.group_documents_user').id])],
|
|
})
|
|
|
|
def _create_txt_attachment_for_documents_user(self, company=False):
|
|
with file_open('base/tests/minimal.pdf', 'rb') as f:
|
|
pdf_file = f.read()
|
|
return self.env['documents.document'].with_user(self.documents_user).with_company(company or self.env.company).create({
|
|
'raw': pdf_file,
|
|
'name': 'file.pdf',
|
|
'mimetype': 'application/pdf',
|
|
'folder_id': self.folder_internal.id,
|
|
})
|
|
|
|
def test_create_document_to_expense(self):
|
|
"""
|
|
Makes sure the hr expense is created from the document.
|
|
|
|
Steps:
|
|
- Create user with employee
|
|
- Create attachment
|
|
- Performed action 'Create a Expense'
|
|
- Check if the expense is created
|
|
- Check the res_model of the document
|
|
|
|
"""
|
|
self.documents_user.action_create_employee() # Employee is mandatory in expense
|
|
document_txt = self._create_txt_attachment_for_documents_user()
|
|
|
|
self.assertFalse(document_txt.res_model, "The default res model of a document is False.")
|
|
self.assertEqual(document_txt.attachment_id.res_model, 'documents.document', "The default res model of an attachment is documents.document.")
|
|
document_txt.with_user(self.documents_user).document_hr_expense_create_hr_expense()
|
|
self.assertEqual(document_txt.res_model, 'hr.expense', "The document model is updated.")
|
|
self.assertEqual(document_txt.attachment_id.res_model, 'hr.expense', "The attachment model is updated.")
|
|
expense = self.env['hr.expense'].search([('id', '=', document_txt.res_id)])
|
|
self.assertTrue(expense.exists(), 'expense sholud be created.')
|
|
self.assertEqual(document_txt.res_id, expense.id, "Expense should be linked to document")
|
|
self.assertEqual(expense.employee_id, self.documents_user.employee_id)
|
|
|
|
def test_create_document_to_expense_without_employee(self):
|
|
"""
|
|
Make sure UserError is raised when creating expense from document
|
|
while the current user is not linked to an employee and has no rights to create an expense.
|
|
If the current user is not linked to an employee and has rights to create an expense,
|
|
it should create an expense with a random employee
|
|
Finally if there is no employee in the company, an error should raise
|
|
"""
|
|
# First, user has no employee and no rights
|
|
attachment_txt = self._create_txt_attachment_for_documents_user()
|
|
with self.assertRaisesRegex(UserError, "The current user has no related employee. Please, create one."):
|
|
attachment_txt.with_user(self.documents_user).document_hr_expense_create_hr_expense()
|
|
|
|
expense = self.env['hr.expense'].search([('id', '=', attachment_txt.res_id)])
|
|
self.assertFalse(expense.exists())
|
|
|
|
# Then, add rights to the user and try to create an expense -> should create an expense with random employee_id
|
|
self.documents_user.group_ids += self.env.ref('hr_expense.group_hr_expense_manager')
|
|
|
|
attachment_txt.with_user(self.documents_user).document_hr_expense_create_hr_expense()
|
|
expense = self.env['hr.expense'].search([('id', '=', attachment_txt.res_id)])
|
|
self.assertTrue(expense.employee_id)
|
|
self.assertEqual(expense.employee_id.company_id, self.env.company)
|
|
|
|
# Finally, create an expense in a company that has no employees -> should raise
|
|
company = self.env['res.company'].create({
|
|
'name': 'test company',
|
|
})
|
|
self.documents_user.company_ids += company
|
|
attachment_txt = self._create_txt_attachment_for_documents_user(company=company)
|
|
with self.assertRaisesRegex(UserError, "There are no employee in the company. Please create one."):
|
|
attachment_txt.with_user(self.documents_user).with_company(company).document_hr_expense_create_hr_expense()
|