Repository: supabase/cli
Pull request #311 (2022-07-23 05:31:43)

test: list organizations command
## What kind of change does this PR introduce?

unit test

## What is the current behavior?

Please link any relevant issues here.

## What is the new behavior?

- 84.8% coverage

## Additional context

Add any other context or screenshots.



Patch:
diff --git a/cmd/orgs.go b/cmd/orgs.go
index 59fa80d9..5c1c0345 100644
--- a/cmd/orgs.go
+++ b/cmd/orgs.go
@@ -1,6 +1,7 @@
 package cmd
 
 import (
+	"github.com/spf13/afero"
 	"github.com/spf13/cobra"
 	"github.com/supabase/cli/internal/orgs/list"
 )
@@ -15,7 +16,7 @@ var (
 		Use:   "list",
 		Short: "List all organizations the logged-in user belongs.",
 		RunE: func(cmd *cobra.Command, args []string) error {
-			return list.Run()
+			return list.Run(afero.NewOsFs())
 		},
 	}
 )
diff --git a/internal/orgs/list/list.go b/internal/orgs/list/list.go
index 94ccac31..8eda9d2a 100644
--- a/internal/orgs/list/list.go
+++ b/internal/orgs/list/list.go
@@ -9,11 +9,17 @@ import (
 	"strings"
 
 	"github.com/charmbracelet/glamour"
+	"github.com/spf13/afero"
 	"github.com/supabase/cli/internal/utils"
 )
 
-func Run() error {
-	accessToken, err := utils.LoadAccessToken()
+type Organization struct {
+	Id   string `json:"id"`
+	Name string `json:"name"`
+}
+
+func Run(fsys afero.Fs) error {
+	accessToken, err := utils.LoadAccessTokenFS(fsys)
 	if err != nil {
 		return err
 	}
@@ -43,10 +49,7 @@ func Run() error {
 		return err
 	}
 
-	var orgs []struct {
-		Id   string `json:"id"`
-		Name string `json:"name"`
-	}
+	var orgs []Organization
 	if err := json.Unmarshal(body, &orgs); err != nil {
 		return err
 	}


Test patch:
diff --git a/internal/orgs/list/list_test.go b/internal/orgs/list/list_test.go
new file mode 100644
index 00000000..944e0698
--- /dev/null
+++ b/internal/orgs/list/list_test.go
@@ -0,0 +1,85 @@
+package list
+
+import (
+	"errors"
+	"testing"
+
+	"github.com/spf13/afero"
+	"github.com/stretchr/testify/assert"
+	"github.com/supabase/cli/internal/testing/apitest"
+	"gopkg.in/h2non/gock.v1"
+)
+
+func TestOrganizationListCommand(t *testing.T) {
+	t.Run("lists all organizations", func(t *testing.T) {
+		// Setup in-memory fs
+		fsys := afero.NewMemMapFs()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Flush pending mocks after test execution
+		defer gock.Off()
+		gock.New("https://api.supabase.io").
+			Get("/v1/organizations").
+			Reply(200).
+			JSON([]Organization{
+				{
+					Id:   "combined-fuchsia-lion",
+					Name: "Test Organization",
+				},
+			})
+		// Run test
+		assert.NoError(t, Run(fsys))
+	})
+
+	t.Run("throws error on failure to load token", func(t *testing.T) {
+		assert.Error(t, Run(afero.NewMemMapFs()))
+	})
+
+	t.Run("throws error on network error", func(t *testing.T) {
+		// Setup in-memory fs
+		fsys := afero.NewMemMapFs()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Flush pending mocks after test execution
+		defer gock.Off()
+		gock.New("https://api.supabase.io").
+			Get("/v1/organizations").
+			ReplyError(errors.New("network error"))
+		// Run test
+		assert.Error(t, Run(fsys))
+	})
+
+	t.Run("throws error on server unavailable", func(t *testing.T) {
+		// Setup in-memory fs
+		fsys := afero.NewMemMapFs()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Flush pending mocks after test execution
+		defer gock.Off()
+		gock.New("https://api.supabase.io").
+			Get("/v1/organizations").
+			Reply(500).
+			JSON(map[string]string{"message": "unavailable"})
+		// Run test
+		assert.Error(t, Run(fsys))
+	})
+
+	t.Run("throws error on malformed json", func(t *testing.T) {
+		// Setup in-memory fs
+		fsys := afero.NewMemMapFs()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Flush pending mocks after test execution
+		defer gock.Off()
+		gock.New("https://api.supabase.io").
+			Get("/v1/organizations").
+			Reply(200).
+			JSON(map[string]string{})
+		// Run test
+		assert.Error(t, Run(fsys))
+	})
+}