sivin: accept and translate plate numbers in FNI format (#67925)
This commit is contained in:
parent
99fdc5ed22
commit
4e5c746582
|
@ -14,6 +14,7 @@
|
|||
# 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/>.xs
|
||||
|
||||
import re
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from django.core.cache import cache
|
||||
|
@ -35,6 +36,8 @@ PLATFORMS = {
|
|||
|
||||
ENVS = (('test', _('Test')), ('prod', _('Production')))
|
||||
|
||||
FNI_PLATE_PATTERN = re.compile(r'(\d{2,4})([A-Z]{2,3})(\d{2,3})')
|
||||
|
||||
|
||||
class Resource(BaseResource):
|
||||
consumer_key = models.CharField(_('Consumer key'), max_length=128)
|
||||
|
@ -86,6 +89,9 @@ class Resource(BaseResource):
|
|||
def get_infos_by_immat(self, endpoint, immat, codesra=None):
|
||||
# remove dashes / spaces in immat to avoid lookup issues
|
||||
immat = immat.strip().replace('-', '').replace(' ', '').upper()
|
||||
fin_match = FNI_PLATE_PATTERN.match(immat)
|
||||
if fin_match:
|
||||
immat = f'{fin_match.group(3)}{fin_match.group(2)}{fin_match.group(1).zfill(4)}'
|
||||
payload = {'immat': immat}
|
||||
if codesra is not None:
|
||||
payload['codesra'] = codesra
|
||||
|
@ -111,7 +117,7 @@ class Resource(BaseResource):
|
|||
@endpoint(
|
||||
perm='can_access',
|
||||
description=_('Get vehicle details by registration plate'),
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '01XT0747'}},
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '747XT01'}},
|
||||
)
|
||||
def consultervehiculeparimmat(self, request, immat, codesra=None):
|
||||
return self.get_infos_by_immat('consultervehiculeparimmat', immat, codesra)
|
||||
|
@ -119,7 +125,7 @@ class Resource(BaseResource):
|
|||
@endpoint(
|
||||
perm='can_access',
|
||||
description=_('Get vehicle "finition" by registration plate'),
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '01XT0747'}},
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '747XT01'}},
|
||||
)
|
||||
def consulterfinitionparimmat(self, request, immat, codesra=None):
|
||||
result = self.get_infos_by_immat('consulterfinitionparimmat', immat, codesra)
|
||||
|
@ -128,7 +134,7 @@ class Resource(BaseResource):
|
|||
@endpoint(
|
||||
perm='can_access',
|
||||
description=_('Get vehicle "finition" by registration plate, ordered by rangs'),
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '01XT0747'}},
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '747XT01'}},
|
||||
)
|
||||
def consulterfinitionscoresparimmat(self, request, immat, codesra=None):
|
||||
return self.get_infos_by_immat('consulterfinitionscoresparimmat', immat, codesra)
|
||||
|
@ -136,7 +142,7 @@ class Resource(BaseResource):
|
|||
@endpoint(
|
||||
perm='can_access',
|
||||
description=_('Get vehicle theorical "finition" by registration plate'),
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '01XT0747'}},
|
||||
parameters={'immat': {'description': _('Registration plate number'), 'example_value': '747XT01'}},
|
||||
)
|
||||
def consulterfinitiontheoriqueparimmat(self, request, immat, codesra=None):
|
||||
return self.get_infos_by_immat('consulterfinitiontheoriqueparimmat', immat, codesra)
|
||||
|
|
|
@ -178,15 +178,16 @@ def test_get_with_expired_token(mocked_post, app, conn):
|
|||
assert resp['err_desc'] == EXPIRED_TOKEN_MESSAGE
|
||||
|
||||
|
||||
@pytest.mark.parametrize('immat,sent_immat', [('747-xT 01', '01XT0747'), ('FD-734-hR', 'FD734HR')])
|
||||
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, FINITION))
|
||||
def test_get_vehicle_theorical_finition(mocked_post, app, conn):
|
||||
def test_get_vehicle_theorical_finition(mocked_post, app, conn, immat, sent_immat):
|
||||
url = reverse(
|
||||
'generic-endpoint',
|
||||
kwargs={'connector': 'sivin', 'endpoint': 'consulterfinitiontheoriqueparimmat', 'slug': conn.slug},
|
||||
)
|
||||
resp = app.get(url, params={'apikey': 'sivinkey', 'immat': '01-xT0 747'}).json
|
||||
resp = app.get(url, params={'apikey': 'sivinkey', 'immat': immat}).json
|
||||
assert mocked_post.call_count == 2
|
||||
assert mocked_post.mock_calls[-1].kwargs['json'] == {'immat': '01XT0747'}
|
||||
assert mocked_post.mock_calls[-1].kwargs['json'] == {'immat': sent_immat}
|
||||
assert not resp['err']
|
||||
assert resp['data'] == VEHICLE_THEORICAL_FINITION
|
||||
|
||||
|
|
Loading…
Reference in New Issue