1011 lines
41 KiB
Python
1011 lines
41 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Tests for connector AtrealOpenads."""
|
|
# pylint: disable=too-many-lines
|
|
|
|
import base64
|
|
import datetime
|
|
import json
|
|
import os
|
|
import re
|
|
|
|
import magic
|
|
import mock
|
|
import pytest
|
|
|
|
from requests import Response
|
|
|
|
from django.http import Http404
|
|
from django.http.request import HttpRequest, QueryDict
|
|
from django.http.response import JsonResponse
|
|
from django.core.files import File
|
|
from django.core.files.base import ContentFile
|
|
#from django.db.models.query import QuerySet
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from passerelle.utils.jsonresponse import APIError
|
|
from passerelle.base.models import Job
|
|
|
|
from atreal_openads.utils import (
|
|
get_file_data,
|
|
get_file_digest,
|
|
trunc_str_values
|
|
)
|
|
|
|
from atreal_openads.models import (
|
|
ForwardFile,
|
|
Guichet,
|
|
Collectivite,
|
|
AtrealOpenads
|
|
)
|
|
|
|
|
|
CONNECTOR_NAME = 'atreal-openads'
|
|
CONNECTOR_SLUG = 'atreal'
|
|
COLLECTIVITE = 79
|
|
OPENADS_API_LOGIN = 'publik-passerelle'
|
|
OPENADS_API_PASSWORD = base64.urlsafe_b64encode(os.urandom(20))
|
|
OPENADS_API_URL = 'http://openads.api/'
|
|
|
|
FAKE_COOKIE_CRSF = base64.urlsafe_b64encode(os.urandom(20))
|
|
FAKE_NUMERO_DOSSIER = base64.urlsafe_b64encode(os.urandom(10))
|
|
|
|
TESTS_DIR = os.path.dirname(__file__)
|
|
RESOURCES_DIR = os.path.join(TESTS_DIR, 'resources')
|
|
TEST_FILE_CERFA_DIA = os.path.join(RESOURCES_DIR, 'cerfa_10072-02.pdf')
|
|
TEST_FILE_PLAN_CADASTRAL = os.path.join(RESOURCES_DIR, 'plancadastral.pdf')
|
|
|
|
|
|
@pytest.fixture
|
|
# pylint: disable=unused-argument,invalid-name
|
|
def atreal_openads(db):
|
|
"""Return an instance of a connector AtrealOpenads."""
|
|
return AtrealOpenads.objects.create(
|
|
slug=CONNECTOR_SLUG,
|
|
default_collectivite_openADS_id=COLLECTIVITE,
|
|
openADS_API_url=OPENADS_API_URL,
|
|
basic_auth_username=OPENADS_API_LOGIN,
|
|
basic_auth_password=OPENADS_API_PASSWORD
|
|
)
|
|
|
|
@pytest.fixture
|
|
# pylint: disable=unused-argument,redefined-outer-name,invalid-name
|
|
def collectivite_1(db, atreal_openads):
|
|
"""Return an instance of a 'Collectivite'."""
|
|
return Collectivite.objects.create( # pylint: disable=no-member
|
|
name=u'Macollectivité',
|
|
connecteur=atreal_openads,
|
|
openADS_id='3'
|
|
)
|
|
|
|
@pytest.fixture
|
|
# pylint: disable=unused-argument,redefined-outer-name,invalid-name
|
|
def collectivite_1_guichet(db, atreal_openads, collectivite_1):
|
|
"""Return an instance of a 'Guichet'."""
|
|
return Guichet.objects.create( # pylint: disable=no-member
|
|
collectivite=collectivite_1,
|
|
ouverture_jour_h=datetime.time(9, 0),
|
|
fermeture_jour_h=datetime.time(17, 0),
|
|
ouverture_sem_d=1, # Lundi
|
|
fermeture_sem_d=5, # Vendredi
|
|
ouverture_sem_h=datetime.time(8, 30),
|
|
fermeture_sem_h=datetime.time(12, 15)
|
|
)
|
|
|
|
|
|
def build_forwardfile_from_path(connecteur, path, numero_dossier, type_fichier):
|
|
"""Convert a file path to a ForwardFile."""
|
|
if path:
|
|
rand_id = base64.urlsafe_b64encode(os.urandom(6))
|
|
fwd_file = ForwardFile()
|
|
fwd_file.connecteur = connecteur
|
|
fwd_file.numero_demande = rand_id
|
|
fwd_file.numero_dossier = numero_dossier
|
|
fwd_file.type_fichier = type_fichier
|
|
fwd_file.orig_filename = os.path.basename(path)
|
|
fwd_file.content_type = magic.from_file(path, mime=True)
|
|
with open(path, 'r') as file_pt:
|
|
fwd_file.file_hash = get_file_digest(file_pt)
|
|
fwd_file.upload_file = File(open(path, 'r'))
|
|
fwd_file.upload_status = 'pending'
|
|
return fwd_file
|
|
return None
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_forward_file(atreal_openads):
|
|
"""Test ForwardFile object methods."""
|
|
|
|
forwardfile = ForwardFile(
|
|
numero_demande='45641531',
|
|
numero_dossier=FAKE_NUMERO_DOSSIER,
|
|
type_fichier='CERFA',
|
|
orig_filename='afile',
|
|
file_hash='forwardfiledf456fdsvgb4bgfb6g4f5b',
|
|
upload_status='pending',
|
|
connecteur=atreal_openads,
|
|
collectivite=None
|
|
)
|
|
# pylint: disable=no-member
|
|
forwardfile.upload_file.save(forwardfile.orig_filename,
|
|
ContentFile(get_file_data(TEST_FILE_CERFA_DIA)))
|
|
forwardfile.save()
|
|
|
|
assert repr(forwardfile) == (
|
|
u'ForwardFile(id=%s,connecteur=%s,collectivite=%s'
|
|
',demande=%s,dossier=%s,type=%s,filename=%s,status=%s)' % (
|
|
forwardfile.id, unicode(forwardfile.connecteur), None, # pylint: disable=no-member
|
|
forwardfile.numero_demande, forwardfile.numero_dossier,
|
|
forwardfile.type_fichier, forwardfile.orig_filename, forwardfile.upload_status
|
|
)
|
|
).encode('utf-8')
|
|
|
|
assert str(forwardfile) == '%s[%s]' % (trunc_str_values(forwardfile.orig_filename, 20),
|
|
'Pending')
|
|
assert unicode(forwardfile) == u'%s[%s]' % (trunc_str_values(forwardfile.orig_filename, 20),
|
|
'Pending')
|
|
|
|
assert forwardfile.get_status() == 'Pending'
|
|
assert forwardfile.get_status('invalid') == 'invalid'
|
|
|
|
params = forwardfile.get_url_params()
|
|
assert params['connecteur'] == atreal_openads.slug
|
|
|
|
assert forwardfile.upload_file is not None
|
|
assert forwardfile.upload_file.size > 0
|
|
assert forwardfile.size == forwardfile.upload_file.size
|
|
assert forwardfile.file_hash == ('811588016518eedeb4507f3e4c41be95'
|
|
'8a03576b0cd20bdb2cb9c6a186dbd887')
|
|
|
|
forwardfile.content_type = 'application/pdf'
|
|
forwardfile.upload_status = 'success'
|
|
forwardfile.save()
|
|
assert forwardfile.upload_status == 'success'
|
|
assert forwardfile.get_status() == 'Success'
|
|
assert forwardfile.content_type == 'application/pdf'
|
|
|
|
with pytest.raises(ValueError) as exception:
|
|
forwardfile.upload_file.size # pylint: disable=pointless-statement
|
|
assert unicode(exception.value) == "The 'upload_file' attribute has no file associated with it."
|
|
assert forwardfile.size > 0
|
|
assert forwardfile.file_hash == ('811588016518eedeb4507f3e4c41be95'
|
|
'8a03576b0cd20bdb2cb9c6a186dbd887')
|
|
|
|
forwardfile.file_hash = ''
|
|
forwardfile.update_file_hash()
|
|
forwardfile.update_content_type()
|
|
forwardfile.save()
|
|
assert forwardfile.file_hash == ''
|
|
assert forwardfile.content_type == ''
|
|
|
|
forwardfile.orig_filename = ''
|
|
with pytest.raises(ValidationError) as exception:
|
|
forwardfile.save()
|
|
assert len(exception.value.messages) == 1
|
|
assert '__all__' in exception.value.message_dict
|
|
assert unicode(exception.value.message_dict['__all__'][0]) == (
|
|
u"A %s cannot have all the following fields empty: %s." % (
|
|
forwardfile.get_verbose_name(),
|
|
['file_hash', 'orig_filename', 'upload_file']))
|
|
|
|
forwardfile.delete()
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_collectivite(collectivite_1, collectivite_1_guichet):
|
|
"""Test Collectivite object methods."""
|
|
|
|
col = collectivite_1
|
|
|
|
assert repr(col) == (
|
|
u'Collectivite(id=%s,name=%s,connecteur=%s,openADS_id=%s,guichet=%s)' % (
|
|
1, unicode(col.name), unicode(col.connecteur), col.openADS_id,
|
|
unicode(col.guichet) if hasattr(col, 'guichet') else None
|
|
)
|
|
).encode('utf-8')
|
|
|
|
assert str(col) == col.name.encode('utf-8')
|
|
|
|
assert unicode(col) == col.name
|
|
|
|
class_fields = Collectivite.get_fields()
|
|
assert len(class_fields) == 6
|
|
assert class_fields[0].name == 'id'
|
|
assert class_fields[1].name == 'name'
|
|
assert class_fields[2].name == 'connecteur'
|
|
assert class_fields[3].name == 'openADS_id'
|
|
assert class_fields[4].name == 'guichet'
|
|
assert class_fields[5].name == 'forward_file'
|
|
|
|
instance_fields = col.get_fields_kv()
|
|
assert len(instance_fields) == 6
|
|
assert instance_fields[0][0].name == 'id'
|
|
assert instance_fields[1][0].name == 'name'
|
|
assert instance_fields[2][0].name == 'connecteur'
|
|
assert instance_fields[3][0].name == 'openADS_id'
|
|
assert instance_fields[4][0].name == 'guichet'
|
|
assert instance_fields[5][0].name == 'forward_file'
|
|
assert instance_fields[0][1] == col.id
|
|
assert instance_fields[1][1] == col.name
|
|
assert instance_fields[2][1] is col.connecteur
|
|
assert instance_fields[3][1] == col.openADS_id
|
|
assert instance_fields[4][1] is col.guichet
|
|
assert instance_fields[5][1] is None # shouldn't it be QuerySet?
|
|
|
|
params = col.get_url_params()
|
|
assert params['connecteur'] == col.connecteur.slug
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_guichet(collectivite_1_guichet):
|
|
"""Test Guichet object methods."""
|
|
|
|
guichet = collectivite_1_guichet
|
|
|
|
assert repr(guichet) == (
|
|
u'Guichet(id=%s,collectivite=%s,%s)' % (
|
|
1, unicode(guichet.collectivite), unicode(guichet)
|
|
)
|
|
).encode('utf-8')
|
|
|
|
assert str(guichet) == u'Monday 08:30 -> Friday 12:15 [09:00/17:00]'.encode('utf-8')
|
|
|
|
assert unicode(guichet) == u'Monday 08:30 -> Friday 12:15 [09:00/17:00]'
|
|
|
|
params = guichet.get_url_params()
|
|
assert params['collectivite'] == guichet.collectivite.id
|
|
|
|
with pytest.raises(Exception) as exception:
|
|
guichet.get_list_url()
|
|
assert unicode(exception.value) == u"Guichet:get_list_url() method should not be called"
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_guichet_is_open(collectivite_1_guichet): # pylint: disable=too-many-locals
|
|
"""Test the method Guichet.is_open()."""
|
|
|
|
guichet = collectivite_1_guichet
|
|
|
|
dt_fmt = '%Y-%m-%d %H:%M'
|
|
|
|
d_monday = '2019-07-29'
|
|
d_sunday = '2019-07-28'
|
|
d_saturday = '2019-07-27'
|
|
d_friday = '2019-07-26'
|
|
d_thursday = '2019-07-25'
|
|
d_wednesday = '2019-07-24'
|
|
d_tuesday = '2019-07-22'
|
|
t_open = '10:44'
|
|
t_closed_before = '6:33'
|
|
t_closed_after = '20:08'
|
|
|
|
for date_str in [d_monday, d_tuesday, d_wednesday, d_thursday, d_friday]:
|
|
for time_and_cond in [(t_open, True), (t_closed_before, False), (t_closed_after, False)]:
|
|
date_time = datetime.datetime.strptime(date_str + ' ' + time_and_cond[0], dt_fmt)
|
|
assert guichet.is_open(date_time) == time_and_cond[1]
|
|
|
|
date_time = datetime.datetime.strptime(d_friday + ' 16:12', dt_fmt)
|
|
assert not guichet.is_open(date_time)
|
|
|
|
for date_str in [d_saturday, d_sunday]:
|
|
for time_str in [t_open, t_closed_before, t_closed_after]:
|
|
date_time = datetime.datetime.strptime(date_str + ' ' + time_str, dt_fmt)
|
|
assert not guichet.is_open(date_time)
|
|
|
|
with pytest.raises(TypeError) as exception:
|
|
guichet.is_open('invalid datetime')
|
|
assert unicode(exception.value) == u"is_open() expect a datetime object (not a %s)" % type('')
|
|
|
|
assert not guichet.is_open(None)
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_log_json_payload(atreal_openads):
|
|
"""Test the logging of JSON payload."""
|
|
# TODO implement
|
|
assert True
|
|
# change the debug file path
|
|
# check that the function log to it
|
|
# check that what was is logged is correct
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_get_files_from_payload(atreal_openads):
|
|
"""Test the method AtrealOpenads.get_files_from_payload()."""
|
|
|
|
title = 'payload'
|
|
|
|
assert atreal_openads.get_files_from_payload({'files':[{'a':'file'}]}) == [{'a':'file'}]
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.get_files_from_payload({})
|
|
assert unicode(exception.value) == u"Expecting '%s' key in JSON %s" % ('files', title)
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.get_files_from_payload({'files': 'invalid'})
|
|
assert unicode(exception.value) == u"Expecting '%s' value in JSON %s to be a %s (not a %s)" % (
|
|
'files', title, 'list', type(''))
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.get_files_from_payload({'files': {'i':'invalid'}})
|
|
assert unicode(exception.value) == u"Expecting '%s' value in JSON %s to be a %s (not a %s)" % (
|
|
'files', title, 'list', type({}))
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.get_files_from_payload({'files': []})
|
|
assert unicode(exception.value) == u"Expecting non-empty '%s' value in JSON %s" % (
|
|
'files', title)
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_check_file_dict(atreal_openads):
|
|
"""Test the method AtrealOpenads.check_file_dict()."""
|
|
|
|
title = 'payload'
|
|
|
|
dic = {
|
|
'content': get_file_data(TEST_FILE_CERFA_DIA, b64=False),
|
|
'filename': os.path.basename(TEST_FILE_CERFA_DIA),
|
|
'content_type': 'application/pdf'
|
|
}
|
|
|
|
d64 = {
|
|
'b64_content': get_file_data(TEST_FILE_CERFA_DIA, b64=True),
|
|
'filename': os.path.basename(TEST_FILE_CERFA_DIA),
|
|
'content_type': 'application/pdf'
|
|
}
|
|
|
|
assert atreal_openads.check_file_dict(dic, b64=False) is None
|
|
assert atreal_openads.check_file_dict(d64) is None
|
|
|
|
dic['filename'] = {'a', 'filename'}
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.check_file_dict(dic, b64=False)
|
|
assert unicode(exception.value) == (
|
|
u"Expecting '%s' value in JSON %s in file dict to be a %s (not a %s)" % (
|
|
'file.filename', title, 'string', type(dic['filename'])))
|
|
|
|
dic['content'] = {'a', 'filename'}
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.check_file_dict(dic, b64=False)
|
|
assert unicode(exception.value) == (
|
|
u"Expecting '%s' value in JSON %s in file dict to be a %s (not a %s)" % (
|
|
'file.content', title, 'string', type(dic['content'])))
|
|
|
|
del dic['content']
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.check_file_dict(dic, b64=False)
|
|
assert unicode(exception.value) == u"Expecting 'file.%s' key in JSON %s" % ('content', title)
|
|
|
|
del d64['b64_content']
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.check_file_dict(dic, b64=True)
|
|
assert unicode(exception.value) == (
|
|
u"Expecting 'file.%s' key in JSON %s" % ('b64_content', title))
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_get_first_file_from_payload(atreal_openads): # pylint: disable=invalid-name
|
|
"""Test the method AtrealOpenads.get_first_file_from_payload()."""
|
|
|
|
title = 'payload'
|
|
|
|
dic = {
|
|
'files': [{
|
|
'content': get_file_data(TEST_FILE_CERFA_DIA, b64=False),
|
|
'filename': os.path.basename(TEST_FILE_CERFA_DIA),
|
|
'content_type': 'application/pdf'
|
|
}]
|
|
}
|
|
|
|
assert atreal_openads.get_first_file_from_payload(
|
|
dic, title, ensure_content=True, b64=False) == dic['files'][0]
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_check_status(atreal_openads):
|
|
"""Test the method AtrealOpenads.check_status()."""
|
|
|
|
fake_resp_json = {
|
|
'message': 'Service online'
|
|
}
|
|
fake_resp = JsonResponse(fake_resp_json)
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = mock.Mock(content=fake_resp, status_code=200)
|
|
jresp = atreal_openads.check_status(None)
|
|
assert jresp['response'] == 200
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
# pylint: disable=too-many-statements,too-many-locals
|
|
def test_create_dossier(atreal_openads, collectivite_1, collectivite_1_guichet):
|
|
"""Test the method AtrealOpenads.create_dossier()."""
|
|
|
|
fake_req_json = {
|
|
"fields": {
|
|
|
|
# proprietaire
|
|
"proprietaire" : "Non",
|
|
"proprietaire_raw": "Non",
|
|
|
|
# mandataire
|
|
"mandataire_prenom" : "John",
|
|
"mandataire_nom" : "Man",
|
|
"mandataire_email" : "mandataire_email@domain.example",
|
|
|
|
"mandataire_qualite" : "Une personne morale",
|
|
"mandataire_qualite_raw" : "Une personne morale",
|
|
|
|
"mandataire_denomination" : "SELARL",
|
|
"mandataire_raison_sociale" : "Super Juriste",
|
|
|
|
"mandataire_numero_voie": "808",
|
|
"mandataire_nom_voie" : "Avenue de l'argent",
|
|
"mandataire_lieu_dit" : "geoisbour",
|
|
"mandataire_code_postal": "13004",
|
|
"mandataire_localite" : "Marseille",
|
|
|
|
# petitionnaire
|
|
"prenom": "Toto",
|
|
"nom" : "Loulou",
|
|
"email" : "petitionnaire_email@domain.example",
|
|
|
|
"qualite" : "Un particulier",
|
|
"qualite_raw": "Un particulier",
|
|
|
|
"numero_voie": "52",
|
|
"nom_voie" : "Avenue de la Blaque",
|
|
"lieu_dit" : "tierquar",
|
|
"code_postal": "13004",
|
|
"localite" : "Marseille",
|
|
|
|
# terrain
|
|
"terrain_numero_voie": "23",
|
|
"terrain_nom_voie" : "Boulevard de la République",
|
|
"terrain_lieu_dit" : "Leecorne",
|
|
"terrain_code_postal": "13002",
|
|
"terrain_localite" : "Marseille",
|
|
|
|
# références cadastrales
|
|
"reference_cadastrale" : [["999", "Z", "0010"]],
|
|
"autres_parcelles" : True,
|
|
"references_cadastrales": [["123", "D", "9874"]],
|
|
|
|
# user attached files
|
|
"cerfa": {
|
|
"content" : get_file_data(TEST_FILE_CERFA_DIA),
|
|
"content_type": "invalid/content type",
|
|
"field_id" : "50",
|
|
"filename" : os.path.basename(TEST_FILE_CERFA_DIA)
|
|
},
|
|
"plan_cadastral_1": {
|
|
"content" : get_file_data(TEST_FILE_PLAN_CADASTRAL),
|
|
"content_type": "application/pdf",
|
|
"filename" : os.path.basename(TEST_FILE_PLAN_CADASTRAL)
|
|
},
|
|
"plan_cadastral_2": {
|
|
"content" : get_file_data(TEST_FILE_PLAN_CADASTRAL),
|
|
"content_type": "application/pdf",
|
|
#"filename" : 'plan_cad'
|
|
},
|
|
"pouvoir_mandat": {
|
|
"content" : get_file_data(TEST_FILE_CERFA_DIA),
|
|
"content_type": "application/pdf",
|
|
"filename" : 'mandat'
|
|
}
|
|
}
|
|
}
|
|
req = HttpRequest()
|
|
req._body = json.dumps(fake_req_json) # pylint: disable=protected-access
|
|
req.path = '/test'
|
|
req.method = 'POST'
|
|
req.encoding = 'utf-8'
|
|
req.GET = QueryDict(mutable=True) # required because of encoding setter
|
|
req.POST = QueryDict(mutable=True) # required because of encoding setter
|
|
req.content_type = 'application/json'
|
|
req.content_params = None
|
|
req.COOKIES = {}
|
|
req.META = {}
|
|
req._read_started = False # pylint: disable=protected-access
|
|
|
|
fake_resp_bad = Response()
|
|
fake_resp_bad.status_code = 502
|
|
fake_resp_bad.reason = 'Bad gateway'
|
|
|
|
with pytest.raises(ValueError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp_bad
|
|
atreal_openads.create_dossier(req, 'DIA', collectivite='not an integer')
|
|
assert unicode(exception.value) == "invalid literal for int() with base 10: 'not an integer'"
|
|
|
|
# guichet is open from Monday/8:30 to Friday/12:15, between 9:00 and 17:00
|
|
now_open = datetime.datetime(2019, 8, 7, 14, 0, 0) # wednesday
|
|
now_closed = datetime.datetime(2019, 8, 10, 16, 0, 0) # saturday
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp_bad
|
|
atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now=now_open)
|
|
assert unicode(exception.value) == "HTTP error: 502"
|
|
|
|
# TODO update the code and return message when it will be
|
|
# correctly implemented in the openADS.API side.
|
|
fake_resp_404 = Response()
|
|
fake_resp_404.status_code = 404
|
|
fake_resp_404.reason = 'Page not found'
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp_404
|
|
atreal_openads.create_dossier(req, 'DIA', collectivite=999)
|
|
assert unicode(exception.value) == "HTTP error: 404"
|
|
|
|
jresp = atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now_closed)
|
|
assert jresp is not None
|
|
assert len(jresp) == 1
|
|
assert 'message' in jresp
|
|
assert jresp['message'] == u"Guichet closed for collectivite '%s'" % collectivite_1
|
|
|
|
now_str_fmt = '%Y-%m-%d %H:%M:%S'
|
|
now_closed_str = now_closed.strftime(now_str_fmt)
|
|
jresp = atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now_closed_str)
|
|
assert jresp is not None
|
|
assert len(jresp) == 1
|
|
assert 'message' in jresp
|
|
assert jresp['message'] == u"Guichet closed for collectivite '%s'" % collectivite_1
|
|
|
|
now_invalid = {'invalid': 'type'}
|
|
with pytest.raises(APIError) as exception:
|
|
jresp = atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now_invalid)
|
|
assert unicode(exception.value) == (
|
|
u"Invalid value of type '%s' for now argument of endpoint '%s' (must be: %s)" % (
|
|
type(now_invalid),
|
|
'create_dossier',
|
|
"datetime or string formatted to '%s'" % now_str_fmt))
|
|
|
|
fake_resp_json = {
|
|
'numero_dossier' : FAKE_NUMERO_DOSSIER,
|
|
'files': [{
|
|
'b64_content' : get_file_data(TEST_FILE_CERFA_DIA),
|
|
'content_type' : 'text/plain',
|
|
'filename' : 'recepisse_depot_%s.pdf' % FAKE_NUMERO_DOSSIER,
|
|
}]
|
|
}
|
|
fake_resp = Response()
|
|
fake_resp.status_code = 200
|
|
fake_resp.headers = {'Content-Type': 'application/json'}
|
|
fake_resp.encoding = 'utf-8'
|
|
fake_resp.reason = 'OK'
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
jresp = atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now=now_open)
|
|
assert jresp['numero_dossier'] == fake_resp_json['numero_dossier']
|
|
assert jresp['recepisse']['b64_content'] == fake_resp_json['files'][0]['b64_content']
|
|
assert jresp['recepisse']['content_type'] == 'application/pdf'
|
|
assert jresp['recepisse']['filename'] == fake_resp_json['files'][0]['filename']
|
|
|
|
fake_resp_json['numero_dossier'] = {'a':'invalid type'}
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now=now_open)
|
|
assert unicode(exception.value) == (
|
|
u"Expecting '%s' value in JSON response to be a %s (not a %s)" % (
|
|
'numero_dossier', 'string', type({})))
|
|
|
|
del fake_resp_json['numero_dossier']
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now=now_open)
|
|
assert unicode(exception.value) == u"Expecting 'numero_dossier' key in JSON response"
|
|
|
|
fake_resp_json['files'][0]['b64_content'] = 'invalid_;{[content}'
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now=now_open)
|
|
assert unicode(exception.value) == u'Failed to decode recepisse content from base 64'
|
|
|
|
fake_resp._content = 'df[{gfd;g#vfd' # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
atreal_openads.create_dossier(req, 'DIA', collectivite_1.openADS_id, now=now_open)
|
|
# pylint: disable=protected-access
|
|
assert unicode(exception.value) == u'No JSON content returned: %r' % fake_resp._content
|
|
|
|
job = Job.objects.filter(natural_id=FAKE_NUMERO_DOSSIER).last() # pylint: disable=no-member
|
|
assert job
|
|
job_id = job.id
|
|
assert job.status == 'registered'
|
|
assert job.method_name == 'upload_user_files'
|
|
assert job.natural_id == FAKE_NUMERO_DOSSIER
|
|
assert job.parameters is not None
|
|
assert len(job.parameters) == 4
|
|
assert 'file_ids' in job.parameters
|
|
assert len(job.parameters['file_ids']) == 4
|
|
file_ids = job.parameters['file_ids']
|
|
|
|
forwardfiles = ForwardFile.objects.filter(id__in=file_ids) # pylint: disable=no-member
|
|
for forwardfile in forwardfiles:
|
|
assert forwardfile.numero_demande
|
|
assert forwardfile.numero_dossier == FAKE_NUMERO_DOSSIER
|
|
assert forwardfile.file_hash
|
|
assert forwardfile.upload_status == 'pending'
|
|
|
|
fake_resp_json = "You want add some files on %s " % FAKE_NUMERO_DOSSIER
|
|
fake_resp = Response()
|
|
fake_resp.status_code = 200
|
|
fake_resp.headers = {'Content-Type': 'application/json'}
|
|
fake_resp.encoding = 'utf-8'
|
|
fake_resp.reason = 'OK'
|
|
fake_resp._content = json.dumps(fake_resp_json)
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
atreal_openads.jobs()
|
|
|
|
job = Job.objects.get(id=job_id) # pylint: disable=no-member
|
|
assert job.status == 'completed'
|
|
|
|
forwardfiles = ForwardFile.objects.filter(id__in=file_ids) # pylint: disable=no-member
|
|
for forwardfile in forwardfiles:
|
|
assert forwardfile.upload_status == 'success'
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_get_dossier(atreal_openads):
|
|
"""Test the method AtrealOpenads.get_dossier()."""
|
|
|
|
fake_resp_bad = Response()
|
|
fake_resp_bad.status_code = 502
|
|
fake_resp_bad.reason = 'Bad gateway'
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp_bad
|
|
atreal_openads.get_dossier(None, 'DIA', FAKE_NUMERO_DOSSIER)
|
|
assert unicode(exception.value) == "HTTP error: 502"
|
|
|
|
fake_resp_json = {
|
|
'etat' : u"Non préemption en cours",
|
|
'date_depot' : "24/04/2019",
|
|
'date_decision' : "",
|
|
'decision' : "",
|
|
'date_limite_instruction': "24/06/2019"
|
|
}
|
|
fake_resp = Response()
|
|
fake_resp.status_code = 200
|
|
fake_resp.headers = {'Content-Type': 'application/json'}
|
|
fake_resp.encoding = 'utf-8'
|
|
fake_resp.reason = 'OK'
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp
|
|
jresp = atreal_openads.get_dossier(None, 'DIA', FAKE_NUMERO_DOSSIER)
|
|
assert jresp['etat'] == fake_resp_json['etat']
|
|
assert jresp['date_depot'] == fake_resp_json['date_depot']
|
|
assert jresp['date_decision'] == fake_resp_json['date_decision']
|
|
assert jresp['decision'] == fake_resp_json['decision']
|
|
assert jresp['date_limite_instruction'] == fake_resp_json['date_limite_instruction']
|
|
|
|
fake_resp._content = 'df[{gfd;g#vfd' # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp
|
|
atreal_openads.get_dossier(None, 'DIA', FAKE_NUMERO_DOSSIER)
|
|
# pylint: disable=protected-access
|
|
assert unicode(exception.value) == u'No JSON content returned: %r' % fake_resp._content
|
|
|
|
fake_resp_json = {
|
|
'errors' : [{
|
|
'location' : 'path',
|
|
'name' : 'Invalid Type',
|
|
'description' : '"invalid_type" is not one of DIA, PC, DP, AT, PD'
|
|
}]
|
|
}
|
|
fake_resp.status_code = 404
|
|
fake_resp.reason = 'Resource not found'
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp
|
|
atreal_openads.get_dossier(None, 'invalid_type', FAKE_NUMERO_DOSSIER)
|
|
assert unicode(exception.value) == (u"HTTP error: 404, [path] (Invalid Type) "
|
|
"\"invalid_type\" is not one of DIA, PC, DP, AT, PD")
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_get_fwd_files(atreal_openads):
|
|
"""Test the method AtrealOpenads.get_fwd_files()."""
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
atreal_openads.get_fwd_files(None, FAKE_NUMERO_DOSSIER, fichier_id='not an integer')
|
|
assert unicode(exception.value) == u"fichier_id must be an integer"
|
|
|
|
with pytest.raises(Http404) as exception:
|
|
atreal_openads.get_fwd_files(None, FAKE_NUMERO_DOSSIER, fichier_id=18)
|
|
assert unicode(exception.value) == (
|
|
u"No file matches 'numero_dossier=%s' and 'id=%s'." % (FAKE_NUMERO_DOSSIER, 18))
|
|
|
|
resp_empty = atreal_openads.get_fwd_files(None, FAKE_NUMERO_DOSSIER, fichier_id=None)
|
|
assert resp_empty is not None
|
|
assert not resp_empty
|
|
|
|
forwardfile = build_forwardfile_from_path(atreal_openads,
|
|
TEST_FILE_CERFA_DIA,
|
|
FAKE_NUMERO_DOSSIER,
|
|
'cerfa')
|
|
forwardfile.save()
|
|
assert isinstance(forwardfile, ForwardFile)
|
|
|
|
jresp = atreal_openads.get_fwd_files(None, FAKE_NUMERO_DOSSIER, fichier_id=None)
|
|
assert jresp is not None
|
|
assert len(jresp) == 1
|
|
assert jresp[0]['id'] == forwardfile.id # pylint: disable=no-member
|
|
for attr in ['numero_dossier', 'type_fichier', 'file_hash', 'orig_filename', 'content_type',
|
|
'upload_status', 'upload_msg']:
|
|
assert jresp[0][attr] == getattr(forwardfile, attr)
|
|
# pylint: disable=no-member
|
|
assert jresp[0]['content_size'] == len(get_file_data(forwardfile.upload_file.path, b64=False))
|
|
assert jresp[0]['last_update_datetime'] == forwardfile.last_update_datetime
|
|
|
|
# pylint: disable=no-member
|
|
jresp = atreal_openads.get_fwd_files(None, FAKE_NUMERO_DOSSIER, fichier_id=forwardfile.id)
|
|
assert jresp is not None
|
|
assert len(jresp) == 1
|
|
assert jresp[0]['id'] == forwardfile.id # pylint: disable=no-member
|
|
for attr in ['numero_dossier', 'type_fichier', 'file_hash', 'orig_filename', 'content_type',
|
|
'upload_status', 'upload_msg']:
|
|
assert jresp[0][attr] == getattr(forwardfile, attr)
|
|
assert jresp[0]['content_size'] == len(get_file_data(forwardfile.upload_file.path, b64=False))
|
|
assert jresp[0]['last_update_datetime'] == forwardfile.last_update_datetime
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_get_fwd_files_status(atreal_openads):
|
|
"""Test the method AtrealOpenads.get_fwd_files_status()."""
|
|
|
|
with pytest.raises(Http404) as exception:
|
|
atreal_openads.get_fwd_files_status(None, FAKE_NUMERO_DOSSIER, fichier_id=18)
|
|
assert re.search(r"^No file matches 'numero_dossier=[^']+' and 'id=[^']+'.$",
|
|
str(exception.value))
|
|
|
|
forwardfile = build_forwardfile_from_path(atreal_openads,
|
|
TEST_FILE_CERFA_DIA,
|
|
FAKE_NUMERO_DOSSIER,
|
|
'cerfa')
|
|
forwardfile.save()
|
|
assert isinstance(forwardfile, ForwardFile)
|
|
|
|
jresp = atreal_openads.get_fwd_files_status(None, FAKE_NUMERO_DOSSIER, fichier_id=None)
|
|
assert jresp is not None
|
|
assert not jresp['all_forwarded']
|
|
# pylint: disable=no-member
|
|
status_msg = '[%s] %s => %s' % (forwardfile.id, forwardfile.orig_filename,
|
|
forwardfile.upload_msg)
|
|
assert len(jresp['pending']) == 1
|
|
assert jresp['pending'][0] == status_msg
|
|
assert not jresp['uploading']
|
|
assert not jresp['success']
|
|
assert not jresp['failed']
|
|
|
|
# pylint: disable=no-member
|
|
jresp = atreal_openads.get_fwd_files_status(None, FAKE_NUMERO_DOSSIER,
|
|
fichier_id=forwardfile.id)
|
|
assert jresp is not None
|
|
assert not jresp['all_forwarded']
|
|
# pylint: disable=no-member
|
|
status_msg = '[%s] %s => %s' % (forwardfile.id, forwardfile.orig_filename,
|
|
forwardfile.upload_msg)
|
|
assert len(jresp['pending']) == 1
|
|
assert jresp['pending'][0] == status_msg
|
|
assert not jresp['uploading']
|
|
assert not jresp['success']
|
|
assert not jresp['failed']
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_get_courrier(atreal_openads):
|
|
"""Test the method AtrealOpenads.get_courrier()."""
|
|
|
|
lettre_type = 'dia_renonciation_preempter'
|
|
|
|
fake_resp_bad = Response()
|
|
fake_resp_bad.status_code = 502
|
|
fake_resp_bad.reason = 'Bad gateway'
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp_bad
|
|
atreal_openads.get_courrier(None, 'DIA', FAKE_NUMERO_DOSSIER, lettre_type)
|
|
assert unicode(exception.value) == "HTTP error: 502"
|
|
|
|
fake_resp_json = {
|
|
'files': [{
|
|
'filename' : "instruction_4.pdf",
|
|
'content_type' : "text/plain",
|
|
'b64_content' : get_file_data(TEST_FILE_CERFA_DIA)
|
|
}]
|
|
}
|
|
fake_resp = Response()
|
|
fake_resp.status_code = 200
|
|
fake_resp.headers = {'Content-Type': 'application/json'}
|
|
fake_resp.encoding = 'utf-8'
|
|
fake_resp.reason = 'OK'
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp
|
|
jresp = atreal_openads.get_courrier(None, 'DIA', FAKE_NUMERO_DOSSIER, lettre_type)
|
|
assert jresp['courrier']['filename'] == fake_resp_json['files'][0]['filename']
|
|
assert jresp['courrier']['content_type'] == fake_resp_json['files'][0]['content_type']
|
|
assert jresp['courrier']['b64_content'] == fake_resp_json['files'][0]['b64_content']
|
|
|
|
fake_resp_json['files'][0]['b64_content'] = 'invalid_;{[content}'
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp
|
|
atreal_openads.get_courrier(None, 'DIA', FAKE_NUMERO_DOSSIER, lettre_type)
|
|
assert unicode(exception.value) == u'Failed to decode courrier content from base 64'
|
|
|
|
fake_resp._content = 'df[{gfd;g#vfd' # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
|
requests_get.return_value = fake_resp
|
|
atreal_openads.get_courrier(None, 'DIA', FAKE_NUMERO_DOSSIER, lettre_type)
|
|
# pylint: disable=protected-access
|
|
assert unicode(exception.value) == u'No JSON content returned: %r' % fake_resp._content
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
def test_get_response_error(atreal_openads):
|
|
"""Test the method AtrealOpenads.get_response_error()."""
|
|
|
|
fake_resp_json = {
|
|
'errors': [
|
|
{
|
|
'location' : 'entity.name',
|
|
'name' : 'constraint',
|
|
'description': 'Must start with an uppercase letter'
|
|
}
|
|
]
|
|
}
|
|
fake_resp = Response()
|
|
fake_resp.status_code = 404
|
|
fake_resp.headers = {'Content-Type': 'application/json'}
|
|
fake_resp.encoding = 'utf-8'
|
|
fake_resp.reason = 'Not Found'
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
|
|
error_msg = atreal_openads.get_response_error(fake_resp)
|
|
expected_msg = u'[%s] (%s) %s' % (
|
|
fake_resp_json['errors'][0]['location'],
|
|
fake_resp_json['errors'][0]['name'],
|
|
fake_resp_json['errors'][0]['description']
|
|
)
|
|
assert error_msg == u"HTTP error: %s, %s" % (fake_resp.status_code, ','.join([expected_msg]))
|
|
|
|
fake_resp._content = 'invalid_;{[content}' # pylint: disable=protected-access
|
|
error_msg = atreal_openads.get_response_error(fake_resp)
|
|
# pylint: disable=protected-access
|
|
assert error_msg == u"HTTP error: %s, %s" % (fake_resp.status_code, fake_resp._content)
|
|
|
|
|
|
# pylint: disable=unused-argument,redefined-outer-name
|
|
# pylint: disable=too-many-statements
|
|
def test_upload_user_files(atreal_openads):
|
|
"""Test the method AtrealOpenads.upload_user_files()."""
|
|
|
|
req = HttpRequest()
|
|
req._body = '' # pylint: disable=protected-access
|
|
req.path = '/upload_user_files'
|
|
req.method = 'GET'
|
|
req.encoding = 'utf-8'
|
|
req.GET = QueryDict(mutable=True) # required because of encoding setter
|
|
req.POST = QueryDict(mutable=True) # required because of encoding setter
|
|
req.content_type = 'application/json'
|
|
req.content_params = None
|
|
req.COOKIES = {}
|
|
req.META = {}
|
|
req._read_started = False # pylint: disable=protected-access
|
|
|
|
with pytest.raises(ForwardFile.DoesNotExist) as exception: # pylint: disable=no-member
|
|
atreal_openads.upload_user_files(req, 'DIA', FAKE_NUMERO_DOSSIER, file_ids=[999])
|
|
assert unicode(exception.value) == u"The following ForwardFile IDs were not found: %s." % [999]
|
|
|
|
with pytest.raises(ValueError) as exception:
|
|
atreal_openads.upload_user_files(req, 'DIA', FAKE_NUMERO_DOSSIER,
|
|
file_ids='invalid string')
|
|
assert unicode(exception.value) == (
|
|
u"invalid literal for int() with base 10: '%s'" % 'invalid string')
|
|
|
|
with pytest.raises(TypeError) as exception:
|
|
atreal_openads.upload_user_files(req, 'DIA', FAKE_NUMERO_DOSSIER,
|
|
file_ids={'invalid':'type'})
|
|
assert unicode(exception.value) == (
|
|
u"Invalid 'file_ids' argument type '%s' (must be string or list)" % (
|
|
type({'invalid':'type'})))
|
|
|
|
forwardfile = build_forwardfile_from_path(atreal_openads,
|
|
TEST_FILE_CERFA_DIA,
|
|
FAKE_NUMERO_DOSSIER,
|
|
'cerfa')
|
|
forwardfile.save()
|
|
assert isinstance(forwardfile, ForwardFile)
|
|
assert forwardfile.upload_status == 'pending'
|
|
file_id = forwardfile.id # pylint: disable=no-member
|
|
assert file_id
|
|
|
|
fake_resp_bad = Response()
|
|
fake_resp_bad.status_code = 502
|
|
fake_resp_bad.reason = 'Bad gateway'
|
|
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp_bad
|
|
atreal_openads.upload_user_files(req, 'DIA', FAKE_NUMERO_DOSSIER,
|
|
file_ids=str(file_id))
|
|
assert unicode(exception.value) == u'HTTP error: 502'
|
|
|
|
ffup = ForwardFile.objects.get(id=file_id) # pylint: disable=no-member
|
|
assert isinstance(ffup, ForwardFile)
|
|
for k in ['numero_dossier', 'type_fichier', 'file_hash', 'orig_filename', 'content_type']:
|
|
assert getattr(ffup, k) == getattr(forwardfile, k)
|
|
assert ffup.upload_attempt == 1
|
|
assert ffup.upload_status == 'failed'
|
|
assert ffup.upload_msg == "HTTP error: 502"
|
|
|
|
ffup.upload_status = 'pending'
|
|
ffup.save()
|
|
|
|
fake_resp = Response()
|
|
fake_resp.status_code = 200
|
|
fake_resp.headers = {'Content-Type': 'application/json'}
|
|
fake_resp.encoding = 'utf-8'
|
|
fake_resp.reason = 'OK'
|
|
|
|
fake_resp._content = 'invalid_;{[content}' # pylint: disable=protected-access
|
|
with pytest.raises(APIError) as exception:
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
atreal_openads.upload_user_files(req, 'DIA', FAKE_NUMERO_DOSSIER, file_ids=[file_id])
|
|
assert unicode(exception.value) == u'No JSON content returned: %r' % fake_resp.content
|
|
|
|
ffup = ForwardFile.objects.get(id=file_id) # pylint: disable=no-member
|
|
assert isinstance(ffup, ForwardFile)
|
|
for k in ['numero_dossier', 'type_fichier', 'file_hash', 'orig_filename', 'content_type']:
|
|
assert getattr(ffup, k) == getattr(forwardfile, k)
|
|
assert ffup.upload_attempt == 2
|
|
assert ffup.upload_status == 'failed'
|
|
# pylint: disable=protected-access
|
|
assert ffup.upload_msg == u'No JSON content returned: %r' % fake_resp._content
|
|
|
|
jresp = atreal_openads.upload_user_files(req, 'DIA', FAKE_NUMERO_DOSSIER)
|
|
assert jresp == {'message': 'no file to transfer'}
|
|
|
|
ffup = ForwardFile.objects.get(id=file_id) # pylint: disable=no-member
|
|
ffup.upload_status = 'pending'
|
|
ffup.save()
|
|
|
|
fake_resp_json = "You want add some files on %s " % FAKE_NUMERO_DOSSIER
|
|
fake_resp._content = json.dumps(fake_resp_json) # pylint: disable=protected-access
|
|
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
|
requests_post.return_value = fake_resp
|
|
jresp = atreal_openads.upload_user_files(req, 'DIA', FAKE_NUMERO_DOSSIER)
|
|
assert jresp == {'message': 'all files transfered successfully'}
|
|
|
|
ffup = ForwardFile.objects.get(id=file_id) # pylint: disable=no-member
|
|
assert isinstance(ffup, ForwardFile)
|
|
for k in ['numero_dossier', 'type_fichier', 'file_hash', 'orig_filename', 'content_type']:
|
|
assert getattr(ffup, k) == getattr(forwardfile, k)
|
|
assert ffup.upload_attempt == 3
|
|
assert ffup.upload_status == 'success'
|
|
assert ffup.upload_msg == 'uploaded successfuly'
|