github.com/GGP1/kure@v0.8.4/commands/2fa/2fa_test.go (about)

     1  package tfa
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	cmdutil "github.com/GGP1/kure/commands"
     8  	"github.com/GGP1/kure/config"
     9  	"github.com/GGP1/kure/db/entry"
    10  	"github.com/GGP1/kure/db/totp"
    11  	"github.com/GGP1/kure/pb"
    12  
    13  	"github.com/atotto/clipboard"
    14  	"github.com/stretchr/testify/assert"
    15  	bolt "go.etcd.io/bbolt"
    16  )
    17  
    18  func Test2FA(t *testing.T) {
    19  	if clipboard.Unsupported {
    20  		t.Skip("No clipboard utilities available")
    21  	}
    22  	db := cmdutil.SetContext(t)
    23  	createElements(t, db)
    24  
    25  	cases := []struct {
    26  		desc    string
    27  		name    string
    28  		copy    string
    29  		info    string
    30  		timeout string
    31  	}{
    32  		{
    33  			desc: "List all",
    34  			name: "",
    35  		},
    36  		{
    37  			desc: "List one",
    38  			name: "test",
    39  		},
    40  		{
    41  			desc: "Copy with default timeout",
    42  			name: "test",
    43  			copy: "true",
    44  		},
    45  		{
    46  			desc:    "Copy with custom timeout",
    47  			name:    "test",
    48  			copy:    "true",
    49  			timeout: "1ns",
    50  		},
    51  		{
    52  			desc: "Show setup key information",
    53  			name: "test",
    54  			info: "true",
    55  		},
    56  	}
    57  
    58  	cmd := NewCmd(db)
    59  	config.Set("clipboard.timeout", "1ns") // Set default
    60  
    61  	for _, tc := range cases {
    62  		t.Run(tc.desc, func(t *testing.T) {
    63  			cmd.SetArgs([]string{tc.name})
    64  			f := cmd.Flags()
    65  			f.Set("copy", tc.copy)
    66  			f.Set("info", tc.info)
    67  			f.Set("timeout", tc.timeout)
    68  
    69  			err := cmd.Execute()
    70  			assert.NoError(t, err, "Failed generating TOTP code")
    71  		})
    72  	}
    73  }
    74  
    75  func Test2FAErrors(t *testing.T) {
    76  	db := cmdutil.SetContext(t)
    77  
    78  	cases := []struct {
    79  		desc string
    80  		name string
    81  	}{
    82  		{
    83  			desc: "Entry does not exist",
    84  			name: "non-existent",
    85  		},
    86  	}
    87  
    88  	cmd := NewCmd(db)
    89  
    90  	for _, tc := range cases {
    91  		t.Run(tc.desc, func(t *testing.T) {
    92  			cmd.SetArgs([]string{tc.name})
    93  
    94  			err := cmd.Execute()
    95  			assert.Error(t, err)
    96  		})
    97  	}
    98  }
    99  
   100  func TestGenerateTOTP(t *testing.T) {
   101  	cases := []struct {
   102  		desc     string
   103  		expected string
   104  		digits   int
   105  	}{
   106  		{
   107  			desc:     "6 digits",
   108  			digits:   6,
   109  			expected: "419244",
   110  		},
   111  		{
   112  			desc:     "7 digits",
   113  			digits:   7,
   114  			expected: "0419244",
   115  		},
   116  		{
   117  			desc:     "8 digits",
   118  			digits:   8,
   119  			expected: "80419244",
   120  		},
   121  	}
   122  
   123  	for _, tc := range cases {
   124  		t.Run(tc.desc, func(t *testing.T) {
   125  			unixTime := time.Unix(10, 0)
   126  
   127  			got := GenerateTOTP("IFGEWRKSIFJUMR2R", unixTime, tc.digits)
   128  			assert.Equal(t, tc.expected, got)
   129  		})
   130  	}
   131  }
   132  
   133  func TestPostRun(t *testing.T) {
   134  	NewCmd(nil).PostRun(nil, nil)
   135  }
   136  
   137  func createElements(t *testing.T, db *bolt.DB) {
   138  	t.Helper()
   139  	err := entry.Create(db, &pb.Entry{Name: "test"})
   140  	assert.NoError(t, err)
   141  
   142  	err = totp.Create(db, &pb.TOTP{Name: "test", Raw: "AG5H1H2"})
   143  	assert.NoError(t, err)
   144  }