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

Commit 918536f

Browse files
committed
Add contract tests.
1 parent 5ee62db commit 918536f

File tree

11 files changed

+1600
-1
lines changed

11 files changed

+1600
-1
lines changed

.make/test.mk

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@ test-integration-benchmark: prebuild-check migrate-database $(SOURCES)
171171
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
172172
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)
173173

174+
.PHONY: test-contracts-consumer
175+
## Runs the consumer part of the contract tests to re-generate the local pact file
176+
test-contracts-consumer:
177+
$(call log-info,"Running test: $@")
178+
$(eval TEST_PACKAGES:=$(shell go list ./... | grep 'contracts/consumer'))
179+
PACT_DIR=$(PWD)/test/contracts/pacts \
180+
PACT_CONSUMER=Fabric8AuthGeneralConsumer \
181+
PACT_PROVIDER=Fabric8Auth \
182+
PACT_VERSION=1.0.0 \
183+
go test -count=1 $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)
184+
185+
.PHONY: test-contracts-no-coverage
186+
## Runs the contract tests WITHOUT producing coverage files for each package.
187+
## Make sure you ran "make dev" before you run this target.
188+
## The [chromedriver](http://chromedriver.chromium.org/) is required to be installed for the user login part of the tests.
189+
## The following env variables needs to be set in environment:
190+
## - RHD account credentials:
191+
## OSIO_USERNAME
192+
## OSIO_PASSWORD
193+
## - Service account credentials (according to https://github.com/fabric8-services/fabric8-auth/blob/master/configuration/conf-files/service-account-secrets.conf#L30)
194+
## AUTH_SERVICE_ACCOUNT_CLIENT_ID
195+
## AUTH_SERVICE_ACCOUNT_CLIENT_SERCRET
196+
test-contracts-no-coverage: prebuild-check migrate-database $(SOURCES)
197+
$(call log-info,"Running test: $@")
198+
$(eval TEST_PACKAGES:=$(shell go list ./... | grep 'contracts/provider'))
199+
PACT_DIR=$(PWD)/test/contracts/pacts \
200+
PACT_CONSUMER=Fabric8AuthGeneralConsumer \
201+
PACT_PROVIDER=Fabric8Auth \
202+
PACT_VERSION=1.0.0 \
203+
PACT_PROVIDER_BASE_URL=http://localhost:8089 \
204+
go test -count=1 $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)
205+
174206
.PHONY: test-remote-with-coverage
175207
## Runs the remote tests and produces coverage files for each package.
176208
test-remote-with-coverage: prebuild-check clean-coverage-remote $(COV_PATH_REMOTE)

Gopkg.lock

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/contracts/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
consumer/logs
2+
provider/log
3+
pacts/provider-*.json
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package consumer
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/fabric8-services/fabric8-auth/test/contracts/model"
10+
"github.com/pact-foundation/pact-go/dsl"
11+
)
12+
13+
// AuthAPIStatus defines contract of /api/status endpoint
14+
func AuthAPIStatus(t *testing.T, pact *dsl.Pact) {
15+
16+
log.Printf("Invoking AuthAPIStatus now\n")
17+
18+
// Pass in test case
19+
var test = func() error {
20+
u := fmt.Sprintf("http://localhost:%d/api/status", pact.Server.Port)
21+
req, err := http.NewRequest("GET", u, nil)
22+
23+
req.Header.Set("Content-Type", "application/json")
24+
if err != nil {
25+
return err
26+
}
27+
28+
_, err = http.DefaultClient.Do(req)
29+
if err != nil {
30+
return err
31+
}
32+
return err
33+
}
34+
35+
// Set up our expected interactions.
36+
pact.
37+
AddInteraction().
38+
Given("Auth service is up and running.").
39+
UponReceiving("A request to get status").
40+
WithRequest(dsl.Request{
41+
Method: "GET",
42+
Path: dsl.String("/api/status"),
43+
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
44+
}).
45+
WillRespondWith(dsl.Response{
46+
Status: 200,
47+
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.status+json")},
48+
Body: dsl.Match(model.APIStatusMessage{}),
49+
})
50+
51+
// Verify
52+
if err := pact.Verify(test); err != nil {
53+
log.Fatalf("Error on Verify: %v", err)
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package consumer
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/fabric8-services/fabric8-auth/test/contracts/model"
10+
"github.com/pact-foundation/pact-go/dsl"
11+
)
12+
13+
// AuthAPITokenKeys defines contract of /api/status endpoint
14+
func AuthAPITokenKeys(t *testing.T, pact *dsl.Pact) {
15+
16+
log.Printf("Invoking AuthAPITokenKeys now\n")
17+
18+
// Pass in test case
19+
var test = func() error {
20+
u := fmt.Sprintf("http://localhost:%d/api/token/keys", pact.Server.Port)
21+
req, err := http.NewRequest("GET", u, nil)
22+
23+
req.Header.Set("Accept", "application/json")
24+
if err != nil {
25+
return err
26+
}
27+
28+
_, err = http.DefaultClient.Do(req)
29+
if err != nil {
30+
return err
31+
}
32+
return err
33+
}
34+
35+
// Set up our expected interactions.
36+
pact.
37+
AddInteraction().
38+
Given("Auth service is up and running.").
39+
UponReceiving("A request to get public keys").
40+
WithRequest(dsl.Request{
41+
Method: "GET",
42+
Path: dsl.String("/api/token/keys"),
43+
Headers: dsl.MapMatcher{"Accept": dsl.String("application/json")},
44+
}).
45+
WillRespondWith(dsl.Response{
46+
Status: 200,
47+
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.publickeys+json")},
48+
Body: dsl.Match(model.TokenKeys{}),
49+
})
50+
51+
// Verify
52+
if err := pact.Verify(test); err != nil {
53+
log.Fatalf("Error on Verify: %v", err)
54+
}
55+
}

0 commit comments

Comments
 (0)