litteralis: receive and forward file as an attachment (#89739)
gitea/passerelle/pipeline/head This commit looks good Details

This commit is contained in:
Emmanuel Cazenave 2024-04-18 16:14:58 +02:00
parent c10b2327dd
commit c25a371c10
2 changed files with 17 additions and 11 deletions

View File

@ -21,6 +21,7 @@ import urllib
import requests
from django.db import models
from django.http.response import HttpResponse
from django.utils import dateparse
from django.utils.timezone import is_naive, make_aware
from django.utils.translation import gettext_lazy as _
@ -81,7 +82,7 @@ class Litteralis(BaseResource, HTTPResource):
except (json.JSONDecodeError, requests.exceptions.JSONDecodeError) as e:
raise APIError(str(e))
return resp.text
return resp
def _upload(self, url, post_data):
try:
@ -97,7 +98,7 @@ class Litteralis(BaseResource, HTTPResource):
url,
method='post',
files=files,
)
).text
}
@endpoint(
@ -210,8 +211,7 @@ class Litteralis(BaseResource, HTTPResource):
},
)
def demandes_recues_arrete(self, request, id_demande):
return {
'data': self._call(
'demandes-recues/%s/arrete' % id_demande,
)
}
resp = self._call('demandes-recues/%s/arrete' % id_demande)
response = HttpResponse(resp.content, content_type=resp.headers['Content-Type'])
response['Content-Disposition'] = resp.headers['Content-Disposition']
return response

View File

@ -216,12 +216,18 @@ def test_demandes_recues_reponses(app, connector):
def test_demandes_recues_arrete(app, connector):
with responses.RequestsMock() as rsps:
rsps.get('http://litteralis.invalid/demandes-recues/1234/arrete', status=200, json={'foo': 'bar'})
headers = {'Content-Type': 'text/plain', 'Content-Disposition': 'attachment; filename=foo.txt'}
rsps.get(
'http://litteralis.invalid/demandes-recues/1234/arrete',
status=200,
body='who hwat',
headers=headers,
)
resp = app.get('/litteralis/slug-litteralis/demandes-recues-arrete?id_demande=1234')
assert len(rsps.calls) == 1
json_resp = resp.json
assert json_resp['err'] == 0
assert json_resp['data'] == {'foo': 'bar'}
assert resp.text == 'who hwat'
assert resp.headers['Content-Type'] == 'text/plain'
assert resp.headers['Content-Disposition'] == 'attachment; filename=foo.txt'
def test_demandes_recues_arrete_error_with_json(app, connector):