|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
"""Tests for the es.4 document validator"""
|
|
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
|
|
from nacl.signing import SigningKey
|
|
|
|
|
import pytest
|
|
|
|
@ -13,21 +12,7 @@ from earthsnake.identity import Identity
|
|
|
|
|
from earthsnake.path import Path
|
|
|
|
|
from earthsnake.share import Share
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VALID_DOCUMENT: Dict[str, Any] = {
|
|
|
|
|
'format': 'es.4',
|
|
|
|
|
'timestamp': 1651738871668993,
|
|
|
|
|
'deleteAfter': None,
|
|
|
|
|
'author': '@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya',
|
|
|
|
|
'path': '/test.txt',
|
|
|
|
|
'workspace': '+test.suffix',
|
|
|
|
|
'signature': (
|
|
|
|
|
'bughac3ooy5qfect4bxdke2zkun2wqufhpivt57vj2mzq52mhqzesjvhyywttxm7qqjyhoskmiqd2lw72qy2u766r'
|
|
|
|
|
'fqvnbwab4qp3gbi'
|
|
|
|
|
),
|
|
|
|
|
'content': 'test',
|
|
|
|
|
'contentHash': 'bt6dnbamijr6wlgrp5kqmkwwqcwr36ty3fmfyelgrlvwblmhqbiea',
|
|
|
|
|
}
|
|
|
|
|
from .helpers import VALID_DOCUMENT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
@ -199,18 +184,16 @@ def test_check_content_hash() -> None:
|
|
|
|
|
assert str(ctx.value) == 'content does not match contentHash'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sign_different_author(identity: Identity) -> None:
|
|
|
|
|
def test_sign_different_author(identity: Identity, es4_document: Es4Document) -> None:
|
|
|
|
|
"""Test if sign() fails if the signing identity is not the same as the document author"""
|
|
|
|
|
|
|
|
|
|
document = Es4Document.from_json(VALID_DOCUMENT)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValidationError) as ctx:
|
|
|
|
|
document.sign(identity=identity)
|
|
|
|
|
es4_document.sign(identity=identity)
|
|
|
|
|
|
|
|
|
|
assert str(ctx.value) == 'when signing a document, keypair address must match document author'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sign_new_identity() -> None:
|
|
|
|
|
def test_sign_new_identity(es4_document: Es4Document) -> None:
|
|
|
|
|
"""Test if signing a document with a different entity also sets the author"""
|
|
|
|
|
|
|
|
|
|
other_key_seed = (
|
|
|
|
@ -219,48 +202,44 @@ def test_sign_new_identity() -> None:
|
|
|
|
|
identity = Identity('name', sign_key=SigningKey(other_key_seed))
|
|
|
|
|
assert str(identity) == '@name.bu5seaewd4p7cx7ot4ue3m6wpigfa5hmowxgeophe2so72roao5wq'
|
|
|
|
|
|
|
|
|
|
document = Es4Document.from_json(VALID_DOCUMENT)
|
|
|
|
|
document.author = identity
|
|
|
|
|
document.sign()
|
|
|
|
|
es4_document.author = identity
|
|
|
|
|
es4_document.sign()
|
|
|
|
|
|
|
|
|
|
assert (
|
|
|
|
|
document.signature
|
|
|
|
|
es4_document.signature
|
|
|
|
|
== 'bk76oxkhbydy3itajeeqtryyj7ej5y7hqjffae6ilf4kpyklh2w32hx3ndg6cb3zvlphj46zmuxbetk4cj2fh6'
|
|
|
|
|
'5bhxdtzflvf7oamcbi'
|
|
|
|
|
)
|
|
|
|
|
assert document.author == identity
|
|
|
|
|
assert es4_document.author == identity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.id_name('test')
|
|
|
|
|
def test_sign(identity: Identity) -> None:
|
|
|
|
|
def test_sign(identity: Identity, es4_document: Es4Document) -> None:
|
|
|
|
|
"""Test if the sign() method updates the document with a correct signature"""
|
|
|
|
|
|
|
|
|
|
document = Es4Document.from_json(VALID_DOCUMENT)
|
|
|
|
|
document.sign(identity=identity)
|
|
|
|
|
es4_document.sign(identity=identity)
|
|
|
|
|
|
|
|
|
|
assert document.signature == (
|
|
|
|
|
assert es4_document.signature == (
|
|
|
|
|
'bughac3ooy5qfect4bxdke2zkun2wqufhpivt57vj2mzq52mhqzesjvhyywttxm7qqjyhoskmiqd2lw72qy2u766r'
|
|
|
|
|
'fqvnbwab4qp3gbi'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_content_setter() -> None:
|
|
|
|
|
def test_content_setter(es4_document: Es4Document) -> None:
|
|
|
|
|
"""Check if the content setter recalculates content_hash, too"""
|
|
|
|
|
|
|
|
|
|
document = Es4Document.from_json(VALID_DOCUMENT)
|
|
|
|
|
document.content = 'new content'
|
|
|
|
|
assert document.content_hash != VALID_DOCUMENT['contentHash']
|
|
|
|
|
assert document.content_hash == 'b7yzgbde66w3m67r7srsiajj765xsj5hmaz4phuhqp6mejs77syaq'
|
|
|
|
|
es4_document.content = 'new content'
|
|
|
|
|
assert es4_document.content_hash != VALID_DOCUMENT['contentHash']
|
|
|
|
|
assert es4_document.content_hash == 'b7yzgbde66w3m67r7srsiajj765xsj5hmaz4phuhqp6mejs77syaq'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_content_hash_getter() -> None:
|
|
|
|
|
def test_content_hash_getter(es4_document: Es4Document) -> None:
|
|
|
|
|
"""Test if the content_hash getter recalculates the content hash if it’s not already set"""
|
|
|
|
|
|
|
|
|
|
document = Es4Document.from_json(VALID_DOCUMENT)
|
|
|
|
|
document._content = 'new content' # pylint: disable=protected-access
|
|
|
|
|
document._content_hash = None # pylint: disable=protected-access
|
|
|
|
|
es4_document._content = 'new content' # pylint: disable=protected-access
|
|
|
|
|
es4_document._content_hash = None # pylint: disable=protected-access
|
|
|
|
|
|
|
|
|
|
assert document.content_hash == 'b7yzgbde66w3m67r7srsiajj765xsj5hmaz4phuhqp6mejs77syaq'
|
|
|
|
|
assert es4_document.content_hash == 'b7yzgbde66w3m67r7srsiajj765xsj5hmaz4phuhqp6mejs77syaq'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_unsigned_document(identity: Identity) -> None:
|
|
|
|
@ -274,9 +253,7 @@ def test_validate_unsigned_document(identity: Identity) -> None:
|
|
|
|
|
assert str(ctx.value) == 'document has no signature assigned'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_content_length() -> None:
|
|
|
|
|
def test_content_length(es4_document: Es4Document) -> None:
|
|
|
|
|
"""Test the content_length property"""
|
|
|
|
|
|
|
|
|
|
document = Es4Document.from_json(VALID_DOCUMENT)
|
|
|
|
|
|
|
|
|
|
assert document.content_length == 4
|
|
|
|
|
assert es4_document.content_length == 4
|
|
|
|
|