From 739a01e1f7694431eac4960f100ece1b71e38083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?PaveL=20Mac=C3=ADk?= Date: Mon, 21 Jan 2019 13:20:47 +0100 Subject: [PATCH] contract-tests: Update according to #2939 suggestions --- .make/test.mk | 2 +- .../consumer/fabric8auth/api_status.go | 24 +--- .../consumer/fabric8auth/api_token.go | 28 +---- .../consumer/fabric8auth/api_user.go | 107 ++---------------- .../consumer/fabric8auth/consumer_test.go | 20 ++-- .../fabric8auth/verify_interactions.go | 51 +++++++++ 6 files changed, 80 insertions(+), 152 deletions(-) create mode 100644 test/contracts/consumer/fabric8auth/verify_interactions.go diff --git a/.make/test.mk b/.make/test.mk index 79c771d3da..93143c017c 100644 --- a/.make/test.mk +++ b/.make/test.mk @@ -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. diff --git a/test/contracts/consumer/fabric8auth/api_status.go b/test/contracts/consumer/fabric8auth/api_status.go index a757f1b48e..8d2f85f674 100644 --- a/test/contracts/consumer/fabric8auth/api_status.go +++ b/test/contracts/consumer/fabric8auth/api_status.go @@ -1,9 +1,7 @@ package fabric8auth import ( - "fmt" "log" - "net/http" "testing" "github.com/fabric8-services/fabric8-auth/test/contracts/model" @@ -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(). @@ -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) } } diff --git a/test/contracts/consumer/fabric8auth/api_token.go b/test/contracts/consumer/fabric8auth/api_token.go index 26aa70f244..979818418b 100644 --- a/test/contracts/consumer/fabric8auth/api_token.go +++ b/test/contracts/consumer/fabric8auth/api_token.go @@ -1,9 +1,7 @@ package fabric8auth import ( - "fmt" "log" - "net/http" "testing" "github.com/fabric8-services/fabric8-auth/test/contracts/model" @@ -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, @@ -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) } } diff --git a/test/contracts/consumer/fabric8auth/api_user.go b/test/contracts/consumer/fabric8auth/api_user.go index 193c7ee81d..696951c87d 100644 --- a/test/contracts/consumer/fabric8auth/api_user.go +++ b/test/contracts/consumer/fabric8auth/api_user.go @@ -3,7 +3,6 @@ package fabric8auth import ( "fmt" "log" - "net/http" "testing" "github.com/fabric8-services/fabric8-auth/test/contracts/model" @@ -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(). @@ -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) } } @@ -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(). @@ -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) } } @@ -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(). @@ -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) } } @@ -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(). @@ -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) } } @@ -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(). @@ -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) } } diff --git a/test/contracts/consumer/fabric8auth/consumer_test.go b/test/contracts/consumer/fabric8auth/consumer_test.go index 104960ad12..43cba4f0a1 100644 --- a/test/contracts/consumer/fabric8auth/consumer_test.go +++ b/test/contracts/consumer/fabric8auth/consumer_test.go @@ -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") @@ -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) @@ -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) } } diff --git a/test/contracts/consumer/fabric8auth/verify_interactions.go b/test/contracts/consumer/fabric8auth/verify_interactions.go new file mode 100644 index 0000000000..dfd35e35e0 --- /dev/null +++ b/test/contracts/consumer/fabric8auth/verify_interactions.go @@ -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 " 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 +}