-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathlogout_test.go
94 lines (86 loc) · 2.95 KB
/
logout_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package logout
import (
"context"
"os"
"testing"
"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/testing/fstest"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/credentials"
"github.com/zalando/go-keyring"
)
func TestLogoutCommand(t *testing.T) {
token := string(apitest.RandomAccessToken(t))
t.Run("login with token and logout", func(t *testing.T) {
keyring.MockInitWithError(keyring.ErrUnsupportedPlatform)
t.Cleanup(fstest.MockStdin(t, "y"))
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.SaveAccessToken(token, fsys))
// Run test
err := Run(context.Background(), os.Stdout, fsys)
// Check error
assert.NoError(t, err)
saved, err := utils.LoadAccessTokenFS(fsys)
assert.ErrorIs(t, err, utils.ErrMissingToken)
assert.Empty(t, saved)
})
t.Run("removes all Supabase CLI credentials", func(t *testing.T) {
t.Cleanup(credentials.MockInit())
require.NoError(t, credentials.StoreProvider.Set(utils.AccessTokenKey, token))
require.NoError(t, credentials.StoreProvider.Set("project1", "password1"))
require.NoError(t, credentials.StoreProvider.Set("project2", "password2"))
t.Cleanup(fstest.MockStdin(t, "y"))
// Run test
err := Run(context.Background(), os.Stdout, afero.NewMemMapFs())
// Check error
assert.NoError(t, err)
// Check that access token has been removed
saved, _ := credentials.StoreProvider.Get(utils.AccessTokenKey)
assert.Empty(t, saved)
// check that project 1 has been removed
saved, _ = credentials.StoreProvider.Get("project1")
assert.Empty(t, saved)
// check that project 2 has been removed
saved, _ = credentials.StoreProvider.Get("project2")
assert.Empty(t, saved)
})
t.Run("skips logout by default", func(t *testing.T) {
keyring.MockInit()
require.NoError(t, credentials.StoreProvider.Set(utils.AccessTokenKey, token))
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), os.Stdout, fsys)
// Check error
assert.ErrorIs(t, err, context.Canceled)
saved, err := credentials.StoreProvider.Get(utils.AccessTokenKey)
assert.NoError(t, err)
assert.Equal(t, token, saved)
})
t.Run("exits 0 if not logged in", func(t *testing.T) {
keyring.MockInit()
t.Cleanup(fstest.MockStdin(t, "y"))
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), os.Stdout, fsys)
// Check error
assert.NoError(t, err)
})
t.Run("throws error on failure to delete", func(t *testing.T) {
keyring.MockInitWithError(keyring.ErrNotFound)
t.Cleanup(fstest.MockStdin(t, "y"))
// Setup empty home directory
t.Setenv("HOME", "")
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), os.Stdout, fsys)
// Check error
assert.ErrorContains(t, err, "$HOME is not defined")
})
}