Projet

Général

Profil

0001-ovh-handle-error-during-token-request-55667.patch

Valentin Deniaud, 19 juillet 2021 16:36

Télécharger (3,41 ko)

Voir les différences:

Subject: [PATCH] ovh: handle error during token request (#55667)

 passerelle/apps/ovh/views.py | 23 +++++++++++++++++++----
 tests/test_sms.py            | 24 ++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 4 deletions(-)
passerelle/apps/ovh/views.py
6 6
from django.shortcuts import reverse
7 7
from django.utils.translation import ugettext_lazy as _
8 8
from django.views.generic.base import RedirectView
9
from requests import RequestException
9 10

  
10 11
from .models import OVHSMSGateway
11 12

  
......
25 26
        }
26 27
        headers = {"X-Ovh-Application": connector.application_key}
27 28

  
28
        resp = connector.requests.post(
29
            'https://eu.api.ovh.com/1.0/auth/credential', json=data, headers=headers
30
        )
31
        result = resp.json()
29
        try:
30
            resp = connector.requests.post(
31
                'https://eu.api.ovh.com/1.0/auth/credential', json=data, headers=headers
32
            )
33
        except RequestException as e:
34
            messages.error(self.request, _('There has been an error requesting token: %s.') % e)
35
            return connector.get_absolute_url()
36
        try:
37
            result = resp.json()
38
        except ValueError as e:
39
            messages.error(self.request, _('There has been an error requesting token: bad JSON response.'))
40
            return connector.get_absolute_url()
41
        try:
42
            resp.raise_for_status()
43
        except RequestException as e:
44
            error_text = result.get('message', result)
45
            messages.error(self.request, _('There has been an error requesting token: %s.') % error_text)
46
            return connector.get_absolute_url()
32 47

  
33 48
        self.request.session['ovh-token-%s' % request_id] = result['consumerKey']
34 49
        return result['validationUrl']
tests/test_sms.py
502 502
    assert connector.consumer_key == 'xyz'
503 503

  
504 504

  
505
def test_ovh_token_request_error(admin_user, app):
506
    connector = OVHSMSGateway.objects.create(
507
        slug='test-ovh',
508
        title='Test OVH',
509
        account='sms-test42',
510
        application_key='wrong',
511
        application_secret='oups',
512
    )
513

  
514
    app = login(app)
515
    resp = app.get(connector.get_absolute_url())
516
    ovh_request_token_url = 'https://eu.api.ovh.com/1.0/auth/credential'
517
    ovh_response = {'message': 'Invalid application key'}
518

  
519
    with utils.mock_url(ovh_request_token_url, ovh_response, 401) as mocked:
520
        resp = resp.click('request access').follow()
521
    assert 'error requesting token: Invalid application key.' in resp.text
522

  
523
    ovh_response = 'not-json'
524
    with utils.mock_url(ovh_request_token_url, ovh_response, 401) as mocked:
525
        resp = resp.click('request access').follow()
526
    assert 'error requesting token: bad JSON response' in resp.text
527

  
528

  
505 529
@pytest.mark.parametrize('connector', [ChoositSMSGateway], indirect=True)
506 530
def test_manager(admin_user, app, connector):
507 531
    app = login(app)
508
-