github.com/replicatedhq/ship@v0.55.0/contracts/replicatedapp/get_release_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_GetRelease(t *testing.T) { 17 customerID := "ship-fetch-release-customer-0" 18 installationID := "ship-fetch-release-installation-0" 19 semver := "1.0.2" 20 spec := `assets: 21 v1: 22 - inline: 23 contents: | 24 #!/bin/bash 25 echo "installing nothing" 26 echo "config option: {{repl ConfigOption "test_option" }}" 27 dest: ./scripts/install.sh 28 mode: 0777 29 - inline: 30 contents: | 31 #!/bin/bash 32 echo "tested nothing" 33 echo "customer {{repl Installation "customer_id" }}" 34 echo "install {{repl Installation "installation_id" }}" 35 dest: ./scripts/test.sh 36 mode: 0777 37 config: 38 v1: 39 - name: test_options 40 title: Test Options 41 description: testing testing 123 42 items: 43 - name: test_option 44 title: Test Option 45 default: abc123_test-option-value 46 type: text 47 48 lifecycle: 49 v1: 50 - render: {}` 51 var test = func() (err error) { 52 req := require.New(t) 53 54 v := viper.New() 55 v.Set("customer-endpoint", fmt.Sprintf("http://localhost:%d/graphql", pact.Server.Port)) 56 57 gqlClient, err := replapp.NewGraphqlClient(v, http.DefaultClient) 58 req.NoError(err) 59 60 selector := replapp.Selector{ 61 CustomerID: customerID, 62 InstallationID: installationID, 63 ReleaseSemver: semver, 64 } 65 66 _, err = gqlClient.GetRelease(&selector) 67 req.NoError(err) 68 69 return nil 70 } 71 72 pact.AddInteraction(). 73 Given("A request to get a single app release"). 74 UponReceiving("A request to get the amn app release from a semver and customer id"). 75 WithRequest(dsl.Request{ 76 Method: "POST", 77 Path: dsl.String("/graphql"), 78 Headers: dsl.MapMatcher{ 79 "Authorization": dsl.String(fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", customerID, installationID))))), 80 "Content-Type": dsl.String("application/json"), 81 }, 82 Body: map[string]interface{}{ 83 "operationName": "", 84 "query": replapp.GetAppspecQuery, 85 "variables": map[string]interface{}{ 86 "semver": semver, 87 }, 88 }, 89 }). 90 WillRespondWith(dsl.Response{ 91 Status: 200, 92 Body: map[string]interface{}{ 93 "data": map[string]interface{}{ 94 "shipRelease": map[string]interface{}{ 95 "id": dsl.Like(dsl.String("generated")), 96 "spec": dsl.Like(dsl.String(spec)), 97 }, 98 }, 99 }, 100 }) 101 102 if err := pact.Verify(test); err != nil { 103 t.Fatalf("Error on Verify: %v", err) 104 } 105 }