supabase-cli/internal/secrets/list/list_test.go

115 lines
3.5 KiB
Go

package list
import (
"context"
"errors"
"testing"
"github.com/h2non/gock"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
)
func TestSecretListCommand(t *testing.T) {
t.Run("lists all secrets", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup valid project ref
project := apitest.RandomProjectRef()
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/secrets").
Reply(200).
JSON([]api.SecretResponse{
{
Name: "Test Secret",
Value: "dummy-secret-value",
},
})
// Run test
err := Run(context.Background(), project, fsys)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on missing access token", func(t *testing.T) {
t.Skip()
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), "", fsys)
// Check error
assert.ErrorContains(t, err, "Unexpected error retrieving project secrets")
})
t.Run("throws error on network error", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup valid project ref
project := apitest.RandomProjectRef()
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/secrets").
ReplyError(errors.New("network error"))
// Run test
err := Run(context.Background(), project, fsys)
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on server unavailable", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup valid project ref
project := apitest.RandomProjectRef()
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/secrets").
Reply(500).
JSON(map[string]string{"message": "unavailable"})
// Run test
err := Run(context.Background(), project, fsys)
// Check error
assert.ErrorContains(t, err, `Unexpected error retrieving project secrets: {"message":"unavailable"}`)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on malformed json", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup valid project ref
project := apitest.RandomProjectRef()
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/secrets").
Reply(200).
JSON(map[string]string{})
// Run test
err := Run(context.Background(), project, fsys)
// Check error
assert.ErrorContains(t, err, "json: cannot unmarshal object into Go value of type []api.SecretResponse")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}