From e4b7b7d11bb83c52d999333507a74c20612d1d75 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sat, 7 May 2022 06:45:15 +0200 Subject: [PATCH] [Refactor] Create an Es4Document test fixture and use it wherever we can --- tests/conftest.py | 10 +++++- tests/helpers.py | 16 ++++++++++ tests/test_document_es4.py | 65 ++++++++++++-------------------------- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a26b8c7..5a012b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,9 +5,10 @@ from nacl.signing import SigningKey import pytest from _pytest.fixtures import SubRequest +from earthsnake.document.es4 import Es4Document 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 @@ -30,3 +31,10 @@ def identity(request: SubRequest) -> Identity: sign = SigningKey(seed) return Identity(name, sign_key=sign) + + +@pytest.fixture +def es4_document() -> Es4Document: + """A valid es.4 document""" + + return Es4Document.from_json(VALID_DOCUMENT) diff --git a/tests/helpers.py b/tests/helpers.py index 478fd87..7e706bc 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -2,6 +2,7 @@ """ from random import choice +from typing import Any, Dict 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' ) +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: """Generate a valid random author name""" diff --git a/tests/test_document_es4.py b/tests/test_document_es4.py index e5e3dd4..6532cb9 100644 --- a/tests/test_document_es4.py +++ b/tests/test_document_es4.py @@ -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