From fc22d566c1f0b63fdff88fb9a148f400ac9f6b75 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Sun, 30 Oct 2022 11:39:44 +0100 Subject: [PATCH] Move wot tests to unit and integ. dirs (#441) Mocking correctly test_membership doesn't worth it --- tests/{ => integration}/wot/__init__.py | 0 tests/integration/wot/test_membership.py | 111 +++++++++++++++++++++ tests/unit/wot/__init__.py | 14 +++ tests/{ => unit}/wot/test_certification.py | 0 tests/{ => unit}/wot/test_idty_tools.py | 0 tests/{ => unit}/wot/test_membership.py | 78 +-------------- tests/{ => unit}/wot/test_revocation.py | 0 tests/{ => unit}/wot/test_tools.py | 0 8 files changed, 126 insertions(+), 77 deletions(-) rename tests/{ => integration}/wot/__init__.py (100%) create mode 100644 tests/integration/wot/test_membership.py create mode 100644 tests/unit/wot/__init__.py rename tests/{ => unit}/wot/test_certification.py (100%) rename tests/{ => unit}/wot/test_idty_tools.py (100%) rename tests/{ => unit}/wot/test_membership.py (68%) rename tests/{ => unit}/wot/test_revocation.py (100%) rename tests/{ => unit}/wot/test_tools.py (100%) diff --git a/tests/wot/__init__.py b/tests/integration/wot/__init__.py similarity index 100% rename from tests/wot/__init__.py rename to tests/integration/wot/__init__.py diff --git a/tests/integration/wot/test_membership.py b/tests/integration/wot/test_membership.py new file mode 100644 index 00000000..502a2c98 --- /dev/null +++ b/tests/integration/wot/test_membership.py @@ -0,0 +1,111 @@ +# Copyright 2016-2022 Maël Azimi <m.a@moul.re> +# +# Silkaj is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Silkaj is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. + +from unittest.mock import Mock + +import pytest +from click.testing import CliRunner +from duniterpy.documents import get_block_id +from duniterpy.key import SigningKey + +from silkaj import auth +from silkaj.blockchain import tools as bc_tools +from silkaj.cli import cli +from silkaj.wot import membership +from silkaj.wot import tools as w_tools +from tests.patched.blockchain_tools import fake_block_id, patched_get_head_block + +# Values and patches +PUBKEY = "EA7Dsw39ShZg4SpURsrgMaMqrweJPUFPYHwZA8e92e3D" +identity_block_id = get_block_id( + "0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855" +) +identity_uid = "toto" + +membership_block_id = fake_block_id + + +def patched_auth_method(): + return SigningKey.from_credentials(identity_uid, identity_uid) + + +def patched_choose_identity(pubkey): + return ( + {"uid": identity_uid, "meta": {"timestamp": identity_block_id}}, + PUBKEY, + None, + ) + + +@pytest.mark.parametrize( + "dry_run, display, confirmation", + [ + (True, False, False), + (False, True, False), + (False, True, True), + (False, False, True), + ], +) +def test_membership_cmd(dry_run, display, confirmation, monkeypatch): + # Monkeypatch and Mock + monkeypatch.setattr(auth, "auth_method", patched_auth_method) + monkeypatch.setattr(bc_tools, "get_head_block", patched_get_head_block) + monkeypatch.setattr(w_tools, "choose_identity", patched_choose_identity) + + patched_display_confirmation_table = Mock() + monkeypatch.setattr( + membership, + "display_confirmation_table", + patched_display_confirmation_table, + ) + if not dry_run and not display: + patched_generate_membership_document = Mock() + monkeypatch.setattr( + membership, + "generate_membership_document", + patched_generate_membership_document, + ) + + # Run membership command + command = [] + if dry_run: + command += ["--dry-run"] + if display: + command += ["--display"] + command += ["wot", "membership"] + pass_license = "No\nYes\n" + confirmations = pass_license + ("Yes" if confirmation else "No") + result = CliRunner().invoke(cli, args=command, input=confirmations) + + # Assert functions are called + patched_display_confirmation_table.assert_called_once_with( + identity_uid, + PUBKEY, + identity_block_id, + ) + if dry_run or display: + assert "Type: Membership" in result.output + else: + # signing_key = patched_auth_method() + patched_generate_membership_document.assert_called_once() + # membership_block_id is different + # patched_generate_membership_document.assert_called_once_with( + # PUBKEY, + # membership_block_id, + # identity_uid, + # identity_block_id, + # currency, + # signing_key, + # ) diff --git a/tests/unit/wot/__init__.py b/tests/unit/wot/__init__.py new file mode 100644 index 00000000..58426bbc --- /dev/null +++ b/tests/unit/wot/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2016-2022 Maël Azimi <m.a@moul.re> +# +# Silkaj is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Silkaj is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. diff --git a/tests/wot/test_certification.py b/tests/unit/wot/test_certification.py similarity index 100% rename from tests/wot/test_certification.py rename to tests/unit/wot/test_certification.py diff --git a/tests/wot/test_idty_tools.py b/tests/unit/wot/test_idty_tools.py similarity index 100% rename from tests/wot/test_idty_tools.py rename to tests/unit/wot/test_idty_tools.py diff --git a/tests/wot/test_membership.py b/tests/unit/wot/test_membership.py similarity index 68% rename from tests/wot/test_membership.py rename to tests/unit/wot/test_membership.py index da06e11c..9cfc50eb 100644 --- a/tests/wot/test_membership.py +++ b/tests/unit/wot/test_membership.py @@ -13,28 +13,22 @@ # You should have received a copy of the GNU Affero General Public License # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. -from unittest.mock import Mock - import pendulum import pytest -from click.testing import CliRunner from duniterpy.api import bma from duniterpy.documents import Membership, get_block_id from duniterpy.key import SigningKey -from silkaj import auth, tui +from silkaj import tui from silkaj.blockchain import tools as bc_tools -from silkaj.cli import cli from silkaj.constants import DATE from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum from silkaj.wot import membership -from silkaj.wot import tools as w_tools from tests.patched.blockchain_tools import ( currency, fake_block_id, patched_block, - patched_get_head_block, patched_params, ) from tests.patched.wot import ( @@ -56,76 +50,6 @@ def patched_auth_method(): return SigningKey.from_credentials(identity_uid, identity_uid) -def patched_choose_identity(pubkey): - return ( - {"uid": identity_uid, "meta": {"timestamp": identity_block_id}}, - PUBKEY, - None, - ) - - -@pytest.mark.parametrize( - "dry_run, display, confirmation", - [ - (True, False, False), - (False, True, False), - (False, True, True), - (False, False, True), - ], -) -def test_membership_cmd(dry_run, display, confirmation, monkeypatch): - # Monkeypatch and Mock - monkeypatch.setattr(auth, "auth_method", patched_auth_method) - monkeypatch.setattr(bc_tools, "get_head_block", patched_get_head_block) - monkeypatch.setattr(w_tools, "choose_identity", patched_choose_identity) - - patched_display_confirmation_table = Mock() - monkeypatch.setattr( - membership, - "display_confirmation_table", - patched_display_confirmation_table, - ) - if not dry_run and not display: - patched_generate_membership_document = Mock() - monkeypatch.setattr( - membership, - "generate_membership_document", - patched_generate_membership_document, - ) - - # Run membership command - command = [] - if dry_run: - command += ["--dry-run"] - if display: - command += ["--display"] - command += ["wot", "membership"] - pass_license = "No\nYes\n" - confirmations = pass_license + ("Yes" if confirmation else "No") - result = CliRunner().invoke(cli, args=command, input=confirmations) - - # Assert functions are called - patched_display_confirmation_table.assert_called_once_with( - identity_uid, - PUBKEY, - identity_block_id, - ) - if dry_run or display: - assert "Type: Membership" in result.output - else: - # signing_key = patched_auth_method() - patched_generate_membership_document.assert_called_once() - # membership_block_id is different - # patched_generate_membership_document.assert_called_once_with( - # PUBKEY, - # membership_block_id, - # identity_uid, - # identity_block_id, - # currency, - # signing_key, - # ) - - @pytest.mark.parametrize( "patched_wot_requirements", [patched_wot_requirements_no_pending, patched_wot_requirements_one_pending], diff --git a/tests/wot/test_revocation.py b/tests/unit/wot/test_revocation.py similarity index 100% rename from tests/wot/test_revocation.py rename to tests/unit/wot/test_revocation.py diff --git a/tests/wot/test_tools.py b/tests/unit/wot/test_tools.py similarity index 100% rename from tests/wot/test_tools.py rename to tests/unit/wot/test_tools.py -- GitLab