Projet

Général

Profil

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

Valentin Deniaud, 19 juillet 2021 14:27

Télécharger (2,44 ko)

Voir les différences:

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

 passerelle/apps/ovh/views.py |  7 +++++++
 tests/test_sms.py            | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+)
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

  
......
29 30
            'https://eu.api.ovh.com/1.0/auth/credential', json=data, headers=headers
30 31
        )
31 32
        result = resp.json()
33
        try:
34
            resp.raise_for_status()
35
        except RequestException as e:
36
            error_text = result.get('message', e)
37
            messages.error(self.request, 'There has been an error requesting token: %s.' % error_text)
38
            return connector.get_absolute_url()
32 39

  
33 40
        self.request.session['ovh-token-%s' % request_id] = result['consumerKey']
34 41
        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

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