0001-litteralis-add-annexes-endpoint-69970.patch
passerelle/apps/litteralis/models.py | ||
---|---|---|
83 | 83 | |
84 | 84 |
return resp.text |
85 | 85 | |
86 |
def _upload(self, url, post_data): |
|
87 |
try: |
|
88 |
file_byte_content = base64.b64decode(post_data['file']['content']) |
|
89 |
except (TypeError, binascii.Error): |
|
90 |
raise APIError("Can't decode file") |
|
91 | ||
92 |
files = { |
|
93 |
'file': (post_data['file']['filename'], file_byte_content, post_data['file']['content_type']) |
|
94 |
} |
|
95 |
return { |
|
96 |
'data': self._call( |
|
97 |
url, |
|
98 |
method='post', |
|
99 |
files=files, |
|
100 |
) |
|
101 |
} |
|
102 | ||
86 | 103 |
@endpoint( |
87 | 104 |
name='demandes-recues', |
88 | 105 |
description=_('Create submission'), |
... | ... | |
129 | 146 | |
130 | 147 |
@endpoint( |
131 | 148 |
name='upload', |
132 |
description=_('Upload file'), |
|
149 |
description=_('Upload summary file'),
|
|
133 | 150 |
perm='can_access', |
134 | 151 |
post={ |
135 | 152 |
'request_body': { |
136 | 153 |
'schema': { |
137 |
'application/json': schemas.UPLOAD, |
|
154 |
'application/json': schemas.UPLOAD_ANNEXES,
|
|
138 | 155 |
} |
139 | 156 |
} |
140 | 157 |
}, |
141 | 158 |
) |
142 | 159 |
def upload(self, request, post_data): |
143 |
try: |
|
144 |
file_byte_content = base64.b64decode(post_data['file']['content']) |
|
145 |
except (TypeError, binascii.Error): |
|
146 |
raise APIError("Can't decode file") |
|
160 |
url = 'demandes-recues/%s/upload' % post_data['id_demande'] |
|
161 |
return self._upload(url, post_data) |
|
147 | 162 | |
148 |
files = { |
|
149 |
'file': (post_data['file']['filename'], file_byte_content, post_data['file']['content_type']) |
|
150 |
} |
|
151 |
return { |
|
152 |
'data': self._call( |
|
153 |
'demandes-recues/%s/upload' % post_data['id_demande'], |
|
154 |
method='post', |
|
155 |
files=files, |
|
156 |
) |
|
157 |
} |
|
163 |
@endpoint( |
|
164 |
name='annexes', |
|
165 |
description=_('Upload appendix file'), |
|
166 |
perm='can_access', |
|
167 |
post={ |
|
168 |
'request_body': { |
|
169 |
'schema': { |
|
170 |
'application/json': schemas.UPLOAD_ANNEXES, |
|
171 |
} |
|
172 |
} |
|
173 |
}, |
|
174 |
) |
|
175 |
def annexes(self, request, post_data): |
|
176 |
url = 'demandes-recues/%s/annexes' % post_data['id_demande'] |
|
177 |
return self._upload(url, post_data) |
|
158 | 178 | |
159 | 179 |
@endpoint( |
160 | 180 |
methods=['get'], |
passerelle/apps/litteralis/schemas.py | ||
---|---|---|
150 | 150 |
'unflatten': True, |
151 | 151 |
} |
152 | 152 | |
153 |
UPLOAD = { |
|
153 |
UPLOAD_ANNEXES = {
|
|
154 | 154 |
'$schema': 'http://json-schema.org/draft-04/schema#', |
155 | 155 |
'type': 'object', |
156 | 156 |
'additionalProperties': False, |
tests/test_litteralis.py | ||
---|---|---|
84 | 84 |
assert json_resp['data'] == '' |
85 | 85 | |
86 | 86 | |
87 |
def test_annexes(app, connector): |
|
88 |
params = { |
|
89 |
'file': { |
|
90 |
'filename': 'bla', |
|
91 |
'content': base64.b64encode(b'who what').decode(), |
|
92 |
'content_type': 'text/plain', |
|
93 |
}, |
|
94 |
'id_demande': '1234', |
|
95 |
} |
|
96 |
with responses.RequestsMock() as rsps: |
|
97 |
rsps.post('http://litteralis.invalid/demandes-recues/1234/annexes', status=200, body='') |
|
98 |
resp = app.post_json('/litteralis/slug-litteralis/annexes', params=params) |
|
99 |
assert len(rsps.calls) == 1 |
|
100 |
assert rsps.calls[0].request.headers['Content-Type'].startswith('multipart/form-data') |
|
101 |
json_resp = resp.json |
|
102 |
assert json_resp['err'] == 0 |
|
103 |
assert json_resp['data'] == '' |
|
104 | ||
105 | ||
87 | 106 |
def test_demandes_recues_reponses(app, connector): |
88 | 107 |
with responses.RequestsMock() as rsps: |
89 | 108 |
rsps.get('http://litteralis.invalid/demandes-recues/1234/reponses', status=200, json={'foo': 'bar'}) |
90 |
- |