github.com/supabase/cli@v1.168.1/internal/logout/logout_test.go (about)

     1  package logout
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/supabase/cli/internal/testing/apitest"
    12  	"github.com/supabase/cli/internal/testing/fstest"
    13  	"github.com/supabase/cli/internal/utils"
    14  	"github.com/supabase/cli/internal/utils/credentials"
    15  	"github.com/zalando/go-keyring"
    16  )
    17  
    18  func TestLogoutCommand(t *testing.T) {
    19  	token := string(apitest.RandomAccessToken(t))
    20  
    21  	t.Run("login with token and logout", func(t *testing.T) {
    22  		keyring.MockInitWithError(keyring.ErrUnsupportedPlatform)
    23  		defer fstest.MockStdin(t, "y")()
    24  		// Setup in-memory fs
    25  		fsys := afero.NewMemMapFs()
    26  		require.NoError(t, utils.SaveAccessToken(token, fsys))
    27  		// Run test
    28  		err := Run(context.Background(), os.Stdout, fsys)
    29  		// Check error
    30  		assert.NoError(t, err)
    31  		saved, err := utils.LoadAccessTokenFS(fsys)
    32  		assert.ErrorIs(t, err, utils.ErrMissingToken)
    33  		assert.Empty(t, saved)
    34  	})
    35  
    36  	t.Run("skips logout by default", func(t *testing.T) {
    37  		keyring.MockInit()
    38  		require.NoError(t, credentials.Set(utils.AccessTokenKey, token))
    39  		// Setup in-memory fs
    40  		fsys := afero.NewMemMapFs()
    41  		// Run test
    42  		err := Run(context.Background(), os.Stdout, fsys)
    43  		// Check error
    44  		assert.NoError(t, err)
    45  		saved, err := credentials.Get(utils.AccessTokenKey)
    46  		assert.NoError(t, err)
    47  		assert.Equal(t, token, saved)
    48  	})
    49  
    50  	t.Run("exits 0 if not logged in", func(t *testing.T) {
    51  		keyring.MockInit()
    52  		defer fstest.MockStdin(t, "y")()
    53  		// Setup in-memory fs
    54  		fsys := afero.NewMemMapFs()
    55  		// Run test
    56  		err := Run(context.Background(), os.Stdout, fsys)
    57  		// Check error
    58  		assert.NoError(t, err)
    59  	})
    60  
    61  	t.Run("throws error on failure to delete", func(t *testing.T) {
    62  		keyring.MockInitWithError(keyring.ErrNotFound)
    63  		defer fstest.MockStdin(t, "y")()
    64  		// Setup empty home directory
    65  		t.Setenv("HOME", "")
    66  		// Setup in-memory fs
    67  		fsys := afero.NewMemMapFs()
    68  		// Run test
    69  		err := Run(context.Background(), os.Stdout, fsys)
    70  		// Check error
    71  		assert.ErrorContains(t, err, "$HOME is not defined")
    72  	})
    73  }