From 017b47587e488df7e3da1d274cf9fa6831eb1f05 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivolgin Date: Thu, 16 Nov 2023 16:17:11 -0800 Subject: [PATCH] Tests for token authentication (#341) --- pacts/replicated-cli-vendor-api.json | 50 +++++++++++++++++++ pkg/kotsclient/token_test.go | 73 ++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 pkg/kotsclient/token_test.go diff --git a/pacts/replicated-cli-vendor-api.json b/pacts/replicated-cli-vendor-api.json index 532fa0f1..f2fafdcd 100644 --- a/pacts/replicated-cli-vendor-api.json +++ b/pacts/replicated-cli-vendor-api.json @@ -305,6 +305,56 @@ } } }, + { + "description": "A request to list clusters", + "providerState": "List clusters using service account", + "request": { + "method": "GET", + "path": "/v3/clusters", + "headers": { + "Authorization": "replicated-cli-tokens-sa-token", + "Content-Type": "application/json" + } + }, + "response": { + "status": 200, + "headers": { + }, + "body": { + "totalClusters": 0 + }, + "matchingRules": { + "$.body.totalClusters": { + "match": "type" + } + } + } + }, + { + "description": "A request to list clusters", + "providerState": "List clusters using personal token", + "request": { + "method": "GET", + "path": "/v3/clusters", + "headers": { + "Authorization": "replicated-cli-tokens-personal-token", + "Content-Type": "application/json" + } + }, + "response": { + "status": 200, + "headers": { + }, + "body": { + "totalClusters": 0 + }, + "matchingRules": { + "$.body.totalClusters": { + "match": "type" + } + } + } + }, { "description": "A request to create a kots installer", "providerState": "Create KOTS installer", diff --git a/pkg/kotsclient/token_test.go b/pkg/kotsclient/token_test.go new file mode 100644 index 00000000..eba807ad --- /dev/null +++ b/pkg/kotsclient/token_test.go @@ -0,0 +1,73 @@ +package kotsclient + +import ( + "fmt" + "testing" + + "github.com/pact-foundation/pact-go/dsl" + "github.com/replicatedhq/replicated/pkg/platformclient" + "github.com/stretchr/testify/assert" +) + +func Test_ClusterList(t *testing.T) { + tests := []struct { + name string + token string + clientFunc func() error + }{ + { + name: "List clusters using service account", + token: "replicated-cli-tokens-sa-token", + clientFunc: func() (err error) { + u := fmt.Sprintf("http://localhost:%d", pact.Server.Port) + + api := platformclient.NewHTTPClient(u, "replicated-cli-tokens-sa-token") + client := VendorV3Client{HTTPClient: *api} + + _, err = client.ListClusters(true, nil, nil) + assert.Nil(t, err) + + return nil + }, + }, + { + name: "List clusters using personal token", + token: "replicated-cli-tokens-personal-token", + clientFunc: func() (err error) { + u := fmt.Sprintf("http://localhost:%d", pact.Server.Port) + + api := platformclient.NewHTTPClient(u, "replicated-cli-tokens-personal-token") + client := VendorV3Client{HTTPClient: *api} + + _, err = client.ListClusters(true, nil, nil) + assert.Nil(t, err) + + return nil + }, + }, + } + + for _, test := range tests { + pact.AddInteraction(). + Given(test.name). + UponReceiving("A request to list clusters"). + WithRequest(dsl.Request{ + Method: "GET", + Path: dsl.String("/v3/clusters"), + Headers: dsl.MapMatcher{ + "Authorization": dsl.String(test.token), + "Content-Type": dsl.String("application/json"), + }, + }). + WillRespondWith(dsl.Response{ + Status: 200, + Body: map[string]interface{}{ + "totalClusters": dsl.Like(0), + }, + }) + + if err := pact.Verify(test.clientFunc); err != nil { + t.Fatalf("Error on Verify test %s: %v", test.name, err) + } + } +}