|
| 1 | +package com.ocadotechnology.sttp.oauth2.cache.cats |
| 2 | + |
| 3 | +import cats.Functor |
| 4 | +import cats.effect.IO |
| 5 | +import cats.effect.Ref |
| 6 | +import cats.effect.kernel.Clock |
| 7 | +import cats.effect.kernel.Outcome.Succeeded |
| 8 | +import cats.effect.testkit.TestContext |
| 9 | +import cats.effect.testkit.TestInstances |
| 10 | +import cats.implicits._ |
| 11 | +import com.ocadotechnology.sttp.oauth2.Introspection.TokenIntrospectionResponse |
| 12 | +import com.ocadotechnology.sttp.oauth2.Secret |
| 13 | +import com.ocadotechnology.sttp.oauth2.TokenIntrospection |
| 14 | +import org.scalatest.Assertion |
| 15 | +import org.scalatest.matchers.should.Matchers |
| 16 | +import org.scalatest.wordspec.AnyWordSpec |
| 17 | + |
| 18 | +import java.time.Instant |
| 19 | +import scala.concurrent.duration._ |
| 20 | + |
| 21 | +class CachingTokenIntrospectionSpec extends AnyWordSpec with Matchers with TestInstances { |
| 22 | + private implicit val ticker: Ticker = Ticker(TestContext()) |
| 23 | + |
| 24 | + private val testToken: Secret[String] = Secret("secret") |
| 25 | + |
| 26 | + private def testToken60Seconds(now: Instant): TokenIntrospectionResponse = TokenIntrospectionResponse( |
| 27 | + active = true, |
| 28 | + exp = Some(now.plusSeconds(60)) |
| 29 | + ) |
| 30 | + |
| 31 | + private def testToken5Seconds(now: Instant): TokenIntrospectionResponse = TokenIntrospectionResponse( |
| 32 | + active = true, |
| 33 | + exp = Some(now.plusSeconds(5)) |
| 34 | + ) |
| 35 | + |
| 36 | + private def testToken2Seconds(now: Instant): TokenIntrospectionResponse = TokenIntrospectionResponse( |
| 37 | + active = true, |
| 38 | + exp = Some(now.plusSeconds(2)) |
| 39 | + ) |
| 40 | + |
| 41 | + private val inactiveToken: TokenIntrospectionResponse = TokenIntrospectionResponse(active = false) |
| 42 | + |
| 43 | + private val testFailedResponse: TokenIntrospectionResponse = TokenIntrospectionResponse(active = false) |
| 44 | + |
| 45 | + "CachingTokenIntrospection" should { |
| 46 | + "delegate token retrieval on first call" in runTest { case (delegate, cachingIntrospection) => |
| 47 | + for { |
| 48 | + now <- Clock[IO].realTimeInstant |
| 49 | + _ <- delegate.updateTokenResponse(testToken, testToken60Seconds(now)) |
| 50 | + result <- cachingIntrospection.introspect(testToken) |
| 51 | + } yield result shouldBe testToken60Seconds(now) |
| 52 | + } |
| 53 | + |
| 54 | + "return cached response if it's not yet expired" in runTest { case (delegate, cachingIntrospection) => |
| 55 | + for { |
| 56 | + now <- Clock[IO].realTimeInstant |
| 57 | + _ <- delegate.updateTokenResponse(testToken, testToken60Seconds(now)) |
| 58 | + _ <- cachingIntrospection.introspect(testToken) |
| 59 | + _ <- Clock[IO].sleep(3.seconds) |
| 60 | + _ <- delegate.updateTokenResponse(testToken, testToken5Seconds(now)) |
| 61 | + result <- cachingIntrospection.introspect(testToken) |
| 62 | + } yield result shouldBe testToken60Seconds(now) |
| 63 | + } |
| 64 | + |
| 65 | + "fetch the new introspection result if the cache has expired" in runTest { case (delegate, cachingIntrospection) => |
| 66 | + for { |
| 67 | + now <- Clock[IO].realTimeInstant |
| 68 | + _ <- delegate.updateTokenResponse(testToken, testToken60Seconds(now)) |
| 69 | + _ <- cachingIntrospection.introspect(testToken) |
| 70 | + _ <- Clock[IO].sleep(61.seconds) |
| 71 | + _ <- delegate.updateTokenResponse(testToken, inactiveToken) |
| 72 | + result <- cachingIntrospection.introspect(testToken) |
| 73 | + } yield result shouldBe inactiveToken |
| 74 | + } |
| 75 | + |
| 76 | + "fetch the new introspection result if the cache has reached the default ttl" in runTest { case (delegate, cachingIntrospection) => |
| 77 | + for { |
| 78 | + now <- Clock[IO].realTimeInstant |
| 79 | + _ <- delegate.updateTokenResponse(testToken, testToken60Seconds(now)) |
| 80 | + _ <- cachingIntrospection.introspect(testToken) |
| 81 | + _ <- Clock[IO].sleep(58.seconds) |
| 82 | + _ <- delegate.updateTokenResponse(testToken, testToken2Seconds(now)) |
| 83 | + result <- cachingIntrospection.introspect(testToken) |
| 84 | + } yield result shouldBe testToken2Seconds(now) |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + def runTest(test: ((TestTokenIntrospection[IO], CachingTokenIntrospection[IO])) => IO[Assertion]): Assertion = |
| 90 | + unsafeRun(prepareTest.flatMap(test)) match { |
| 91 | + case Succeeded(Some(assertion)) => assertion |
| 92 | + case wrongResult => fail(s"Test should finish successfully. Instead ended with $wrongResult") |
| 93 | + } |
| 94 | + |
| 95 | + private def prepareTest: IO[(TestTokenIntrospection[IO], CachingTokenIntrospection[IO])] = |
| 96 | + for { |
| 97 | + state <- Ref.of[IO, Map[Secret[String], TokenIntrospectionResponse]](Map.empty) |
| 98 | + cache <- CatsRefExpiringCache[IO, Secret[String], TokenIntrospectionResponse] |
| 99 | + delegate = TestTokenIntrospection(state) |
| 100 | + cachingIntrospection = CachingTokenIntrospection[IO](delegate, cache, 5.seconds) |
| 101 | + } yield (delegate, cachingIntrospection) |
| 102 | + |
| 103 | + trait TestTokenIntrospection[F[_]] extends TokenIntrospection[F] { |
| 104 | + def introspect(token: Secret[String]): F[TokenIntrospectionResponse] |
| 105 | + def updateTokenResponse(token: Secret[String], response: TokenIntrospectionResponse): F[Unit] |
| 106 | + } |
| 107 | + |
| 108 | + object TestTokenIntrospection { |
| 109 | + |
| 110 | + def apply[F[_]: Functor](ref: Ref[F, Map[Secret[String], TokenIntrospectionResponse]]): TestTokenIntrospection[F] = |
| 111 | + new TestTokenIntrospection[F] { |
| 112 | + def introspect(token: Secret[String]): F[TokenIntrospectionResponse] = |
| 113 | + ref.get.map(_.get(token).getOrElse(testFailedResponse)) |
| 114 | + |
| 115 | + def updateTokenResponse(token: Secret[String], response: TokenIntrospectionResponse): F[Unit] = |
| 116 | + ref.update(state => state + (token -> response)) |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments