예제 #1
0
 async def test_mock_coverage_not_found_after_creation(self):
     """
     Coverage through mock framework: missing created wallet.
     """
     wallet_key = await IndyWallet.generate_wallet_key()
     storage_config_json = json.dumps({"url": "dummy"})
     storage_creds_json = json.dumps(
         {
             "account": "postgres",
             "password": "******",
             "admin_account": "postgres",
             "admin_password": "******",
         }, )
     with async_mock.patch.object(
             test_module, "load_postgres_plugin",
             async_mock.MagicMock()) as mock_load, async_mock.patch.object(
                 indy.wallet, "create_wallet", async_mock.CoroutineMock(
                 )) as mock_create, async_mock.patch.object(
                     indy.wallet, "open_wallet", async_mock.CoroutineMock()
                 ) as mock_open, async_mock.patch.object(
                     indy.anoncreds, "prover_create_master_secret",
                     async_mock.CoroutineMock()
                 ) as mock_master, async_mock.patch.object(
                     indy.wallet, "close_wallet", async_mock.CoroutineMock(
                     )) as mock_close, async_mock.patch.object(
                         indy.wallet, "delete_wallet",
                         async_mock.CoroutineMock()) as mock_delete:
         mock_open.side_effect = test_module.IndyError(
             test_module.ErrorCode.WalletNotFoundError,
             {"message": "outlier"})
         fake_wallet = IndyWallet({
             "auto_create": True,
             "auto_remove": True,
             "name": "test_pg_wallet",
             "key": wallet_key,
             "key_derivation_method": "RAW",
             "storage_type": "postgres_storage",
             "storage_config": storage_config_json,
             "storage_creds": storage_creds_json,
         })
         mock_load.assert_called_once_with(storage_config_json,
                                           storage_creds_json)
         await fake_wallet.create()
         with pytest.raises(test_module.WalletError) as excinfo:
             await fake_wallet.open()
         assert "outlier" in str(excinfo.value)
 async def test_mock_coverage_wallet_exists_x(self):
     """
     Coverage through mock framework: raise on creation of existing wallet
     """
     wallet_key = await IndyWallet.generate_wallet_key()
     storage_config_json = json.dumps({"url": "dummy"})
     storage_creds_json = json.dumps(
         {
             "account": "postgres",
             "password": "******",
             "admin_account": "postgres",
             "admin_password": "******",
         },
     )
     with async_mock.patch.object(
         test_module, "load_postgres_plugin", async_mock.MagicMock()
     ) as mock_load, async_mock.patch.object(
         indy.wallet, "create_wallet", async_mock.CoroutineMock()
     ) as mock_create, async_mock.patch.object(
         indy.wallet, "open_wallet", async_mock.CoroutineMock()
     ) as mock_open, async_mock.patch.object(
         indy.anoncreds, "prover_create_master_secret", async_mock.CoroutineMock()
     ) as mock_master, async_mock.patch.object(
         indy.wallet, "close_wallet", async_mock.CoroutineMock()
     ) as mock_close, async_mock.patch.object(
         indy.wallet, "delete_wallet", async_mock.CoroutineMock()
     ) as mock_delete:
         mock_create.side_effect = test_module.IndyError(
             test_module.ErrorCode.WalletAlreadyExistsError
         )
         fake_wallet = IndyWallet(
             {
                 "auto_create": False,
                 "auto_remove": True,
                 "name": "test_pg_wallet",
                 "key": wallet_key,
                 "key_derivation_method": "RAW",
                 "storage_type": "postgres_storage",
                 "storage_config": storage_config_json,
                 "storage_creds": storage_creds_json,
             }
         )
         with pytest.raises(test_module.WalletError) as excinfo:
             await fake_wallet.create()
         assert "Wallet was not removed by SDK" in str(excinfo.value)
 async def test_mock_coverage(self):
     """
     Coverage through mock framework.
     """
     wallet_key = await IndyWallet.generate_wallet_key()
     storage_config_json = json.dumps({"url": "dummy"})
     storage_creds_json = json.dumps(
         {
             "account": "postgres",
             "password": "******",
             "admin_account": "postgres",
             "admin_password": "******",
         },
     )
     with async_mock.patch.object(
         test_module, "load_postgres_plugin", async_mock.MagicMock()
     ) as mock_load, async_mock.patch.object(
         indy.wallet, "create_wallet", async_mock.CoroutineMock()
     ) as mock_create, async_mock.patch.object(
         indy.wallet, "open_wallet", async_mock.CoroutineMock()
     ) as mock_open, async_mock.patch.object(
         indy.anoncreds, "prover_create_master_secret", async_mock.CoroutineMock()
     ) as mock_master, async_mock.patch.object(
         indy.wallet, "close_wallet", async_mock.CoroutineMock()
     ) as mock_close, async_mock.patch.object(
         indy.wallet, "delete_wallet", async_mock.CoroutineMock()
     ) as mock_delete:
         fake_wallet = IndyWallet(
             {
                 "auto_create": False,
                 "auto_remove": False,
                 "name": "test_pg_wallet",
                 "key": wallet_key,
                 "key_derivation_method": "RAW",
                 "storage_type": "postgres_storage",
                 "storage_config": storage_config_json,
                 "storage_creds": storage_creds_json,
             }
         )
         mock_load.assert_called_once_with(storage_config_json, storage_creds_json)
         await fake_wallet.create()
         await fake_wallet.open()
         assert fake_wallet._wallet_access
         await fake_wallet.close()
         await fake_wallet.remove()
    async def test_postgres_wallet_scheme_works(self):
        """
        Ensure that postgres wallet operations work (create and open wallet, create did, drop wallet)
        """
        postgres_url = os.environ.get("POSTGRES_URL")
        if not postgres_url:
            pytest.fail("POSTGRES_URL not configured")

        wallet_key = await IndyWallet.generate_wallet_key()
        postgres_wallet = IndyWallet({
            "auto_create":
            False,
            "auto_remove":
            False,
            "name":
            "test_pg_wallet",
            "key":
            wallet_key,
            "key_derivation_method":
            "RAW",
            "storage_type":
            "postgres_storage",
            "storage_config":
            '{"url":"' + postgres_url +
            '", "wallet_scheme":"MultiWalletSingleTable"}',
            "storage_creds":
            '{"account":"postgres","password":"******","admin_account":"postgres","admin_password":"******"}',
        })
        await postgres_wallet.create()
        await postgres_wallet.open()

        with pytest.raises(WalletError) as excinfo:
            await wallet.create()
        assert "Wallet was not removed" in str(excinfo.value)

        await postgres_wallet.create_local_did(self.test_seed)
        py_packed = await postgres_wallet.pack_message(self.test_message,
                                                       [self.test_verkey],
                                                       self.test_verkey)

        await postgres_wallet.close()
        await postgres_wallet.remove()
    async def test_storage_del_close(self):
        with async_mock.patch.object(
            indy.wallet, "create_wallet", async_mock.CoroutineMock()
        ) as mock_create, async_mock.patch.object(
            indy.wallet, "open_wallet", async_mock.CoroutineMock()
        ) as mock_open, async_mock.patch.object(
            indy.anoncreds, "prover_create_master_secret", async_mock.CoroutineMock()
        ) as mock_master, async_mock.patch.object(
            indy.wallet, "close_wallet", async_mock.CoroutineMock()
        ) as mock_close, async_mock.patch.object(
            indy.wallet, "delete_wallet", async_mock.CoroutineMock()
        ) as mock_delete:
            fake_wallet = IndyWallet(
                {
                    "auto_create": True,
                    "auto_remove": True,
                    "name": "test_indy_wallet",
                    "key": await IndyWallet.generate_wallet_key(),
                    "key_derivation_method": "RAW",
                }
            )
            await fake_wallet.open()
            storage = IndyStorage(fake_wallet)

            with async_mock.patch.object(
                indy.non_secrets, "open_wallet_search", async_mock.CoroutineMock()
            ) as mock_indy_open_search, async_mock.patch.object(
                indy.non_secrets, "close_wallet_search", async_mock.CoroutineMock()
            ) as mock_indy_close_search:
                mock_indy_open_search.return_value = 1
                search = storage.search_records("connection")
                mock_indy_open_search.assert_not_awaited()
                await search._open()
                mock_indy_open_search.assert_awaited_once()
                del search
                c = 0
                # give the pending cleanup task time to be scheduled
                while not mock_indy_close_search.await_count and c < 10:
                    await asyncio.sleep(0.1)
                    c += 1
                mock_indy_close_search.assert_awaited_once_with(1)
예제 #6
0
    async def test_postgres_wallet_works(self):
        """
        Ensure that postgres wallet operations work (create and open wallet, create did, drop wallet)
        """
        postgres_url = os.environ.get("POSTGRES_URL")
        if not postgres_url:
            pytest.fail("POSTGRES_URL not configured")

        load_postgres_plugin()
        wallet_key = await IndyWallet.generate_wallet_key()
        postgres_wallet = IndyWallet({
            "auto_create":
            False,
            "auto_remove":
            False,
            "name":
            "test_pg_wallet",
            "key":
            wallet_key,
            "key_derivation_method":
            "RAW",
            "storage_type":
            "postgres_storage",
            "storage_config":
            '{"url":"' + postgres_url + '"}',
            "storage_creds":
            '{"account":"postgres","password":"******","admin_account":"postgres","admin_password":"******"}',
        })
        await postgres_wallet.create()
        await postgres_wallet.open()

        await postgres_wallet.create_local_did(self.test_seed)
        py_packed = await postgres_wallet.pack_message(self.test_message,
                                                       [self.test_verkey],
                                                       self.test_verkey)

        await postgres_wallet.close()
        await postgres_wallet.remove()
예제 #7
0
    async def test_postgres_wallet_storage_works(self):
        """
        Ensure that postgres wallet operations work (create and open wallet, store and search, drop wallet)
        """
        postgres_url = os.environ.get("POSTGRES_URL")
        if not postgres_url:
            pytest.fail("POSTGRES_URL not configured")

        load_postgres_plugin()
        wallet_key = await IndyWallet.generate_wallet_key()
        postgres_wallet = IndyWallet({
            "auto_create":
            False,
            "auto_remove":
            False,
            "name":
            "test_pg_wallet",
            "key":
            wallet_key,
            "key_derivation_method":
            "RAW",
            "storage_type":
            "postgres_storage",
            "storage_config":
            '{"url":"' + postgres_url + '", "max_connections":5}',
            "storage_creds":
            '{"account":"postgres","password":"******","admin_account":"postgres","admin_password":"******"}',
        })
        await postgres_wallet.create()
        await postgres_wallet.open()

        storage = IndyStorage(postgres_wallet)

        # add and then fetch a record
        record = StorageRecord(
            value=
            '{"initiator": "self", "invitation_key": "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg", "state": "invitation", "routing_state": "none", "error_msg": null, "their_label": null, "created_at": "2019-05-14 21:58:24.143260+00:00", "updated_at": "2019-05-14 21:58:24.143260+00:00"}',
            tags={
                "initiator": "self",
                "invitation_key":
                "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                "state": "invitation",
                "routing_state": "none",
            },
            type="connection",
            id="f96f76ec-0e9b-4f32-8237-f4219e6cf0c7",
        )
        await storage.add_record(record)
        g_rec = await storage.get_record(record.type, record.id)

        # now try search
        search = None
        try:
            search = storage.search_records("connection")
            await search.open()
            records = await search.fetch(10)
        finally:
            if search:
                await search.close()

        await postgres_wallet.close()
        await postgres_wallet.remove()
    async def test_record(self):
        with async_mock.patch.object(
                test_wallet, "load_postgres_plugin",
                async_mock.MagicMock()) as mock_load, async_mock.patch.object(
                    indy.wallet, "create_wallet", async_mock.CoroutineMock(
                    )) as mock_create, async_mock.patch.object(
                        indy.wallet, "open_wallet", async_mock.CoroutineMock()
                    ) as mock_open, async_mock.patch.object(
                        indy.anoncreds, "prover_create_master_secret",
                        async_mock.CoroutineMock()
                    ) as mock_master, async_mock.patch.object(
                        indy.wallet, "close_wallet", async_mock.CoroutineMock(
                        )) as mock_close, async_mock.patch.object(
                            indy.wallet, "delete_wallet",
                            async_mock.CoroutineMock()) as mock_delete:
            fake_wallet = IndyWallet({
                "auto_create":
                True,
                "auto_remove":
                True,
                "name":
                "test_pg_wallet",
                "key":
                await IndyWallet.generate_wallet_key(),
                "key_derivation_method":
                "RAW",
                "storage_type":
                "postgres_storage",
                "storage_config":
                json.dumps({"url": "dummy"}),
                "storage_creds":
                json.dumps({
                    "account": "postgres",
                    "password": "******",
                    "admin_account": "postgres",
                    "admin_password": "******",
                }),
            })
            await fake_wallet.open()
            storage = IndyStorage(fake_wallet)

            for record_x in [
                    None,
                    StorageRecord(
                        type="connection",
                        value=json.dumps({
                            "initiator":
                            "self",
                            "invitation_key":
                            "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                            "state":
                            "invitation",
                            "routing_state":
                            "none",
                            "error_msg":
                            None,
                            "their_label":
                            None,
                            "created_at":
                            "2019-05-14 21:58:24.143260+00:00",
                            "updated_at":
                            "2019-05-14 21:58:24.143260+00:00",
                        }),
                        tags={
                            "initiator": "self",
                            "invitation_key":
                            "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                            "state": "invitation",
                            "routing_state": "none",
                        },
                        id=None,
                    ),
                    StorageRecord(
                        type=None,
                        value=json.dumps({
                            "initiator":
                            "self",
                            "invitation_key":
                            "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                            "state":
                            "invitation",
                            "routing_state":
                            "none",
                            "error_msg":
                            None,
                            "their_label":
                            None,
                            "created_at":
                            "2019-05-14 21:58:24.143260+00:00",
                            "updated_at":
                            "2019-05-14 21:58:24.143260+00:00",
                        }),
                        tags={
                            "initiator": "self",
                            "invitation_key":
                            "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                            "state": "invitation",
                            "routing_state": "none",
                        },
                        id="f96f76ec-0e9b-4f32-8237-f4219e6cf0c7",
                    ),
                    StorageRecord(
                        type="connection",
                        value=None,
                        tags={
                            "initiator": "self",
                            "invitation_key":
                            "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                            "state": "invitation",
                            "routing_state": "none",
                        },
                        id="f96f76ec-0e9b-4f32-8237-f4219e6cf0c7",
                    ),
            ]:
                with pytest.raises(StorageError):
                    await storage.add_record(record_x)

            with pytest.raises(StorageError):
                await storage.get_record(None, "dummy-id")
            with pytest.raises(StorageError):
                await storage.get_record("connection", None)

            with async_mock.patch.object(
                    test_module.non_secrets, "get_wallet_record",
                    async_mock.CoroutineMock()) as mock_get_record:
                mock_get_record.side_effect = test_module.IndyError(
                    test_module.ErrorCode.CommonInvalidStructure)
                with pytest.raises(test_module.StorageError):
                    await storage.get_record("connection", "dummy-id")

            with async_mock.patch.object(
                    test_module.non_secrets,
                    "update_wallet_record_value",
                    async_mock.CoroutineMock(),
            ) as mock_update_value, async_mock.patch.object(
                    test_module.non_secrets,
                    "update_wallet_record_tags",
                    async_mock.CoroutineMock(),
            ) as mock_update_tags, async_mock.patch.object(
                    test_module.non_secrets,
                    "delete_wallet_record",
                    async_mock.CoroutineMock(),
            ) as mock_delete:
                mock_update_value.side_effect = test_module.IndyError(
                    test_module.ErrorCode.CommonInvalidStructure)
                mock_update_tags.side_effect = test_module.IndyError(
                    test_module.ErrorCode.CommonInvalidStructure)
                mock_delete.side_effect = test_module.IndyError(
                    test_module.ErrorCode.CommonInvalidStructure)

                rec = StorageRecord(
                    type="connection",
                    value=json.dumps({
                        "initiator":
                        "self",
                        "invitation_key":
                        "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                        "state":
                        "invitation",
                        "routing_state":
                        "none",
                        "error_msg":
                        None,
                        "their_label":
                        None,
                        "created_at":
                        "2019-05-14 21:58:24.143260+00:00",
                        "updated_at":
                        "2019-05-14 21:58:24.143260+00:00",
                    }),
                    tags={
                        "initiator": "self",
                        "invitation_key":
                        "9XgL7Y4TBTJyVJdomT6axZGUFg9npxcrXnRT4CG8fWYg",
                        "state": "invitation",
                        "routing_state": "none",
                    },
                    id="f96f76ec-0e9b-4f32-8237-f4219e6cf0c7",
                )

                with pytest.raises(test_module.StorageError):
                    await storage.update_record(rec, "dummy-value",
                                                {"tag": "tag"})

                with pytest.raises(test_module.StorageError):
                    await storage.delete_record(rec)
    async def test_storage_search_x(self):
        with async_mock.patch.object(
                test_wallet, "load_postgres_plugin",
                async_mock.MagicMock()) as mock_load, async_mock.patch.object(
                    indy.wallet, "create_wallet", async_mock.CoroutineMock(
                    )) as mock_create, async_mock.patch.object(
                        indy.wallet, "open_wallet", async_mock.CoroutineMock()
                    ) as mock_open, async_mock.patch.object(
                        indy.anoncreds, "prover_create_master_secret",
                        async_mock.CoroutineMock()
                    ) as mock_master, async_mock.patch.object(
                        indy.wallet, "close_wallet", async_mock.CoroutineMock(
                        )) as mock_close, async_mock.patch.object(
                            indy.wallet, "delete_wallet",
                            async_mock.CoroutineMock()) as mock_delete:
            fake_wallet = IndyWallet({
                "auto_create":
                True,
                "auto_remove":
                True,
                "name":
                "test_pg_wallet",
                "key":
                await IndyWallet.generate_wallet_key(),
                "key_derivation_method":
                "RAW",
                "storage_type":
                "postgres_storage",
                "storage_config":
                json.dumps({"url": "dummy"}),
                "storage_creds":
                json.dumps({
                    "account": "postgres",
                    "password": "******",
                    "admin_account": "postgres",
                    "admin_password": "******",
                }),
            })
            await fake_wallet.open()
            storage = IndyStorage(fake_wallet)

            search = storage.search_records("connection")
            with pytest.raises(StorageSearchError):
                await search.fetch(10)

            with async_mock.patch.object(
                    indy.non_secrets, "open_wallet_search",
                    async_mock.CoroutineMock(
                    )) as mock_indy_open_search, async_mock.patch.object(
                        indy.non_secrets, "close_wallet_search",
                        async_mock.CoroutineMock()) as mock_indy_close_search:
                mock_indy_open_search.side_effect = test_module.IndyError(
                    "no open")
                search = storage.search_records("connection")
                with pytest.raises(StorageSearchError):
                    await search.open()
                await search.close()

            with async_mock.patch.object(
                    indy.non_secrets, "open_wallet_search",
                    async_mock.CoroutineMock(
                    )) as mock_indy_open_search, async_mock.patch.object(
                        indy.non_secrets,
                        "fetch_wallet_search_next_records",
                        async_mock.CoroutineMock(),
                    ) as mock_indy_fetch, async_mock.patch.object(
                        indy.non_secrets, "close_wallet_search",
                        async_mock.CoroutineMock()) as mock_indy_close_search:
                mock_indy_fetch.side_effect = test_module.IndyError("no fetch")
                search = storage.search_records("connection")
                await search.open()
                with pytest.raises(StorageSearchError):
                    await search.fetch(10)
                await search.close()

            with async_mock.patch.object(
                    indy.non_secrets, "open_wallet_search",
                    async_mock.CoroutineMock(
                    )) as mock_indy_open_search, async_mock.patch.object(
                        indy.non_secrets, "close_wallet_search",
                        async_mock.CoroutineMock()) as mock_indy_close_search:
                mock_indy_close_search.side_effect = test_module.IndyError(
                    "no close")
                search = storage.search_records("connection")
                await search.open()
                with pytest.raises(StorageSearchError):
                    await search.close()