github.com/GGP1/kure@v0.8.4/commands/add/phrase/phrase_test.go (about)

     1  package phrase
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	cmdutil "github.com/GGP1/kure/commands"
     8  	"github.com/GGP1/kure/db/entry"
     9  	"github.com/GGP1/kure/pb"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestPhrase(t *testing.T) {
    15  	db := cmdutil.SetContext(t)
    16  
    17  	cases := []struct {
    18  		desc      string
    19  		name      string
    20  		separator string
    21  		length    string
    22  		list      string
    23  	}{
    24  		{
    25  			desc:      "No list",
    26  			name:      "test",
    27  			separator: "/",
    28  			length:    "7",
    29  			list:      "nolist",
    30  		},
    31  		{
    32  			desc:      "Word list",
    33  			name:      "test wordlist",
    34  			separator: "&",
    35  			length:    "5",
    36  			list:      "wordlist",
    37  		},
    38  		{
    39  			desc:   "Syllable list",
    40  			name:   "test syllablelist",
    41  			length: "9",
    42  			list:   "syllablelist",
    43  		},
    44  	}
    45  
    46  	for _, tc := range cases {
    47  		t.Run(tc.desc, func(t *testing.T) {
    48  			buf := bytes.NewBufferString("username\nurl\n03/05/2024\nnotes<")
    49  			cmd := NewCmd(db, buf)
    50  			cmd.SetArgs([]string{tc.name})
    51  
    52  			f := cmd.Flags()
    53  			f.Set("separator", tc.separator)
    54  			f.Set("length", tc.length)
    55  			f.Set("list", tc.list)
    56  
    57  			err := cmd.Execute()
    58  			assert.NoError(t, err)
    59  		})
    60  	}
    61  }
    62  
    63  func TestPhraseErrors(t *testing.T) {
    64  	db := cmdutil.SetContext(t)
    65  
    66  	err := entry.Create(db, &pb.Entry{Name: "test"})
    67  	assert.NoError(t, err)
    68  
    69  	cases := []struct {
    70  		desc    string
    71  		name    string
    72  		length  string
    73  		include string
    74  		exclude string
    75  		list    string
    76  	}{
    77  		{
    78  			desc:   "Invalid name",
    79  			name:   "",
    80  			length: "4",
    81  			list:   "word list",
    82  		},
    83  		{
    84  			desc:   "Invalid length",
    85  			name:   "test-invalid-length",
    86  			length: "0",
    87  			list:   "nolist",
    88  		},
    89  		{
    90  			desc:   "Invalid list",
    91  			name:   "test-invalid-list",
    92  			length: "3",
    93  			list:   "invalid",
    94  		},
    95  		{
    96  			desc:   "Already exists",
    97  			name:   "test",
    98  			length: "3",
    99  		},
   100  		{
   101  			desc:    "Included word is also excluded",
   102  			name:    "test words",
   103  			length:  "4",
   104  			include: "test",
   105  			exclude: "test",
   106  		},
   107  	}
   108  
   109  	for _, tc := range cases {
   110  		t.Run(tc.desc, func(t *testing.T) {
   111  			buf := bytes.NewBufferString("username\nurl\n03/05/2024\nnotes<")
   112  			cmd := NewCmd(db, buf)
   113  			cmd.SetArgs([]string{tc.name})
   114  
   115  			f := cmd.Flags()
   116  			f.Set("length", tc.length)
   117  			f.Set("include", tc.include)
   118  			f.Set("exclude", tc.exclude)
   119  			f.Set("list", tc.list)
   120  
   121  			err := cmd.Execute()
   122  			assert.Error(t, err)
   123  		})
   124  	}
   125  }
   126  
   127  func TestEntryInput(t *testing.T) {
   128  	expected := &pb.Entry{
   129  		Name:     "test",
   130  		Username: "username",
   131  		URL:      "url",
   132  		Notes:    "notes",
   133  		Expires:  "Fri, 03 May 2024 00:00:00 +0000",
   134  	}
   135  
   136  	buf := bytes.NewBufferString("username\nurl\n03/05/2024\nnotes<")
   137  	got, err := entryInput(buf, "test")
   138  	assert.NoError(t, err, "Failed creating entry")
   139  
   140  	// As it's randomly generated, use the same one
   141  	expected.Password = got.Password
   142  	assert.Equal(t, expected, got)
   143  }
   144  
   145  func TestInvalidExpirationTime(t *testing.T) {
   146  	buf := bytes.NewBufferString("username\nurl\nnotes\ninvalid<\n")
   147  	_, err := entryInput(buf, "test")
   148  	assert.Error(t, err)
   149  }
   150  
   151  func TestPostRun(t *testing.T) {
   152  	NewCmd(nil, nil).PostRun(nil, nil)
   153  }