From 2f9a76c59255bed63629ae1762229fbcd3c07583 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 29 Aug 2022 14:21:07 +0200 Subject: [PATCH 1/4] bdp: use requests wrapper (#68469) --- passerelle/apps/bdp/models.py | 7 ++--- tests/test_bdp.py | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 tests/test_bdp.py diff --git a/passerelle/apps/bdp/models.py b/passerelle/apps/bdp/models.py index 709fadb2..59b74d2c 100644 --- a/passerelle/apps/bdp/models.py +++ b/passerelle/apps/bdp/models.py @@ -1,6 +1,5 @@ import json -import requests from django.db import models from django.utils.translation import ugettext_lazy as _ from requests.auth import HTTPBasicAuth @@ -40,13 +39,15 @@ class Bdp(BaseResource): def get_api(self, endpoint, **params): options = self.requests_options() - return requests.get(self.service_url + '/api/' + endpoint, params=params, **options).json() + return self.requests.get(self.service_url + '/api/' + endpoint, params=params, **options).json() def post_api(self, endpoint, obj): data = json.dumps(obj) headers = {'Content-Type': 'application/json'} options = self.requests_options() - request = requests.post(self.service_url + '/api/' + endpoint, data=data, headers=headers, **options) + request = self.requests.post( + self.service_url + '/api/' + endpoint, data=data, headers=headers, **options + ) result = { 'status_code': request.status_code, 'x_request_id': request.headers.get('x-request-id'), diff --git a/tests/test_bdp.py b/tests/test_bdp.py new file mode 100644 index 00000000..c8f654a2 --- /dev/null +++ b/tests/test_bdp.py @@ -0,0 +1,51 @@ +# Copyright (C) 2021 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import pytest +from httmock import HTTMock, response, urlmatch + +from . import utils + + +class TestBdp: + @pytest.fixture + def connector(self, db): + from passerelle.apps.bdp.models import Bdp + + return utils.setup_access_rights( + Bdp.objects.create( + slug='slug', + service_url='https://bdp.example.com', + username='username', + password='password', + ) + ) + + def test_detail_view(self, app, connector): + @urlmatch(netloc=r'^bdp.example.com$', path=r'^/api/coin') + def api_coin(url, request): + return response( + 200, + [ + { + 'id': 'id', + 'text': 'coin', + } + ], + ) + + with HTTMock(api_coin): + resp = app.get('/bdp/slug/coin/') + assert resp.json['data'] -- 2.37.2