Skip to content

Commit

Permalink
Fixed AuthorizationInterceptorSpec again (#11418)
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
tudor-da authored Oct 26, 2021
1 parent c8006b8 commit 811a6d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
1 change: 0 additions & 1 deletion ledger/ledger-api-auth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ da_scala_test_suite(
":ledger-api-auth",
"//ledger/error",
"//ledger/test-common",
"//libs-scala/concurrent",
"@maven//:com_google_api_grpc_proto_google_common_protos",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:io_grpc_grpc_api",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package com.daml.ledger.api.auth

import com.daml.dec.DirectExecutionContext
import com.daml.error.ErrorCodesVersionSwitcher
import com.daml.ledger.api.auth.interceptor.AuthorizationInterceptor
import com.google.rpc.ErrorInfo
Expand All @@ -12,13 +11,16 @@ import io.grpc.{Metadata, ServerCall, Status}
import org.mockito.captor.ArgCaptor
import org.mockito.{ArgumentMatchersSugar, MockitoSugar}
import org.scalatest.Assertion
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.flatspec.AsyncFlatSpec
import org.scalatest.matchers.should.Matchers

import java.util.concurrent.CompletableFuture
import scala.concurrent.ExecutionContext.global
import scala.concurrent.Promise
import scala.util.Success

class AuthorizationInterceptorSpec
extends AnyFlatSpec
extends AsyncFlatSpec
with MockitoSugar
with Matchers
with ArgumentMatchersSugar {
Expand Down Expand Up @@ -55,21 +57,26 @@ class AuthorizationInterceptorSpec
throw new RuntimeException("some internal failure")
)

val errorCodesStatusSwitcher = new ErrorCodesVersionSwitcher(usesSelfServiceErrorCodes)
val authorizationInterceptor = {
// Use parasitic execution context to ensure that all expected async calls dispatched by this constructor
// finish within the constructor's boundaries.
AuthorizationInterceptor(authService, DirectExecutionContext, errorCodesStatusSwitcher)
val promise = Promise[Unit]()
// Using a promise to ensure the verify call below happens after the expected call to `serverCall.close`
when(serverCall.close(any[Status], any[Metadata])).thenAnswer {
promise.complete(Success(()))
()
}

val errorCodesStatusSwitcher = new ErrorCodesVersionSwitcher(usesSelfServiceErrorCodes)
val authorizationInterceptor =
AuthorizationInterceptor(authService, global, errorCodesStatusSwitcher)

val statusCaptor = ArgCaptor[Status]
val metadataCaptor = ArgCaptor[Metadata]

when(authService.decodeMetadata(any[Metadata])).thenReturn(failedMetadataDecode)
authorizationInterceptor.interceptCall[Nothing, Nothing](serverCall, new Metadata(), null)

verify(serverCall).close(statusCaptor.capture, metadataCaptor.capture)

assertRpcStatus(statusCaptor.value, metadataCaptor.value)
promise.future.map { _ =>
verify(serverCall).close(statusCaptor.capture, metadataCaptor.capture)
assertRpcStatus(statusCaptor.value, metadataCaptor.value)
}
}
}

0 comments on commit 811a6d3

Please sign in to comment.