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
WIP: Create contract tests for interaction with Auth service. #659
Open
pmacik
wants to merge
3
commits into
fabric8-services:master
Choose a base branch
from
pmacik:contract-tests-auth-poc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+476
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,4 @@ | ||
**/*log | ||
**/*logs | ||
**/*pacts | ||
.password |
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,264 @@ | ||
// 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:"example=osio-ci+ee10@redhat.com"` | ||
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"` | ||
} | ||
|
||
type InvalidToken struct { | ||
Errors []struct { | ||
Code string `json:"code" pact:"example=token_validation_failed"` | ||
Detail string `json:"detail" pact:"example=token is invalid"` | ||
ID string `json:"id" pact:"example=76J0ww+6"` | ||
Status string `json:"status" pact:"example=401"` | ||
Title string `json:"title" pact:"example=Unauthorized"` | ||
} `json:"errors"` | ||
} | ||
|
||
type MissingToken struct { | ||
Errors []struct { | ||
Code string `json:"code" pact:"example=jwt_security_error"` | ||
Detail string `json:"detail" pact:"example=missing header \"Authorization\""` | ||
ID string `json:"id" pact:"example=FRzHbogQ"` | ||
Status string `json:"status" pact:"example=401"` | ||
Title string `json:"title" pact:"example=Unauthorized"` | ||
} `json:"errors"` | ||
} | ||
|
||
const jwsRegex = "[a-zA-Z0-9\\-_]+?\\.?[a-zA-Z0-9\\-_]+?\\.?([a-zA-Z0-9\\-_]+)?" | ||
const userNameRegex = "[a-zA-Z\\-0-9]+" | ||
|
||
// AuthAPIUserByNameConsumer 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, | ||
userNameRegex, | ||
), | ||
}, | ||
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) | ||
} | ||
} | ||
|
||
// AuthAPIUserByIDConsumer 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), | ||
fmt.Sprintf("/api/users/%s", userNameRegex), | ||
), | ||
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) | ||
} | ||
} | ||
|
||
// AuthAPIUserInvalidToken defines contract of /api/user endpoint with invalid auth token | ||
func AuthAPIUserInvalidToken(t *testing.T, pact *dsl.Pact) { | ||
|
||
// Base64 encoded '{"alg":"RS256","kid":"1aA2bBc3CDDdEEefff7gGHH_ii9jJjkkkLl2mmm4NNO","typ":"JWT"}somerandombytes' | ||
var invalidToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjFhQTJiQmMzQ0REZEVFZWZmZjdnR0hIX2lpOWpKamtra0xsMm1tbTROTk8iLCJ0eXAiOiJKV1QifXNvbWVyYW5kb21ieXRlcw" | ||
|
||
// 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(). | ||
UponReceiving("A request to get user's information with invalid auth token "). | ||
WithRequest(dsl.Request{ | ||
Method: "GET", | ||
Path: dsl.String("/api/user"), | ||
Headers: dsl.MapMatcher{ | ||
"Content-Type": dsl.String("application/json"), | ||
"Authorization": dsl.Term( | ||
fmt.Sprintf("Bearer %s", invalidToken), | ||
fmt.Sprintf("^Bearer %s$", jwsRegex), | ||
), | ||
}, | ||
}). | ||
WillRespondWith(dsl.Response{ | ||
Status: 401, | ||
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.api+json")}, | ||
Body: dsl.Match(InvalidToken{}), | ||
}) | ||
|
||
// Verify | ||
if err := pact.Verify(test); err != nil { | ||
log.Fatalf("Error on Verify: %v", err) | ||
} | ||
} | ||
|
||
// AuthAPIUserNoToken defines contract of /api/user endpoint with invalid auth token | ||
func AuthAPIUserNoToken(t *testing.T, pact *dsl.Pact) { | ||
|
||
// 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(). | ||
UponReceiving("A request to get user's information with no auth token "). | ||
WithRequest(dsl.Request{ | ||
Method: "GET", | ||
Path: dsl.String("/api/user"), | ||
Headers: dsl.MapMatcher{ | ||
"Content-Type": dsl.String("application/json"), | ||
}, | ||
}). | ||
WillRespondWith(dsl.Response{ | ||
Status: 401, | ||
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.api+json")}, | ||
Body: dsl.Match(MissingToken{}), | ||
}) | ||
|
||
// Verify | ||
if err := pact.Verify(test); err != nil { | ||
log.Fatalf("Error on Verify: %v", err) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't relay on OSO-starter to run the broker. Need a better place for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the same home as sentry and ike bots? /cc @kbsingh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah.. it should be preview or prod cluster. @pmacik talk to the SD team.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexeykazakov @bartoszmajsak Thanks for the suggestion, created appropriate SD ticket for that (https://gitlab.cee.redhat.com/dtsd/housekeeping/issues/2383)