Skip to content

Commit

Permalink
Added check and response for non-text input
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzbrand committed Jul 12, 2023
1 parent 0f78d27 commit 159f029
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 4 additions & 3 deletions aaq/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ def test_get_first_page_view_gibberish_input(self):
}

@responses.activate
def test_get_first_page_view_blank(self):
def test_get_first_page_view_non_text(self):
"""
Check that we get an error message if a blank question is submitted
This happens when a voicenote or document is sent, or if an image is
sent without a text caption
"""
user = get_user_model().objects.create_user("test")
self.client.force_authenticate(user)
Expand All @@ -120,13 +122,12 @@ def test_get_first_page_view_blank(self):
)

payload = json.dumps({"question": ""})
# test string value, bad question, empty str, emoji, int

response = self.client.post(
self.url, data=payload, content_type="application/json"
)

assert response.json() == {"question": ["This field may not be blank."]}
assert response.json() == {"message": "Non-text Input Detected"}


class AddFeedbackViewTests(APITestCase):
Expand Down
8 changes: 7 additions & 1 deletion aaq/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
@api_view(("POST",))
@renderer_classes((JSONRenderer,))
def get_first_page(request, *args, **kwargs):
print(request.data)
if request.data == {"question": ""}:
json_msg = {
"message": "Non-text Input Detected",
}
return Response(json_msg, status=status.HTTP_202_ACCEPTED)

serializer = InboundCheckSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

question = serializer.validated_data["question"]
url = urllib.parse.urljoin(settings.AAQ_CORE_API_URL, "/inbound/check")
payload = {"text_to_match": f"{question}"}
Expand Down

0 comments on commit 159f029

Please sign in to comment.