github.com/replicatedhq/ship@v0.55.0/contracts/replicatedapp/get_license_test.go (about) 1 package replicatedapp 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/pact-foundation/pact-go/dsl" 10 "github.com/spf13/viper" 11 "github.com/stretchr/testify/require" 12 13 replapp "github.com/replicatedhq/ship/pkg/specs/replicatedapp" 14 ) 15 16 func Test_GetLicense(t *testing.T) { 17 licenseID := "get-license-installation" 18 19 var test = func() (err error) { 20 req := require.New(t) 21 22 v := viper.New() 23 v.Set("customer-endpoint", fmt.Sprintf("http://localhost:%d/graphql", pact.Server.Port)) 24 25 gqlClient, err := replapp.NewGraphqlClient(v, http.DefaultClient) 26 req.NoError(err) 27 28 selector := replapp.Selector{ 29 LicenseID: licenseID, 30 } 31 32 _, err = gqlClient.GetLicense(&selector) 33 req.NoError(err) 34 35 return nil 36 } 37 38 pact.AddInteraction(). 39 Given("A request to get a license"). 40 UponReceiving("A request to get the license from from licenseID"). 41 WithRequest(dsl.Request{ 42 Method: "POST", 43 Path: dsl.String("/graphql"), 44 Headers: dsl.MapMatcher{ 45 "Authorization": dsl.String(fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", licenseID, ""))))), 46 "Content-Type": dsl.String("application/json"), 47 }, 48 Body: map[string]interface{}{ 49 "operationName": "", 50 "query": replapp.GetLicenseQuery, 51 "variables": map[string]interface{}{ 52 "licenseId": licenseID, 53 }, 54 }, 55 }). 56 WillRespondWith(dsl.Response{ 57 Status: 200, 58 Body: map[string]interface{}{ 59 "data": map[string]interface{}{ 60 "license": map[string]interface{}{ 61 "id": "get-license-installation", 62 "assignee": "Get License - Customer 0", 63 "createdAt": dsl.Like(dsl.String("Tue Jan 01 2019 01:23:46 GMT+0000 (Coordinated Universal Time)")), 64 "expiresAt": nil, 65 "type": dsl.Like(dsl.String("")), 66 }, 67 }, 68 }, 69 }) 70 71 if err := pact.Verify(test); err != nil { 72 t.Fatalf("Error on Verify: %v", err) 73 } 74 }