lille urban card: add code check and card info endpoints (#47064)

This commit is contained in:
Frédéric Péters 2020-09-28 09:58:51 +02:00
parent b8f36d5485
commit a83cd847d3
2 changed files with 91 additions and 0 deletions

View File

@ -200,6 +200,47 @@ class LilleUrbanCard(BaseResource):
raise APIError(response_json['erreur'], data=response_json)
return {'data': response_json}
@endpoint(perm='can_access', description=_('Code check'), methods=['post'])
def code_check(self, request, *args, **kwargs):
data = json_loads(request.body)
response = self.requests.post(
urljoin(self.base_url, '/clu/ws/verifierMdp'),
json=data,
auth=HttpBearerAuth(self.get_token()))
response_json = response.json()
if not isinstance(response_json, dict):
self.logger.error('error checking code (unknown error)')
raise APIError('unknown error', data=response_json)
if response_json.get('message') == 'Le mot de passe est valide':
return {'data': response_json}
for error_attribute in ('erreur', 'message'):
if response_json.get(error_attribute):
self.logger.error('error checking code (%r)', response_json[error_attribute])
response_json['status_code'] = response.status_code
raise APIError(response_json[error_attribute], data=response_json)
raise APIError('invalid response', data=response_json)
@endpoint(perm='can_access', description=_('Get Card Info'), methods=['post'])
def card_info(self, request, *args, **kwargs):
data = json_loads(request.body)
response = self.requests.post(
urljoin(self.base_url, '/clu/ws/consulterCarte'),
json=data,
auth=HttpBearerAuth(self.get_token()))
response_json = response.json()
if not isinstance(response_json, dict):
self.logger.error('error getting card info (unknown error)')
raise APIError('unknown error', data=response_json)
for error_attribute in ('erreur', 'message'):
if response_json.get(error_attribute):
self.logger.error('error getting card info (%r)', response_json[error_attribute])
response_json['status_code'] = response.status_code
raise APIError(response_json[error_attribute], data=response_json)
if not response_json.get('numero_serie'):
self.logger.error('error getting card info (response missing numero_serie)')
raise APIError('response missing numero_serie', data=response_json)
return {'data': response_json}
@endpoint(perm='can_access', description=_('Card Revocation'), methods=['post'])
def card_revocation(self, request, *args, **kwargs):
data = json_loads(request.body)

View File

@ -24,6 +24,7 @@ def connector(db):
TOKEN_ERROR_RESPONSE = '{"erreur":"Authentification échouée"}'
TOKEN_RESPONSE = '{"token": "eyJhbGciO..."}'
def mocked_http(url, request):
if url.path == '/clu/ws/auth/connexion':
return {'content': TOKEN_RESPONSE, 'status_code': 200}
@ -64,12 +65,28 @@ def mocked_http(url, request):
error = {'statut': 'ERREUR_NUM_SERIE', 'erreur': 'Le numero de serie...'}
return {'content': json.dumps(error), 'status_code': 404}
return {'content': json.dumps({}), 'status_code': 200}
if url.path == '/clu/ws/verifierMdp':
request_json = json_loads(request.body)
if request_json.get('simulate_error') == 'wrong num serie':
error = {'statut': 'ERREUR_NUM_SERIE', 'erreur': 'Le numero de serie...'}
return {'content': json.dumps(error), 'status_code': 404}
return {'content': json.dumps({'message': 'Le mot de passe est valide'}), 'status_code': 200}
if url.path in ('/clu/ws/revoquerCarte', '/clu/ws/revoquerAbonnement'):
request_json = json_loads(request.body)
if request_json.get('simulate_error') == 'doublon':
error = {'erreur': 'La demande xx existe...'}
return {'content': json.dumps(error), 'status_code': 409}
return {'content': json.dumps({'message': 'ok'}), 'status_code': 200}
if url.path == '/clu/ws/consulterCarte':
request_json = json_loads(request.body)
if request_json.get('simulate_error') == 'wrong num serie':
error = {'erreur': 'Pas de carte attribuee correspondante'}
return {'content': json.dumps(error), 'status_code': 404}
return {'content': json.dumps({
'numero_serie': 'XXX',
'date_debut_abonnement': 'xx/xx/xxxx'}),
'status_code': 200}
@mock.patch('passerelle.utils.Request.post')
@ -209,6 +226,39 @@ def test_code_change(app, connector):
assert resp.json['data']['status_code'] == 404
def test_code_check(app, connector):
endpoint = utils.generic_endpoint_url('lille-urban-card', 'code_check', slug=connector.slug)
with HTTMock(mocked_http):
resp = app.post_json(endpoint, params={
'numero_serie': 'XXX',
'password': '1234',
})
assert resp.json['err'] == 0
# error handling
resp = app.post_json(endpoint, params={
'simulate_error': 'wrong num serie',
'numero_serie': 'XXX',
'password': '1234',
}, status=200)
assert resp.json['err'] == 1
def test_card_info(app, connector):
endpoint = utils.generic_endpoint_url('lille-urban-card', 'card_info', slug=connector.slug)
with HTTMock(mocked_http):
resp = app.post_json(endpoint, params={
'numero_serie': 'XXX',
})
assert resp.json['err'] == 0
# error handling
resp = app.post_json(endpoint, params={
'simulate_error': 'wrong num serie',
}, status=200)
assert resp.json['err'] == 1
def test_card_revocation(app, connector):
endpoint = utils.generic_endpoint_url('lille-urban-card', 'card_revocation', slug=connector.slug)
with HTTMock(mocked_http):