Skip to content

Commit

Permalink
feat: support system properties with PactUrl authentication #1224
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Oct 18, 2020
1 parent be912c9 commit 63d19cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package au.com.dius.pact.core.support

import au.com.dius.pact.core.support.expressions.DataType
import au.com.dius.pact.core.support.expressions.ExpressionParser.parseExpression
import au.com.dius.pact.core.support.expressions.ValueResolver
import mu.KLogging
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
Expand All @@ -15,6 +18,14 @@ import java.net.URI
sealed class Auth {
data class BasicAuthentication(val username: String, val password: String) : Auth()
data class BearerAuthentication(val token: String) : Auth()

fun resolveProperties(resolver: ValueResolver): Auth {
return when (this) {
is BasicAuthentication -> BasicAuthentication(parseExpression(this.username, DataType.RAW, resolver).toString(),
parseExpression(this.password, DataType.RAW, resolver).toString())
is BearerAuthentication -> BearerAuthentication(parseExpression(this.token, DataType.RAW, resolver).toString())
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import au.com.dius.pact.core.model.PactSource
import au.com.dius.pact.core.model.UrlSource
import au.com.dius.pact.core.model.UrlsSource
import au.com.dius.pact.core.support.Auth
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
import au.com.dius.pact.core.support.expressions.ValueResolver

/**
* Implementation of [PactLoader] that downloads pacts from given urls
*/
open class PactUrlLoader(val urls: Array<String>, val authentication: Auth? = null) : PactLoader {
lateinit var pactSource: UrlsSource<Interaction>
var pactReader: PactReader = DefaultPactReader
var resolver: ValueResolver = SystemPropertyResolver()

constructor(pactUrl: PactUrl) : this(pactUrl.urls, when {
pactUrl.auth.token.isNotEmpty() -> Auth.BearerAuthentication(pactUrl.auth.token)
Expand All @@ -30,7 +33,7 @@ open class PactUrlLoader(val urls: Array<String>, val authentication: Auth? = nu
return urls.map { url ->
val options = mutableMapOf<String, Any>()
if (authentication != null) {
options["authentication"] = authentication
options["authentication"] = authentication.resolveProperties(resolver)
}
val pact = pactReader.loadPact(UrlSource<Interaction>(url), options)
pactSource.addPact(url, pact as Pact<Interaction>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.UrlSource
import au.com.dius.pact.core.support.Auth
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties

@SuppressWarnings('LineLength')
class PactUrlLoaderSpec extends Specification {
Expand All @@ -20,6 +21,9 @@ class PactUrlLoaderSpec extends Specification {
@PactUrl(urls = ['http://localhost:1234'], auth = @Authentication(token = '1234abcd'))
static class TestClass3 { }

@PactUrl(urls = ['http://localhost:1234'], auth = @Authentication(token = '${my.token}'))
static class TestClass4 { }

def 'loads a pact from each URL'() {
given:
def loader = new PactUrlLoader(['http://123.456', 'http://localhost:1234'] as String[], null)
Expand Down Expand Up @@ -87,4 +91,20 @@ class PactUrlLoaderSpec extends Specification {
loader.pactReader.loadPact(new UrlSource('http://localhost:1234'), [authentication: new Auth.BearerAuthentication('1234abcd')]) >> pact1
pacts == [pact1]
}

@RestoreSystemProperties
def 'loads the auth values from system properties'() {
given:
def loader = new PactUrlLoader(TestClass4.getAnnotation(PactUrl))
System.setProperty('my.token', '1234567890')
loader.pactReader = Mock(PactReader)
def pact1 = new RequestResponsePact(new Provider('bob'), new Consumer('consumer1'))

when:
def pacts = loader.load('bob')

then:
loader.pactReader.loadPact(new UrlSource('http://localhost:1234'), [authentication: new Auth.BearerAuthentication('1234567890')]) >> pact1
pacts == [pact1]
}
}

0 comments on commit 63d19cf

Please sign in to comment.