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

Commit

Permalink
contract-tests: Fix golangci issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmacik committed Jan 14, 2019
1 parent defec08 commit 9c669b8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
17 changes: 13 additions & 4 deletions test/contracts/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAuthAPIProvider(t *testing.T) {
providerInfo := Setup(providerSetupHost, providerSetupPort, pactProviderBaseURL, userName, userPassword)

if providerInfo == nil {
log.Fatalf("Error setting up provider initial state")
log.Fatalf("Unable to setup provider initial state")
}
var pactContent string

Expand All @@ -91,24 +91,30 @@ func TestAuthAPIProvider(t *testing.T) {

err := os.MkdirAll(pactDir, os.ModePerm)
if err != nil {
log.Fatal(err)
log.Fatalf("Unable to create a pact directory (%s): %q", pactDir, err)
}

pactFilePath := fmt.Sprintf("%s/provider-%s-%s.json", pactDir, strings.ToLower(pactConsumer), strings.ToLower(pactProvider))
pactFile, err := os.Create(pactFilePath)
if err != nil {
log.Fatal(err)
log.Fatalf("Unable to create a local pact file (%s): %q", pactFilePath, err)
}
defer pactFile.Close()

_, err = pactFile.WriteString(pactContent)
if err != nil {
log.Fatalf("Unable to write into pact file (%s): %q", pactFilePath, err)
}

// Verify the Provider with local Pact Files
pact.VerifyProvider(t, types.VerifyRequest{
_, err = pact.VerifyProvider(t, types.VerifyRequest{
ProviderBaseURL: pactProviderBaseURL,
PactURLs: []string{pactFilePath},
ProviderStatesSetupURL: fmt.Sprintf("http://%s:%d/pact/setup", providerSetupHost, providerSetupPort),
})
if err != nil {
log.Fatalf("Unable to verify provider: %q", err)
}

log.Println("Test Passed!")
}
Expand Down Expand Up @@ -144,6 +150,9 @@ func pactFromBroker(pactBrokerURL string, pactBrokerUsername string, pactBrokerP
defer response.Body.Close()

responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("Unable to read HTTP response from pact broker:\n%q", err)
}

// Replace placeholders in pact file with real data (user name/id/token)
return string(responseBody)
Expand Down
34 changes: 25 additions & 9 deletions test/contracts/provider/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Setup(setupHost string, setupPort int, providerBaseURL string, userName str
log.Printf("Making sure user %s is created...", userName)
var user = createUser(providerBaseURL, userName)
if user == nil {
log.Fatalf("Error creating/getting user")
log.Fatalf("Unable to create/get user")
}
log.Printf("Provider setup with user ID: %s", user.Data.ID)

Expand Down Expand Up @@ -86,7 +86,11 @@ func setupEndpoint(setupHost string, setupPort int) {
}

var providerState providerStateInfo
json.Unmarshal(body, &providerState)
err = json.Unmarshal(body, &providerState)
if err != nil {
log.Fatalf(">>> ERROR: Unable to unmarshall request body.\n %q", err)
return
}

switch providerState.State {
case "User with a given username exists.",
Expand Down Expand Up @@ -137,38 +141,44 @@ func createUser(providerBaseURL string, userName string) *model.User {

messageBytes, err := json.Marshal(message)
if err != nil {
log.Fatalf("createUser: Error marshalling JSON object:\n%q", err)
log.Fatalf("createUser: Unable to marshal JSON object:\n%q", err)
}

request, err := http.NewRequest("POST", fmt.Sprintf("%s/api/users", providerBaseURL), bytes.NewBuffer(messageBytes))
if err != nil {
log.Fatalf("createUser: Error creating HTTP request:\n%q", err)
log.Fatalf("createUser: Unable to create HTTP request:\n%q", err)
}
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authServiceAccountToken))

log.Println("Sending a request to create a user")
response, err := httpClient.Do(request)
if err != nil {
log.Fatalf("createUser: Error sending HTTP request:\n%q", err)
log.Fatalf("createUser: Unable to send HTTP request:\n%q", err)
}
defer response.Body.Close()

responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("createUser: Unable to read HTTP response:\n%q", err)
}

if response.StatusCode != 200 {
if response.StatusCode == 409 { //user already exists
log.Printf("User %s already exists, getting user info.", userName)
response2, err := http.Get(fmt.Sprintf("%s/api/users?filter[username]=%s", providerBaseURL, userName))
if err != nil {
log.Fatalf("userExists: Error creating HTTP request:\n%q", err)
log.Fatalf("userExists: Unable to create HTTP request:\n%q", err)
}
defer response2.Body.Close()

responseBody, err := ioutil.ReadAll(response2.Body)
// log.Printf("User info:\n%s\n", responseBody)
if err != nil {
log.Fatalf("userExists: Error reading HTTP response:\n%q", err)
}
if response2.StatusCode != 200 {
log.Fatalf("userExists: Something went wrong with reading response body: %s", responseBody)
log.Fatalf("userExists: Something went wrong: %s", responseBody)
}
var users model.Users
err = json.Unmarshal(responseBody, &users)
Expand Down Expand Up @@ -222,18 +232,24 @@ func serviceAccountToken(providerBaseURL string) string {
// log.Printf("Message: %s", string(message))

if err != nil {
log.Fatalf("serviceAccountToken: Error marshalling json object: %q\n", err)
log.Fatalf("serviceAccountToken: Unable to marshal JSON object: %q\n", err)
}
request, err := http.NewRequest("POST", fmt.Sprintf("%s/api/token", providerBaseURL), bytes.NewBuffer(message))
request.Header.Add("Content-Type", "application/json")
if err != nil {
log.Fatalf("serviceAccountToken: Unable to create HTTP request: %q\n", err)
}

response, err := httpClient.Do(request)
if err != nil {
log.Fatalf("serviceAccountToken: Error sending HTTP request: %q\n", err)
log.Fatalf("serviceAccountToken: Unable to send HTTP request: %q\n", err)
}
defer response.Body.Close()

responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("serviceAccountToken: Unable to read HTTP response:\n%q", err)
}

if response.StatusCode != 200 {
log.Fatalf("serviceAccountToken: Something went wrong with reading response body: %s", responseBody)
Expand Down

0 comments on commit 9c669b8

Please sign in to comment.