| 1 |
from contextlib import contextmanager |
| 2 |
from requests import PreparedRequest, Response, Session |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
from odoo.exceptions import ValidationError |
| 6 |
from odoo.tests.common import tagged, TransactionCase, freeze_time |
| 7 |
|
| 8 |
ID_CLIENT = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' |
| 9 |
FAKE_UUID = 'yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy' |
| 10 |
|
| 11 |
|
| 12 |
@freeze_time('2023-01-01') |
| 13 |
@tagged('-at_install', 'post_install', 'post_install_l10n') |
| 14 |
class TestNemhandelParticipant(TransactionCase): |
| 15 |
|
| 16 |
@classmethod |
| 17 |
def setUpClass(cls): |
| 18 |
super().setUpClass() |
| 19 |
cls.env['ir.config_parameter'].sudo().set_param('l10n_dk_nemhandel.edi.mode', 'test') |
| 20 |
cls.env.company.vat = 'DK12345674' |
| 21 |
|
| 22 |
@classmethod |
| 23 |
def _get_mock_responses(cls): |
| 24 |
participant_state = cls.env.context.get('participant_state', 'receiver') |
| 25 |
return { |
| 26 |
'/api/nemhandel/1/participant_status': { |
| 27 |
'result': { |
| 28 |
'nemhandel_state': participant_state, |
| 29 |
} |
| 30 |
}, |
| 31 |
'/api/nemhandel/1/register_participant': {'result': {}}, |
| 32 |
'/api/nemhandel/1/connect': { |
| 33 |
'result': { |
| 34 |
'id_client': ID_CLIENT, |
| 35 |
'refresh_token': FAKE_UUID, |
| 36 |
} |
| 37 |
}, |
| 38 |
'/api/nemhandel/1/cancel_nemhandel_registration': {'result': {}}, |
| 39 |
'/api/nemhandel/1/update_user': {'result': {}}, |
| 40 |
'/api/nemhandel/1/verify_phone_number': {'result': {}}, |
| 41 |
'/api/nemhandel/1/send_verification_code': {'result': {}}, |
| 42 |
'/api/nemhandel/1/check_user_valid': {'result': {'status': 'valid'}} |
| 43 |
} |
| 44 |
|
| 45 |
@classmethod |
| 46 |
def _request_handler(cls, s: Session, r: PreparedRequest, /, **kw): |
| 47 |
response = Response() |
| 48 |
response.status_code = 200 |
| 49 |
if r.url.endswith('/iso6523-actorid-upis%3A%3A0088%3A0000000000'): |
| 50 |
response.status_code = 404 |
| 51 |
return response |
| 52 |
|
| 53 |
if r.url.endswith('/iso6523-actorid-upis%3A%3A0184%3A0000000000'): |
| 54 |
response._content = b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<smp:ServiceGroup xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:id="http://busdox.org/transport/identifiers/1.0/" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:smp="http://busdox.org/serviceMetadata/publishing/1.0/"><id:ParticipantIdentifier scheme="iso6523-actorid-upis">0184:0000000000</id:ParticipantIdentifier></smp:ServiceGroup>' |
| 55 |
return response |
| 56 |
|
| 57 |
url = r.path_url |
| 58 |
responses = cls._get_mock_responses() |
| 59 |
|
| 60 |
if url not in responses: |
| 61 |
return super()._request_handler(s, r, **kw) |
| 62 |
response.json = lambda: responses[url] |
| 63 |
return response |
| 64 |
|
| 65 |
def _get_participant_vals(self): |
| 66 |
return { |
| 67 |
'identifier_type': '0088', |
| 68 |
'identifier_value': '0000000000', |
| 69 |
'phone_number': '+32483123456', |
| 70 |
'contact_email': '[email protected]', |
| 71 |
} |
| 72 |
|
| 73 |
@contextmanager |
| 74 |
def _set_context(self, other_context): |
| 75 |
cls = self.__class__ |
| 76 |
env = cls.env(context=dict(cls.env.context, **other_context)) |
| 77 |
with patch.object(cls, "env", env): |
| 78 |
yield |
| 79 |
|
| 80 |
def test_nemhandel_create_participant_missing_data(self): |
| 81 |
|
| 82 |
wizard = self.env['nemhandel.registration'].create({ |
| 83 |
'identifier_type': False, |
| 84 |
'identifier_value': False, |
| 85 |
}) |
| 86 |
with self.assertRaises(ValidationError), self.cr.savepoint(): |
| 87 |
wizard.button_nemhandel_registration_sms() |
| 88 |
|
| 89 |
def test_nemhandel_create_participant_receiver(self): |
| 90 |
company = self.env.company |
| 91 |
wizard = self.env['nemhandel.registration'].create(self._get_participant_vals()) |
| 92 |
wizard.button_nemhandel_registration_sms() |
| 93 |
self.assertEqual(company.l10n_dk_nemhandel_proxy_state, 'in_verification') |
| 94 |
wizard.verification_code = '888888' |
| 95 |
wizard.button_nemhandel_receiver_registration() |
| 96 |
self.assertEqual(company.l10n_dk_nemhandel_proxy_state, 'receiver') |
| 97 |
|
| 98 |
def test_nemhandel_create_reject_participant(self): |
| 99 |
|
| 100 |
|
| 101 |
company = self.env.company |
| 102 |
wizard = self.env['nemhandel.registration'].create(self._get_participant_vals()) |
| 103 |
|
| 104 |
with self._set_context({'participant_state': 'rejected'}): |
| 105 |
wizard.button_nemhandel_registration_sms() |
| 106 |
self.env['account_edi_proxy_client.user']._cron_nemhandel_get_participant_status() |
| 107 |
self.assertEqual(company.l10n_dk_nemhandel_proxy_state, 'rejected') |
| 108 |
|
| 109 |
def test_nemhandel_create_duplicate_participant(self): |
| 110 |
""" If you create a duplicate participant, it will take over the previous one""" |
| 111 |
wizard = self.env['nemhandel.registration'].create(self._get_participant_vals()) |
| 112 |
wizard.button_nemhandel_registration_sms() |
| 113 |
wizard.verification_code = '888888' |
| 114 |
wizard.button_nemhandel_receiver_registration() |
| 115 |
wizard.l10n_dk_nemhandel_proxy_state = 'not_registered' |
| 116 |
wizard.button_nemhandel_registration_sms() |
| 117 |
self.assertEqual(self.env.company.l10n_dk_nemhandel_proxy_state, 'in_verification') |
| 118 |
|
| 119 |
|
| 120 |
with self._set_context({'participant_state': 'receiver'}): |
| 121 |
self.env['account_edi_proxy_client.user']._cron_nemhandel_get_participant_status() |
| 122 |
self.assertEqual(self.env.company.l10n_dk_nemhandel_proxy_state, 'receiver') |