-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: #2715
- Loading branch information
Showing
8 changed files
with
294 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package unpause | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-errors/errors" | ||
"github.com/supabase/cli/internal/utils" | ||
) | ||
|
||
func PreRun(ctx context.Context, ref string) error { | ||
if err := utils.AssertProjectRefIsValid(ref); err != nil { | ||
return err | ||
} | ||
title := fmt.Sprintf("Do you want to unpause project %s?", utils.Aqua(ref)) | ||
if shouldUnpause, err := utils.NewConsole().PromptYesNo(ctx, title, false); err != nil { | ||
return err | ||
} else if !shouldUnpause { | ||
return errors.New(context.Canceled) | ||
} | ||
return nil | ||
} | ||
|
||
func Run(ctx context.Context, ref string) error { | ||
resp, err := utils.GetSupabase().V1UnpauseAProjectWithResponse(ctx, ref) | ||
if err != nil { | ||
return errors.Errorf("failed to unpause project: %w", err) | ||
} | ||
|
||
switch resp.StatusCode() { | ||
case http.StatusNotFound: | ||
return errors.New("Project does not exist:" + utils.Aqua(ref)) | ||
case http.StatusCreated: | ||
break | ||
default: | ||
return errors.Errorf("Failed to unpause project %s: %s", utils.Aqua(ref), string(resp.Body)) | ||
} | ||
|
||
fmt.Println("Unpausing project: " + utils.Aqua(ref) + " it should be ready in a few minutes.\nRun: " + utils.Bold("supabase projects list") + " to see your projects status.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package unpause | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/supabase/cli/internal/testing/apitest" | ||
"github.com/supabase/cli/internal/utils" | ||
"github.com/supabase/cli/pkg/api" | ||
"github.com/zalando/go-keyring" | ||
) | ||
|
||
func TestUnpauseCommand(t *testing.T) { | ||
ref := apitest.RandomProjectRef() | ||
// Setup valid access token | ||
token := apitest.RandomAccessToken(t) | ||
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) | ||
// Mock credentials store | ||
keyring.MockInit() | ||
|
||
t.Run("unpause project", func(t *testing.T) { | ||
// Setup in-memory fs | ||
fsys := afero.NewMemMapFs() | ||
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(ref), 0644)) | ||
// Setup api mock | ||
defer gock.OffAll() | ||
gock.New(utils.DefaultApiHost). | ||
Post("/v1/projects/" + ref + "/unpause"). | ||
Reply(http.StatusCreated). | ||
JSON(api.V1UnpauseAProjectResponse{}) | ||
// Run test | ||
err := Run(context.Background(), ref) | ||
// Check error | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("throws error on network failure", func(t *testing.T) { | ||
// Setup api mock | ||
defer gock.OffAll() | ||
gock.New(utils.DefaultApiHost). | ||
Post("/v1/projects/" + ref + "/unpause"). | ||
ReplyError(errors.New("network error")) | ||
// Run test | ||
err := Run(context.Background(), ref) | ||
// Check error | ||
assert.ErrorContains(t, err, "network error") | ||
}) | ||
|
||
t.Run("throws error on project not found", func(t *testing.T) { | ||
// Setup api mock | ||
defer gock.OffAll() | ||
gock.New(utils.DefaultApiHost). | ||
Post("/v1/projects/" + ref + "/unpause"). | ||
Reply(http.StatusNotFound) | ||
// Run test | ||
err := Run(context.Background(), ref) | ||
// Check error | ||
assert.ErrorContains(t, err, "Project does not exist:") | ||
}) | ||
|
||
t.Run("throws error on service unavailable", func(t *testing.T) { | ||
// Setup api mock | ||
defer gock.OffAll() | ||
gock.New(utils.DefaultApiHost). | ||
Post("/v1/projects/" + ref + "/unpause"). | ||
Reply(http.StatusServiceUnavailable) | ||
// Run test | ||
err := Run(context.Background(), ref) | ||
// Check error | ||
assert.ErrorContains(t, err, "Failed to unpause project") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.