async def test_mock_coverage_open_master_secret_exists(self):
     """
     Coverage through mock framework: open, master secret exists (OK).
     """
     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_master.side_effect = test_module.IndyError(
             test_module.ErrorCode.AnoncredsMasterSecretDuplicateNameError)
         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._master_secret_id == fake_wallet.name
         fake_wallet._handle = 1234
         await fake_wallet.open()  # open an open wallet: should be OK
         assert fake_wallet._wallet_access
         await fake_wallet.close()
         await fake_wallet.remove()
    async def test_properties(self, wallet):
        assert wallet.name
        assert wallet.type == "indy"
        assert wallet.handle
        none_wallet = IndyWallet()
        assert none_wallet.name == IndyWallet.DEFAULT_NAME

        assert "IndyWallet" in str(wallet)
        assert wallet.created
        assert wallet.master_secret_id == wallet.name
        assert wallet._wallet_config
예제 #3
0
async def store():
    key = await IndyWallet.generate_wallet_key()
    wallet = IndyWallet({
        "auto_create": True,
        "auto_remove": True,
        "name": "test-wallet",
        "key": key,
        "key_derivation_method":
        "RAW",  # much slower tests with argon-hashed keys
    })
    await wallet.open()
    yield IndyStorage(wallet)
    await wallet.close()
 async def test_mock_coverage_remove_x(self):
     """
     Coverage through mock framework: exception on removal.
     """
     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_delete.side_effect = test_module.IndyError(
             test_module.ErrorCode.CommonIOError, {"message": "outlier"})
         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()
         with pytest.raises(test_module.WalletError) as excinfo:
             await fake_wallet.remove()
         assert "outlier" 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_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)
예제 #8
0
    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_value(rec, "dummy-value")

                with pytest.raises(test_module.StorageError):
                    await storage.update_record_tags(rec, {"tag": "tag"})

                with pytest.raises(test_module.StorageError):
                    await storage.delete_record(rec)
예제 #9
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")

        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(
            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",
        )
        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()
예제 #10
0
    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()