1package action
2
3import (
4	"bytes"
5	"context"
6	"os"
7	"testing"
8
9	"github.com/gopasspw/gopass/internal/out"
10	"github.com/gopasspw/gopass/pkg/ctxutil"
11	"github.com/gopasspw/gopass/tests/gptest"
12
13	"github.com/stretchr/testify/assert"
14	"github.com/stretchr/testify/require"
15)
16
17func TestEdit(t *testing.T) {
18	u := gptest.NewUnitTester(t)
19	defer u.Remove()
20
21	ctx := context.Background()
22	ctx = ctxutil.WithAlwaysYes(ctx, true)
23	ctx = ctxutil.WithTerminal(ctx, false)
24	act, err := newMock(ctx, u)
25	require.NoError(t, err)
26	require.NotNil(t, act)
27
28	buf := &bytes.Buffer{}
29	out.Stdout = buf
30	defer func() {
31		out.Stdout = os.Stdout
32	}()
33
34	// edit
35	assert.Error(t, act.Edit(gptest.CliCtx(ctx, t)))
36	buf.Reset()
37
38	// edit foo (existing)
39	assert.Error(t, act.Edit(gptest.CliCtx(ctx, t, "foo")))
40	buf.Reset()
41
42	// edit bar (new)
43	assert.Error(t, act.Edit(gptest.CliCtx(ctx, t, "foo")))
44	buf.Reset()
45}
46
47func TestEditUpdate(t *testing.T) {
48	u := gptest.NewUnitTester(t)
49	defer u.Remove()
50
51	ctx := context.Background()
52	ctx = ctxutil.WithAlwaysYes(ctx, true)
53	ctx = ctxutil.WithTerminal(ctx, false)
54	act, err := newMock(ctx, u)
55	require.NoError(t, err)
56	require.NotNil(t, act)
57
58	buf := &bytes.Buffer{}
59	out.Stdout = buf
60	defer func() {
61		out.Stdout = os.Stdout
62	}()
63
64	content := []byte("foobar")
65	// no changes
66	assert.NoError(t, act.editUpdate(ctx, "foo", content, content, false, "test"))
67	buf.Reset()
68
69	// changes
70	nContent := []byte("barfoo")
71	assert.NoError(t, act.editUpdate(ctx, "foo", content, nContent, false, "test"))
72	buf.Reset()
73}
74