-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
messages/views: Prepare for the deprecation of user metadata.
The "user" field is to be deprecated from messages' reactions and reaction events in the API. Add support for `user_id` in MsgInfoView, by using the recently introduced `get_user_id_from_reaction()`. Remove dependence on reaction["user"]["full_name"] by instead using `_all_users_by_id` with the "user_id". Tests updated.
- Loading branch information
Showing
4 changed files
with
68 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from collections import OrderedDict, defaultdict | ||
from datetime import date | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import pytz | ||
|
@@ -1597,60 +1598,6 @@ def test_transform_content(self, mocker, raw_html, expected_content): | |
@pytest.mark.parametrize( | ||
"to_vary_in_each_message, expected_text, expected_attributes", | ||
[ | ||
case( | ||
{ | ||
"reactions": [ | ||
{ | ||
"emoji_name": "thumbs_up", | ||
"emoji_code": "1f44d", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Iago", | ||
"id": 5, | ||
}, | ||
"reaction_type": "unicode_emoji", | ||
}, | ||
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Iago", | ||
"id": 5, | ||
}, | ||
"reaction_type": "zulip_extra_emoji", | ||
}, | ||
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "aaron", | ||
"id": 1, | ||
}, | ||
"reaction_type": "zulip_extra_emoji", | ||
}, | ||
{ | ||
"emoji_name": "heart", | ||
"emoji_code": "2764", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Iago", | ||
"id": 5, | ||
}, | ||
"reaction_type": "unicode_emoji", | ||
}, | ||
], | ||
}, | ||
" :thumbs_up: 1 :zulip: 2 :heart: 1 ", | ||
[ | ||
("reaction", 15), | ||
(None, 1), | ||
("reaction_mine", 11), | ||
(None, 1), | ||
("reaction", 11), | ||
], | ||
), | ||
case( | ||
{ | ||
"reactions": [ | ||
|
@@ -1701,31 +1648,19 @@ def test_transform_content(self, mocker, raw_html, expected_content): | |
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Iago", | ||
"id": 5, | ||
}, | ||
"user_id": 5, | ||
"reaction_type": "unicode_emoji", | ||
}, | ||
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "aaron", | ||
"id": 1, | ||
}, | ||
"user_id": 1, | ||
"reaction_type": "zulip_extra_emoji", | ||
}, | ||
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Shivam", | ||
"id": 6, | ||
}, | ||
"user_id": 6, | ||
"reaction_type": "unicode_emoji", | ||
}, | ||
], | ||
|
@@ -1741,31 +1676,19 @@ def test_transform_content(self, mocker, raw_html, expected_content): | |
{ | ||
"emoji_name": "heart", | ||
"emoji_code": "2764", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Iago", | ||
"id": 5, | ||
}, | ||
"user_id": 5, | ||
"reaction_type": "unicode_emoji", | ||
}, | ||
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "aaron", | ||
"id": 1, | ||
}, | ||
"user_id": 1, | ||
"reaction_type": "zulip_extra_emoji", | ||
}, | ||
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Shivam", | ||
"id": 6, | ||
}, | ||
"user_id": 6, | ||
"reaction_type": "unicode_emoji", | ||
}, | ||
], | ||
|
@@ -1821,10 +1744,26 @@ def test_reactions_view( | |
msg_box = MessageBox(varied_message, self.model, None) | ||
reactions = to_vary_in_each_message["reactions"] | ||
|
||
reactions_view = msg_box.reactions_view(reactions) | ||
mock_all_users_by_id = { | ||
1: {"full_name": "aaron"}, | ||
5: {"full_name": "Iago"}, | ||
6: {"full_name": "Shivam"}, | ||
} | ||
|
||
def mock_get_user_id(reaction): | ||
if "user_id" in reaction: | ||
return reaction["user_id"] | ||
return reaction["user"]["id"] | ||
|
||
with patch.object( | ||
self.model, | ||
"get_user_id_from_reaction", | ||
side_effect=mock_get_user_id, | ||
), patch.object(self.model, "_all_users_by_id", mock_all_users_by_id): | ||
reactions_view = msg_box.reactions_view(reactions) | ||
|
||
assert reactions_view.original_widget.text == expected_text | ||
assert reactions_view.original_widget.attrib == expected_attributes | ||
assert reactions_view.original_widget.text == expected_text | ||
assert reactions_view.original_widget.attrib == expected_attributes | ||
|
||
@pytest.mark.parametrize( | ||
"message_links, expected_text, expected_attrib, expected_footlinks_width", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from collections import OrderedDict | ||
from typing import Any, Callable, Dict, List, Optional, Tuple | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from pytest import param as case | ||
|
@@ -1169,21 +1170,13 @@ def test_height_noreactions(self) -> None: | |
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "Iago", | ||
"id": 5, | ||
}, | ||
"user_id": 5, | ||
"reaction_type": "zulip_extra_emoji", | ||
}, | ||
{ | ||
"emoji_name": "zulip", | ||
"emoji_code": "zulip", | ||
"user": { | ||
"email": "[email protected]", | ||
"full_name": "aaron", | ||
"id": 1, | ||
}, | ||
"user_id": 1, | ||
"reaction_type": "zulip_extra_emoji", | ||
}, | ||
{ | ||
|
@@ -1207,18 +1200,37 @@ def test_height_reactions( | |
) -> None: | ||
varied_message = message_fixture | ||
varied_message.update(to_vary_in_each_message) | ||
self.msg_info_view = MsgInfoView( | ||
self.controller, | ||
varied_message, | ||
"Message Information", | ||
OrderedDict(), | ||
OrderedDict(), | ||
list(), | ||
) | ||
# 12 = 7 labels + 2 blank lines + 1 'Reactions' (category) | ||
# + 4 reactions (excluding 'Message Links'). | ||
expected_height = 14 | ||
assert self.msg_info_view.height == expected_height | ||
|
||
mock_all_users_by_id = { | ||
1: {"full_name": "aaron"}, | ||
5: {"full_name": "Iago"}, | ||
} | ||
|
||
def mock_get_user_id(reaction: Dict[str, Any]) -> int: | ||
if "user_id" in reaction: | ||
return reaction["user_id"] | ||
return reaction["user"]["id"] | ||
|
||
with patch.object( | ||
self.controller.model, | ||
"get_user_id_from_reaction", | ||
side_effect=mock_get_user_id, | ||
), patch.object( | ||
self.controller.model, "_all_users_by_id", mock_all_users_by_id | ||
): | ||
self.msg_info_view = MsgInfoView( | ||
self.controller, | ||
varied_message, | ||
"Message Information", | ||
OrderedDict(), | ||
OrderedDict(), | ||
list(), | ||
) | ||
|
||
# 12 = 7 labels + 2 blank lines + 1 'Reactions' (category) | ||
# + 4 reactions (excluding 'Message Links'). | ||
expected_height = 14 | ||
assert self.msg_info_view.height == expected_height | ||
|
||
@pytest.mark.parametrize( | ||
[ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters