From 236d884622e94762ed0454f5ff073d5922cf946a Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Wed, 25 Mar 2020 10:30:26 -0400 Subject: [PATCH] app, test: do not show drafts in preview --- securedrop_client/db.py | 16 ++++++++++++++-- securedrop_client/gui/widgets.py | 4 ++-- tests/gui/test_widgets.py | 19 +++++++++++++++++++ tests/test_models.py | 27 +++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/securedrop_client/db.py b/securedrop_client/db.py index 914cd5f075..c9cec14e00 100644 --- a/securedrop_client/db.py +++ b/securedrop_client/db.py @@ -50,8 +50,8 @@ def __repr__(self) -> str: @property def collection(self) -> List: - """Return the list of submissions and replies for this source, sorted - in ascending order by the filename/interaction count.""" + """Return the list of submissions, replies, messages, and drafts for this + source, sorted in ascending order by the filename/interaction count.""" collection = [] # type: List collection.extend(self.messages) collection.extend(self.files) @@ -63,6 +63,18 @@ def collection(self) -> List: datetime.datetime(datetime.MINYEAR, 1, 1)))) return collection + @property + def server_collection(self) -> List: + """Return the list of submissions, replies, and messages for this source. + These are the items that have been either successfully sent to the server, + or they have been retrieved from the server.""" + collection = [] # type: List + collection.extend(self.messages) + collection.extend(self.files) + collection.extend(self.replies) + collection.sort(key=lambda x: x.file_counter) + return collection + @property def journalist_filename(self) -> str: valid_chars = 'abcdefghijklmnopqrstuvwxyz1234567890-_' diff --git a/securedrop_client/gui/widgets.py b/securedrop_client/gui/widgets.py index 21209e3032..68d0fa3f45 100644 --- a/securedrop_client/gui/widgets.py +++ b/securedrop_client/gui/widgets.py @@ -1186,10 +1186,10 @@ def update(self): self.timestamp.setText(_(arrow.get(self.source.last_updated).format('DD MMM'))) self.name.setText(self.source.journalist_designation) - if not self.source.collection: + if not self.source.server_collection: self.set_snippet(self.source_uuid, '') else: - last_collection_obj = self.source.collection[-1] + last_collection_obj = self.source.server_collection[-1] self.set_snippet(self.source_uuid, str(last_collection_obj)) if self.source.document_count == 0: diff --git a/tests/gui/test_widgets.py b/tests/gui/test_widgets.py index e4eb17fe76..c808e3db27 100644 --- a/tests/gui/test_widgets.py +++ b/tests/gui/test_widgets.py @@ -1042,6 +1042,25 @@ def test_SourceWidget_update_attachment_icon(mocker): assert sw.paperclip.isHidden() +def test_SourceWidget_draft_only(mocker, session_maker, session, homedir): + """ + Snippets/previews do not include draft messages. + """ + mock_gui = mocker.MagicMock() + controller = logic.Controller('http://localhost', mock_gui, session_maker, homedir) + source = factory.Source(document_count=1) + f = factory.File(source=source) + reply = factory.DraftReply(source=source) + session.add(f) + session.add(source) + session.add(reply) + session.commit() + + sw = SourceWidget(controller, source) + sw.set_snippet(source.uuid, f.filename) + assert sw.preview.text() == f.filename + + def test_SourceWidget_set_snippet(mocker, session_maker, session, homedir): """ Snippets are set as expected. diff --git a/tests/test_models.py b/tests/test_models.py index 66293c1215..30adeb86c3 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -142,6 +142,33 @@ def test_source_collection(): assert source.collection[2] == message +def test_source_server_collection(): + # Create some test submissions and replies + source = factory.Source() + file_ = File(source=source, uuid="test", size=123, filename="2-test.doc.gpg", + download_url='http://test/test') + message = Message(source=source, uuid="test", size=123, filename="3-test.doc.gpg", + download_url='http://test/test') + user = User(username='hehe') + reply = Reply(source=source, journalist=user, filename="1-reply.gpg", + size=1234, uuid='test') + draft_reply = DraftReply(source=source, journalist=user, + uuid='test', + timestamp=datetime.datetime(2002, 6, 6, 6, 0)) + source.files = [file_] + source.messages = [message] + source.replies = [reply] + source.draftreplies = [draft_reply] + + # Now these items should be in the source collection in the proper order + assert source.server_collection[0] == reply + assert source.server_collection[1] == file_ + assert source.server_collection[2] == message + + # Drafts do not appear in the server_collection, they are local only. + assert draft_reply not in source.server_collection + + def test_source_collection_ordering_with_multiple_draft_replies(): # Create some test submissions, replies, and draft replies. source = factory.Source()