[Refactor] Create an Es4Document test fixture and use it wherever we can

This commit is contained in:
Gergely Polonkai 2022-05-07 06:45:15 +02:00
parent e8bc0cfd38
commit e4b7b7d11b
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
3 changed files with 46 additions and 45 deletions

View File

@ -5,9 +5,10 @@ from nacl.signing import SigningKey
import pytest import pytest
from _pytest.fixtures import SubRequest from _pytest.fixtures import SubRequest
from earthsnake.document.es4 import Es4Document
from earthsnake.identity import Identity from earthsnake.identity import Identity
from .helpers import DEFAULT_IDENTITY_SEED, random_name from .helpers import DEFAULT_IDENTITY_SEED, VALID_DOCUMENT, random_name
@pytest.fixture @pytest.fixture
@ -30,3 +31,10 @@ def identity(request: SubRequest) -> Identity:
sign = SigningKey(seed) sign = SigningKey(seed)
return Identity(name, sign_key=sign) return Identity(name, sign_key=sign)
@pytest.fixture
def es4_document() -> Es4Document:
"""A valid es.4 document"""
return Es4Document.from_json(VALID_DOCUMENT)

View File

@ -2,6 +2,7 @@
""" """
from random import choice from random import choice
from typing import Any, Dict
from earthsnake.types import ALPHA_LOWER, ALPHA_LOWER_OR_DIGIT from earthsnake.types import ALPHA_LOWER, ALPHA_LOWER_OR_DIGIT
@ -10,6 +11,21 @@ DEFAULT_IDENTITY_SEED = (
b'z\x84\xb7\xd2\x02q\xfa\xe8W\xd8z\x05E\xfb2\xd5' b'z\x84\xb7\xd2\x02q\xfa\xe8W\xd8z\x05E\xfb2\xd5'
) )
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',
}
def random_name() -> str: def random_name() -> str:
"""Generate a valid random author name""" """Generate a valid random author name"""

View File

@ -1,7 +1,6 @@
"""Tests for the es.4 document validator""" """Tests for the es.4 document validator"""
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Any, Dict
from nacl.signing import SigningKey from nacl.signing import SigningKey
import pytest import pytest
@ -13,21 +12,7 @@ from earthsnake.identity import Identity
from earthsnake.path import Path from earthsnake.path import Path
from earthsnake.share import Share from earthsnake.share import Share
from .helpers import VALID_DOCUMENT
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',
}
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -199,18 +184,16 @@ def test_check_content_hash() -> None:
assert str(ctx.value) == 'content does not match contentHash' 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""" """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: 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' 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""" """Test if signing a document with a different entity also sets the author"""
other_key_seed = ( other_key_seed = (
@ -219,48 +202,44 @@ def test_sign_new_identity() -> None:
identity = Identity('name', sign_key=SigningKey(other_key_seed)) identity = Identity('name', sign_key=SigningKey(other_key_seed))
assert str(identity) == '@name.bu5seaewd4p7cx7ot4ue3m6wpigfa5hmowxgeophe2so72roao5wq' assert str(identity) == '@name.bu5seaewd4p7cx7ot4ue3m6wpigfa5hmowxgeophe2so72roao5wq'
document = Es4Document.from_json(VALID_DOCUMENT) es4_document.author = identity
document.author = identity es4_document.sign()
document.sign()
assert ( assert (
document.signature es4_document.signature
== 'bk76oxkhbydy3itajeeqtryyj7ej5y7hqjffae6ilf4kpyklh2w32hx3ndg6cb3zvlphj46zmuxbetk4cj2fh6' == 'bk76oxkhbydy3itajeeqtryyj7ej5y7hqjffae6ilf4kpyklh2w32hx3ndg6cb3zvlphj46zmuxbetk4cj2fh6'
'5bhxdtzflvf7oamcbi' '5bhxdtzflvf7oamcbi'
) )
assert document.author == identity assert es4_document.author == identity
@pytest.mark.id_name('test') @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""" """Test if the sign() method updates the document with a correct signature"""
document = Es4Document.from_json(VALID_DOCUMENT) es4_document.sign(identity=identity)
document.sign(identity=identity)
assert document.signature == ( assert es4_document.signature == (
'bughac3ooy5qfect4bxdke2zkun2wqufhpivt57vj2mzq52mhqzesjvhyywttxm7qqjyhoskmiqd2lw72qy2u766r' 'bughac3ooy5qfect4bxdke2zkun2wqufhpivt57vj2mzq52mhqzesjvhyywttxm7qqjyhoskmiqd2lw72qy2u766r'
'fqvnbwab4qp3gbi' 'fqvnbwab4qp3gbi'
) )
def test_content_setter() -> None: def test_content_setter(es4_document: Es4Document) -> None:
"""Check if the content setter recalculates content_hash, too""" """Check if the content setter recalculates content_hash, too"""
document = Es4Document.from_json(VALID_DOCUMENT) es4_document.content = 'new content'
document.content = 'new content' assert es4_document.content_hash != VALID_DOCUMENT['contentHash']
assert document.content_hash != VALID_DOCUMENT['contentHash'] assert es4_document.content_hash == 'b7yzgbde66w3m67r7srsiajj765xsj5hmaz4phuhqp6mejs77syaq'
assert 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 its not already set""" """Test if the content_hash getter recalculates the content hash if its not already set"""
document = Es4Document.from_json(VALID_DOCUMENT) es4_document._content = 'new content' # pylint: disable=protected-access
document._content = 'new content' # pylint: disable=protected-access es4_document._content_hash = None # pylint: disable=protected-access
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: 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' 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""" """Test the content_length property"""
document = Es4Document.from_json(VALID_DOCUMENT) assert es4_document.content_length == 4
assert document.content_length == 4