Project

General

Profile

0001-vivaticket-PEP8ness-30458.patch

Benjamin Dauvergne, 08 Feb 2019 08:43 AM

Download (7.72 KB)

View differences:

Subject: [PATCH 01/13] vivaticket: PEP8ness (#30458)

 passerelle/apps/vivaticket/models.py | 41 ++++++++++++++++------------
 tests/test_vivaticket.py             | 20 +++++++++-----
 2 files changed, 36 insertions(+), 25 deletions(-)
passerelle/apps/vivaticket/models.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import json
18 17
import hashlib
19 18
import urlparse
20 19

  
21 20
from django.core.cache import cache
22 21
from django.db import models
23 22
from django.utils.translation import ugettext_lazy as _
24
from django.utils.text import slugify
25 23

  
26 24
from passerelle.base.models import BaseResource
27 25
from passerelle.utils.api import endpoint
......
102 100
            raise APIError(response.content)
103 101
        api_key = response.json()['Key']
104 102
        # api key is available for 30 minutes
105
        cache.set(cache_key_name, api_key, 60*30)
103
        cache.set(cache_key_name, api_key, 60 * 30)
106 104
        return api_key
107 105

  
108 106
    def get(self, endpoint, **kwargs):
......
174 172
        return {'InternalCode': response.json()['InternalCode']}
175 173

  
176 174
    @endpoint(perm='can_access', description=_('Book an event'),
177
              post={'description': _('Creates a booking for an event'),
178
                    'request_body': {
179
                        'schema': {'application/json': EVENTBOOK_SCHEMA}
180
                    }
181
              })
175
              post={
176
                  'description': _('Creates a booking for an event'),
177
                  'request_body': {
178
                      'schema': {
179
                          'application/json': EVENTBOOK_SCHEMA
180
                      }
181
                  }
182
    })
182 183
    def book(self, request, post_data, nameid=None):
183
        booking = {'externalCode': post_data['id'], 'startDateTime': post_data['datetime'],
184
                'endDateTime': post_data['datetime'],
185
                'contact': self.get_or_create_contact(post_data['email'], nameid),
186
                'roomList': [{'eventCategoryCode': post_data['event'],
187
                              'roomCode': post_data['room'],
188
                              'themeCode': post_data['theme'],
189
                              'quantity': post_data['quantity'],
190
                              'startDateTime': post_data['datetime'],
191
                              'endDateTime': post_data['datetime']}]
192
                }
184
        booking = {
185
            'externalCode': post_data['id'],
186
            'startDateTime': post_data['datetime'],
187
            'endDateTime': post_data['datetime'],
188
            'contact': self.get_or_create_contact(post_data['email'], nameid),
189
            'roomList': [{
190
                'eventCategoryCode': post_data['event'],
191
                'roomCode': post_data['room'],
192
                'themeCode': post_data['theme'],
193
                'quantity': post_data['quantity'],
194
                'startDateTime': post_data['datetime'],
195
                'endDateTime': post_data['datetime']
196
            }]
197
        }
193 198
        r = self.post('Booking/Post', {'Booking': booking})
194 199
        return {'data': r.json()}
tests/test_vivaticket.py
200 200
        slug='test', login='foo', password='bar',
201 201
        url='http://example.net/vivaticket'))
202 202

  
203

  
203 204
@mock.patch('passerelle.utils.Request.post')
204 205
def test_get_api_key(mocked_post, app, connector):
205
    with pytest.raises(APIError) as excinfo:
206
    with pytest.raises(APIError):
206 207
        mocked_post.return_value = utils.FakedResponse(content=ERROR_RESPONSE, ok=False)
207 208
        connector.get_apikey()
208 209
    mocked_post.return_value = utils.FakedResponse(content=KEY_RESPONSE, ok=True)
209 210
    connector.get_apikey()
210 211
    assert mocked_post.call_count == 2
211
    assert "Connect/PostConnect" in  mocked_post.call_args[0][0]
212
    assert "Connect/PostConnect" in mocked_post.call_args[0][0]
212 213
    assert mocked_post.call_args[1]['json']['Login'] == 'foo'
213 214
    assert mocked_post.call_args[1]['json']['Password'] == 'bar'
214 215
    # make sure the key from cache is used
......
228 229
        assert 'id' in item
229 230
        assert 'text' in item
230 231

  
232

  
231 233
@mock.patch('passerelle.utils.Request.post')
232 234
@mock.patch('passerelle.utils.Request.get')
233 235
def test_get_events_with_expired_key(mocked_get, mocked_post, app, connector):
234 236
    mocked_get.return_value = utils.FakedResponse(content=EVENTS_RESPONSE, status_code=401)
235 237
    mocked_post.return_value = utils.FakedResponse(content=KEY_RESPONSE, status_code=200)
236
    result = utils.endpoint_get('/vivaticket/test/events', app, connector, 'events')
238
    utils.endpoint_get('/vivaticket/test/events', app, connector, 'events')
237 239
    assert mocked_post.call_count == 1
238 240

  
239 241

  
......
249 251
    result = utils.endpoint_get('/vivaticket/test/rooms', app, connector, 'rooms', params={'event': '02'})
250 252
    assert mocked_get.call_args[1]['params']['eventCategory'] == '02'
251 253

  
254

  
252 255
@mock.patch('passerelle.utils.Request.get')
253 256
def test_get_themes(mocked_get, app, connector):
254 257
    mocked_get.return_value = utils.FakedResponse(content=ROOMS_RESPONSE, status_code=200)
......
268 271
    mocked_get.return_value = utils.FakedResponse(content=CONTACT_RESPONSE, ok=True)
269 272
    assert connector.get_or_create_contact('foo@example.com') == {'InternalCode': '0000000273'}
270 273
    mocked_get.return_value = utils.FakedResponse(content=CONTACT_RESPONSE, ok=False)
271
    mocked_post.return_value = utils.FakedResponse(content='{"InternalCode": "0000000277", "ReturnCode": 0, "Error": null}',
272
                                                   status_code=200)
274
    mocked_post.return_value = utils.FakedResponse(
275
        content='{"InternalCode": "0000000277", "ReturnCode": 0, "Error": null}',
276
        status_code=200)
273 277
    connector.get_or_create_contact('foo@example.com')
274 278
    assert mocked_post.call_args[1]['json']['Key'] == '86569D0CA1B1CBEF8D77DD5BDC9F5CBAE5C99074'
275 279
    assert mocked_post.call_args[1]['json']['Contact']['Email'] == 'foo@example.com'
......
281 285
def test_get_and_update_contact(mocked_get, mocked_put, app, connector):
282 286
    mocked_get.return_value = utils.FakedResponse(content=CONTACT_RESPONSE, ok=True)
283 287
    assert connector.get_or_create_contact('foo@example.com') == {'InternalCode': '0000000273'}
284
    mocked_put.return_value = utils.FakedResponse(content='{"InternalCode": "0000000277", "ReturnCode": 0, "Error": null}',
285
                                                   status_code=200)
288
    mocked_put.return_value = utils.FakedResponse(
289
        content='{"InternalCode": "0000000277", "ReturnCode": 0, "Error": null}',
290
        status_code=200)
286 291
    connector.get_or_create_contact('bar@example.com', 'bar')
287 292
    assert mocked_put.call_args[1]['params']['id'] == '0000000273'
288 293
    assert mocked_put.call_args[1]['json']['Key'] == '86569D0CA1B1CBEF8D77DD5BDC9F5CBAE5C99074'
289 294
    assert mocked_put.call_args[1]['json']['Contact']['Email'] == 'bar@example.com'
290 295

  
296

  
291 297
@mock.patch('passerelle.utils.Request.post')
292 298
@mock.patch('passerelle.utils.Request.get')
293 299
def test_book(mocked_get, mocked_post, app, connector):
294
-