Skip to content

Commit

Permalink
implement updateUser in ledger client (digital-asset#16589)
Browse files Browse the repository at this point in the history
* implement updateUser in ledger client

* Use unique parties for updateUser trigger test

* scalafmt
  • Loading branch information
dasormeter authored Mar 23, 2023
1 parent 41dc1a1 commit 6a3a7a8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.daml.ledger.api.v1.{admin => admin_proto}
import com.daml.ledger.client.LedgerClient
import com.daml.lf.data.Ref
import com.daml.lf.data.Ref.{Party, UserId}
import com.google.protobuf.field_mask.FieldMask

import scala.concurrent.{ExecutionContext, Future}

Expand All @@ -34,6 +35,21 @@ final class UserManagementClient(service: UserManagementServiceStub)(implicit
.map(res => fromProtoUser(res.user.get))
}

def updateUser(
user: User,
updateMask: Option[FieldMask],
token: Option[String] = None,
): Future[User] = {
val request = proto.UpdateUserRequest(
Some(UserManagementClient.toProtoUser(user)),
updateMask,
)
LedgerClient
.stub(service, token)
.updateUser(request)
.map(res => fromProtoUser(res.user.get))
}

def getUser(userId: UserId, token: Option[String] = None): Future[User] =
LedgerClient
.stub(service, token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.scalatest._
import org.scalatest.freespec.AsyncFreeSpec
import org.scalatest.matchers.should.Matchers
import com.daml.timer.RetryStrategy
import com.google.protobuf.field_mask.FieldMask
import org.slf4j.LoggerFactory

import java.util.UUID
Expand Down Expand Up @@ -142,6 +143,16 @@ class IntegrationTest
)
}

private def updateUser(userName: String, primaryParty: Ref.Party)(implicit
client: LedgerClient
): Future[domain.User] = {
client.userManagementClient
.updateUser(
domain.User(UserId.assertFromString(userName), Some(primaryParty)),
Some(FieldMask(Seq("primary_party"))),
)
}

private def createUser(userName: String)(implicit client: LedgerClient): Future[domain.User] = {
client.userManagementClient
.createUser(
Expand Down Expand Up @@ -201,6 +212,10 @@ class IntegrationTest
_ <- okSessionBody(
"""{"method":{"type":"select","users":["user-name-1","user-name-2"]},"type":"sign-in"}"""
)
_ <- updateUser("user-name-2", partyDetails.party)
_ <- okSessionBody(
"""{"method":{"type":"select","users":["user-name-1","user-name-2"]},"type":"sign-in"}"""
)
} yield succeed
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package com.daml.lf.engine.trigger
package test

import java.nio.file.Paths

import com.daml.ledger.api.domain.{ObjectMeta, User, UserRight}
import com.daml.ledger.api.refinements.ApiTypes.Party
import com.daml.ledger.api.testing.utils.SuiteResourceManagementAroundAll
Expand All @@ -18,11 +17,13 @@ import com.daml.ledger.client.configuration.{
import com.daml.lf.data.Ref
import com.daml.lf.data.Ref.UserId
import com.daml.platform.sandbox.fixture.SandboxFixture
import com.google.protobuf.field_mask.FieldMask
import io.grpc.StatusRuntimeException
import io.grpc.Status.Code
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec

import java.io.File
import scala.language.implicitConversions

class ConfigSpec
Expand All @@ -38,7 +39,7 @@ class ConfigSpec
token = None,
)

override protected val packageFiles = List.empty
override protected val packageFiles: List[File] = List.empty

private implicit def toParty(s: String): Party =
Party(s)
Expand Down Expand Up @@ -103,7 +104,7 @@ class ConfigSpec
_ <- client.partyManagementClient.allocateParty(hint = Some("alice"), None, None)
_ <- client.partyManagementClient.allocateParty(hint = Some("bob"), None, None)
_ <- client.userManagementClient.createUser(
User(userId, Some("primary"), isDeactivated = false, metadata = ObjectMeta.empty),
User(userId, Some("primary"), metadata = ObjectMeta.empty),
Seq(
UserRight.CanActAs("primary"),
UserRight.CanActAs("alice"),
Expand All @@ -126,7 +127,7 @@ class ConfigSpec
client <- LedgerClient(channel, clientConfig)
userId = randomUserId()
_ <- client.userManagementClient.createUser(
User(userId, None, false, ObjectMeta.empty),
User(userId, None, metadata = ObjectMeta.empty),
Seq.empty,
)
ex <- recoverToExceptionIf[IllegalArgumentException](
Expand All @@ -139,13 +140,37 @@ class ConfigSpec
client <- LedgerClient(channel, clientConfig)
userId = randomUserId()
_ <- client.userManagementClient.createUser(
User(userId, Some("primary"), false, ObjectMeta.empty),
User(userId, Some("primary"), isDeactivated = false, ObjectMeta.empty),
Seq.empty,
)
ex <- recoverToExceptionIf[IllegalArgumentException](
UserSpecification(userId).resolveClaims(client)
)
} yield ex.getMessage should include("no actAs claims")
}
"succeed for user after primaryParty update" in {
for {
client <- LedgerClient(channel, clientConfig)
userId = randomUserId()
_ <- client.partyManagementClient.allocateParty(hint = Some("original"), None, None)
_ <- client.partyManagementClient.allocateParty(hint = Some("updated"), None, None)
_ <- client.partyManagementClient.allocateParty(hint = Some("other"), None, None)
_ <- client.userManagementClient.createUser(
User(userId, Some("original"), metadata = ObjectMeta.empty),
Seq(
UserRight.CanActAs("original"),
UserRight.CanActAs("updated"),
UserRight.CanReadAs("other"),
),
)
_ <- client.userManagementClient.updateUser(
User(userId, Some("updated"), metadata = ObjectMeta.empty),
Some(FieldMask(Seq("primary_party"))),
None,
)

r <- UserSpecification(userId).resolveClaims(client)
} yield r shouldBe TriggerParties("updated", Set("other", "original"))
}
}
}

0 comments on commit 6a3a7a8

Please sign in to comment.