forked from devsisters/shardcake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShardManagerAuthExampleSpec.scala
More file actions
65 lines (59 loc) · 2.43 KB
/
ShardManagerAuthExampleSpec.scala
File metadata and controls
65 lines (59 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package example
import com.devsisters.shardcake.{ Config, ManagerConfig, Server, ShardManager, ShardManagerClient }
import com.devsisters.shardcake.interfaces.{ Pods, PodsHealth, Storage }
import sttp.client3.SttpBackend
import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import sttp.client3.httpclient.zio.ZioWebSocketsStreams
import zio.Clock.ClockLive
import zio.http.{ Header, Middleware }
import zio.test._
import zio.{ Config => _, _ }
object ShardManagerAuthExampleSpec extends ZIOSpecDefault {
val validToken = "validBearerToken"
val shardManagerServerLayer: ZLayer[ManagerConfig, Throwable, Unit] =
ZLayer.makeSome[ManagerConfig, Unit](
ZLayer(
Server
.run(Middleware.bearerAuthZIO(secret => ZIO.succeed(secret.stringValue.equals(validToken))))
.forkDaemon *> ClockLive.sleep(3 seconds).unit
),
Storage.memory,
ShardManager.live,
Pods.noop,
PodsHealth.noop
)
def sttpBackendWithAuthTokenLayer(token: String): ZLayer[Scope, Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZLayer {
val authHeader = Header.Authorization.Bearer(token)
AsyncHttpClientZioBackend.scoped(customizeRequest =
builder => builder.addHeader(authHeader.headerName, authHeader.renderedValue)
)
}
def spec: Spec[TestEnvironment, Any] =
suite("ShardManagerAuthSpec")(
test("auth example for shard manager") {
ZIO.scoped {
for {
validClient <- ZIO
.service[ShardManagerClient]
.provideSome[Config & Scope](
sttpBackendWithAuthTokenLayer(validToken),
ShardManagerClient.live
)
invalidClient <- ZIO
.service[ShardManagerClient]
.provideSome[Config & Scope](
sttpBackendWithAuthTokenLayer("invalid"),
ShardManagerClient.live
)
validRequest <- validClient.getAssignments.exit
invalidRequest <- invalidClient.getAssignments.exit
} yield assertTrue(validRequest.isSuccess, invalidRequest.isFailure)
}
}
).provide(
shardManagerServerLayer,
ZLayer.succeed(Config.default),
ZLayer.succeed(ManagerConfig.default)
)
}