Skip to content

Commit

Permalink
feat(Gradle): Add auth option for no auth
Browse files Browse the repository at this point in the history
  • Loading branch information
uglyog committed Jun 24, 2022
1 parent 7027f09 commit 8327184
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import java.net.URI
* Authentication options
*/
sealed class Auth {
/**
* No Auth
*/
object None : Auth()

/**
* Basic authentication (username/password)
*/
Expand All @@ -44,13 +49,15 @@ sealed class Auth {
ep.parseExpression(this.username, DataType.RAW, resolver).toString(),
ep.parseExpression(this.password, DataType.RAW, resolver).toString())
is BearerAuthentication -> BearerAuthentication(ep.parseExpression(this.token, DataType.RAW, resolver).toString())
else -> this
}
}

fun legacyForm(): List<String> {
return when (this) {
is BasicAuthentication -> listOf("basic", this.username, this.password)
is BearerAuthentication -> listOf("bearer", this.token)
else -> emptyList()
}
}
}
Expand Down Expand Up @@ -83,6 +90,7 @@ object HttpClient : KLogging() {
defaultHeaders["Authorization"] = "Bearer " + authentication.token
SystemDefaultCredentialsProvider()
}
else -> SystemDefaultCredentialsProvider()
}
}
is List<*> -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.dius.pact.provider.gradle

import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector
import au.com.dius.pact.core.support.Auth
import au.com.dius.pact.provider.PactVerification
import org.gradle.api.GradleScriptException
import org.gradle.api.Project
Expand All @@ -9,7 +10,7 @@ import spock.lang.Unroll

class GradleProviderInfoSpec extends Specification {

def 'defaults the consumer verification type to what is set on the provider'() {
def 'hasPactWith - defaults the consumer verification type to what is set on the provider'() {
given:
def provider = new GradleProviderInfo('provider', Mock(Project))
provider.verificationType = PactVerification.ANNOTATED_METHOD
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package au.com.dius.pact.provider.gradle

import au.com.dius.pact.core.support.Auth
import au.com.dius.pact.provider.PactVerification
import org.gradle.api.Project
import org.gradle.api.ProjectConfigurationException
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

class PactPluginSpec extends Specification {

private PactPlugin plugin
private Project project

void setup() {
project = ProjectBuilder.builder().build()
plugin = new PactPlugin()
plugin.apply(project)
}

def 'defines a pactVerify task'() {
expect:
project.tasks.pactVerify
}

def 'defines a pactPublish task'() {
expect:
project.tasks.pactPublish
}

def 'defines a task for each defined provider'() {
given:
project.pact {
serviceProviders {
provider1 {

}

provider2 {

}
}
}

when:
project.evaluate()

then:
project.tasks.pactVerify_provider1
project.tasks.pactVerify_provider2
}

def 'replaces white space with underscores'() {
given:
project.pact {
serviceProviders {
'invalid Name' {

}
}
}

when:
project.evaluate()

then:
project.tasks.pactVerify_invalid_Name
}

def 'defines a task for each file in the pact file directory'() {
given:
def resource = getClass().classLoader.getResource('pacts/foo_pact.json')
File pactFileDirectory = new File(resource.file).parentFile
project.pact {
serviceProviders {
provider1 {
hasPactsWith('many consumers') {
pactFileLocation = project.file("${pactFileDirectory.absolutePath}")
stateChange = 'http://localhost:8080/state'
}
}
}
}

when:
project.evaluate()
def consumers = project.tasks.pactVerify_provider1.providerToVerify.consumers

then:
consumers.size() == 2
consumers.find { it.name == 'Foo Consumer' }
consumers.find { it.name == 'Bar Consumer' }
}

def 'configures the providers and consumers correctly'() {
given:
def pactFileUrl = 'http://localhost:8000/pacts/provider/prividera/consumer/consumera/latest'
def stateChangeUrl = 'http://localhost:8080/stateChange'
project.pact {
serviceProviders {
ProviderA { providerInfo ->
startProviderTask = 'jettyEclipseRun'
terminateProviderTask = 'jettyEclipseStop'

port = 1234

hasPactWith('ConsumerA') {
pactSource = pactFileUrl
stateChange = stateChangeUrl
verificationType = 'REQUEST_RESPONSE'
}
}
}
}

when:
project.evaluate()

def provider = project.tasks.pactVerify_ProviderA.providerToVerify
def consumer = provider.consumers.first()

then:
provider.startProviderTask == 'jettyEclipseRun'
provider.terminateProviderTask == 'jettyEclipseStop'
provider.port == 1234

consumer.name == 'ConsumerA'
consumer.pactSource == pactFileUrl
consumer.stateChange == stateChangeUrl
consumer.verificationType == PactVerification.REQUEST_RESPONSE
}

def 'do not set the state change url automatically'() {
given:
def pactFileUrl = 'http://localhost:8000/pacts/provider/prividera/consumer/consumera/latest'
project.pact {
serviceProviders {
ProviderA { providerInfo ->
hasPactWith('ConsumerA') {
pactSource = url(pactFileUrl)
}
}
}
}

when:
project.evaluate()
def consumer = project.tasks.pactVerify_ProviderA.providerToVerify.consumers.first()

then:
consumer.pactSource.toString() == pactFileUrl
consumer.stateChange == null
}

def 'configures the publish task correctly'() {
given:
project.pact {

serviceProviders {
ProviderA {
hasPactWith('ConsumerA') {

}
}
}

publish {
pactDirectory = '/pact/dir'
pactBrokerUrl = 'http://pactbroker:1234'
}
}

when:
project.evaluate()

then:
project.pact.publish.pactDirectory == '/pact/dir'
project.pact.publish.pactBrokerUrl == 'http://pactbroker:1234'
}

def 'fails if there pact is not a valid configuration'() {
given:
project.ext.pact = '123'
project.pact {
serviceProviders {
ProviderA {
hasPactWith('ConsumerA') {

}
}
}
}

when:
project.evaluate()

then:
thrown(ProjectConfigurationException)
}

def 'hasPactWith - allows all the values for the consumer to be configured'() {
given:
project.pact {
serviceProviders {
ProviderA {
hasPactWith('boddy the consumer') {
stateChange = url('http://localhost:8001/tasks/pactStateChange')
stateChangeUsesBody = false
packagesToScan = ['one', 'two']
verificationType = PactVerification.REQUEST_RESPONSE
pactSource = project.file('path/to/pact')
}
}
}
}

when:
project.evaluate()
def consumer = project.tasks.pactVerify_ProviderA.providerToVerify.consumers.first()

then:
consumer.name == 'boddy the consumer'
consumer.auth == Auth.None.INSTANCE
consumer.packagesToScan == ['one', 'two']
consumer.pactSource instanceof File
consumer.pactSource.toString().endsWith('path/to/pact')
consumer.stateChange.toString() == 'http://localhost:8001/tasks/pactStateChange'
!consumer.stateChangeUsesBody
}
}
Loading

0 comments on commit 8327184

Please sign in to comment.