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 package names and workaround pact-foundation/p…
Browse files Browse the repository at this point in the history
  • Loading branch information
pmacik committed Jan 22, 2019
1 parent 739a01e commit 4242601
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
"github.com/fabric8-services/fabric8-wit/test/contracts"
consumer "github.com/fabric8-services/fabric8-wit/test/contracts/consumer_test"
"github.com/pact-foundation/pact-go/dsl"
)

Expand All @@ -30,8 +32,6 @@ func AuthAPIStatus(t *testing.T, pact *dsl.Pact) {
})

// Verify

if err := pact.Verify(SimpleGetInteraction(pact, "/api/status")); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
err := pact.Verify(consumer.SimpleGetInteraction(pact, "/api/status"))
contracts.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
"github.com/fabric8-services/fabric8-wit/test/contracts"
consumer "github.com/fabric8-services/fabric8-wit/test/contracts/consumer_test"
"github.com/pact-foundation/pact-go/dsl"
)

Expand All @@ -29,7 +31,6 @@ func AuthAPITokenKeys(t *testing.T, pact *dsl.Pact) {
})

// Verify
if err := pact.Verify(SimpleGetInteraction(pact, "/api/token/keys")); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
err := pact.Verify(consumer.SimpleGetInteraction(pact, "/api/token/keys"))
contracts.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/fabric8-services/fabric8-auth/test/contracts/model"
"github.com/fabric8-services/fabric8-wit/test/contracts"
consumer "github.com/fabric8-services/fabric8-wit/test/contracts/consumer_test"
"github.com/pact-foundation/pact-go/dsl"
)

Expand Down Expand Up @@ -37,9 +39,8 @@ func AuthAPIUserByName(t *testing.T, pact *dsl.Pact, userName string) {
})

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

// AuthAPIUserByID defines contract of /api/users/<user_id> endpoint
Expand Down Expand Up @@ -67,9 +68,8 @@ func AuthAPIUserByID(t *testing.T, pact *dsl.Pact, userID string) {
})

// Verify
if err := pact.Verify(SimpleGetInteraction(pact, fmt.Sprintf("/api/users/%s", userID))); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
err := pact.Verify(consumer.SimpleGetInteraction(pact, fmt.Sprintf("/api/users/%s", userID)))
contracts.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}

// AuthAPIUserByToken defines contract of /api/user endpoint with valid auth token
Expand Down Expand Up @@ -102,9 +102,8 @@ func AuthAPIUserByToken(t *testing.T, pact *dsl.Pact, userToken string) {
})

// Verify
if err := pact.Verify(SimpleGetInteractionWithToken(pact, "/api/user", userToken)); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
err := pact.Verify(consumer.SimpleGetInteractionWithToken(pact, "/api/user", userToken))
contracts.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}

// AuthAPIUserInvalidToken defines contract of /api/user endpoint with invalid auth token
Expand Down Expand Up @@ -135,9 +134,8 @@ func AuthAPIUserInvalidToken(t *testing.T, pact *dsl.Pact, invalidToken string)
})

// Verify
if err := pact.Verify(SimpleGetInteractionWithToken(pact, "/api/user", invalidToken)); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
err := pact.Verify(consumer.SimpleGetInteractionWithToken(pact, "/api/user", invalidToken))
contracts.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}

// AuthAPIUserNoToken defines contract of /api/user endpoint with missing auth token
Expand All @@ -164,7 +162,6 @@ func AuthAPIUserNoToken(t *testing.T, pact *dsl.Pact) {
})

// Verify
if err := pact.Verify(SimpleGetInteraction(pact, "/api/user")); err != nil {
log.Fatalf("Error on Verify: %+v", err)
}
err := pact.Verify(consumer.SimpleGetInteraction(pact, "/api/user"))
contracts.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"github.com/pact-foundation/pact-go/types"
)

// TestWitAPIConsumer runs all user related tests
func TestWitAPIConsumer(t *testing.T) {
// TestFabric8AuthConsumer runs all consumer side contract tests
// for the fabric8-wit (consumer) to fabric8-auth (provider) contract.
func TestFabric8AuthConsumer(t *testing.T) {
log.SetOutput(os.Stdout)

var pactDir = os.Getenv("PACT_DIR")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fabric8auth
package consumer

import (
"fmt"
Expand Down
21 changes: 0 additions & 21 deletions test/contracts/main.go

This file was deleted.

37 changes: 37 additions & 0 deletions test/contracts/pact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package contracts

import (
"fmt"
"os"
"strings"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/stretchr/testify/require"
)

// PactDir returns a path to the directory to store pact files (taken from PACT_DIR env variable)
func PactDir() string {
return os.Getenv("PACT_DIR")
}

// PactFile returns a path to the generated pact file
func PactFile(pactConsumer string, pactProvider string) string {
return fmt.Sprintf("%s/%s-%s.json", PactDir(), strings.ToLower(pactConsumer), strings.ToLower(pactProvider))
}

// CheckErrorAndCleanPact is a workaround for the https://github.com/pact-foundation/pact-go/issues/108 issue
// by manually clearing the interactions from pact and the mock service.
func CheckErrorAndCleanPact(t *testing.T, pact *dsl.Pact, err1 error) {
if err1 != nil {
pact.Interactions = make([]*dsl.Interaction, 0)
mockServer := &dsl.MockService{
BaseURL: fmt.Sprintf("http://%s:%d", pact.Host, pact.Server.Port),
Consumer: pact.Consumer,
Provider: pact.Provider,
}
err2 := mockServer.DeleteInteractions()
require.NoError(t, err2)
}
require.NoError(t, err1)
}

0 comments on commit 4242601

Please sign in to comment.