-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser_service_test.go
120 lines (100 loc) · 3.54 KB
/
user_service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"os"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/pact-foundation/pact-go/v2/models"
"github.com/pact-foundation/pact-go/v2/provider"
"github.com/pact-foundation/pact-go/v2/utils"
)
func parseDate(dateStr string) *time.Time {
date, err := time.Parse("2006-01-02", dateStr)
if err != nil {
panic(err)
}
return &date
}
func TestPactProvider(t *testing.T) {
go startProvider()
verifier := provider.NewVerifier()
// Verify the Provider - fetch pacts from PactFlow
verifyRequest := provider.VerifyRequest{
Provider: "pactflow-example-provider-golang",
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port),
BrokerToken: os.Getenv("PACT_BROKER_TOKEN"),
BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"),
BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"),
PublishVerificationResults: envBool("PACT_BROKER_PUBLISH_VERIFICATION_RESULTS"),
ProviderVersion: os.Getenv("GIT_COMMIT"),
StateHandlers: stateHandlers,
ProviderBranch: os.Getenv("GIT_BRANCH"),
}
if os.Getenv("PACT_URL") != "" {
// For builds triggered by a 'contract_requiring_verification_published' webhook, verify the changed pact against latest of mainBranch and any version currently deployed to an environment
// https://docs.pact.io/pact_broker/webhooks#using-webhooks-with-the-contract_requiring_verification_published-event
// The URL will have been passed in from the webhook to the CI job.
verifyRequest.PactURLs = []string{os.Getenv("PACT_URL")}
} else {
// For 'normal' provider builds, fetch the the latest version from the main branch of each consumer, as specified by
// the consumer's mainBranch property and all the currently deployed and currently released and supported versions of each consumer.
// https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors
verifyRequest.BrokerURL = fmt.Sprint(os.Getenv("PACT_BROKER_BASE_URL"))
verifyRequest.ConsumerVersionSelectors = getSelectors()
verifyRequest.EnablePending = true
verifyRequest.IncludeWIPPactsSince = parseDate("2024-01-01")
}
err := verifier.VerifyProvider(t, verifyRequest)
if err != nil {
t.Fatalf("%v", err)
}
}
// Provider state handlers
var stateHandlers = models.StateHandlers{
"a product with ID 10 exists": func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) {
productRepository = productExists
return models.ProviderStateResponse{}, nil
},
"no products exist": func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) {
productRepository = noProductsExist
return models.ProviderStateResponse{}, nil
},
}
// Starts the provider API with hooks for provider states.
func startProvider() {
router := gin.Default()
router.GET("/product/:id", GetProduct)
router.Run(fmt.Sprintf(":%d", port))
}
// Provider States data sets
var productExists = &ProductRepository{
Products: map[string]*Product{
"10": {
Name: "Pizza",
ID: "10",
Type: "food",
Version: "1.0.0",
},
},
}
var noProductsExist = &ProductRepository{}
// Configuration / Test Data
var port, _ = utils.GetFreePort()
func getSelectors() []provider.Selector {
selectors := make([]provider.Selector, 0)
if os.Getenv("SELECTORS") != "" {
selectors = []provider.Selector{
&provider.ConsumerVersionSelector{
DeployedOrReleased: true,
},
&provider.ConsumerVersionSelector{
MainBranch: true,
},
}
}
return selectors
}
func envBool(k string) bool {
return os.Getenv(k) != ""
}