forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_test.go
108 lines (93 loc) · 2.51 KB
/
init_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package action
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/backend/crypto/plain"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInit(t *testing.T) {
u := gptest.NewUnitTester(t)
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)
ctx = backend.WithCryptoBackend(ctx, backend.Plain)
ctx = backend.WithStorageBackend(ctx, backend.FS)
act, err := newMock(ctx, u.StoreDir(""))
require.NoError(t, err)
require.NotNil(t, act)
ctx = act.cfg.WithConfig(ctx)
buf := &bytes.Buffer{}
out.Stdout = buf
out.Stderr = buf
defer func() {
out.Stdout = os.Stdout
out.Stderr = os.Stderr
}()
c := gptest.CliCtx(ctx, t, "[email protected]")
assert.NoError(t, act.IsInitialized(c))
assert.Error(t, act.Init(c))
assert.NoError(t, act.Setup(c))
crypto := act.Store.Crypto(ctx, "")
require.NotNil(t, crypto)
assert.Equal(t, "plain", crypto.Name())
assert.True(t, act.initHasUseablePrivateKeys(ctx, crypto))
assert.Error(t, act.initGenerateIdentity(ctx, crypto, "foo bar", "[email protected]"))
buf.Reset()
act.printRecipients(ctx, "")
assert.Contains(t, buf.String(), "0xDEADBEEF")
buf.Reset()
// un-initialize the store
assert.NoError(t, os.Remove(filepath.Join(u.StoreDir(""), plain.IDFile)))
assert.Error(t, act.IsInitialized(c))
buf.Reset()
}
func TestInitParseContext(t *testing.T) {
buf := &bytes.Buffer{}
out.Stdout = buf
out.Stderr = buf
defer func() {
out.Stdout = os.Stdout
out.Stderr = os.Stderr
}()
for _, tc := range []struct {
name string
flags map[string]string
check func(context.Context) error
}{
{
name: "crypto age",
flags: map[string]string{"crypto": "age"},
check: func(ctx context.Context) error {
if be := backend.GetCryptoBackend(ctx); be != backend.Age {
return fmt.Errorf("wrong backend: %d", be)
}
return nil
},
},
{
name: "default",
check: func(ctx context.Context) error {
if backend.GetStorageBackend(ctx) != backend.GitFS {
return fmt.Errorf("wrong backend")
}
return nil
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
c := gptest.CliCtxWithFlags(context.Background(), t, tc.flags)
assert.NoError(t, tc.check(initParseContext(c.Context, c)), tc.name)
buf.Reset()
})
}
}