github.com/GGP1/kure@v0.8.4/commands/2fa/rm/rm_test.go (about) 1 package rm 2 3 import ( 4 "bytes" 5 "testing" 6 7 cmdutil "github.com/GGP1/kure/commands" 8 "github.com/GGP1/kure/db/totp" 9 "github.com/GGP1/kure/pb" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestRm(t *testing.T) { 15 db := cmdutil.SetContext(t) 16 17 err := totp.Create(db, &pb.TOTP{Name: "test"}) 18 assert.NoError(t, err) 19 20 cases := []struct { 21 desc string 22 name string 23 confirmation string 24 }{ 25 { 26 desc: "Abort", 27 name: "test", 28 confirmation: "n", 29 }, 30 { 31 desc: "Proceed", 32 name: "test", 33 confirmation: "y", 34 }, 35 } 36 37 for _, tc := range cases { 38 t.Run(tc.desc, func(t *testing.T) { 39 buf := bytes.NewBufferString(tc.confirmation) 40 cmd := NewCmd(db, buf) 41 cmd.SetArgs([]string{tc.name}) 42 43 err := cmd.Execute() 44 assert.NoError(t, err, "Failed removing the TOTP") 45 }) 46 } 47 } 48 49 func TestRmErrors(t *testing.T) { 50 db := cmdutil.SetContext(t) 51 52 cases := []struct { 53 desc string 54 name string 55 confirmation string 56 }{ 57 { 58 desc: "Invalid name", 59 name: "", 60 }, 61 { 62 desc: "Does not exists", 63 name: "non-existent", 64 confirmation: "y", 65 }, 66 } 67 68 for _, tc := range cases { 69 t.Run(tc.desc, func(t *testing.T) { 70 buf := bytes.NewBufferString(tc.confirmation) 71 cmd := NewCmd(db, buf) 72 cmd.SetArgs([]string{tc.name}) 73 74 err := cmd.Execute() 75 assert.Error(t, err) 76 }) 77 } 78 }