github.com/tommi2day/pwcli@v0.0.0-20240317203041-4d1177a5ab91/cmd/pwcli_test.go (about) 1 package cmd 2 3 import ( 4 "os" 5 "path" 6 "testing" 7 8 "github.com/tommi2day/pwcli/test" 9 10 "github.com/tommi2day/gomodules/common" 11 12 "github.com/tommi2day/gomodules/pwlib" 13 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 const plain = ` 19 # Testfile 20 !default:defuser2:failure 21 !default:testuser:default 22 test:testuser:testpass 23 testdp:testuser:xxx:yyy 24 !default:defuser2:default 25 !default:testuser:failure 26 !default:defuser:default 27 ` 28 const kp = "pwcli_test" 29 const wrong = "xxx" 30 31 // nolint gosec 32 const totpSecret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ" 33 34 func TestCLI(t *testing.T) { 35 var err error 36 var out = "" 37 test.Testinit(t) 38 _ = os.RemoveAll(test.TestData) 39 _ = os.Mkdir(test.TestData, 0700) 40 app := "test_pwcli" 41 configFile := path.Join(test.TestData, app+".yaml") 42 pc := pwlib.NewConfig(app, test.TestData, test.TestData, app, typeGO) 43 err = os.Chdir(test.TestDir) 44 require.NoErrorf(t, err, "ChDir failed") 45 filename := pc.PlainTextFile 46 _ = os.Remove(filename) 47 //nolint gosec 48 err = os.WriteFile(filename, []byte(plain), 0644) 49 require.NoErrorf(t, err, "Create testdata failed") 50 t.Run("CMD GenPass", func(t *testing.T) { 51 args := []string{ 52 "gen", 53 "--profile", "10 1 1 1 0 1", 54 "--special_chars", "#!", 55 "--info", 56 "--unit-test", 57 } 58 out, err = common.CmdRun(RootCmd, args) 59 assert.NoErrorf(t, err, "Gen command should not return an error:%s", err) 60 t.Logf(out) 61 }) 62 t.Run("CMD CheckPass default", func(t *testing.T) { 63 args := []string{ 64 "check", 65 "--info", 66 "--unit-test", 67 "NEML2xqZcC", 68 } 69 out, err = common.CmdRun(RootCmd, args) 70 assert.NoErrorf(t, err, "Check command should not return an error:%s", err) 71 assert.Contains(t, out, "matches the given profile", "Output should confirm match") 72 t.Logf(out) 73 }) 74 t.Run("CMD CheckCustom OK", func(t *testing.T) { 75 args := []string{ 76 "check", 77 "--profile", "4 1 1 0 0 1", 78 "--info", 79 "--unit-test", 80 "qZcC", 81 } 82 out, err = common.CmdRun(RootCmd, args) 83 require.NoErrorf(t, err, "Check command should not return an error:%s", err) 84 assert.Contains(t, out, "matches the given profile", "Output should confirm match") 85 t.Logf(out) 86 }) 87 t.Run("CMD CheckPass failure", func(t *testing.T) { 88 args := []string{ 89 "check", 90 "--profile", "12 1 1 1 1 1", 91 "--special_chars", "#!", 92 "--info", 93 "--unit-test", 94 "NEML2xqZcC", 95 } 96 out, err = common.CmdRun(RootCmd, args) 97 require.Errorf(t, err, "Check command should return an error") 98 assert.Contains(t, err.Error(), "matches NOT the given profile", "Output should confirm Nomatch") 99 t.Logf(out) 100 }) 101 t.Run("CMD save config", func(t *testing.T) { 102 args := []string{ 103 "config", 104 "save", 105 "--config", configFile, 106 "--app", app, 107 "--datadir", test.TestData, 108 "--keydir", test.TestData, 109 "--info", 110 "--unit-test", 111 } 112 out, err = common.CmdRun(RootCmd, args) 113 require.NoErrorf(t, err, "Gen command should not return an error:%s", err) 114 assert.Contains(t, out, "config saved to", "Output should confirm saving") 115 t.Logf(out) 116 }) 117 t.Run("CMD Generate Keypair", func(t *testing.T) { 118 args := []string{ 119 "genkey", 120 "--keypass", kp, 121 "--method", typeGO, 122 "--config", configFile, 123 "--app", app, 124 "--info", 125 "--unit-test", 126 } 127 out, err = common.CmdRun(RootCmd, args) 128 require.NoErrorf(t, err, "Generate command should not return an error:%s", err) 129 assert.FileExistsf(t, pc.PrivateKeyFile, "Private key file not found") 130 assert.FileExistsf(t, pc.PubKeyFile, "Public key file not found") 131 assert.Contains(t, out, "New key pair generated as", "Output should confirm key generation") 132 t.Logf(out) 133 }) 134 t.Run("CMD Encrypt go", func(t *testing.T) { 135 args := []string{ 136 "encrypt", 137 "--keypass", kp, 138 "--config", configFile, 139 "--method", typeGO, 140 "--info", 141 "--unit-test", 142 } 143 out, err = common.CmdRun(RootCmd, args) 144 require.NoErrorf(t, err, "Encrypt command should not return an error:%s", err) 145 assert.FileExistsf(t, pc.CryptedFile, "Crypted file '%s' not found", pc.CryptedFile) 146 assert.Contains(t, out, "successfully created", "Output should confirm encryption") 147 t.Logf(out) 148 }) 149 150 t.Run("CMD Encrypt Openssl", func(t *testing.T) { 151 args := []string{ 152 "encrypt", 153 "--keypass", kp, 154 "--config", configFile, 155 "--method", typeOpenSSL, 156 "--info", 157 "--unit-test", 158 } 159 out, err = common.CmdRun(RootCmd, args) 160 expected := path.Join(test.TestData, app+".pw") 161 require.NoErrorf(t, err, "Encrypt command should not return an error:%s", err) 162 assert.FileExistsf(t, expected, "Crypted file '%s' not found", pc.CryptedFile) 163 assert.Contains(t, out, "successfully created", "Output should confirm encryption") 164 t.Logf(out) 165 }) 166 167 t.Run("CMD list", func(t *testing.T) { 168 args := []string{ 169 "list", 170 "--keypass", kp, 171 "--config", configFile, 172 "--info", 173 "--unit-test", 174 } 175 out, err = common.CmdRun(RootCmd, args) 176 require.NoErrorf(t, err, "list command should not return an error:%s", err) 177 assert.Contains(t, out, "List returned 10 lines", "Output should lines of plainfile") 178 t.Logf(out) 179 }) 180 t.Run("CMD get", func(t *testing.T) { 181 args := []string{ 182 "get", 183 "--keypass", kp, 184 "--config", configFile, 185 "--info", 186 "--unit-test", 187 "--system", "test", 188 "--user", "testuser", 189 } 190 out, err = common.CmdRun(RootCmd, args) 191 require.NoErrorf(t, err, "get command should not return an error:%s", err) 192 assert.Contains(t, out, "Found matching entry", "Output should confirm match") 193 assert.Contains(t, out, "'testpass'", "Output should return correct match") 194 t.Logf(out) 195 }) 196 t.Run("CMD get nomatch", func(t *testing.T) { 197 args := []string{ 198 "get", 199 "--keypass", kp, 200 "--config", configFile, 201 "--info", 202 "--unit-test", 203 "--system", "test", 204 "--user", wrong, 205 } 206 out, err = common.CmdRun(RootCmd, args) 207 require.Errorf(t, err, "get command should return an error") 208 assert.NotContains(t, out, "Found matching entry", "Output should not confirm match") 209 t.Logf(out) 210 }) 211 212 t.Run("CMD TOTP", func(t *testing.T) { 213 t.Run("CMD TOTP no secret", func(t *testing.T) { 214 _ = os.Unsetenv("TOTP_SECRET") 215 out = "" 216 args := []string{ 217 "totp", 218 "--info", 219 "--unit-test", 220 } 221 out, err = common.CmdRun(RootCmd, args) 222 require.Errorf(t, err, "totp command should return an error") 223 }) 224 t.Run("CMD TOTP Env", func(t *testing.T) { 225 _ = os.Setenv("TOTP_SECRET", totpSecret) 226 out = "" 227 args := []string{ 228 "totp", 229 "--info", 230 "--unit-test", 231 } 232 out, err = common.CmdRun(RootCmd, args) 233 require.NoErrorf(t, err, "totp command should not return an error:%s", err) 234 assert.Contains(t, out, "TOTP returned", "Output should confirm success") 235 t.Logf(out) 236 }) 237 t.Run("CMD TOTP wrong secret", func(t *testing.T) { 238 args := []string{ 239 "totp", 240 "--secret", wrong, 241 "--info", 242 "--unit-test", 243 } 244 out, err = common.CmdRun(RootCmd, args) 245 require.Errorf(t, err, "totp command should return an error") 246 }) 247 t.Run("CMD TOTP with secret", func(t *testing.T) { 248 args := []string{ 249 "totp", 250 "--secret", totpSecret, 251 "--info", 252 "--unit-test", 253 } 254 out, err = common.CmdRun(RootCmd, args) 255 require.NoErrorf(t, err, "totp command should not return an error:%s", err) 256 assert.Contains(t, out, "TOTP returned", "Output should confirm success") 257 t.Logf(out) 258 }) 259 }) 260 }