From dbe4f9722bbd63f685b36606abbfd97cbf1dfb10 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 22 Jun 2024 16:35:47 +0700 Subject: all: implement command "export" The "export" command export all issuers to file or standard output, $ gotp export [FILE] List of known supported FORMAT is: uri. If FILE is not defined it will print to standard output. The list of exported issuers are printed in order of its label. --- cli_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'cli_test.go') diff --git a/cli_test.go b/cli_test.go index ef6a07c..485cad5 100644 --- a/cli_test.go +++ b/cli_test.go @@ -10,6 +10,7 @@ import ( "io/fs" "os" "path/filepath" + "strings" "testing" "git.sr.ht/~shulhan/pakakeh.go/lib/test" @@ -90,6 +91,37 @@ test = SHA1:x:6:30: } } +func TestCli_Export(t *testing.T) { + var ( + tdata *test.Data + err error + ) + + tdata, err = test.LoadData(`testdata/cli_Export_test.txt`) + if err != nil { + t.Fatal(err) + } + + var cli = &Cli{ + cfg: &config{ + Issuers: map[string]string{ + `test l@bel`: `SHA1:s3cr3t:6:30:IssuerName`, + }, + }, + } + + var sb = strings.Builder{} + + err = cli.Export(&sb, formatNameURI) + if err != nil { + t.Fatal(err) + } + + var exp = string(tdata.Output[`uri`]) + + test.Assert(t, `Export: uri`, exp, sb.String()) +} + func TestCli_SetPrivateKey(t *testing.T) { var ( tdata *test.Data @@ -265,6 +297,10 @@ func TestCli_withPassphrase(t *testing.T) { testAddWithPassphrase(t, tdata, cli) }) + t.Run(`Export`, func(t *testing.T) { + testExportWithPassphrase(t, tdata, cli) + }) + t.Run(`Generate`, func(t *testing.T) { testGenerateWithPassphrase(t, tdata, cli) }) @@ -330,6 +366,24 @@ func testAddWithPassphrase(t *testing.T, tdata *test.Data, cli *Cli) { mockTermrw.BufRead.Reset() } +func testExportWithPassphrase(t *testing.T, tdata *test.Data, cli *Cli) { + var pass = string(tdata.Input[`gotp.pass`]) + "\r\n" + mockTermrw.BufRead.WriteString(pass) + + t.Cleanup(mockTermrw.BufRead.Reset) + + var got = strings.Builder{} + + var err = cli.Export(&got, formatNameURI) + if err != nil { + t.Fatal(err) + } + + var exp = string(tdata.Output[`gotp.conf:export`]) + + test.Assert(t, `testExportWithPassphrase`, exp, got.String()) +} + func testGenerateWithPassphrase(t *testing.T, tdata *test.Data, cli *Cli) { type testCase struct { label string -- cgit v1.3