Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing captchetat responses #392

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Upgrade vue dependency [#386](https://github.com/etalab/udata-front/pull/386)
- Add codes and optgroups in Multiselect to display Insee codes and Licence groups [#347] (https://github.com/etalab/udata-front/pull/347)
- Order Organization's reuses by publishing date [#390](https://github.com/etalab/udata-front/pull/390)
- Fix failing captchEtat responses [#392](https://github.com/etalab/udata-front/pull/392)

## 3.5.4 (2024-03-20)

Expand Down
13 changes: 4 additions & 9 deletions udata_front/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import current_app, json, make_response, abort
from flask import current_app, make_response, abort

import logging
import requests
Expand Down Expand Up @@ -72,11 +72,6 @@ def get(self):
except requests.exceptions.RequestException:
abort(500, description='Catptcha internal error')

if args['get'] in ['image', 'sound']:
resp = make_response(bytes(req.content))
resp.headers['Content-Type'] = 'image/*' if args['get'] == 'image' else 'audio/x-wav'
return resp
if args['get'] == "p":
return json.loads(req.content)

return req.content
resp = make_response(bytes(req.content))
resp.headers['Content-Type'] = req.headers.get('Content-Type')
return resp
15 changes: 11 additions & 4 deletions udata_front/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ def test_capchetat_get_html(self, rmock, style):
rmock.post(self.oauth_token_url(), json={"access_token": "some_token", "expires_in": 3600})
rmock.get(
self.captchetat_url() + "?get=html&c=" + style,
text=f"some HTML with {style} and {self.captcha_id}"
text=f"some HTML with {style} and {self.captcha_id}",
headers={"Content-Type": "text/html;charset=utf-8"}
)
response = self.get(url_for('apiv2.captchetat', get='html', c=style))
self.assert200(response)
assert response.content_type == "text/html;charset=utf-8"
snippet = response.data.decode('utf8')
assert style in snippet
assert self.captcha_id in snippet
Expand All @@ -65,9 +67,11 @@ def test_capchetat_get_image(self, rmock, style):
rmock.post(self.oauth_token_url(), json={"access_token": "some_token", "expires_in": 3600})
content = bytes("some string", 'UTF-8')
rmock.get(f"{self.captchetat_url()}?get=image&c={style}&t={self.captcha_id}",
content=content)
content=content,
headers={"Content-Type": "image/png"})
response = self.get(url_for('apiv2.captchetat', get='image', c=style, t=self.captcha_id))
self.assert200(response)
assert response.content_type == "image/png"
assert content in bytes(response.data)

@pytest.mark.options(CAPTCHETAT_OAUTH_BASE_URL=oauth_url)
Expand All @@ -78,7 +82,8 @@ def test_capchetat_get_sound(self, rmock, style):
rmock.post(self.oauth_token_url(), json={"access_token": "some_token", "expires_in": 3600})
content = bytes(10)
rmock.get(f"{self.captchetat_url()}?get=sound&c={style}&t={self.captcha_id}",
content=content)
content=content,
headers={"Content-Type": "audio/x-wav"})
response = self.get(
url_for('apiv2.captchetat', get='sound', c=style, t=self.captcha_id),
content_type="audio/*"
Expand All @@ -93,7 +98,9 @@ def test_capchetat_get_additional_data(self, rmock, style):
'''It should return json additional data from the service.'''
rmock.post(self.oauth_token_url(), json={"access_token": "some_token", "expires_in": 3600})
json = {"hs": "some_id", "sp": "another_id"}
rmock.get(f"{self.captchetat_url()}?get=p&c={style}&t={self.captcha_id}", json=json)
rmock.get(f"{self.captchetat_url()}?get=p&c={style}&t={self.captcha_id}",
json=json,
headers={"Content-Type": "application/json"})
response = self.get(url_for('apiv2.captchetat', get='p', c=style, t=self.captcha_id))
self.assert200(response)
assert response.content_type == "application/json"
Expand Down