Skip to content

Commit

Permalink
Rework token service mocking for test #3421
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Jan 29, 2025
1 parent ecc9dce commit 10452ec
Showing 1 changed file with 7 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
package au.org.ala.fieldcapture

import au.org.ala.web.Pac4jContextProvider

import au.org.ala.ws.service.WebService
import au.org.ala.ws.tokens.TokenClient
import au.org.ala.ws.tokens.TokenService
import com.nimbusds.oauth2.sdk.id.Issuer
import com.nimbusds.openid.connect.sdk.SubjectType
import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata
import com.nimbusds.oauth2.sdk.token.AccessToken
import com.nimbusds.oauth2.sdk.token.BearerAccessToken
import groovy.json.JsonSlurper
import org.apache.http.HttpStatus
import org.apache.http.entity.ContentType
import org.grails.testing.GrailsUnitTest
import org.pac4j.core.config.Config
import org.pac4j.core.context.FrameworkParameters
import org.pac4j.core.context.WebContext
import org.pac4j.jee.context.JEEContextFactory
import org.pac4j.jee.context.JEEFrameworkParameters
import org.pac4j.jee.context.session.JEESessionStore
import org.pac4j.jee.context.session.JEESessionStoreFactory
import org.pac4j.oidc.config.OidcConfiguration
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import pages.RlpProjectPage
import spock.lang.Stepwise

Expand All @@ -43,32 +31,10 @@ class ParatooIntegrationSpec extends StubbedCasSpec implements GrailsUnitTest {
}

def setup() {
def config = Stub(Config)
def oidcConfiguration = Stub(OidcConfiguration)
oidcConfiguration.clientId >> testConfig.security.oidc.clientId
oidcConfiguration.secret >> testConfig.security.oidc.secret
def providerMetadata = new OIDCProviderMetadata(new Issuer(testConfig.issuerURI), [SubjectType.PUBLIC], testConfig.jwkURI.toURI())
providerMetadata.setTokenEndpointURI(testConfig.tokenURI.toURI())
oidcConfiguration.findProviderMetadata() >> providerMetadata
def request = new MockHttpServletRequest()
request.getSession(true)
def response = new MockHttpServletResponse()
def pac4jContextProvider = new Pac4jContextProvider() {
@Override
WebContext webContext() {
JEEContextFactory.INSTANCE.newContext(request, response)
}

@Override
FrameworkParameters frameworkParameters() {
new JEEFrameworkParameters(request, response)
}
}
JEEFrameworkParameters params = new JEEFrameworkParameters(request, response)
def sessionStore = JEESessionStoreFactory.INSTANCE.newSessionStore(params)
def tokenClient = new TokenClient(oidcConfiguration)
def tokenService = new TokenService(config, oidcConfiguration, pac4jContextProvider, sessionStore, tokenClient, testConfig.webservice["client-id"], testConfig.webservice["client-secret"], testConfig.webservice["jwt-scopes"], false)

String token = tokenForUser('1')
AccessToken accessToken = new BearerAccessToken(token)
TokenService tokenService = Stub(TokenService)
tokenService.getAuthToken(_) >> accessToken
webService = new WebService(grailsApplication: getGrailsApplication(), tokenService: tokenService)
}

Expand Down

0 comments on commit 10452ec

Please sign in to comment.