This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Create contract tests for interaction with Auth service.
- Loading branch information
Showing
11 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**/*log | ||
**/*logs | ||
**/*pacts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Package contracts contains a runnable Consumer Pact test example. | ||
package contracts | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/pact-foundation/pact-go/dsl" | ||
) | ||
|
||
// AuthAPIStatus defines contract of /api/status endpoint | ||
func AuthAPIStatus(t *testing.T, pact *dsl.Pact) { | ||
// 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 | ||
} | ||
|
||
type STATUS struct { | ||
buildTime string `json:"buildTime" pact:"example=2018-10-05T10:03:04Z"` | ||
commit string `json:"commit" pact:"example=0f9921980549b2baeb43f6f16cbe794f430f498c"` | ||
configurationStatus string `json:"configurationStatus" pact:"example=OK"` | ||
databaseStatus string `json:"databaseStatus" pact:"example=OK"` | ||
startTime string `json:"startTime" pact:"example=2018-10-09T15:04:50Z"` | ||
} | ||
|
||
// Set up our expected interactions. | ||
pact. | ||
AddInteraction(). | ||
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(STATUS{}), | ||
}) | ||
|
||
// Verify | ||
if err := pact.Verify(test); err != nil { | ||
log.Fatalf("Error on Verify: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Package contracts contains a runnable Consumer Pact test example. | ||
package contracts | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/pact-foundation/pact-go/dsl" | ||
) | ||
|
||
type Data struct { | ||
Attributes struct { | ||
Bio string `json:"bio" pact:"example=n/a"` | ||
Cluster string `json:"cluster" pact:"example=https://api.starter-us-east-2a.openshift.com/"` | ||
Company string `json:"company" pact:"example=n/a"` | ||
ContextInformation struct { | ||
RecentContexts []struct { | ||
User string `json:"user" pact:"example=c46445eb-2448-4c91-916a-2c1de3e6f63e"` | ||
} `json:"recentContexts"` | ||
RecentSpaces []string `json:"recentSpaces"` | ||
} `json:"contextInformation"` | ||
CreatedAt string `json:"created-at" pact:"example=2018-03-16T14:34:31.615511Z"` | ||
Email string `json:"email" pact:"[email protected]"` | ||
EmailPrivate bool `json:"emailPrivate" pact:"example=false"` | ||
EmailVerified bool `json:"emailVerified" pact:"example=true"` | ||
FeatureLevel string `json:"featureLevel" pact:"example=internal"` | ||
FullName string `json:"fullName" pact:"example=Osio10 Automated Tests"` | ||
IdentityID string `json:"identityID" pact:"example=c46445eb-2448-4c91-916a-2c1de3e6f63e"` | ||
ImageURL string `json:"imageURL" pact:"example=n/a"` | ||
ProviderType string `json:"providerType" pact:"example=kc"` | ||
RegistrationCompleted bool `json:"registrationCompleted" pact:"example=true"` | ||
UpdatedAt string `json:"updated-at" pact:"example=2018-05-30T11:05:23.513612Z"` | ||
URL string `json:"url" pact:"example=n/a"` | ||
UserID string `json:"userID" pact:"example=5f41b66e-6f84-42b3-ab5f-8d9ef21149b1"` | ||
Username string `json:"username" pact:"example=osio-ci-ee10"` | ||
} `json:"attributes"` | ||
ID string `json:"id" pact:"example=c46445eb-2448-4c91-916a-2c1de3e6f63e"` | ||
Links struct { | ||
Related string `json:"related" pact:"example=https://auth.openshift.io/api/users/c46445eb-2448-4c91-916a-2c1de3e6f63e"` | ||
Self string `json:"self" pact:"example=https://auth.openshift.io/api/users/c46445eb-2448-4c91-916a-2c1de3e6f63e"` | ||
} `json:"links"` | ||
Type string `json:"type" pact:"example=identities"` | ||
} | ||
|
||
type USER struct { | ||
data Data `json:"data"` | ||
} | ||
|
||
type USERS struct { | ||
data []Data `json:"data"` | ||
} | ||
|
||
// APIUserByNameConsumer defines contract of /api/users?filter[username]=<user_name> endpoint | ||
func AuthAPIUserByNameConsumer(t *testing.T, pact *dsl.Pact) { | ||
userName := os.Getenv("OSIO_USERNAME") | ||
|
||
// 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(). | ||
UponReceiving("A request to get user's information by name"). | ||
WithRequest(dsl.Request{ | ||
Method: "GET", | ||
Path: dsl.String("/api/users"), | ||
Query: dsl.MapMatcher{ | ||
"filter[username]": dsl.Term( | ||
userName, | ||
".*", | ||
), | ||
}, | ||
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")}, | ||
}). | ||
WillRespondWith(dsl.Response{ | ||
Status: 200, | ||
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.api+json")}, | ||
Body: dsl.Match(USERS{}), | ||
}) | ||
|
||
// Verify | ||
if err := pact.Verify(test); err != nil { | ||
log.Fatalf("Error on Verify: %v", err) | ||
} | ||
} | ||
|
||
// APIUserByIDConsumer defines contract of /api/users/<user_id> endpoint | ||
func AuthAPIUserByIDConsumer(t *testing.T, pact *dsl.Pact) { | ||
userID := os.Getenv("OSIO_USER_ID") | ||
|
||
// 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(). | ||
UponReceiving("A request to get user's information by ID"). | ||
WithRequest(dsl.Request{ | ||
Method: "GET", | ||
Path: dsl.Term( | ||
fmt.Sprintf("/api/users/%s", userID), | ||
"/api/users/.*", | ||
), | ||
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")}, | ||
}). | ||
WillRespondWith(dsl.Response{ | ||
Status: 200, | ||
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.api+json")}, | ||
Body: dsl.Match(USER{}), | ||
}) | ||
|
||
// Verify | ||
if err := pact.Verify(test); err != nil { | ||
log.Fatalf("Error on Verify: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
. ./setenv.sh | ||
|
||
# run test | ||
go test -v -run 'Test*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Package contracts contains a runnable Consumer Pact test example. | ||
package contracts | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/pact-foundation/pact-go/dsl" | ||
) | ||
|
||
// TestAuthAPI runs all user related tests | ||
func TestAuthAPI(t *testing.T) { | ||
// Create Pact connecting to local Daemon | ||
pact := &dsl.Pact{ | ||
Consumer: os.Getenv("PACT_CONSUMER"), | ||
Provider: os.Getenv("PACT_PROVIDER"), | ||
Host: "localhost", | ||
PactFileWriteMode: "merge", | ||
} | ||
defer pact.Teardown() | ||
|
||
// Test interactions | ||
AuthAPIStatus(t, pact) | ||
AuthAPIUserByNameConsumer(t, pact) | ||
AuthAPIUserByIDConsumer(t, pact) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
|
||
. ./setenv.sh | ||
|
||
for PACT_FILE in $(find "$PACT_DIR" -name "*.json"); do | ||
echo "Publishing $PACT_FILE to a Pact broker at $PACT_BROKER_URL" | ||
|
||
PACT_CONSUMER=$(jq '.["consumer"]["name"]' "$PACT_FILE" | tr -d '"') | ||
PACT_PROVIDER=$(jq '.["provider"]["name"]' "$PACT_FILE" | tr -d '"') | ||
|
||
result=$(curl -L --silent -XPUT -H "Content-Type: application/json" -d@$PACT_FILE "$PACT_BROKER_URL/pacts/provider/$PACT_PROVIDER/consumer/$PACT_CONSUMER/version/$PACT_VERSION") | ||
|
||
if [[ $result = *'"consumer":{"name":"'$PACT_CONSUMER'"},"provider":{"name":"'$PACT_PROVIDER'"}'* ]]; then | ||
echo "Pact successfully published." | ||
else | ||
echo "Unable to publish pact:" | ||
echo "$result" | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
set -a | ||
|
||
# Add the current directory to Go path. | ||
GOPATH="$GOPATH:$(pwd)" | ||
|
||
# A directory to save pact files | ||
PACT_DIR="${PACT_DIR:-pacts}" | ||
PACT_CONSUMER="${PACT_CONSUMER:-Fabric8TenantService}" | ||
PACT_PROVIDER="${PACT_PROVIDER:-Fabric8AuthService}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
|
||
. ./setenv.sh | ||
|
||
for PACT_FILE in $(find "$PACT_DIR" -name "*.json"); do | ||
PACT_CONSUMER=$(jq '.["consumer"]["name"]' "$PACT_FILE" | tr -d '"') | ||
PACT_PROVIDER=$(jq '.["provider"]["name"]' "$PACT_FILE" | tr -d '"') | ||
|
||
pact-provider-verifier "$PACT_BROKER_URL/pacts/provider/$PACT_PROVIDER/consumer/$PACT_CONSUMER/versions/$PACT_VERSION" --provider-base-url "$PACT_PROVIDER_BASE_URL" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
. ./setenv.sh | ||
|
||
for PACT_FILE in $(find "$PACT_DIR" -name "*.json"); do | ||
pact-provider-verifier "$PACT_FILE" --provider-base-url "$PACT_PROVIDER_BASE_URL" | ||
done |