Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
Add contract tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmacik committed Nov 15, 2018
1 parent 5ee62db commit 880cd85
Show file tree
Hide file tree
Showing 11 changed files with 1,599 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .make/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,37 @@ test-integration-benchmark: prebuild-check migrate-database $(SOURCES)
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
AUTH_DEVELOPER_MODE_ENABLED=1 AUTH_LOG_LEVEL=error AUTH_RESOURCE_DATABASE=1 AUTH_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test -vet off -run=^$$ -bench=. -cpu 1,2,4 -test.benchmem $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)

.PHONY: test-contracts-consumer
## Runs the consumer part of the contract tests to re-generate the local pact file
test-contracts-consumer:
$(call log-info,"Running test: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep 'contracts/consumer'))
PACT_DIR=$(PWD)/test/contracts/pacts \
PACT_CONSUMER=Fabric8AuthGeneralConsumer \
PACT_PROVIDER=Fabric8Auth \
PACT_VERSION=1.0.0 \
go test -count=1 $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)

.PHONY: test-contracts-no-coverage
## Runs the contract tests WITHOUT producing coverage files for each package.
## Make sure you ran "make dev" before you run this target.
## The following env variables needs to be set in environment:
## - RHD account credentials:
## OSIO_USERNAME
## OSIO_PASSWORD
## - Service account credentials (according to https://github.com/fabric8-services/fabric8-auth/blob/master/configuration/conf-files/service-account-secrets.conf#L30)
## AUTH_SERVICE_ACCOUNT_CLIENT_ID
## AUTH_SERVICE_ACCOUNT_CLIENT_SERCRET
test-contracts-no-coverage: prebuild-check migrate-database $(SOURCES)
$(call log-info,"Running test: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep 'contracts/provider'))
PACT_DIR=$(PWD)/test/contracts/pacts \
PACT_CONSUMER=Fabric8AuthGeneralConsumer \
PACT_PROVIDER=Fabric8Auth \
PACT_VERSION=1.0.0 \
PACT_PROVIDER_BASE_URL=http://localhost:8089 \
go test -count=1 $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)

.PHONY: test-remote-with-coverage
## Runs the remote tests and produces coverage files for each package.
test-remote-with-coverage: prebuild-check clean-coverage-remote $(COV_PATH_REMOTE)
Expand Down
39 changes: 38 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
consumer/logs
provider/log
pacts/provider-*.json
55 changes: 55 additions & 0 deletions test/contracts/consumer/auth_api_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package consumer

import (
"fmt"
"log"
"net/http"
"testing"

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
"github.com/pact-foundation/pact-go/dsl"
)

// AuthAPIStatus defines contract of /api/status endpoint
func AuthAPIStatus(t *testing.T, pact *dsl.Pact) {

log.Printf("Invoking AuthAPIStatus now\n")

// Pass in test case
var test = func() error {
u := fmt.Sprintf("http://localhost:%d/api/status", pact.Server.Port)
req, err := http.NewRequest("GET", u, nil)

req.Header.Set("Content-Type", "application/json")
if err != nil {
return err
}

_, err = http.DefaultClient.Do(req)
if err != nil {
return err
}
return err
}

// Set up our expected interactions.
pact.
AddInteraction().
Given("Auth service is up and running.").
UponReceiving("A request to get status").
WithRequest(dsl.Request{
Method: "GET",
Path: dsl.String("/api/status"),
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.status+json")},
Body: dsl.Match(model.APIStatusMessage{}),
})

// Verify
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
}
}
55 changes: 55 additions & 0 deletions test/contracts/consumer/auth_api_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package consumer

import (
"fmt"
"log"
"net/http"
"testing"

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
"github.com/pact-foundation/pact-go/dsl"
)

// AuthAPITokenKeys defines contract of /api/status endpoint
func AuthAPITokenKeys(t *testing.T, pact *dsl.Pact) {

log.Printf("Invoking AuthAPITokenKeys now\n")

// Pass in test case
var test = func() error {
u := fmt.Sprintf("http://localhost:%d/api/token/keys", pact.Server.Port)
req, err := http.NewRequest("GET", u, nil)

req.Header.Set("Accept", "application/json")
if err != nil {
return err
}

_, err = http.DefaultClient.Do(req)
if err != nil {
return err
}
return err
}

// Set up our expected interactions.
pact.
AddInteraction().
Given("Auth service is up and running.").
UponReceiving("A request to get public keys").
WithRequest(dsl.Request{
Method: "GET",
Path: dsl.String("/api/token/keys"),
Headers: dsl.MapMatcher{"Accept": dsl.String("application/json")},
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.publickeys+json")},
Body: dsl.Match(model.TokenKeys{}),
})

// Verify
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
}
}
Loading

0 comments on commit 880cd85

Please sign in to comment.