Skip to content

Commit

Permalink
Remove markup from message flashing (#946)
Browse files Browse the repository at this point in the history
* WIP

* Revert "WIP"

This reverts commit ea03cbc.

* WIP 2

* WIP 3

* WIP 4

* restore notification messages

* fix tests

* auto patch increment

* add latest message

* small changes based on comments

* do other inboxes

* fix formatting

* fix formatting 2

* WIP unit test

* cover line in unit tests

* cleanup

* fix vulnerability

* more cleanup

* add test for thread id

* cleanup unit test

* re-do unit test

* black reformat

---------

Co-authored-by: ras-rm-pr-bot <[email protected]>
  • Loading branch information
anwilkie and ras-rm-pr-bot authored Jul 5, 2024
1 parent e2b6320 commit de65ae5
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 55 deletions.
18 changes: 9 additions & 9 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions _infra/helm/response-operations-ui/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version
version: 3.1.77
version: 3.1.78

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 3.1.77
appVersion: 3.1.78
6 changes: 5 additions & 1 deletion response_operations_ui/templates/messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
})
%}
{% for message in messages %}
<p id="flashed-message-{{ loop.index }}">{{ message }}</p>
<p id="flashed-message-{{ loop.index }}">{{ message }}
{% if thread_url %}
<a href={{thread_url}}>View conversation</a>
{% endif %}
</p>
{% endfor %}
{% endcall %}
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
})
%}
{% for message in messages %}
<p id="flashed-message-{{ loop.index }}">{{ message }}</p>
<p id="flashed-message-{{ loop.index }}">{{ message }}
{% if thread_url %}
<a href={{thread_url}}>View conversation</a>
{% endif %}
</p>
{% endfor %}
{% endcall %}
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
})
%}
{% for message in messages %}
<p id="flashed-message-{{ loop.index }}">{{ message }}</p>
<p id="flashed-message-{{ loop.index }}">{{ message }}
{% if thread_url %}
<a href={{thread_url}}>View conversation</a>
{% endif %}
</p>
{% endfor %}
{% endcall %}
{% endif %}
Expand Down
97 changes: 56 additions & 41 deletions response_operations_ui/views/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
url_for,
)
from flask_login import current_user, login_required
from markupsafe import Markup
from structlog import wrap_logger

from config import FDI_LIST, VACANCIES_LIST
Expand Down Expand Up @@ -91,25 +90,15 @@ def view_conversation(thread_id):
verify_permission("messages.edit")
payload = {"is_closed": False}
message_controllers.patch_thread(thread_id, payload)
thread_url = (
url_for(
"messages_bp.view_conversation",
thread_id=thread_id,
conversation_tab=conversation_tab,
page=page,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
)
+ "#latest-message"
)
flash(Markup(f"Conversation re-opened. <a href={thread_url}>View conversation</a>"))
flash("Conversation re-opened.")
return redirect(
url_for(
"messages_bp.view_select_survey",
conversation_tab=conversation_tab,
page=page,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
thread_id=thread_id,
)
)

Expand Down Expand Up @@ -143,25 +132,15 @@ def view_conversation(thread_id):
)
else:
message_controllers.send_message(_get_message_json(form, thread_id=refined_thread[0]["thread_id"]))
thread_url = (
url_for(
"messages_bp.view_conversation",
thread_id=thread_id,
page=page,
conversation_tab=conversation_tab,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
)
+ "#latest-message"
)
flash(Markup(f"Message sent. <a href={thread_url}>View Message</a>"))
flash("Message sent.")
return redirect(
url_for(
"messages_bp.view_select_survey",
page=page,
conversation_tab=conversation_tab,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
thread_id=thread_id,
)
)

Expand Down Expand Up @@ -357,21 +336,25 @@ def mark_message_unread(message_id):
conversation_tab = request.args.get("conversation_tab")
ru_ref_filter = request.args.get("ru_ref_filter")
business_id_filter = request.args.get("business_id_filter")
thread_id = request.args.get("thread_id")
flash(f"Message from {msg_from} to {msg_to} marked unread")
message_controllers.add_unread_label(message_id)

return _view_select_survey(conversation_tab, ru_ref_filter, business_id_filter)
return _view_select_survey(conversation_tab, ru_ref_filter, business_id_filter, thread_id)


@messages_bp.route("/", methods=["GET"])
@login_required
def view_select_survey():
return _view_select_survey(
request.args.get("conversation_tab"), request.args.get("ru_ref_filter"), request.args.get("business_id_filter")
request.args.get("conversation_tab"),
request.args.get("ru_ref_filter"),
request.args.get("business_id_filter"),
request.args.get("thread_id"),
)


def _view_select_survey(conversation_tab, ru_ref_filter, business_id_filter):
def _view_select_survey(conversation_tab, ru_ref_filter, business_id_filter, thread_id):
"""
Redirects to either a survey stored in the session under the 'messages_survey_selection'
key or to the survey selection screen if the key isn't present in the session
Expand All @@ -389,6 +372,7 @@ def _view_select_survey(conversation_tab, ru_ref_filter, business_id_filter):
conversation_tab=conversation_tab,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
thread_id=thread_id,
)
)
elif selected_survey == "misc":
Expand All @@ -399,6 +383,7 @@ def _view_select_survey(conversation_tab, ru_ref_filter, business_id_filter):
conversation_tab=conversation_tab,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
thread_id=thread_id,
)
)
else:
Expand All @@ -410,6 +395,7 @@ def _view_select_survey(conversation_tab, ru_ref_filter, business_id_filter):
conversation_tab=conversation_tab,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
thread_id=thread_id,
)
)

Expand Down Expand Up @@ -461,11 +447,13 @@ def clear_filter(selected_survey):
def view_technical_inbox(): # noqa: C901
session["messages_survey_selection"] = "technical"
breadcrumbs = [{"text": "Technical" + " Messages"}]
thread_id = request.args.get("thread_id")
return _process_non_survey_category_page(
render_html="secure-message/technical-inbox.html",
redirect_url="messages_bp.view_technical_inbox",
breadcrumbs=breadcrumbs,
category="TECHNICAL",
thread_id=thread_id,
)


Expand All @@ -480,6 +468,7 @@ def view_selected_survey(selected_survey): # noqa: C901
limit = request.args.get("limit", default=10, type=int)
conversation_tab = request.args.get("conversation_tab", default="open")
ru_ref_filter = request.args.get("ru_ref_filter", default="")
thread_id = request.args.get("thread_id")
business_id_filter = request.args.get("business_id_filter", default="")
category = "SURVEY"

Expand Down Expand Up @@ -532,6 +521,21 @@ def view_selected_survey(selected_survey): # noqa: C901

pagination = pagination_processor(tab_counts["current"], limit, page, href)

if thread_id:
thread_url = (
url_for(
"messages_bp.view_conversation",
thread_id=thread_id,
conversation_tab=conversation_tab,
page=page,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
)
+ "#latest-message"
)
else:
thread_url = None

return render_template(
"messages.html",
form=form,
Expand All @@ -548,6 +552,7 @@ def view_selected_survey(selected_survey): # noqa: C901
ru_ref_filter=ru_ref_filter,
tab_titles=_get_tab_titles(tab_counts, ru_ref_filter),
show_pagination=bool(tab_counts["current"] > limit),
thread_url=thread_url,
)

except (TypeError, KeyError):
Expand Down Expand Up @@ -605,26 +610,15 @@ def close_conversation(thread_id):
if request.method == "POST":
payload = {"is_closed": True}
message_controllers.patch_thread(thread_id, payload)
thread_url = (
url_for(
"messages_bp.view_conversation",
thread_id=thread_id,
conversation_tab=conversation_tab,
page=page,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
)
+ "#latest-message"
)

flash(Markup(f"Conversation closed. <a href={thread_url}>View conversation</a>"))
flash("Conversation closed.")
return redirect(
url_for(
"messages_bp.view_select_survey",
page=request.args.get("page"),
conversation_tab=conversation_tab,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
thread_id=thread_id,
)
)

Expand Down Expand Up @@ -653,11 +647,13 @@ def close_conversation(thread_id):
def view_misc_inbox(): # noqa: C901
session["messages_survey_selection"] = "misc"
breadcrumbs = [{"text": "Miscellaneous" + " Messages"}]
thread_id = request.args.get("thread_id")
return _process_non_survey_category_page(
render_html="secure-message/misc-inbox.html",
redirect_url="messages_bp.view_misc_inbox",
breadcrumbs=breadcrumbs,
category="MISC",
thread_id=thread_id,
)


Expand Down Expand Up @@ -1076,6 +1072,7 @@ def _process_non_survey_category_page(
redirect_url: str,
breadcrumbs: list,
category: str,
thread_id: str,
):
"""
This method processes message category selected and returns appropriate inbox.
Expand All @@ -1089,6 +1086,8 @@ def _process_non_survey_category_page(
page = request.args.get("page", default=1, type=int)
limit = request.args.get("limit", default=10, type=int)
conversation_tab = request.args.get("conversation_tab", default="open")
ru_ref_filter = request.args.get("ru_ref_filter", default="")
business_id_filter = request.args.get("business_id_filter", default="")
category = category
try:
tab_counts = _get_tab_counts("", conversation_tab, "", None, category)
Expand All @@ -1113,6 +1112,21 @@ def _process_non_survey_category_page(

pagination = pagination_processor(tab_counts["current"], limit, page, href)

if thread_id:
thread_url = (
url_for(
"messages_bp.view_conversation",
thread_id=thread_id,
conversation_tab=conversation_tab,
page=page,
ru_ref_filter=ru_ref_filter,
business_id_filter=business_id_filter,
)
+ "#latest-message"
)
else:
thread_url = None

return render_template(
render_html,
page=page,
Expand All @@ -1123,6 +1137,7 @@ def _process_non_survey_category_page(
conversation_tab=conversation_tab,
tab_titles=_get_tab_titles(tab_counts, ""),
show_pagination=bool(tab_counts["current"] > limit),
thread_url=thread_url,
)

except (TypeError, KeyError):
Expand Down
22 changes: 22 additions & 0 deletions tests/views/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,28 @@ def test_reopen_conversation(self, mock_request, mock_get_jwt):
self.assertIn("Conversation re-opened.".encode(), response.data)
self.assertIn("Ashe Messages".encode(), response.data)

@requests_mock.mock()
def test_reopen_conversation_technical_messages(self, mock_request):
sign_in_with_permission(self, mock_request, user_permission_messages_edit_json)
with self.client.session_transaction() as session:
session["messages_survey_selection"] = "technical"
mock_request.get(url_get_thread, json=thread_json)
mock_request.get(url_get_surveys_list, json=survey_list)
mock_request.patch(url_get_thread, json=thread_json)
mock_request.get(url_messages + "/count", json={"total": 1}, status_code=200)
mock_request.get(url_get_threads_list, json=thread_list)

with self.app.app_context():
response = self.client.post(
"/messages/threads/fb0e79bd-e132-4f4f-a7fd-5e8c6b41b9af?category=TECHNICAL",
data={"reopen": "Re-open conversation"},
follow_redirects=True,
)

self.assertEqual(200, response.status_code)
self.assertIn("Conversation re-opened.".encode(), response.data)
self.assertIn("Technical Messages".encode(), response.data)

def test_calculate_page_change(self):
result = _verify_requested_page_is_within_bounds(3, 10, 15)
self.assertEqual(2, result)
Expand Down

0 comments on commit de65ae5

Please sign in to comment.