Project

General

Profile

0002-grenoble_gru-PEP8ness-code-style-30458.patch

Benjamin Dauvergne, 08 Feb 2019 08:43 AM

Download (14.1 KB)

View differences:

Subject: [PATCH 02/13] grenoble_gru: PEP8ness, code style (#30458)

 passerelle/contrib/grenoble_gru/models.py |  18 ++-
 tests/test_grenoble_gru.py                | 133 ++++++++++++++++------
 2 files changed, 111 insertions(+), 40 deletions(-)
passerelle/contrib/grenoble_gru/models.py
42 42
    '23': _('Demand already exists')
43 43
}
44 44

  
45

  
45 46
def xml2dict(element):
46 47
    data = {}
47 48
    for attr in element.keys():
......
110 111

  
111 112
            # comments
112 113
            'obs_demande_urgente': 1 if data.get('urgent_demand') is True else 0,
113
            'obs_type_dysfonctionnement': check_value(data, 'dysfonction_type', self.types('//typeDysfonctionnement', True)),
114
            'obs_type_dysfonctionnement': check_value(
115
                data, 'dysfonction_type', self.types('//typeDysfonctionnement', True)),
114 116
            'obs_motif': check_value(data, 'intervention_reason', self.types('//motif', True)),
115 117
            'obs_description_probleme': data.get('comment_description', ''),
116 118
        }
......
138 140
        cache.set(cache_key, xml_content, 3600)
139 141
        if as_list:
140 142
            return [el.find('identifiant').text for el in root.xpath(path)]
141
        return {'data': [{'id': el.find('identifiant').text, 'text': el.find('libelle').text} for el in root.xpath(path)]}
142

  
143
        return {
144
            'data': [
145
                {
146
                    'id': el.find('identifiant').text,
147
                    'text': el.find('libelle').text
148
                } for el in root.xpath(path)
149
            ]
150
        }
143 151

  
144 152
    @endpoint(name='contact-modes', perm='can_access', description=_('Lists contact modes'))
145 153
    def contact_modes(self, request, *args, **kwargs):
......
185 193
        return {'data': 'Demand successfully created'}
186 194

  
187 195
    @endpoint(name='demand', perm='can_access', methods=['post'], description=_('Add attachment to a demand'),
188
              pattern='(?P<demand_id>[\w-]+)/add-attachment/$',)
196
              pattern=r'(?P<demand_id>[\w-]+)/add-attachment/$',)
189 197
    def add_attachment_to_demand(self, request, demand_id, **kwargs):
190 198
        data = json.loads(request.body)
191 199
        if 'file' not in data:
......
211 219
        return False
212 220

  
213 221
    @endpoint(name='demand', perm='can_access', description=_('Get demand'),
214
              pattern='(?P<demand_id>[\w-]+)/$')
222
              pattern=r'(?P<demand_id>[\w-]+)/$')
215 223
    def get_demand(self, request, demand_id, **kwargs):
216 224
        payload = {'dem_tiers_id': demand_id}
217 225
        response = self.request('ws_get_demande.php', payload)
tests/test_grenoble_gru.py
18 18
import os
19 19
import mock
20 20
import pytest
21
import six
21 22

  
22 23
import utils
23 24

  
......
28 29

  
29 30
@pytest.fixture
30 31
def setup(db):
31
    return utils.setup_access_rights(GrenobleGRU.objects.create(slug='test',
32
                                    base_url='https://itvvoirie-test.lametro.fr'))
32
    return utils.setup_access_rights(
33
        GrenobleGRU.objects.create(
34
            slug='test',
35
            base_url='https://itvvoirie-test.lametro.fr'))
33 36

  
34 37

  
35 38
BASIC_PAYLOAD = {
......
115 118

  
116 119
@pytest.fixture(params=['01', '02', '10', '20', '21', '23', '4242'])
117 120
def remote_response(request):
118
    text = unicode(request.param)
121
    text = six.text_type(request.param)
119 122
    return utils.FakedResponse(index=request.param_index, text=text)
120 123

  
124

  
121 125
def test_contact_mode_typologies_list_with_invalid_xml(app, setup):
122 126
    with mock.patch('passerelle.utils.Request.post') as request_post:
123 127
        response = mock.Mock()
124 128
        types_filename = os.path.join(os.path.dirname(__file__), 'data', 'grenoble_gru_typologies.xml')
125
        types = file(types_filename).read()
129
        types = open(types_filename).read()
126 130
        response.content = types.replace('Courrier', 'Courrier & autres')
127 131
        request_post.return_value = response
128
        endpoint = reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
129
                        'slug': setup.slug, 'endpoint': 'contact-modes'})
132
        endpoint = reverse(
133
            'generic-endpoint',
134
            kwargs={
135
                'connector': 'grenoble-gru',
136
                'slug': setup.slug,
137
                'endpoint': 'contact-modes'
138
            }
139
        )
130 140
        response = app.get(endpoint)
131 141
        request_post.assert_called_once()
132 142
        assert response.json['err']
133 143
        assert 'Invalid XML returned:' in response.json['err_desc']
134
        assert response.json['data'] == None
144
        assert response.json['data'] is None
145

  
135 146

  
136 147
def test_contact_mode_typologies_list(app, setup):
137 148
    with mock.patch('passerelle.utils.Request.post') as request_post:
138 149
        response = mock.Mock()
139 150
        types_filename = os.path.join(os.path.dirname(__file__), 'data', 'grenoble_gru_typologies.xml')
140
        response.content = file(types_filename).read()
151
        response.content = open(types_filename).read()
141 152
        request_post.return_value = response
142
        endpoint = reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
143
                        'slug': setup.slug, 'endpoint': 'contact-modes'})
153
        endpoint = reverse(
154
            'generic-endpoint',
155
            kwargs={
156
                'connector': 'grenoble-gru',
157
                'slug': setup.slug,
158
                'endpoint': 'contact-modes'
159
            }
160
        )
144 161
        response = app.get(endpoint)
145 162
        request_post.assert_called_once()
146 163
        assert len(response.json['data']) == 6
......
156 173
def test_demand_creation_return_codes(app, setup, remote_response):
157 174
    with mock.patch('passerelle.utils.Request.post') as request_post:
158 175
        request_post.return_value = remote_response
159
        response = app.post_json(reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
160
                                'slug': setup.slug, 'endpoint': 'create-demand'}),
161
                                 params=BASIC_PAYLOAD, status=200)
176
        response = app.post_json(
177
            reverse('generic-endpoint',
178
                    kwargs={
179
                        'connector': 'grenoble-gru',
180
                        'slug': setup.slug,
181
                        'endpoint': 'create-demand'
182
                    }),
183
            params=BASIC_PAYLOAD,
184
            status=200)
162 185
        index = remote_response.index
163 186
        if index == 0:
164 187
            assert response.json['data'] == 'Demand successfully created'
......
179 202
def test_demand_creation_with_invalid_params(app, setup):
180 203
    invalid_payload = BASIC_PAYLOAD.copy()
181 204
    invalid_payload['applicant_status'] = 42
182
    response = app.post_json(reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
183
                                    'slug': setup.slug, 'endpoint': 'create-demand'}),
184
                                     params=invalid_payload)
205
    response = app.post_json(
206
        reverse('generic-endpoint',
207
                kwargs={
208
                    'connector': 'grenoble-gru',
209
                    'slug': setup.slug,
210
                    'endpoint': 'create-demand'
211
                }),
212
        params=invalid_payload)
185 213
    assert response.json['err'] == 1
186 214
    assert 'applicant_status must be one of' in response.json['err_desc']
187
    assert response.json['data'] == None
215
    assert response.json['data'] is None
188 216

  
189 217

  
190 218
def test_demand_creation_params(app, setup):
......
192 220
        payload = BASIC_PAYLOAD.copy()
193 221
        payload['intervention_free_address'] = u'169, rue du Château'
194 222
        payload['applicant_free_address'] = u'1, rue de l\'Est'
195
        response = app.post_json(reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
196
                                'slug': setup.slug, 'endpoint': 'create-demand'}),
197
                                 params=payload)
223
        app.post_json(
224
            reverse('generic-endpoint',
225
                    kwargs={
226
                        'connector': 'grenoble-gru',
227
                        'slug': setup.slug,
228
                        'endpoint': 'create-demand'
229
                    }),
230
            params=payload)
198 231
        assert request_post.call_args[1]['data']['int_adresse_manuelle'] == u'169, rue du Château'
199 232
        assert request_post.call_args[1]['data']['dem_adresse_manuelle'] == u'1, rue de l\'Est'
200 233
        assert request_post.call_args[1]['data']['obs_demande_urgente'] == 0
......
203 236
        payload['applicant_requires_reply'] = True
204 237
        payload['urgent_demand'] = True
205 238

  
206
        response = app.post_json(reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
207
                                'slug': setup.slug, 'endpoint': 'create-demand'}),
208
                                 params=payload, status=200)
239
        app.post_json(
240
            reverse(
241
                'generic-endpoint',
242
                kwargs={
243
                    'connector': 'grenoble-gru',
244
                    'slug': setup.slug,
245
                    'endpoint': 'create-demand'
246
                }),
247
            params=payload,
248
            status=200)
209 249
        assert request_post.call_args[1]['data']['obs_demande_urgente'] == 1
210 250
        assert request_post.call_args[1]['data']['dem_reponse'] == 1
211 251

  
......
215 255
        response = mock.Mock()
216 256
        response.content = '01'
217 257
        request_post.return_value = response
218
        endpoint = reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
219
                        'slug': setup.slug, 'endpoint': 'demand', 'rest': '42-42/add-attachment/'})
258
        endpoint = reverse(
259
            'generic-endpoint',
260
            kwargs={
261
                'connector': 'grenoble-gru',
262
                'slug': setup.slug,
263
                'endpoint': 'demand',
264
                'rest': '42-42/add-attachment/'
265
            })
220 266
        payload = {'file': 'test.jpg'}
221 267
        response = app.post_json(endpoint, params=payload, status=200)
222
        assert response.json['data'] == None
268
        assert response.json['data'] is None
223 269
        assert response.json['err'] == 1
224 270
        assert response.json['err_desc'] == 'file should be a dict'
225 271
        file_b64_content = '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFh'
226
        payload['file' ] = {'content_type': 'image/jpg', 'filename': 'photo.jpg'}
272
        payload['file'] = {
273
            'content_type': 'image/jpg',
274
            'filename': 'photo.jpg'
275
        }
227 276
        response = app.post_json(endpoint, params=payload, status=400)
228
        assert response.json['data'] == None
277
        assert response.json['data'] is None
229 278
        assert response.json['err'] == 1
230 279
        assert response.json['err_desc'] == 'missing parameters: \'file[content]\'.'
231 280
        payload['file']['content'] = file_b64_content
......
233 282
        assert request_post.call_args[1]['data']['dem_tiers_id'] == '42-42'
234 283
        assert 'filename=photo.jpg' in request_post.call_args[1]['data']['piece_jointe']
235 284
        assert 'filetype=image%2Fjpg' in request_post.call_args[1]['data']['piece_jointe']
236
        assert 'filecontent=%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFh' in request_post.call_args[1]['data']['piece_jointe']
237

  
285
        assert ('filecontent=%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFh'
286
                in request_post.call_args[1]['data']['piece_jointe'])
238 287

  
239 288

  
240 289
def test_get_demand(app, setup):
......
242 291
        response = mock.Mock()
243 292
        response.content = DEMAND.replace('\n', '')
244 293
        request_post.return_value = response
245
        response = app.get(reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
246
                                'slug': setup.slug, 'endpoint': 'demand', 'rest': '42-42/'}))
294
        response = app.get(
295
            reverse('generic-endpoint',
296
                    kwargs={
297
                        'connector': 'grenoble-gru',
298
                        'slug': setup.slug,
299
                        'endpoint': 'demand',
300
                        'rest': '42-42/'
301
                    })
302
        )
247 303
        assert request_post.call_args[1]['data']['dem_tiers_id'] == '42-42'
248 304
        demand = response.json['data']
249 305
        assert 'IdGRU' in demand
......
257 313
        response = mock.Mock()
258 314
        response.content = DEMAND.replace('\n', '').replace('de Grenoble', '& Grenoble')
259 315
        request_post.return_value = response
260
        response = app.get(reverse('generic-endpoint', kwargs={'connector': 'grenoble-gru',
261
                                'slug': setup.slug, 'endpoint': 'demand', 'rest': '42-42/'}))
316
        response = app.get(
317
            reverse('generic-endpoint',
318
                    kwargs={
319
                        'connector': 'grenoble-gru',
320
                        'slug': setup.slug,
321
                        'endpoint': 'demand',
322
                        'rest': '42-42/'
323
                    })
324
        )
262 325
        assert request_post.call_args[1]['data']['dem_tiers_id'] == '42-42'
263 326
        assert response.json['err']
264 327
        assert 'Invalid XML returned:' in response.json['err_desc']
265
        assert response.json['data'] == None
328
        assert response.json['data'] is None
266
-