Repository: freedomofpress/securedrop

Issue:
journalist notification incorrectly claims submissions were received
## Description

A journalist notification mail is sent claiming a submission was received in the past 24h. But after checking the journalist interface, there is nothing. 

## Steps to Reproduce

* drain the source of entropy
* as a new source submit a document and check that the source is flagged because no new key was created
* wait 24h and see a journalist notification is mailed because there is a new submission
* login back as the source using the same passphrase and check that the source is no longer flagged
* wait 24h and see a journalist notification is mailed although there is no new submission

## Expected Behavior

* no journalist notification is sent when a source key generation is delayed

## Actual Behavior

* a journalist notification is sent when a source key generation is delayed


Discussion:
> It could because a source removed a message and that changes the last update field.

But no because `securedrop/source_app/main.py def delete():`  does not change `last_updated` 
`securedrop/source_app/utils.py async_genkey` changes `last_updated` so the following can happen:

* A source makes a submission but is flagged for reply because there is not enough entropy
* A few days later the source comes back and uses its passphrase
* The `securedrop/source_app/main.py lookup` method calls `async_genkey` to generate the key and sets `last_updated`
* The generation of the key is mistaken by `securedrop/manage.py were_there_submissions_today` to mean a new submission happened
Yesterday there was a case of a directory created in /var/lib/securedrop/store directory created and empty. There seems to be a condition where that can happen but I wonder when...
**dang**

When a source is created the **last_updated** field is set to the date of creation:

<pre>
    last_updated = Column(DateTime, default=datetime.datetime.utcnow)
</pre>

and since a new entry is created every time someone clicks on **Submit Documents** and clicks on `/generate`, it counts as an activity which is mistaken as a submission.

I propose the `last_updated` date is always set to zero. Since the source is `pending` in this case, it does not show at all in the journalist interface and the `last_updated` field does not need to reflect the date at which the source was created.

Thoughts?

Resolving patch:
diff --git a/securedrop/models.py b/securedrop/models.py
--- a/securedrop/models.py
+++ b/securedrop/models.py
@@ -49,7 +49,7 @@ class Source(db.Model):
     filesystem_id = Column(String(96), unique=True)
     journalist_designation = Column(String(255), nullable=False)
     flagged = Column(Boolean, default=False)
-    last_updated = Column(DateTime, default=datetime.datetime.utcnow)
+    last_updated = Column(DateTime)
     star = relationship("SourceStar", uselist=False, backref="source")
 
     # sources are "pending" and don't get displayed to journalists until they


Test patch:
diff --git a/securedrop/tests/utils/db_helper.py b/securedrop/tests/utils/db_helper.py
--- a/securedrop/tests/utils/db_helper.py
+++ b/securedrop/tests/utils/db_helper.py
@@ -2,6 +2,7 @@
 """Testing utilities that involve database (and often related
 filesystem) interaction.
 """
+import datetime
 import mock
 import os
 
@@ -143,6 +144,8 @@ def submit(source, num_submissions):
     :returns: A list of the :class:`models.Submission`s submitted.
     """
     assert num_submissions >= 1
+    source.last_updated = datetime.datetime.utcnow()
+    db.session.add(source)
     submissions = []
     for _ in range(num_submissions):
         source.interaction_count += 1