Skip to content

Commit

Permalink
Tests for token authentication (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin authored Nov 17, 2023
1 parent 7078892 commit 017b475
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pacts/replicated-cli-vendor-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
73 changes: 73 additions & 0 deletions pkg/kotsclient/token_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 017b475

Please sign in to comment.