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

     1  package list
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/Redstoneguy129/cli/internal/testing/apitest"
    12  	"github.com/Redstoneguy129/cli/internal/utils"
    13  	"github.com/Redstoneguy129/cli/pkg/api"
    14  	"gopkg.in/h2non/gock.v1"
    15  )
    16  
    17  func TestSecretListCommand(t *testing.T) {
    18  	t.Run("lists all secrets", func(t *testing.T) {
    19  		// Setup in-memory fs
    20  		fsys := afero.NewMemMapFs()
    21  		_, err := fsys.Create(utils.ConfigPath)
    22  		require.NoError(t, err)
    23  		// Setup valid project ref
    24  		project := apitest.RandomProjectRef()
    25  		err = afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644)
    26  		require.NoError(t, err)
    27  		// Setup valid access token
    28  		token := apitest.RandomAccessToken(t)
    29  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    30  		// Flush pending mocks after test execution
    31  		defer gock.OffAll()
    32  		gock.New("https://api.supabase.io").
    33  			Get("/v1/projects/" + project + "/secrets").
    34  			Reply(200).
    35  			JSON(api.CreateSecretsJSONBody{
    36  				{
    37  					Name:  "Test Secret",
    38  					Value: "dummy-secret-value",
    39  				},
    40  			})
    41  		// Run test
    42  		assert.NoError(t, Run(context.Background(), fsys))
    43  		// Validate api
    44  		assert.Empty(t, apitest.ListUnmatchedRequests())
    45  	})
    46  
    47  	t.Run("throws error on missing config file", func(t *testing.T) {
    48  		assert.Error(t, Run(context.Background(), afero.NewMemMapFs()))
    49  	})
    50  
    51  	t.Run("throws error on missing project ref", func(t *testing.T) {
    52  		// Setup in-memory fs
    53  		fsys := afero.NewMemMapFs()
    54  		_, err := fsys.Create(utils.ConfigPath)
    55  		require.NoError(t, err)
    56  		// Run test
    57  		assert.Error(t, Run(context.Background(), fsys))
    58  	})
    59  
    60  	t.Run("throws error on missing access token", func(t *testing.T) {
    61  		// Setup in-memory fs
    62  		fsys := afero.NewMemMapFs()
    63  		_, err := fsys.Create(utils.ConfigPath)
    64  		require.NoError(t, err)
    65  		_, err = fsys.Create(utils.ProjectRefPath)
    66  		require.NoError(t, err)
    67  		// Run test
    68  		assert.Error(t, Run(context.Background(), fsys))
    69  	})
    70  
    71  	t.Run("throws error on network error", func(t *testing.T) {
    72  		// Setup in-memory fs
    73  		fsys := afero.NewMemMapFs()
    74  		_, err := fsys.Create(utils.ConfigPath)
    75  		require.NoError(t, err)
    76  		// Setup valid project ref
    77  		project := apitest.RandomProjectRef()
    78  		err = afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644)
    79  		require.NoError(t, err)
    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/projects/" + project + "/secrets").
    87  			ReplyError(errors.New("network error"))
    88  		// Run test
    89  		assert.Error(t, Run(context.Background(), fsys))
    90  		// Validate api
    91  		assert.Empty(t, apitest.ListUnmatchedRequests())
    92  	})
    93  
    94  	t.Run("throws error on server unavailable", func(t *testing.T) {
    95  		// Setup in-memory fs
    96  		fsys := afero.NewMemMapFs()
    97  		_, err := fsys.Create(utils.ConfigPath)
    98  		require.NoError(t, err)
    99  		// Setup valid project ref
   100  		project := apitest.RandomProjectRef()
   101  		err = afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644)
   102  		require.NoError(t, err)
   103  		// Setup valid access token
   104  		token := apitest.RandomAccessToken(t)
   105  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
   106  		// Flush pending mocks after test execution
   107  		defer gock.OffAll()
   108  		gock.New("https://api.supabase.io").
   109  			Get("/v1/projects/" + project + "/secrets").
   110  			Reply(500).
   111  			JSON(map[string]string{"message": "unavailable"})
   112  		// Run test
   113  		assert.Error(t, Run(context.Background(), fsys))
   114  		// Validate api
   115  		assert.Empty(t, apitest.ListUnmatchedRequests())
   116  	})
   117  
   118  	t.Run("throws error on malformed json", func(t *testing.T) {
   119  		// Setup in-memory fs
   120  		fsys := afero.NewMemMapFs()
   121  		_, err := fsys.Create(utils.ConfigPath)
   122  		require.NoError(t, err)
   123  		// Setup valid project ref
   124  		project := apitest.RandomProjectRef()
   125  		err = afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644)
   126  		require.NoError(t, err)
   127  		// Setup valid access token
   128  		token := apitest.RandomAccessToken(t)
   129  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
   130  		// Flush pending mocks after test execution
   131  		defer gock.OffAll()
   132  		gock.New("https://api.supabase.io").
   133  			Get("/v1/projects/" + project + "/secrets").
   134  			Reply(200).
   135  			JSON(map[string]string{})
   136  		// Run test
   137  		assert.Error(t, Run(context.Background(), fsys))
   138  		// Validate api
   139  		assert.Empty(t, apitest.ListUnmatchedRequests())
   140  	})
   141  }