github.com/supabase/cli@v1.168.1/internal/encryption/get/get_test.go (about)

     1  package get
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/supabase/cli/internal/testing/apitest"
    10  	"github.com/supabase/cli/internal/utils"
    11  	"github.com/supabase/cli/pkg/api"
    12  	"gopkg.in/h2non/gock.v1"
    13  )
    14  
    15  func TestGetRootKey(t *testing.T) {
    16  	t.Run("fetches project encryption key", func(t *testing.T) {
    17  		// Setup valid project ref
    18  		project := apitest.RandomProjectRef()
    19  		// Setup valid access token
    20  		token := apitest.RandomAccessToken(t)
    21  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    22  		// Flush pending mocks after test execution
    23  		defer gock.OffAll()
    24  		gock.New(utils.DefaultApiHost).
    25  			Get("/v1/projects/" + project + "/pgsodium").
    26  			Reply(http.StatusOK).
    27  			JSON(api.PgsodiumConfigResponse{RootKey: "test-key"})
    28  		// Run test
    29  		err := Run(context.Background(), project)
    30  		// Check error
    31  		assert.NoError(t, err)
    32  		assert.Empty(t, apitest.ListUnmatchedRequests())
    33  	})
    34  
    35  	t.Run("throws on invalid credentials", func(t *testing.T) {
    36  		// Setup valid project ref
    37  		project := apitest.RandomProjectRef()
    38  		// Setup valid access token
    39  		token := apitest.RandomAccessToken(t)
    40  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    41  		// Flush pending mocks after test execution
    42  		defer gock.OffAll()
    43  		gock.New(utils.DefaultApiHost).
    44  			Get("/v1/projects/" + project + "/pgsodium").
    45  			Reply(http.StatusForbidden)
    46  		// Run test
    47  		err := Run(context.Background(), project)
    48  		// Check error
    49  		assert.ErrorContains(t, err, "Unexpected error retrieving project root key:")
    50  		assert.Empty(t, apitest.ListUnmatchedRequests())
    51  	})
    52  }