Projet

Général

Profil

0001-bdp-use-requests-wrapper-68469.patch

Benjamin Dauvergne, 29 août 2022 17:03

Télécharger (3,45 ko)

Voir les différences:

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
passerelle/apps/bdp/models.py
1 1
import json
2 2

  
3
import requests
4 3
from django.db import models
5 4
from django.utils.translation import ugettext_lazy as _
6 5
from requests.auth import HTTPBasicAuth
......
40 39

  
41 40
    def get_api(self, endpoint, **params):
42 41
        options = self.requests_options()
43
        return requests.get(self.service_url + '/api/' + endpoint, params=params, **options).json()
42
        return self.requests.get(self.service_url + '/api/' + endpoint, params=params, **options).json()
44 43

  
45 44
    def post_api(self, endpoint, obj):
46 45
        data = json.dumps(obj)
47 46
        headers = {'Content-Type': 'application/json'}
48 47
        options = self.requests_options()
49
        request = requests.post(self.service_url + '/api/' + endpoint, data=data, headers=headers, **options)
48
        request = self.requests.post(
49
            self.service_url + '/api/' + endpoint, data=data, headers=headers, **options
50
        )
50 51
        result = {
51 52
            'status_code': request.status_code,
52 53
            'x_request_id': request.headers.get('x-request-id'),
tests/test_bdp.py
1
# Copyright (C) 2021  Entr'ouvert
2
#
3
# This program is free software: you can redistribute it and/or modify it
4
# under the terms of the GNU Affero General Public License as published
5
# by the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU Affero General Public License for more details.
12
#
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

  
16
import pytest
17
from httmock import HTTMock, response, urlmatch
18

  
19
from . import utils
20

  
21

  
22
class TestBdp:
23
    @pytest.fixture
24
    def connector(self, db):
25
        from passerelle.apps.bdp.models import Bdp
26

  
27
        return utils.setup_access_rights(
28
            Bdp.objects.create(
29
                slug='slug',
30
                service_url='https://bdp.example.com',
31
                username='username',
32
                password='password',
33
            )
34
        )
35

  
36
    def test_detail_view(self, app, connector):
37
        @urlmatch(netloc=r'^bdp.example.com$', path=r'^/api/coin')
38
        def api_coin(url, request):
39
            return response(
40
                200,
41
                [
42
                    {
43
                        'id': 'id',
44
                        'text': 'coin',
45
                    }
46
                ],
47
            )
48

  
49
        with HTTMock(api_coin):
50
            resp = app.get('/bdp/slug/coin/')
51
            assert resp.json['data']
0
-