(t *testing.T)
| 55 | } |
| 56 | |
| 57 | func TestUseKeyring(t *testing.T) { |
| 58 | // Verify that the --use-keyring flag default opts into using a keyring backend |
| 59 | // for storing session tokens instead of plain text files. |
| 60 | t.Parallel() |
| 61 | |
| 62 | t.Run("Login", func(t *testing.T) { |
| 63 | t.Parallel() |
| 64 | |
| 65 | if runtime.GOOS != "windows" && runtime.GOOS != "darwin" { |
| 66 | t.Skip("keyring is not supported on this OS") |
| 67 | } |
| 68 | |
| 69 | // Create a test server |
| 70 | client := coderdtest.New(t, nil) |
| 71 | coderdtest.CreateFirstUser(t, client) |
| 72 | |
| 73 | // Create a pty for interactive prompts |
| 74 | pty := ptytest.New(t) |
| 75 | |
| 76 | // Create CLI invocation which defaults to using the keyring |
| 77 | env := setupKeyringTestEnv(t, client.URL.String(), |
| 78 | "login", |
| 79 | "--force-tty", |
| 80 | "--no-open", |
| 81 | client.URL.String()) |
| 82 | inv := env.inv |
| 83 | inv.Stdin = pty.Input() |
| 84 | inv.Stdout = pty.Output() |
| 85 | |
| 86 | // Run login in background |
| 87 | doneChan := make(chan struct{}) |
| 88 | go func() { |
| 89 | defer close(doneChan) |
| 90 | err := inv.Run() |
| 91 | assert.NoError(t, err) |
| 92 | }() |
| 93 | |
| 94 | // Provide the token when prompted |
| 95 | pty.ExpectMatch("Paste your token here:") |
| 96 | pty.WriteLine(client.SessionToken()) |
| 97 | pty.ExpectMatch("Welcome to Coder") |
| 98 | <-doneChan |
| 99 | |
| 100 | // Verify that session file was NOT created (using keyring instead) |
| 101 | sessionFile := path.Join(string(env.cfg), "session") |
| 102 | _, err := os.Stat(sessionFile) |
| 103 | require.True(t, os.IsNotExist(err), "session file should not exist when using keyring") |
| 104 | |
| 105 | // Verify that the credential IS stored in OS keyring |
| 106 | cred, err := env.keyring.Read(env.clientURL) |
| 107 | require.NoError(t, err, "credential should be stored in OS keyring") |
| 108 | require.Equal(t, client.SessionToken(), cred, "stored token should match login token") |
| 109 | }) |
| 110 | |
| 111 | t.Run("Logout", func(t *testing.T) { |
| 112 | t.Parallel() |
| 113 | |
| 114 | if runtime.GOOS != "windows" && runtime.GOOS != "darwin" { |
nothing calls this directly
no test coverage detected