mirror of
https://github.com/gopasspw/gopass.git
synced 2026-05-30 11:18:48 +02:00
dfb8fcb787
This commit adds yet another config handler for gopass. It is based on the format used by git itself. This has the potential to address a lot of long standing issues, but it also causes a lot of changes to how we handle configuration, so bugs are inevitable. Fixes #1567 Fixes #1764 Fixes #1819 Fixes #1878 Fixes #2387 RELEASE_NOTES=[BREAKING] New config format based on git config. Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> Co-authored-by: Yolan Romailler <AnomalRoil@users.noreply.github.com>
189 lines
5.9 KiB
Go
189 lines
5.9 KiB
Go
package action
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/fatih/color"
|
|
"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 TestInsert(t *testing.T) { //nolint:paralleltest
|
|
u := gptest.NewUnitTester(t)
|
|
defer u.Remove()
|
|
|
|
ctx := context.Background()
|
|
ctx = ctxutil.WithAlwaysYes(ctx, true)
|
|
ctx = ctxutil.WithTerminal(ctx, false)
|
|
ctx = ctxutil.WithShowParsing(ctx, true)
|
|
|
|
act, err := newMock(ctx, u.StoreDir(""))
|
|
require.NoError(t, err)
|
|
require.NotNil(t, act)
|
|
ctx = act.cfg.WithConfig(ctx)
|
|
|
|
require.NoError(t, act.cfg.Set("", "core.autoclip", "true"))
|
|
|
|
buf := &bytes.Buffer{}
|
|
out.Stdout = buf
|
|
color.NoColor = true
|
|
defer func() {
|
|
out.Stdout = os.Stdout
|
|
}()
|
|
|
|
t.Run("insert bar", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.Insert(gptest.CliCtx(ctx, t, "bar")))
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert bar baz", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.Insert(gptest.CliCtx(ctx, t, "bar", "baz")))
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert baz via stdin w/o newline", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.insertStdin(ctx, "baz", []byte("foobar"), false))
|
|
buf.Reset()
|
|
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "baz", false))
|
|
assert.Equal(t, "foobar", buf.String())
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert baz via stdin w/ newline", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.insertStdin(ctx, "baz", []byte("foobar\n"), false))
|
|
buf.Reset()
|
|
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "baz", false))
|
|
assert.Equal(t, "foobar\n", buf.String())
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert baz via stdin w/ yaml", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.insertStdin(ctx, "baz", []byte("foobar\n---\nuser: name\nother: meh"), false))
|
|
buf.Reset()
|
|
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "baz", false))
|
|
assert.Equal(t, "foobar\n---\nother: meh\nuser: name\n", buf.String())
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert baz via stdin w/ k-v", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.insertStdin(ctx, "baz", []byte("foobar\ninvalid key-value\nOther: meh\nUser: name\nbody text"), false))
|
|
buf.Reset()
|
|
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "baz", false))
|
|
assert.Equal(t, "foobar\nother: meh\nuser: name\ninvalid key-value\nbody text", buf.String())
|
|
buf.Reset()
|
|
|
|
assert.NoError(t, act.show(ctxutil.WithShowParsing(ctx, false), gptest.CliCtx(ctx, t), "baz", false))
|
|
assert.Equal(t, "foobar\ninvalid key-value\nOther: meh\nUser: name\nbody text", buf.String())
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert zab#key", func(t *testing.T) { //nolint:paralleltest
|
|
ctx = ctxutil.WithInteractive(ctx, false)
|
|
require.NoError(t, act.cfg.Set("", "core.showsafecontent", "true"))
|
|
assert.NoError(t, act.insertYAML(ctx, "zab", "key", []byte("foobar"), nil))
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "zab", false))
|
|
assert.Contains(t, buf.String(), "key: foobar")
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert --multiline bar baz", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.Insert(gptest.CliCtxWithFlags(ctx, t, map[string]string{"multiline": "true"}, "bar", "baz")))
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert key:value", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.Insert(gptest.CliCtxWithFlags(ctx, t, nil, "keyvaltest", "baz:val")))
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "keyvaltest", false))
|
|
assert.Contains(t, buf.String(), "baz: val")
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert baz via stdin w/ yaml and input parsing and safecontent", func(t *testing.T) { //nolint:paralleltest
|
|
assert.NoError(t, act.insertStdin(ctx, "baz", []byte("foobar\n---\nuser: name\nother: 0123"), false))
|
|
buf.Reset()
|
|
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "baz", false))
|
|
assert.Equal(t, "other: 83\nuser: name", buf.String())
|
|
buf.Reset()
|
|
})
|
|
|
|
t.Run("insert baz via stdin w/ yaml and no input parsing", func(t *testing.T) { //nolint:paralleltest
|
|
ctx = ctxutil.WithShowParsing(ctx, false)
|
|
require.NoError(t, act.cfg.Set("", "core.showsafecontent", "false"))
|
|
assert.NoError(t, act.insertStdin(ctx, "baz", []byte("foobar\n---\nuser: name\nother: 0123"), false))
|
|
buf.Reset()
|
|
|
|
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "baz", false))
|
|
assert.Equal(t, "foobar\n---\nuser: name\nother: 0123", buf.String())
|
|
buf.Reset()
|
|
|
|
ctx = ctxutil.WithShowParsing(ctx, true)
|
|
})
|
|
}
|
|
|
|
func TestInsertStdin(t *testing.T) { //nolint:paralleltest
|
|
u := gptest.NewUnitTester(t)
|
|
defer u.Remove()
|
|
|
|
ctx := context.Background()
|
|
ctx = ctxutil.WithAlwaysYes(ctx, true)
|
|
ctx = ctxutil.WithTerminal(ctx, false)
|
|
ctx = ctxutil.WithStdin(ctx, true)
|
|
|
|
act, err := newMock(ctx, u.StoreDir(""))
|
|
require.NoError(t, err)
|
|
require.NotNil(t, act)
|
|
ctx = act.cfg.WithConfig(ctx)
|
|
|
|
require.NoError(t, act.cfg.Set("", "core.autoclip", "false"))
|
|
|
|
buf := &bytes.Buffer{}
|
|
ibuf := &bytes.Buffer{}
|
|
out.Stdout = buf
|
|
stdin = ibuf
|
|
color.NoColor = true
|
|
defer func() {
|
|
out.Stdout = os.Stdout
|
|
stdin = os.Stdin
|
|
}()
|
|
|
|
ibuf.WriteString("foobar")
|
|
assert.Error(t, act.insert(ctx, gptest.CliCtx(ctx, t), "foo", "", false, false, false, false, nil))
|
|
ibuf.Reset()
|
|
buf.Reset()
|
|
|
|
// force
|
|
ibuf.WriteString("foobar")
|
|
assert.NoError(t, act.insert(ctx, gptest.CliCtx(ctx, t), "foo", "", false, false, true, false, nil))
|
|
ibuf.Reset()
|
|
buf.Reset()
|
|
|
|
// append
|
|
ibuf.WriteString("foobar")
|
|
assert.NoError(t, act.insert(ctx, gptest.CliCtx(ctx, t), "foo", "", false, false, false, true, nil))
|
|
ibuf.Reset()
|
|
buf.Reset()
|
|
|
|
// echo
|
|
ibuf.WriteString("foobar")
|
|
assert.NoError(t, act.insert(ctx, gptest.CliCtx(ctx, t), "bar", "", true, false, false, false, nil))
|
|
ibuf.Reset()
|
|
buf.Reset()
|
|
|
|
// multiline
|
|
ibuf.WriteString("foobar")
|
|
assert.NoError(t, act.insert(ctx, gptest.CliCtx(ctx, t), "baz", "", false, true, false, false, nil))
|
|
ibuf.Reset()
|
|
buf.Reset()
|
|
}
|