github.com/supabase/cli@v1.168.1/internal/projects/delete/delete_test.go (about) 1 package delete 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "testing" 8 9 "github.com/spf13/afero" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "github.com/supabase/cli/internal/testing/apitest" 13 "github.com/supabase/cli/internal/utils" 14 "github.com/supabase/cli/pkg/api" 15 "github.com/zalando/go-keyring" 16 "gopkg.in/h2non/gock.v1" 17 ) 18 19 func TestDeleteCommand(t *testing.T) { 20 ref := apitest.RandomProjectRef() 21 // Setup valid access token 22 token := apitest.RandomAccessToken(t) 23 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 24 // Mock credentials store 25 keyring.MockInit() 26 27 t.Run("deletes project", func(t *testing.T) { 28 // Setup in-memory fs 29 fsys := afero.NewMemMapFs() 30 require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(ref), 0644)) 31 // Setup api mock 32 defer gock.OffAll() 33 gock.New(utils.DefaultApiHost). 34 Delete("/v1/projects/" + ref). 35 Reply(http.StatusOK). 36 JSON(api.V1ProjectRefResponse{ 37 Ref: ref, 38 Name: "test-project", 39 }) 40 // Run test 41 err := Run(context.Background(), ref, afero.NewReadOnlyFs(fsys)) 42 // Check error 43 assert.NoError(t, err) 44 }) 45 46 t.Run("throws error on network failure", func(t *testing.T) { 47 // Setup in-memory fs 48 fsys := afero.NewMemMapFs() 49 // Setup api mock 50 defer gock.OffAll() 51 gock.New(utils.DefaultApiHost). 52 Delete("/v1/projects/" + ref). 53 ReplyError(errors.New("network error")) 54 // Run test 55 err := Run(context.Background(), ref, fsys) 56 // Check error 57 assert.ErrorContains(t, err, "network error") 58 }) 59 60 t.Run("throws error on project not found", func(t *testing.T) { 61 // Setup in-memory fs 62 fsys := afero.NewMemMapFs() 63 // Setup api mock 64 defer gock.OffAll() 65 gock.New(utils.DefaultApiHost). 66 Delete("/v1/projects/" + ref). 67 Reply(http.StatusNotFound) 68 // Run test 69 err := Run(context.Background(), ref, fsys) 70 // Check error 71 assert.ErrorContains(t, err, "Project does not exist:") 72 }) 73 74 t.Run("throws error on service unavailable", func(t *testing.T) { 75 // Setup in-memory fs 76 fsys := afero.NewMemMapFs() 77 // Setup api mock 78 defer gock.OffAll() 79 gock.New(utils.DefaultApiHost). 80 Delete("/v1/projects/" + ref). 81 Reply(http.StatusServiceUnavailable) 82 // Run test 83 err := Run(context.Background(), ref, fsys) 84 // Check error 85 assert.ErrorContains(t, err, "Failed to delete project") 86 }) 87 }