Skip to content

Commit d2ffb14

Browse files
billoneiltsuyoshizawa
authored andcommitted
Give the AuthorizationCode GrantHandler more flow control with flatMap (#90)
* Add sleep in auth code test to show control flow issues * Added a better test for auth code error handling. * Switch onSuccess to flatMap for better control flow in AuthorizationCode GrantHandler
1 parent 48a2f65 commit d2ffb14

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

scala-oauth2-core/src/main/scala/scalaoauth2/provider/GrantHandler.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ class AuthorizationCode extends GrantHandler {
123123
}
124124

125125
val f = issueAccessToken(handler, authInfo)
126-
f onSuccess { case _ => handler.deleteAuthCode(code) }
127-
f
126+
for {
127+
accessToken <- f
128+
deleteResult <- handler.deleteAuthCode(code)
129+
} yield accessToken
128130
}
129131
}
130132

scala-oauth2-core/src/test/scala/scalaoauth2/provider/AuthorizationCodeSpec.scala

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scalaoauth2.provider
33
import org.scalatest.Matchers._
44
import org.scalatest._
55
import org.scalatest.concurrent.ScalaFutures
6+
import org.scalatest.time._
67

78
import scala.concurrent.Future
89
import scala.concurrent.ExecutionContext.Implicits.global
@@ -22,12 +23,13 @@ class AuthorizationCodeSpec extends FlatSpec with ScalaFutures with OptionValues
2223
override def createAccessToken(authInfo: AuthInfo[User]): Future[AccessToken] = Future.successful(AccessToken("token1", Some("refreshToken1"), Some("all"), Some(3600), new java.util.Date()))
2324

2425
override def deleteAuthCode(code: String): Future[Unit] = {
26+
Thread.sleep(300)
2527
codeDeleted = true
2628
Future.successful(Unit)
2729
}
2830
})
2931

30-
whenReady(f) { result =>
32+
whenReady(f, timeout(Span(1, Seconds)), interval(Span(50, Millis))) { result =>
3133
codeDeleted shouldBe true
3234
result.tokenType shouldBe "Bearer"
3335
result.accessToken shouldBe "token1"
@@ -57,4 +59,25 @@ class AuthorizationCodeSpec extends FlatSpec with ScalaFutures with OptionValues
5759
result.scope shouldBe Some("all")
5860
}
5961
}
62+
63+
it should "return a Failure Future" in {
64+
val authorizationCode = new AuthorizationCode()
65+
val request = new AuthorizationRequest(Map(), Map("client_id" -> Seq("clientId1"), "client_secret" -> Seq("clientSecret1"), "code" -> Seq("code1"), "redirect_uri" -> Seq("http://example.com/")))
66+
val f = authorizationCode.handleRequest(request, new MockDataHandler() {
67+
68+
override def findAuthInfoByCode(code: String): Future[Option[AuthInfo[User]]] = Future.successful(Some(
69+
AuthInfo(user = MockUser(10000, "username"), clientId = Some("clientId1"), scope = Some("all"), redirectUri = Some("http://example.com/"))
70+
))
71+
72+
override def createAccessToken(authInfo: AuthInfo[User]): Future[AccessToken] = Future.successful(AccessToken("token1", Some("refreshToken1"), Some("all"), Some(3600), new java.util.Date()))
73+
74+
override def deleteAuthCode(code: String): Future[Unit] = {
75+
Future.failed(new Exception())
76+
}
77+
})
78+
79+
whenReady(f.failed) { e =>
80+
e shouldBe a[Exception]
81+
}
82+
}
6083
}

0 commit comments

Comments
 (0)