-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add consumer version selectors
- Refactor test case into table based tests - Basic validation on the consumer selectors
- Loading branch information
Matt Fellows
committed
Feb 5, 2020
1 parent
767641c
commit cdc7464
Showing
6 changed files
with
173 additions
and
69 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,21 @@ | ||
package types | ||
|
||
import "fmt" | ||
|
||
// ConsumerVersionSelector are the way we specify which pacticipants and | ||
// versions we want to use when configuring verifications | ||
// See https://docs.pact.io/selectors for more | ||
type ConsumerVersionSelector struct { | ||
Pacticipant string `json:"pacticipant"` | ||
Version string `json:"version"` | ||
Latest bool `json:"latest"` | ||
All bool `json:"all"` | ||
} | ||
|
||
func (c *ConsumerVersionSelector) Validate() error { | ||
if c.Pacticipant == "" { | ||
return fmt.Errorf("must provide a Pacticpant") | ||
} | ||
|
||
return nil | ||
} |
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,28 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConsumerVersionSelectorValidate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
selector ConsumerVersionSelector | ||
err bool | ||
}{ | ||
{name: "no pacticipant", selector: ConsumerVersionSelector{}, err: true}, | ||
{name: "pacticipant only", selector: ConsumerVersionSelector{Pacticipant: "foo"}, err: false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.selector.Validate() | ||
if tt.err { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, 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
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 |
---|---|---|
@@ -1,71 +1,107 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestVerifyRequest_ValidRequest(t *testing.T) { | ||
r := VerifyRequest{ | ||
BrokerURL: "http://localhost:1234", | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
BrokerUsername: "abcd", | ||
BrokerPassword: "1234", | ||
ProviderBaseURL: "http://localhost:8080", | ||
ProviderStatesSetupURL: "http://localhost:8080/setup", | ||
ProviderVersion: "1.0.0", | ||
PublishVerificationResults: true, | ||
Verbose: true, | ||
CustomProviderHeaders: []string{ | ||
"header: value", | ||
}, | ||
} | ||
func TestVerifyRequestValidate(t *testing.T) { | ||
|
||
err := r.Validate() | ||
t.Run("local validation", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
request VerifyRequest | ||
err bool | ||
}{ | ||
{name: "valid parameters", request: VerifyRequest{ | ||
BrokerURL: "http://localhost:1234", | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
BrokerUsername: "abcd", | ||
BrokerPassword: "1234", | ||
ProviderBaseURL: "http://localhost:8080", | ||
ProviderStatesSetupURL: "http://localhost:8080/setup", | ||
ProviderVersion: "1.0.0", | ||
PublishVerificationResults: true, | ||
Verbose: true, | ||
CustomProviderHeaders: []string{ | ||
"header: value", | ||
}, | ||
}, err: false}, | ||
{name: "no base URL provided", request: VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
}, err: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.request.Validate() | ||
if tt.err { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
|
||
if err != nil { | ||
t.Fatal("want nil, got err: ", err) | ||
} | ||
} | ||
|
||
func TestVerifyRequest_NoBaseURL(t *testing.T) { | ||
r := VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
} | ||
|
||
err := r.Validate() | ||
|
||
if err == nil { | ||
t.Fatal("want err, got nil") | ||
} | ||
} | ||
|
||
func TestVerifyRequest_BrokerUsernameWithoutPassword(t *testing.T) { | ||
r := VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
ProviderBaseURL: "http://localhost:8080", | ||
BrokerURL: "http://localhost:1234", | ||
ProviderVersion: "1.0.0.", | ||
BrokerPassword: "1234", | ||
} | ||
|
||
err := r.Validate() | ||
|
||
if err == nil { | ||
t.Fatal("want error, got nil") | ||
} | ||
} | ||
}) | ||
|
||
func TestVerifyRequest_BrokerURLWithoutVersion(t *testing.T) { | ||
r := VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
ProviderBaseURL: "http://localhost:8080", | ||
BrokerURL: "http://localhost:1234", | ||
BrokerPassword: "1234", | ||
} | ||
t.Run("broker integration", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
request VerifyRequest | ||
err bool | ||
}{ | ||
{name: "url without version", request: VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
ProviderBaseURL: "http://localhost:8080", | ||
BrokerURL: "http://localhost:1234", | ||
}, err: true}, | ||
{name: "password without username", request: VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
ProviderBaseURL: "http://localhost:8080", | ||
BrokerURL: "http://localhost:1234", | ||
ProviderVersion: "1.0.0", | ||
BrokerPassword: "1234", | ||
}, err: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.request.Validate() | ||
if tt.err { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
|
||
err := r.Validate() | ||
}) | ||
|
||
if err == nil { | ||
t.Fatal("want error, got nil") | ||
} | ||
t.Run("consumer version selectors", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
request VerifyRequest | ||
err bool | ||
}{ | ||
{name: "no pacticipant", request: VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
ProviderBaseURL: "http://localhost:8080", | ||
ConsumerVersionSelectors: []ConsumerVersionSelector{ConsumerVersionSelector{}}, | ||
}, err: true}, | ||
{name: "pacticipant only", request: VerifyRequest{ | ||
PactURLs: []string{"http://localhost:1234/path/to/pact"}, | ||
ProviderBaseURL: "http://localhost:8080", | ||
ConsumerVersionSelectors: []ConsumerVersionSelector{ConsumerVersionSelector{Pacticipant: "foo"}}, | ||
}, err: false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.request.Validate() | ||
if tt.err { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
}) | ||
} |