Skip to content

Commit

Permalink
Add fix and tests for issue freedomofpress#442
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoll committed Feb 6, 2020
1 parent 7bbb265 commit 27b59f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
17 changes: 15 additions & 2 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,8 @@ class ConversationView(QWidget):
}
'''

conversation_updated = pyqtSignal()

MARGIN_LEFT = 38
MARGIN_RIGHT = 20

Expand Down Expand Up @@ -2550,6 +2552,7 @@ def add_file(self, file: File, index):
index)
self.conversation_layout.insertWidget(index, conversation_item, alignment=Qt.AlignLeft)
self.current_messages[file.uuid] = conversation_item
self.conversation_updated.emit()

def update_conversation_position(self, min_val, max_val):
"""
Expand All @@ -2568,6 +2571,7 @@ def add_message(self, message: Message, index) -> None:
message.uuid, str(message), self.controller.message_ready, index)
self.conversation_layout.insertWidget(index, conversation_item, alignment=Qt.AlignLeft)
self.current_messages[message.uuid] = conversation_item
self.conversation_updated.emit()

def add_reply(self, reply: Union[DraftReply, Reply], index) -> None:
"""
Expand Down Expand Up @@ -2648,6 +2652,8 @@ def __init__(

# Connect reply_box to conversation_view
self.reply_box.reply_sent.connect(self.conversation_view.on_reply_sent)
self.conversation_view.conversation_updated.connect(
self.conversation_title_bar.update_timestamp)


class ReplyBoxWidget(QWidget):
Expand Down Expand Up @@ -3027,11 +3033,11 @@ def __init__(self, source, controller):
header_layout.setContentsMargins(
self.MARGIN_LEFT, self.VERTICAL_MARGIN, self.MARGIN_RIGHT, self.VERTICAL_MARGIN)
title = TitleLabel(self.source.journalist_designation)
updated = LastUpdatedLabel(_(arrow.get(self.source.last_updated).format('DD MMM')))
self.updated = LastUpdatedLabel(_(arrow.get(self.source.last_updated).format('DD MMM')))
menu = SourceMenuButton(self.source, self.controller)
header_layout.addWidget(title, alignment=Qt.AlignLeft)
header_layout.addStretch()
header_layout.addWidget(updated, alignment=Qt.AlignRight)
header_layout.addWidget(self.updated, alignment=Qt.AlignRight)
header_layout.addWidget(menu, alignment=Qt.AlignRight)

# Create horizontal line
Expand All @@ -3042,3 +3048,10 @@ def __init__(self, source, controller):
# Add widgets
layout.addWidget(header)
layout.addWidget(horizontal_line)

def update_timestamp(self):
"""
Ensure the timestamp is always kept up to date with the latest activity
from the source.
"""
self.updated.setText(_(arrow.get(self.source.last_updated).format('DD MMM')))
28 changes: 27 additions & 1 deletion tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Make sure the UI widgets are configured correctly and work as expected.
"""
import pytest
import arrow
from datetime import datetime

from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtGui import QFocusEvent, QMovie
Expand All @@ -16,7 +18,8 @@
DeleteSourceMessageBox, DeleteSourceAction, SourceMenu, TopPane, LeftPane, SyncIcon, \
ErrorStatusBar, ActivityStatusBar, UserProfile, UserButton, UserMenu, LoginButton, \
ReplyBoxWidget, ReplyTextEdit, SourceConversationWrapper, StarToggleButton, LoginOfflineLink, \
LoginErrorBar, EmptyConversationView, ExportDialog, PrintDialog, PasswordEdit, SecureQLabel
LoginErrorBar, EmptyConversationView, ExportDialog, PrintDialog, PasswordEdit, SecureQLabel, \
SourceProfileShortWidget
from tests import factory


Expand Down Expand Up @@ -2174,6 +2177,7 @@ def test_ConversationView_add_message(mocker, session, source):

cv = ConversationView(source, mocked_controller)
cv.conversation_layout = mocker.MagicMock()
cv.conversation_updated = mocker.MagicMock()
# this is the MessageWidget that __init__() would return
mock_msg_widget_res = mocker.MagicMock()
# mock the actual MessageWidget so we can inspect the __init__ call
Expand All @@ -2189,6 +2193,10 @@ def test_ConversationView_add_message(mocker, session, source):
cv.conversation_layout.insertWidget.assert_called_once_with(
0, mock_msg_widget_res, alignment=Qt.AlignLeft)

# Check the signal is emitted to say the message has been added (and thus
# the timestamps need updating.
assert cv.conversation_updated.emit.call_count == 1


def test_ConversationView_add_message_no_content(mocker, session, source):
"""
Expand Down Expand Up @@ -2383,6 +2391,7 @@ def test_ConversationView_add_downloaded_file(mocker, homedir, source, session):

cv = ConversationView(source['source'], mocked_controller)
cv.conversation_layout = mocker.MagicMock()
cv.conversation_updated = mocker.MagicMock()

mock_label = mocker.patch('securedrop_client.gui.widgets.SecureQLabel')
mocker.patch('securedrop_client.gui.widgets.QHBoxLayout.addWidget')
Expand All @@ -2392,6 +2401,7 @@ def test_ConversationView_add_downloaded_file(mocker, homedir, source, session):

mock_label.assert_called_with('123B') # default factory filesize
assert cv.conversation_layout.insertWidget.call_count == 1
assert cv.conversation_updated.emit.call_count == 1

cal = cv.conversation_layout.insertWidget.call_args_list
assert isinstance(cal[0][0][1], FileWidget)
Expand Down Expand Up @@ -3214,3 +3224,19 @@ def test_clear_conversation_deletes_items(mocker, homedir):
cv.clear_conversation()

assert cv.conversation_layout.count() == 0


def test_SourceProfileShortWidget_update_timestamp(mocker):
"""
Ensure the update_timestamp slot actually updates the LastUpdatedLabel
instance with the last_updated value from the source..
"""
mock_controller = mocker.MagicMock()
mock_source = mocker.MagicMock()
mock_source.last_updated = datetime.now()
mock_source.journalist_designation = "wimple horse knackered unittest"
spsw = SourceProfileShortWidget(mock_source, mock_controller)
spsw.updated = mocker.MagicMock()
spsw.update_timestamp()
spsw.updated.setText.assert_called_once_with(
arrow.get(mock_source.last_updated).format('DD MMM'))

0 comments on commit 27b59f1

Please sign in to comment.