-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathunlink_test.go
68 lines (61 loc) · 1.96 KB
/
unlink_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
package unlink
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/utils"
"github.com/supabase/cli/internal/utils/credentials"
"github.com/zalando/go-keyring"
)
func TestUnlinkCommand(t *testing.T) {
keyring.MockInit()
project := apitest.RandomProjectRef()
t.Run("unlinks project", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644))
// Save database password
require.NoError(t, credentials.StoreProvider.Set(project, "test"))
// Run test
err := Run(context.Background(), fsys)
// Check error
assert.NoError(t, err)
// Validate file does not exist
exists, err := afero.Exists(fsys, utils.ProjectRefPath)
assert.NoError(t, err)
assert.False(t, exists)
// Check credentials does not exist
_, err = credentials.StoreProvider.Get(project)
assert.ErrorIs(t, err, keyring.ErrNotFound)
})
t.Run("unlinks project without credentials", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644))
// Run test
err := Run(context.Background(), fsys)
// Check error
assert.NoError(t, err)
})
t.Run("throws error if not linked", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), fsys)
// Check error
assert.ErrorIs(t, err, utils.ErrNotLinked)
})
t.Run("throws error on permission denied", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644))
// Run test
err := Run(context.Background(), afero.NewReadOnlyFs(fsys))
// Check error
assert.ErrorIs(t, err, os.ErrPermission)
})
}