1+ package scalaoauth2 .provider
2+
3+ import java .util .Date
4+
5+ import akka .http .scaladsl .model .headers .OAuth2BearerToken
6+ import akka .http .scaladsl .server .directives .Credentials
7+ import akka .http .scaladsl .testkit .ScalatestRouteTest
8+ import org .scalatest .concurrent .ScalaFutures
9+ import org .scalatest .{ Matchers , WordSpec }
10+ import akka .http .scaladsl .model .StatusCodes ._
11+ import akka .http .scaladsl .model .FormData
12+ import scala .concurrent .Future
13+
14+ class OAuth2ProviderSpec extends WordSpec with Matchers with ScalatestRouteTest with ScalaFutures {
15+
16+ val tokenEndpointCredentials = new TokenEndpoint {
17+ override val handlers = Map (
18+ OAuthGrantType .CLIENT_CREDENTIALS -> new ClientCredentials
19+ )
20+ }
21+
22+ val oauth2ProviderFail = new OAuth2Provider [User ] {
23+ override val oauth2DataHandler = new MockDataHandler ()
24+ override val tokenEndpoint = tokenEndpointCredentials
25+ }
26+
27+ val user = MockUser (1 , " user" )
28+ val someAuthInfo = Some (AuthInfo (user, Some (" clientId" ), None , None ))
29+ val accessToken = AccessToken (" token" , Some (" refresh token" ), None , Some (3600 ), new Date )
30+
31+ val oauth2ProviderSuccess = new OAuth2Provider [User ] {
32+ override val tokenEndpoint = tokenEndpointCredentials
33+ override val oauth2DataHandler = new MockDataHandler () {
34+ override def findAccessToken (token : String ): Future [Option [AccessToken ]] =
35+ Future .successful(Some (accessToken))
36+ override def findAuthInfoByAccessToken (accessToken : AccessToken ): Future [Option [AuthInfo [User ]]] =
37+ Future .successful(someAuthInfo)
38+ override def findUser (request : AuthorizationRequest ): Future [Option [User ]] =
39+ Future .successful(Some (user))
40+ override def validateClient (request : AuthorizationRequest ): Future [Boolean ] =
41+ Future .successful(true )
42+ override def getStoredAccessToken (authInfo : AuthInfo [User ]): Future [Option [AccessToken ]] =
43+ Future .successful(Some (accessToken))
44+ override def createAccessToken (authInfo : AuthInfo [User ]): Future [AccessToken ] =
45+ Future .successful(accessToken)
46+ }
47+ }
48+
49+ " oauth2Authenticator" should {
50+
51+ " return none when data handler cannot find access token" in {
52+ val r = oauth2ProviderFail.oauth2Authenticator(Credentials (Some (OAuth2BearerToken (" token" ))))
53+ whenReady(r) { result => result should be(None ) }
54+ }
55+
56+ " return none when there is not a bearer token in request" in {
57+ val r = oauth2ProviderSuccess.oauth2Authenticator(Credentials (None ))
58+ whenReady(r) { result => result should be(None ) }
59+ }
60+
61+ " return some authinfo when there is a token match" in {
62+ val r = oauth2ProviderSuccess.oauth2Authenticator(Credentials (Some (OAuth2BearerToken (" token" ))))
63+ whenReady(r) { result => result should be(someAuthInfo) }
64+ }
65+
66+ }
67+
68+ " access token route" should {
69+
70+ " return Unauthorized when there is an error on authorization" in {
71+ Post (" /oauth/access_token" , FormData (
72+ " client_id" -> " bob_client_id" ,
73+ " client_secret" -> " bob_client_secret" , " grant_type" -> " client_credentials"
74+ )) ~> oauth2ProviderFail.accessTokenRoute ~> check {
75+ handled shouldEqual true
76+ status shouldEqual Unauthorized
77+ }
78+ }
79+
80+ " return Ok with token respons when there is a valid authorization" in {
81+ Post (" /oauth/access_token" , FormData (
82+ " client_id" -> " bob_client_id" ,
83+ " client_secret" -> " bob_client_secret" , " grant_type" -> " client_credentials"
84+ )) ~> oauth2ProviderSuccess.accessTokenRoute ~> check {
85+ handled shouldEqual true
86+ status shouldEqual OK
87+ }
88+ }
89+
90+ }
91+
92+ }
0 commit comments