Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): add fixtures for client test #11

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lint:
golangci-lint run

test:
go test -race -parallel=4 ./...
go test -v -race -parallel=4 ./...

test-acc:
ACC_TEST=yes go test -race -parallel=4 ./...
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/vault/api v1.9.2
github.com/hashicorp/vault/sdk v0.9.1
gopkg.in/dnaeon/go-vcr.v3 v3.1.2
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/dnaeon/go-vcr.v3 v3.1.2 h1:F1smfXBqQqwpVifDfUBQG6zzaGjzT+EnVZakrOdr5wA=
gopkg.in/dnaeon/go-vcr.v3 v3.1.2/go.mod h1:2IMOnnlx9I6u9x+YBsM3tAMx6AlOxnJ0pWxQAzZ79Ag=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
24 changes: 10 additions & 14 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
DefaultBaseURL = "https://api.vercel.com/v3"
httpTimeout = 60 * time.Second
DefaultBaseURL = "https://api.vercel.com/v3"
defaultHTTPTimeout = 60 * time.Second
)

type Client struct {
Expand All @@ -19,23 +19,19 @@ type Client struct {
token string
}

func New(apiKey string) *Client {
func New(apiKey string, client *http.Client) *Client {
return &Client{
baseURL: DefaultBaseURL,
httpClient: &http.Client{
Timeout: httpTimeout,
},
token: apiKey,
baseURL: DefaultBaseURL,
httpClient: client,
token: apiKey,
}
}

func NewWithBaseURL(apiKey string, baseURL string) *Client {
func NewWithBaseURL(apiKey string, client *http.Client, baseURL string) *Client {
return &Client{
baseURL: baseURL,
httpClient: &http.Client{
Timeout: httpTimeout,
},
token: apiKey,
baseURL: baseURL,
httpClient: client,
token: apiKey,
}
}

Expand Down
125 changes: 125 additions & 0 deletions pkg/client/fixtures/auth_token.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
version: 2
interactions:
- id: 0
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 73
transfer_encoding: []
trailer: {}
host: api.vercel.com
remote_addr: ""
request_uri: ""
body: '{"name":"vault-plugin-secrets-vercel-fixtures-token-1688557985495261000"}'
form: {}
headers:
Content-Type:
- application/json
url: https://api.vercel.com/v3/user/tokens
method: POST
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding: []
trailer: {}
content_length: 351
uncompressed: false
body: '{"token":{"id":"e69a2ddb9d93b314f67bd8caf205380d6a9d1be66548cc3e841687c502be083d","name":"vault-plugin-secrets-vercel-fixtures-token-1688557985495261000","type":"token","origin":"manual"},"bearerToken":"[REDACTED]"}'
headers:
Access-Control-Allow-Headers:
- Authorization, Accept, Content-Type
Access-Control-Allow-Methods:
- OPTIONS, GET, POST, PUT, PATCH, DELETE
Access-Control-Allow-Origin:
- '*'
Cache-Control:
- public, max-age=0, must-revalidate
Connection:
- keep-alive
Content-Length:
- "351"
Content-Type:
- application/json; charset=utf-8
Date:
- Wed, 05 Jul 2023 11:53:06 GMT
Server:
- Vercel
Strict-Transport-Security:
- max-age=63072000; includeSubDomains; preload
X-Ratelimit-Limit:
- "32"
X-Ratelimit-Remaining:
- "26"
X-Ratelimit-Reset:
- "1688559854"
X-Vercel-Cache:
- MISS
X-Vercel-Id:
- sfo1::h4mlx-1688557986279-f70b30120caf
status: 200 OK
code: 200
duration: 902.193791ms
- id: 1
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.vercel.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Content-Type:
- application/json
url: https://api.vercel.com/v3/user/tokens/e69a2ddb9d93b314f67bd8caf205380d6a9d1be66548cc3e841687c502be083d
method: DELETE
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding: []
trailer: {}
content_length: 78
uncompressed: false
body: '{"tokenId":"e69a2ddb9d93b314f67bd8caf205380d6a9d1be66548cc3e841687c502be083d"}'
headers:
Access-Control-Allow-Headers:
- Authorization, Accept, Content-Type
Access-Control-Allow-Methods:
- OPTIONS, GET, POST, PUT, PATCH, DELETE
Access-Control-Allow-Origin:
- '*'
Cache-Control:
- public, max-age=0, must-revalidate
Connection:
- keep-alive
Content-Length:
- "78"
Content-Type:
- application/json; charset=utf-8
Date:
- Wed, 05 Jul 2023 11:53:07 GMT
Server:
- Vercel
Strict-Transport-Security:
- max-age=63072000; includeSubDomains; preload
X-Ratelimit-Limit:
- "50"
X-Ratelimit-Remaining:
- "45"
X-Ratelimit-Reset:
- "1688558217"
X-Vercel-Cache:
- MISS
X-Vercel-Id:
- sfo1::h4mlx-1688557987539-e432e73a1484
status: 200 OK
code: 200
duration: 376.691125ms
32 changes: 32 additions & 0 deletions pkg/client/token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package client

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
"gopkg.in/dnaeon/go-vcr.v3/recorder"
)

func TestCreateDeleteToken(t *testing.T) {
recordHelper(t, "auth_token", func(ctx context.Context, t *testing.T, rec *recorder.Recorder, c *Client) {
require.NotNil(t, c.httpClient)
pfx := "vault-plugin-secrets-vercel-fixtures-token"
ts := time.Now().UnixNano()
name := fmt.Sprintf("%s-%d", pfx, ts)
res, err := c.CreateAuthToken(ctx, &CreateAuthTokenRequest{
Name: name,
})
require.NoError(t, err)

res2, err := c.DeleteAuthToken(ctx, &DeleteAuthTokenRequest{
ID: res.Token.ID,
})

require.NoError(t, err)

require.Equal(t, res2.ID, res.Token.ID)
})
}
61 changes: 61 additions & 0 deletions pkg/client/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package client

import (
"context"
"encoding/json"
"net/http"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/dnaeon/go-vcr.v3/cassette"
"gopkg.in/dnaeon/go-vcr.v3/recorder"
)

func recordHelper(t *testing.T, fixture string, f func(context.Context, *testing.T, *recorder.Recorder, *Client)) {
t.Helper()

r, err := recorder.New("fixtures/" + fixture)
require.NoError(t, err)

hook := func(i *cassette.Interaction) error {
delete(i.Request.Headers, "Authorization")

if strings.Contains(i.Request.URL, "/user/tokens") && i.Request.Method == http.MethodPost {
c := &CreateAuthTokenResponse{}
if e := json.Unmarshal([]byte(i.Response.Body), c); e != nil {
return e
}

c.BearerToken = "[REDACTED]"

newBody, e := json.Marshal(c)

if e != nil {
return e
}

i.Response.Body = string(newBody)
}

return nil
}

r.AddHook(hook, recorder.BeforeSaveHook)

defer func() {
err = r.Stop()
require.NoError(t, err)
}()

// Required when updating fixtures
apiKey := os.Getenv("VERCEL_TOKEN")

httpClient := r.GetDefaultClient()

ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout)
defer cancel()

f(ctx, t, r, NewWithBaseURL(apiKey, httpClient, DefaultBaseURL))
}
9 changes: 7 additions & 2 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"net/http"

"github.com/thevilledev/vault-plugin-secrets-vercel/pkg/client"
)
Expand All @@ -16,14 +17,18 @@ type Token struct {
}

func New(apiKey string) *Service {
c := &http.Client{}

return &Service{
apiClient: client.New(apiKey),
apiClient: client.New(apiKey, c),
}
}

func NewWithBaseURL(apiKey string, baseURL string) *Service {
c := &http.Client{}

return &Service{
apiClient: client.NewWithBaseURL(apiKey, baseURL),
apiClient: client.NewWithBaseURL(apiKey, c, baseURL),
}
}

Expand Down