Add hash check on document read/write (#890)

* Add sha256sum function
* Add hash check to document class
* Make sha256sum test more thorough
* Handle exceptions in the sha256sum function directly
* Update test coverage
* Clarify title on dialog box
* Don't write blank lines in makeAlert
This commit is contained in:
Veronica Berglyd Olsen
2021-09-19 13:55:54 +02:00
committed by GitHub
parent d65c5e93b6
commit c852a5bda3
6 changed files with 140 additions and 29 deletions
+26 -9
View File
@@ -23,7 +23,7 @@ import os
import pytest
from mock import causeOSError
from tools import readFile
from tools import readFile, writeFile
from novelwriter.core import NWProject, NWDoc
from novelwriter.enum import nwItemClass, nwItemLayout
@@ -34,11 +34,14 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, nwMinimal):
"""Test loading and saving a document with the NWDoc class.
"""
theProject = NWProject(mockGUI)
assert theProject.openProject(nwMinimal)
assert theProject.openProject(nwMinimal) is True
assert theProject.projPath == nwMinimal
sHandle = "8c659a11cd429"
# Read Document
# =============
# Not a valid handle
theDoc = NWDoc(theProject, "stuff")
assert theDoc.readDocument() is None
@@ -46,6 +49,7 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, nwMinimal):
# Non-existent handle
theDoc = NWDoc(theProject, "0000000000000")
assert theDoc.readDocument() is None
assert theDoc._currHash is None
# Cause open() to fail while loading
with monkeypatch.context() as mp:
@@ -65,11 +69,14 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, nwMinimal):
theDoc = NWDoc(theProject, xHandle)
assert theDoc.readDocument() == ""
# Write Document
# ==============
# Set handle and save again
theText = "### Test File\n\nText ...\n\n"
theDoc = NWDoc(theProject, xHandle)
assert theDoc.readDocument(xHandle) == ""
assert theDoc.writeDocument(theText)
assert theDoc.writeDocument(theText) is True
# Save again to ensure temp file and previous file is handled
assert theDoc.writeDocument(theText)
@@ -84,36 +91,46 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, nwMinimal):
"Text ...\n\n"
)
# Alter the document on disk and save again
writeFile(docPath, "blablabla")
assert theDoc.writeDocument(theText) is False
# Force the overwrite
assert theDoc.writeDocument(theText, forceWrite=True) is True
# Force no meta data
theDoc._theItem = None
assert theDoc.writeDocument(theText)
assert theDoc.writeDocument(theText) is True
assert readFile(docPath) == theText
# Cause open() to fail while saving
with monkeypatch.context() as mp:
mp.setattr("builtins.open", causeOSError)
assert not theDoc.writeDocument(theText)
assert theDoc.writeDocument(theText) is False
assert theDoc.getError() == "OSError"
# Saving with no handle
theDoc._docHandle = None
assert not theDoc.writeDocument(theText)
assert theDoc.writeDocument(theText) is False
# Delete Document
# ===============
# Delete the last document
theDoc = NWDoc(theProject, "stuff")
assert not theDoc.deleteDocument()
assert theDoc.deleteDocument() is False
assert os.path.isfile(docPath)
# Cause the delete to fail
with monkeypatch.context() as mp:
mp.setattr("os.unlink", causeOSError)
theDoc = NWDoc(theProject, xHandle)
assert not theDoc.deleteDocument()
assert theDoc.deleteDocument() is False
assert theDoc.getError() == "OSError"
# Make the delete pass
theDoc = NWDoc(theProject, xHandle)
assert theDoc.deleteDocument()
assert theDoc.deleteDocument() is True
assert not os.path.isfile(docPath)
# END Test testCoreDocument_Load