github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/orgs/list/list_test.go (about)

     1  package list
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/Redstoneguy129/cli/internal/testing/apitest"
     9  	"github.com/spf13/afero"
    10  	"github.com/stretchr/testify/assert"
    11  	"gopkg.in/h2non/gock.v1"
    12  )
    13  
    14  func TestOrganizationListCommand(t *testing.T) {
    15  	t.Run("lists all organizations", func(t *testing.T) {
    16  		// Setup in-memory fs
    17  		fsys := afero.NewMemMapFs()
    18  		// Setup valid access token
    19  		token := apitest.RandomAccessToken(t)
    20  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    21  		// Flush pending mocks after test execution
    22  		defer gock.OffAll()
    23  		gock.New("https://api.supabase.io").
    24  			Get("/v1/organizations").
    25  			Reply(200).
    26  			JSON([]Organization{
    27  				{
    28  					Id:   "combined-fuchsia-lion",
    29  					Name: "Test Organization",
    30  				},
    31  			})
    32  		// Run test
    33  		assert.NoError(t, Run(context.Background(), fsys))
    34  		// Validate api
    35  		assert.Empty(t, apitest.ListUnmatchedRequests())
    36  	})
    37  
    38  	t.Run("throws error on failure to load token", func(t *testing.T) {
    39  		assert.Error(t, Run(context.Background(), afero.NewMemMapFs()))
    40  	})
    41  
    42  	t.Run("throws error on network error", func(t *testing.T) {
    43  		// Setup in-memory fs
    44  		fsys := afero.NewMemMapFs()
    45  		// Setup valid access token
    46  		token := apitest.RandomAccessToken(t)
    47  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    48  		// Flush pending mocks after test execution
    49  		defer gock.OffAll()
    50  		gock.New("https://api.supabase.io").
    51  			Get("/v1/organizations").
    52  			ReplyError(errors.New("network error"))
    53  		// Run test
    54  		assert.Error(t, Run(context.Background(), fsys))
    55  		// Validate api
    56  		assert.Empty(t, apitest.ListUnmatchedRequests())
    57  	})
    58  
    59  	t.Run("throws error on server unavailable", func(t *testing.T) {
    60  		// Setup in-memory fs
    61  		fsys := afero.NewMemMapFs()
    62  		// Setup valid access token
    63  		token := apitest.RandomAccessToken(t)
    64  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    65  		// Flush pending mocks after test execution
    66  		defer gock.OffAll()
    67  		gock.New("https://api.supabase.io").
    68  			Get("/v1/organizations").
    69  			Reply(500).
    70  			JSON(map[string]string{"message": "unavailable"})
    71  		// Run test
    72  		assert.Error(t, Run(context.Background(), fsys))
    73  		// Validate api
    74  		assert.Empty(t, apitest.ListUnmatchedRequests())
    75  	})
    76  
    77  	t.Run("throws error on malformed json", func(t *testing.T) {
    78  		// Setup in-memory fs
    79  		fsys := afero.NewMemMapFs()
    80  		// Setup valid access token
    81  		token := apitest.RandomAccessToken(t)
    82  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    83  		// Flush pending mocks after test execution
    84  		defer gock.OffAll()
    85  		gock.New("https://api.supabase.io").
    86  			Get("/v1/organizations").
    87  			Reply(200).
    88  			JSON(map[string]string{})
    89  		// Run test
    90  		assert.Error(t, Run(context.Background(), fsys))
    91  		// Validate api
    92  		assert.Empty(t, apitest.ListUnmatchedRequests())
    93  	})
    94  }