Skip to content

Commit

Permalink
Merge pull request #335 from digital-asset/python-user-api-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
da-tanabe authored Mar 31, 2022
2 parents 112a6ac + 2e3c2f7 commit 07a162b
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 242 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ $(py_src_gen): $(py_src_gen_root)/%: .cache/witnesses/python
# python: witness that makes sure the current venv is up to date with our lock file
.venv/poetry.lock: poetry.lock
poetry run pip install -U pip
poetry install -E oauth -E prometheus -E pygments -E server
poetry install -E oauth -E prometheus -E pygments -E server -E tls-testing
cp $< $@


Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.7.1
7.7.2
468 changes: 245 additions & 223 deletions poetry.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[tool.poetry]
name = "dazl"
version = "7.7.1"
version = "7.7.2"
description = "high-level Ledger API client for Daml ledgers"
license = "Apache-2.0"
authors = ["Davin K. Tanabe <davin.tanabe@digitalasset.com>"]
Expand All @@ -25,10 +25,11 @@ oauthlib = { version = "*", optional = true }
prometheus_client = { version = "*", optional = true }
protobuf = ">=3.12.0"
pygments = { version = "*", optional = true }
pyOpenSSL = { version = "*", optional = true }
requests = "*"
semver = "*"
toposort = "*"
typing_extensions = { version = "*", python = "<3.8.0" }
typing_extensions = { version = "*", optional = true }

[tool.poetry.dev-dependencies]
black = "*"
Expand All @@ -52,14 +53,14 @@ types-protobuf = "*"
types-requests = "*"
types-setuptools = "*"
watchdog = "*"
pyOpenSSL = "^22.0.0"
types-pyOpenSSL = "^22.0.0"
types-pyOpenSSL = "*"

[tool.poetry.extras]
oauth = ["google-auth", "oauthlib"]
prometheus = ["prometheus_client"]
pygments = ["pygments"]
server = ["aiohttp"]
tls-testing = ["pyOpenSSL"]

[tool.poetry.scripts]
dazl = 'dazl.cli:main'
Expand Down
1 change: 1 addition & 0 deletions python/dazl/damlast/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"MultiPackageLookup",
"PackageLookup",
"PackageExceptionTracker",
"SymbolLookup",
"find_choice",
"matching_normalizations",
"normalize",
Expand Down
13 changes: 13 additions & 0 deletions python/dazl/ledger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import sys

from .api_types import (
ActAs,
Admin,
ArchiveEvent,
Boundary,
Command,
Expand All @@ -20,8 +22,12 @@
ExerciseByKeyCommand,
ExerciseCommand,
ExerciseResponse,
ParticipantMeteringReport,
PartyInfo,
ReadAs,
Right,
SubmitResponse,
User,
)

if sys.version_info >= (3, 8):
Expand All @@ -32,20 +38,27 @@

__all__ = [
"aio",
"ActAs",
"Admin",
"ArchiveEvent",
"Boundary",
"Command",
"CommandMeta",
"CreateAndExerciseCommand",
"CreateCommand",
"CreateEvent",
"Event",
"EventOrBoundary",
"ExerciseByKeyCommand",
"ExerciseCommand",
"ExerciseResponse",
"PartyInfo",
"PackageService",
"ParticipantMeteringReport",
"ReadAs",
"Connection",
"QueryStream",
"User",
]


Expand Down
44 changes: 43 additions & 1 deletion python/dazl/ledger/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ from ..prim import ContractData, ContractId, Party, TimeDeltaLike
from ..query import Queries, Query
from .aio import Connection as AioConnection, QueryStream as AioQueryStream
from .api_types import (
ActAs,
Admin,
ArchiveEvent,
Boundary,
Command,
CommandMeta,
CreateAndExerciseCommand,
CreateCommand,
CreateEvent,
Expand All @@ -39,6 +42,7 @@ from .api_types import (
ExerciseResponse,
ParticipantMeteringReport,
PartyInfo,
ReadAs,
Right,
SubmitResponse,
User,
Expand All @@ -52,19 +56,28 @@ else:
from typing_extensions import Literal, Protocol, runtime_checkable

__all__ = [
"aio",
"ActAs",
"Admin",
"ArchiveEvent",
"Boundary",
"Command",
"CommandMeta",
"CreateAndExerciseCommand",
"CreateCommand",
"CreateEvent",
"Event",
"EventOrBoundary",
"ExerciseByKeyCommand",
"ExerciseCommand",
"ExerciseResponse",
"PartyInfo",
"PackageService",
"ParticipantMeteringReport",
"ReadAs",
"Connection",
"QueryStream",
"User",
]

CreateFn = TypeVar("CreateFn", bound=Callable[[CreateEvent], SubmitResponse])
Expand Down Expand Up @@ -105,7 +118,36 @@ class OnBoundaryDecorator(Protocol):
@overload
def connect(
*,
blocking: Literal[False] = False,
url: Optional[str] = None,
host: Optional[str] = None,
port: Optional[int] = None,
scheme: Optional[str] = None,
read_as: Union[None, Party, Collection[Party]] = None,
act_as: Union[None, Party, Collection[Party]] = None,
admin: Optional[bool] = False,
ledger_id: Optional[str] = None,
application_name: Optional[str] = None,
oauth_token: Optional[str] = None,
oauth_token_file: Optional[str] = None,
ca: Optional[bytes] = None,
ca_file: Optional[PathLike] = None,
cert: Optional[bytes] = None,
cert_file: Optional[PathLike] = None,
cert_key: Optional[bytes] = None,
cert_key_file: Optional[PathLike] = None,
connect_timeout: Optional[TimeDeltaLike] = None,
use_http_proxy: bool = True,
logger: Optional[Logger] = None,
logger_name: Optional[str] = None,
log_level: Optional[str] = None,
lookup: Optional[SymbolLookup] = None,
) -> AioConnection: ...

# noinspection PyShadowingNames
@overload
def connect(
*,
blocking: Literal[False],
url: Optional[str] = None,
host: Optional[str] = None,
port: Optional[int] = None,
Expand Down
11 changes: 4 additions & 7 deletions python/dazl/ledger/grpc/conn_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,10 @@ async def get_user(self, user_id: "Optional[str]" = None) -> "User":

async def create_user(self, user: "User", rights: "Optional[Sequence[Right]]" = None) -> "User":
stub = lapiadminpb.UserManagementServiceStub(self.channel)
request = (
lapiadminpb.CreateUserRequest(
user=Codec.encode_user(user), rights=[Codec.encode_right(right) for right in rights]
)
if rights
else ()
)
request = lapiadminpb.CreateUserRequest(user=Codec.encode_user(user))
if rights is not None:
request.rights.extend(Codec.encode_right(right) for right in rights)

response = await stub.CreateUser(request)
return Codec.decode_user(response.user)

Expand Down
4 changes: 2 additions & 2 deletions python/dazl/testing/_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# SPDX-License-Identifier: Apache-2.0
from typing import Collection, Optional

from OpenSSL import crypto


class Certificate:
def __init__(self, public_cert: bytes, private_key: bytes):
Expand All @@ -23,6 +21,8 @@ def cert_gen(
serial_number: int = 0,
validity_end_in_seconds: int = 10 * 365 * 24 * 60 * 60,
) -> "Certificate":
from OpenSSL import crypto

# can look at generated file using openssl:
# openssl x509 -inform pem -in selfsigned.crt -noout -text
# create a key pair
Expand Down
20 changes: 17 additions & 3 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import pytest


@pytest.fixture(scope="session", params=["1.18.1", "2.0.0"])
def sandbox(request) -> "Generator[str, None, None]":
@pytest.fixture(scope="session", params=["1", "2"])
def sandbox(request, sandbox_v1, sandbox_v2) -> "Generator[str, None, None]":
"""
Run an instance of the Sandbox, or use one configured through environment variables.
Expand All @@ -25,7 +25,21 @@ def sandbox(request) -> "Generator[str, None, None]":
DAML_SDK_VERSION=1.0.0 make test
```
"""
with testing.sandbox(project_root=None, version=request.param) as sb:
if request.param == "1":
yield sandbox_v1
else:
yield sandbox_v2


@pytest.fixture(scope="session")
def sandbox_v1() -> "Generator[str, None, None]":
with testing.sandbox(project_root=None, version="1.18.1") as sb:
yield sb.url


@pytest.fixture(scope="session")
def sandbox_v2() -> "Generator[str, None, None]":
with testing.sandbox(project_root=None, version="2.0.0") as sb:
yield sb.url


Expand Down
22 changes: 22 additions & 0 deletions python/tests/unit/test_ledger_create_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2017-2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from dazl import connect
from dazl.ledger import ActAs, Admin, ReadAs, User
import pytest


@pytest.mark.asyncio
async def test_ledger_create_user(sandbox_v2) -> None:
async with connect(url=sandbox_v2, admin=True) as conn:
party_info = await conn.allocate_party()
await conn.create_user(User("testuser1", party_info.party))


@pytest.mark.asyncio
async def test_ledger_create_user_with_rights(sandbox_v2) -> None:
async with connect(url=sandbox_v2, admin=True) as conn:
party_info = await conn.allocate_party()
await conn.create_user(
User("testuser2", party_info.party),
[ActAs(party_info.party), ReadAs(party_info.party), Admin],
)

0 comments on commit 07a162b

Please sign in to comment.