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

Commit

Permalink
contract-tests: Update according to #2939 suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
pmacik committed Jan 22, 2019
1 parent 0372a2c commit 739a01e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .make/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ test-contracts-consumer-no-coverage:
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -e 'contracts/consumer'))
PACT_DIR=$(PWD)/test/contracts/pacts \
PACT_VERSION="latest" \
go test -v $(GO_TEST_VERBOSITY_FLAG) -count=1 $(TEST_PACKAGES)
go test $(GO_TEST_VERBOSITY_FLAG) -count=1 $(TEST_PACKAGES)

.PHONY: test-remote
## Runs the remote tests and produces coverage files for each package.
Expand Down
24 changes: 3 additions & 21 deletions test/contracts/consumer/fabric8auth/api_status.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package fabric8auth

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

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
Expand All @@ -15,23 +13,6 @@ 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().
Expand All @@ -49,7 +30,8 @@ func AuthAPIStatus(t *testing.T, pact *dsl.Pact) {
})

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

if err := pact.Verify(SimpleGetInteraction(pact, "/api/status")); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
}
28 changes: 4 additions & 24 deletions test/contracts/consumer/fabric8auth/api_token.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package fabric8auth

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

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
Expand All @@ -15,32 +13,14 @@ 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")},
Method: "GET",
Path: dsl.String("/api/token/keys"),
}).
WillRespondWith(dsl.Response{
Status: 200,
Expand All @@ -49,7 +29,7 @@ func AuthAPITokenKeys(t *testing.T, pact *dsl.Pact) {
})

// Verify
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
if err := pact.Verify(SimpleGetInteraction(pact, "/api/token/keys")); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
}
107 changes: 10 additions & 97 deletions test/contracts/consumer/fabric8auth/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fabric8auth
import (
"fmt"
"log"
"net/http"
"testing"

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
Expand All @@ -15,23 +14,6 @@ func AuthAPIUserByName(t *testing.T, pact *dsl.Pact, userName string) {

log.Println("Invoking AuthAPIUserByName test interaction now")

// Pass in test case
var test = func() error {
url := fmt.Sprintf("http://localhost:%d/api/users?filter[username]=%s", pact.Server.Port, userName)
req, err := http.NewRequest("GET", url, 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().
Expand All @@ -55,8 +37,8 @@ func AuthAPIUserByName(t *testing.T, pact *dsl.Pact, userName string) {
})

// Verify
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
if err := pact.Verify(SimpleGetInteraction(pact, fmt.Sprintf("/api/users?filter[username]=%s", userName))); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
}

Expand All @@ -65,23 +47,6 @@ func AuthAPIUserByID(t *testing.T, pact *dsl.Pact, userID string) {

log.Printf("Invoking AuthAPIUserByID test interaction now\n")

// Pass in test case
var test = func() error {
url := fmt.Sprintf("http://localhost:%d/api/users/%s", pact.Server.Port, userID)
req, err := http.NewRequest("GET", url, 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().
Expand All @@ -102,8 +67,8 @@ func AuthAPIUserByID(t *testing.T, pact *dsl.Pact, userID string) {
})

// Verify
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
if err := pact.Verify(SimpleGetInteraction(pact, fmt.Sprintf("/api/users/%s", userID))); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
}

Expand All @@ -114,23 +79,6 @@ func AuthAPIUserByToken(t *testing.T, pact *dsl.Pact, userToken string) {
log.Printf("Invoking AuthAPIUserByToken test interaction now\n")

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

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", userToken))
if err != nil {
return err
}

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

// Set up our expected interactions.
pact.
AddInteraction().
Expand All @@ -154,8 +102,8 @@ func AuthAPIUserByToken(t *testing.T, pact *dsl.Pact, userToken string) {
})

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

Expand All @@ -164,24 +112,6 @@ func AuthAPIUserInvalidToken(t *testing.T, pact *dsl.Pact, invalidToken string)

log.Printf("Invoking AuthAPIUserInvalidToken test interaction now\n")

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

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", invalidToken))
if err != nil {
return err
}

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

// Set up our expected interactions.
pact.
AddInteraction().
Expand All @@ -205,8 +135,8 @@ func AuthAPIUserInvalidToken(t *testing.T, pact *dsl.Pact, invalidToken string)
})

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

Expand All @@ -215,23 +145,6 @@ func AuthAPIUserNoToken(t *testing.T, pact *dsl.Pact) {

log.Printf("Invoking AuthAPIUserNoToken test interaction now\n")

// Pass in test case
var test = func() error {
url := fmt.Sprintf("http://localhost:%d/api/user", pact.Server.Port)
req, err := http.NewRequest("GET", url, 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().
Expand All @@ -251,7 +164,7 @@ func AuthAPIUserNoToken(t *testing.T, pact *dsl.Pact) {
})

// Verify
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
if err := pact.Verify(SimpleGetInteraction(pact, "/api/user")); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
}
20 changes: 11 additions & 9 deletions test/contracts/consumer/fabric8auth/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestWitAPIConsumer(t *testing.T) {
var pactDir = os.Getenv("PACT_DIR")
var pactVersion = os.Getenv("PACT_VERSION")

var pactConsumer = "Fabric8Wit"
var pactProvider = "Fabric8Auth"
var pactConsumer = "fabric8-wit"
var pactProvider = "fabric8-auth"

var pactBrokerURL = os.Getenv("PACT_BROKER_URL")
var pactBrokerUsername = os.Getenv("PACT_BROKER_USERNAME")
Expand All @@ -38,17 +38,19 @@ func TestWitAPIConsumer(t *testing.T) {
defer pact.Teardown()

// Test interactions
AuthAPIStatus(t, pact)
t.Run("api status", func(t *testing.T) { AuthAPIStatus(t, pact) })

AuthAPIUserByName(t, pact, model.TestUserName)
AuthAPIUserByID(t, pact, model.TestUserID)
AuthAPIUserByToken(t, pact, model.TestJWSToken)
t.Run("api user by name", func(t *testing.T) { AuthAPIUserByName(t, pact, model.TestUserName) })
t.Run("api user by id", func(t *testing.T) { AuthAPIUserByID(t, pact, model.TestUserID) })
t.Run("api user by valid token", func(t *testing.T) { AuthAPIUserByToken(t, pact, model.TestJWSToken) })
t.Run("api user with no token", func(t *testing.T) { AuthAPIUserNoToken(t, pact) })
t.Run("api user with invalid token", func(t *testing.T) { AuthAPIUserInvalidToken(t, pact, model.TestInvalidJWSToken) })

AuthAPITokenKeys(t, pact)
t.Run("api token keys", func(t *testing.T) { AuthAPITokenKeys(t, pact) })

// Write a pact file
pactFile := contracts.PactFile(pactConsumer, pactProvider)
log.Printf("All tests done, writting a pact file (%s).\n", pactFile)
log.Printf("All tests done, writing a pact file (%s).\n", pactFile)
pact.WritePact()

log.Printf("Publishing pact to a broker (%s)...\n", pactBrokerURL)
Expand All @@ -64,6 +66,6 @@ func TestWitAPIConsumer(t *testing.T) {
})

if err != nil {
log.Fatalf("Unable to publish pact to a broker (%s):\n%q\n", pactBrokerURL, err)
log.Fatalf("Unable to publish pact to a broker (%s):\n%+v\n", pactBrokerURL, err)
}
}
51 changes: 51 additions & 0 deletions test/contracts/consumer/fabric8auth/verify_interactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fabric8auth

import (
"fmt"
"net/http"

"github.com/pact-foundation/pact-go/dsl"
)

// SimpleGetInteraction executes a simple GET request against Pact Mock server
// to verify the interaction
func SimpleGetInteraction(pact *dsl.Pact, endpoint string) func() error {
var test = func() error {
u := fmt.Sprintf("http://localhost:%d%s", pact.Server.Port, endpoint)
req, err := http.NewRequest("GET", u, nil)

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

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

// SimpleGetInteractionWithToken executes a simple GET request (incl. the "Authorization: Bearer <token>" header)
// against Pact Mock server to verify the interaction
func SimpleGetInteractionWithToken(pact *dsl.Pact, endpoint string, authToken string) func() error {
var test = func() error {
u := fmt.Sprintf("http://localhost:%d%s", pact.Server.Port, endpoint)
req, err := http.NewRequest("GET", u, nil)

if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken))

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

0 comments on commit 739a01e

Please sign in to comment.