116 lines
4.2 KiB
Python
116 lines
4.2 KiB
Python
from django.contrib.contenttypes.models import ContentType
|
|
from mock import Mock
|
|
import pytest
|
|
|
|
from passerelle.base.models import ApiUser, AccessRight
|
|
from passerelle.contrib.iws.models import IWSConnector
|
|
|
|
|
|
@pytest.fixture()
|
|
def setup(db):
|
|
api = ApiUser.objects.create(username='all', keytype='', key='')
|
|
conn = IWSConnector.objects.create(
|
|
wsdl_url='http://example.com/iws?wsdl',
|
|
operation_endpoint='http://example.com/iws', username='admin', password='admin',
|
|
database='somedb', slug='slug-iws')
|
|
obj_type = ContentType.objects.get_for_model(conn)
|
|
AccessRight.objects.create(
|
|
codename='can_access', apiuser=api, resource_type=obj_type, resource_pk=conn.pk)
|
|
return conn
|
|
|
|
|
|
def create_params(**kwargs):
|
|
res = {
|
|
"firstname": "John", "lastname": "Doe", "email": "john.doe@localhost",
|
|
"description": "four : 1", "tel_number": "0101010101", "date": "28/10/2018",
|
|
"token": "token"
|
|
}
|
|
res.update(kwargs)
|
|
return res
|
|
|
|
|
|
def mock_soap_call(monkeypatch, return_value):
|
|
mock_soap_call = Mock(return_value=return_value)
|
|
import passerelle.contrib.iws.models
|
|
monkeypatch.setattr(passerelle.contrib.iws.models.IWSConnector, '_soap_call', mock_soap_call)
|
|
return mock_soap_call
|
|
|
|
|
|
def test_checkdate_dechet_or_encombrant(app, setup):
|
|
response = app.get(
|
|
'/iws/slug-iws/checkdate/3155570464130003/error/3/?city=toulouse', expect_errors=True)
|
|
json_result = response.json_body
|
|
assert json_result['err'] == 1
|
|
assert u'DECHET' in json_result['err_desc']
|
|
assert u'ENCOMBRANT' in json_result['err_desc']
|
|
|
|
|
|
def test_checkdate_iws_error_status(app, setup, monkeypatch):
|
|
mock_soap_call(monkeypatch, {'status': 'KO', 'trace': 'some trace'})
|
|
response = app.get(
|
|
'/iws/slug-iws/checkdate/3155570464130003/DECHET/3/?city=toulouse', expect_errors=True)
|
|
json_result = response.json_body
|
|
assert json_result['err'] == 1
|
|
assert json_result['err_desc'] == 'iws error, status: "KO", trace: "some trace"'
|
|
|
|
|
|
def test_checkdate_iws_error_no_appel(app, setup, monkeypatch):
|
|
mock_soap_call(
|
|
monkeypatch, {
|
|
'status': 'responseOk', 'trace': '',
|
|
'fields': {'NO_APPEL': ''}})
|
|
response = app.get(
|
|
'/iws/slug-iws/checkdate/3155570464130003/DECHET/3/?city=toulouse', expect_errors=True)
|
|
json_result = response.json_body
|
|
assert json_result['err'] == 1
|
|
assert json_result['err_desc'] == 'iws error, missing token'
|
|
|
|
|
|
def test_checkdate_iws_no_dates(app, setup, monkeypatch):
|
|
mock_soap_call(
|
|
monkeypatch, {
|
|
'status': 'responseOk', 'trace': '',
|
|
'fields': {
|
|
'NO_APPEL': 'sometoken',
|
|
'I_APP_DATESPOSSIBLES': 'Aucune dates disponibles'
|
|
}
|
|
})
|
|
response = app.get('/iws/slug-iws/checkdate/3155570464130003/DECHET/3/?city=toulouse')
|
|
json_result = response.json_body
|
|
assert json_result['err'] == 0
|
|
assert json_result['data'] == []
|
|
|
|
|
|
def test_checkdate_iws_has_dates(app, setup, monkeypatch, settings):
|
|
settings.LANGUAGE_CODE = 'fr-fr'
|
|
mock_soap_call(
|
|
monkeypatch, {
|
|
'status': 'responseOk', 'trace': '',
|
|
'fields': {
|
|
'NO_APPEL': 'sometoken',
|
|
'I_APP_DATESPOSSIBLES': '18/06/2018; 19/06/2018'
|
|
}
|
|
})
|
|
response = app.get('/iws/slug-iws/checkdate/3155570464130003/DECHET/3/?city=toulouse')
|
|
json_result = response.json_body
|
|
assert json_result['err'] == 0
|
|
dates = json_result['data']
|
|
assert len(dates) == 2
|
|
assert dates[0] == {"id": "18/06/2018", "text": "lundi 18 juin 2018", "token": "sometoken"}
|
|
assert dates[1] == {"id": "19/06/2018", "text": "mardi 19 juin 2018", "token": "sometoken"}
|
|
|
|
|
|
def test_bookdate(app, setup, monkeypatch):
|
|
mock_soap_call(
|
|
monkeypatch, {
|
|
'status': 'responseOk', 'trace': '',
|
|
'fields': {
|
|
'NO_APPEL': 'sometoken',
|
|
'I_APP_DATESPOSSIBLES': '18/06/2018;'
|
|
}
|
|
})
|
|
response = app.post_json('/iws/slug-iws/bookdate/', params=create_params())
|
|
json_result = response.json_body
|
|
assert json_result['err'] == 0
|
|
assert json_result['data'] == {'NO_APPEL': 'sometoken', 'I_APP_DATESPOSSIBLES': '18/06/2018;'}
|