Compare commits
5 Commits
a78c564e23
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 2b8051efb1 | |||
|
|
5dc4a1b742 | ||
| 691a2acdad | |||
| 19a9e2a3df | |||
| d53b8ec8bc |
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version: [1.21.x, 1.22.x]
|
go-version: [1.23.x]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@@ -42,7 +42,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Calculate coverage
|
- name: Calculate coverage
|
||||||
run: |
|
run: |
|
||||||
TOTAL=$(go test -cover ./... 2>&1 | grep -oP '\d+\.\d+%$' | head -1 | tr -d '%')
|
TOTAL=$(go test -cover ./... 2>&1 | awk '/^ok /{for(i=1;i<=NF;i++) if($i~/%$/) print $i}' | head -1 | tr -d '%')
|
||||||
echo "Coverage: ${TOTAL}"
|
echo "Coverage: ${TOTAL}"
|
||||||
if [ -z "$TOTAL" ]; then
|
if [ -z "$TOTAL" ]; then
|
||||||
echo "No coverage data found"
|
echo "No coverage data found"
|
||||||
@@ -74,7 +74,7 @@ jobs:
|
|||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: 1.21.x
|
go-version: 1.23.x
|
||||||
|
|
||||||
- name: Run GoSec
|
- name: Run GoSec
|
||||||
uses: securego/gosec@v2
|
uses: securego/gosec@v2
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ func loginCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to create session manager: %w", err)
|
return fmt.Errorf("failed to create session manager: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return manager.LoginInteractive(cfg.APIBaseURL)
|
return manager.LoginInteractive(context.Background(), cfg.APIBaseURL)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
152
cmd/auth_test.go
Normal file
152
cmd/auth_test.go
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestLoginCommand tests the login CLI command
|
||||||
|
func TestLoginCommand(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// Create a temporary config file
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
configPath := tmpDir + "/config.yaml"
|
||||||
|
|
||||||
|
configContent := `
|
||||||
|
api:
|
||||||
|
base_url: "http://localhost:8080"
|
||||||
|
timeout: 30s
|
||||||
|
`
|
||||||
|
err := os.WriteFile(configPath, []byte(configContent), 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to write config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set config path environment variable
|
||||||
|
os.Setenv("POP_CONFIG_PATH", configPath)
|
||||||
|
defer os.Unsetenv("POP_CONFIG_PATH")
|
||||||
|
|
||||||
|
// Create root command with login subcommand
|
||||||
|
rootCmd := NewRootCmd()
|
||||||
|
rootCmd.SetArgs([]string{"login"})
|
||||||
|
|
||||||
|
// Capture output
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
rootCmd.SetErr(&buf)
|
||||||
|
|
||||||
|
// Execute command
|
||||||
|
err = rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
// Login requires interactive input, so error is expected in non-interactive mode
|
||||||
|
t.Logf("Login command executed with error (expected in non-interactive mode): %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify command ran
|
||||||
|
output := buf.String()
|
||||||
|
t.Logf("Command output: %s", output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLogoutCommand tests the logout CLI command
|
||||||
|
func TestLogoutCommand(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// Create a temporary config file
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
configPath := tmpDir + "/config.yaml"
|
||||||
|
|
||||||
|
err := os.WriteFile(configPath, []byte("{}"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to write config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Setenv("POP_CONFIG_PATH", configPath)
|
||||||
|
defer os.Unsetenv("POP_CONFIG_PATH")
|
||||||
|
|
||||||
|
// Create root command with logout subcommand
|
||||||
|
rootCmd := NewRootCmd()
|
||||||
|
rootCmd.SetArgs([]string{"logout"})
|
||||||
|
|
||||||
|
// Capture output
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
rootCmd.SetErr(&buf)
|
||||||
|
|
||||||
|
// Execute command
|
||||||
|
err = rootCmd.Execute()
|
||||||
|
// Logout may fail if no session exists, which is expected
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("Logout command executed with error (expected if no session): %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSessionCommand tests the session CLI command
|
||||||
|
func TestSessionCommand(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// Create a temporary config file
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
configPath := tmpDir + "/config.yaml"
|
||||||
|
|
||||||
|
err := os.WriteFile(configPath, []byte("{}"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to write config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Setenv("POP_CONFIG_PATH", configPath)
|
||||||
|
defer os.Unsetenv("POP_CONFIG_PATH")
|
||||||
|
|
||||||
|
// Create root command with session subcommand
|
||||||
|
rootCmd := NewRootCmd()
|
||||||
|
rootCmd.SetArgs([]string{"session"})
|
||||||
|
|
||||||
|
// Capture output
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
rootCmd.SetErr(&buf)
|
||||||
|
|
||||||
|
// Execute command
|
||||||
|
err = rootCmd.Execute()
|
||||||
|
// Session may fail if no active session, which is expected
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("Session command executed with error (expected if no session): %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRootCommandHelp tests the help output
|
||||||
|
func TestRootCommandHelp(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
rootCmd := NewRootCmd()
|
||||||
|
rootCmd.SetArgs([]string{"--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
if len(output) == 0 {
|
||||||
|
t.Error("Help output is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify help contains expected commands
|
||||||
|
helpText := string(output)
|
||||||
|
expectedCommands := []string{"login", "logout", "session", "mail", "contact", "attachment", "folder", "draft"}
|
||||||
|
for _, cmd := range expectedCommands {
|
||||||
|
if !contains(helpText, cmd) {
|
||||||
|
t.Errorf("Help output missing command: %s", cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to check if string contains substring
|
||||||
|
func contains(s, substr string) bool {
|
||||||
|
return len(s) > 0 && len(substr) > 0 && (s == substr || len(s) > len(substr) && (s[:len(substr)] == substr || contains(s[1:], substr)))
|
||||||
|
}
|
||||||
16
cmd/draft.go
16
cmd/draft.go
@@ -64,12 +64,12 @@ func draftSaveCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not authenticated: %w", err)
|
return fmt.Errorf("not authenticated: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -125,12 +125,12 @@ func draftListCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not authenticated: %w", err)
|
return fmt.Errorf("not authenticated: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -190,12 +190,12 @@ func draftEditCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not authenticated: %w", err)
|
return fmt.Errorf("not authenticated: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -241,12 +241,12 @@ func draftSendCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not authenticated: %w", err)
|
return fmt.Errorf("not authenticated: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
|
|||||||
1475
cmd/e2e_full_test.go
Normal file
1475
cmd/e2e_full_test.go
Normal file
File diff suppressed because it is too large
Load Diff
190
cmd/e2e_test.go
Normal file
190
cmd/e2e_test.go
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestMailCommand tests the mail CLI command structure
|
||||||
|
func TestMailCommand(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
rootCmd := newRootCmdBase()
|
||||||
|
rootCmd.SetArgs([]string{"mail", "--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Mail help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
helpText := string(output)
|
||||||
|
|
||||||
|
// Verify mail subcommands are present
|
||||||
|
expectedSubcommands := []string{"list", "read", "send", "delete", "trash", "draft", "search"}
|
||||||
|
for _, subcmd := range expectedSubcommands {
|
||||||
|
if !contains(helpText, subcmd) {
|
||||||
|
t.Errorf("Mail help missing subcommand: %s", subcmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMailListCommand tests the mail list subcommand
|
||||||
|
func TestMailListCommand(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
rootCmd := newRootCmdBase()
|
||||||
|
rootCmd.SetArgs([]string{"mail", "list", "--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Mail list help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
if len(output) == 0 {
|
||||||
|
t.Error("Mail list help output is empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestContactCommand tests the contact CLI command structure
|
||||||
|
func TestContactCommand(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
rootCmd := newRootCmdBase()
|
||||||
|
rootCmd.SetArgs([]string{"contact", "--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Contact help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
helpText := string(output)
|
||||||
|
|
||||||
|
// Verify contact subcommands are present
|
||||||
|
expectedSubcommands := []string{"list", "add", "edit", "delete"}
|
||||||
|
for _, subcmd := range expectedSubcommands {
|
||||||
|
if !contains(helpText, subcmd) {
|
||||||
|
t.Errorf("Contact help missing subcommand: %s", subcmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAttachmentCommand tests the attachment CLI command structure
|
||||||
|
func TestAttachmentCommand(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
rootCmd := newRootCmdBase()
|
||||||
|
rootCmd.SetArgs([]string{"attachment", "--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Attachment help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
helpText := string(output)
|
||||||
|
|
||||||
|
// Verify attachment subcommands are present
|
||||||
|
expectedSubcommands := []string{"upload", "download", "list"}
|
||||||
|
for _, subcmd := range expectedSubcommands {
|
||||||
|
if !contains(helpText, subcmd) {
|
||||||
|
t.Errorf("Attachment help missing subcommand: %s", subcmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFolderCommand tests the folder CLI command structure
|
||||||
|
func TestFolderCommand(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
rootCmd := newRootCmdBase()
|
||||||
|
rootCmd.SetArgs([]string{"folder", "--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Folder help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
helpText := string(output)
|
||||||
|
|
||||||
|
// Verify folder subcommands are present
|
||||||
|
expectedSubcommands := []string{"list", "create", "update", "delete"}
|
||||||
|
for _, subcmd := range expectedSubcommands {
|
||||||
|
if !contains(helpText, subcmd) {
|
||||||
|
t.Errorf("Folder help missing subcommand: %s", subcmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLabelCommand tests the label CLI command structure
|
||||||
|
func TestLabelCommand(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
rootCmd := newRootCmdBase()
|
||||||
|
rootCmd.SetArgs([]string{"label", "--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Label help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
helpText := string(output)
|
||||||
|
|
||||||
|
// Verify label subcommands are present
|
||||||
|
expectedSubcommands := []string{"list", "create", "update", "delete", "apply", "remove"}
|
||||||
|
for _, subcmd := range expectedSubcommands {
|
||||||
|
if !contains(helpText, subcmd) {
|
||||||
|
t.Errorf("Label help missing subcommand: %s", subcmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDraftCommand tests the draft CLI command structure
|
||||||
|
func TestDraftCommand(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
rootCmd := newRootCmdBase()
|
||||||
|
rootCmd.SetArgs([]string{"draft", "--help"})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
rootCmd.SetOut(&buf)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Draft help command failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, _ := io.ReadAll(&buf)
|
||||||
|
helpText := string(output)
|
||||||
|
|
||||||
|
// Verify draft subcommands are present
|
||||||
|
expectedSubcommands := []string{"list", "save", "edit", "send"}
|
||||||
|
for _, subcmd := range expectedSubcommands {
|
||||||
|
if !contains(helpText, subcmd) {
|
||||||
|
t.Errorf("Draft help missing subcommand: %s", subcmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -370,7 +370,7 @@ func newLabelClient() (*labels.Client, error) {
|
|||||||
return nil, fmt.Errorf("not authenticated: %w", err)
|
return nil, fmt.Errorf("not authenticated: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
apiClient := api.NewProtonMailClient(cfg)
|
apiClient := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
apiClient.SetAuthHeader(session.AccessToken)
|
apiClient.SetAuthHeader(session.AccessToken)
|
||||||
|
|
||||||
return labels.NewClient(apiClient), nil
|
return labels.NewClient(apiClient), nil
|
||||||
|
|||||||
40
cmd/mail.go
40
cmd/mail.go
@@ -31,6 +31,22 @@ func checkAuthenticated() (*auth.Session, error) {
|
|||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkAuthenticatedWithManager() (*auth.Session, *auth.SessionManager, error) {
|
||||||
|
sessionMgr, err := auth.NewSessionManager()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("failed to create session manager: %w", err)
|
||||||
|
}
|
||||||
|
authenticated, err := sessionMgr.IsAuthenticated()
|
||||||
|
if err != nil || !authenticated {
|
||||||
|
return nil, nil, fmt.Errorf("not authenticated (run 'pop login' first): %w", err)
|
||||||
|
}
|
||||||
|
session, err := sessionMgr.GetSession()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("not authenticated: %w", err)
|
||||||
|
}
|
||||||
|
return session, sessionMgr, nil
|
||||||
|
}
|
||||||
|
|
||||||
func mailCmd() *cobra.Command {
|
func mailCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "mail",
|
Use: "mail",
|
||||||
@@ -64,12 +80,12 @@ func mailListCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -158,12 +174,12 @@ func mailReadCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -221,12 +237,12 @@ func mailSendCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -276,12 +292,12 @@ func mailDeleteCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -312,12 +328,12 @@ func mailTrashCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
@@ -456,12 +472,12 @@ func mailSearchCmd() *cobra.Command {
|
|||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := checkAuthenticated()
|
session, sessionMgr, err := checkAuthenticatedWithManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := api.NewProtonMailClient(cfg)
|
client := api.NewProtonMailClient(cfg, sessionMgr)
|
||||||
client.SetAuthHeader(session.AccessToken)
|
client.SetAuthHeader(session.AccessToken)
|
||||||
mailClient := internalmail.NewClient(client)
|
mailClient := internalmail.NewClient(client)
|
||||||
|
|
||||||
|
|||||||
22
cmd/root.go
22
cmd/root.go
@@ -16,6 +16,27 @@ It provides commands for managing emails, contacts, and attachments
|
|||||||
with full PGP encryption support.`,
|
with full PGP encryption support.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newRootCmdBase() *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "pop",
|
||||||
|
Short: "ProtonMail CLI tool",
|
||||||
|
Long: `pop is a CLI tool for interacting with ProtonMail.
|
||||||
|
|
||||||
|
It provides commands for managing emails, contacts, and attachments
|
||||||
|
with full PGP encryption support.`,
|
||||||
|
}
|
||||||
|
cmd.AddCommand(loginCmd())
|
||||||
|
cmd.AddCommand(logoutCmd())
|
||||||
|
cmd.AddCommand(sessionCmd())
|
||||||
|
cmd.AddCommand(mailCmd())
|
||||||
|
cmd.AddCommand(mailDraftCmd())
|
||||||
|
cmd.AddCommand(contactCmd())
|
||||||
|
cmd.AddCommand(attachmentCmd())
|
||||||
|
cmd.AddCommand(folderCmd())
|
||||||
|
cmd.AddCommand(labelCmd())
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
func NewRootCmd() *cobra.Command {
|
func NewRootCmd() *cobra.Command {
|
||||||
rootCmd.AddCommand(loginCmd())
|
rootCmd.AddCommand(loginCmd())
|
||||||
rootCmd.AddCommand(logoutCmd())
|
rootCmd.AddCommand(logoutCmd())
|
||||||
@@ -26,7 +47,6 @@ func NewRootCmd() *cobra.Command {
|
|||||||
rootCmd.AddCommand(attachmentCmd())
|
rootCmd.AddCommand(attachmentCmd())
|
||||||
rootCmd.AddCommand(folderCmd())
|
rootCmd.AddCommand(folderCmd())
|
||||||
rootCmd.AddCommand(labelCmd())
|
rootCmd.AddCommand(labelCmd())
|
||||||
|
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
213
cmd/testutil_test.go
Normal file
213
cmd/testutil_test.go
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var stdoutMu sync.Mutex
|
||||||
|
|
||||||
|
// e2eTestEnv provides a self-contained test environment with a temp config dir
|
||||||
|
// and a mock API server. All CLI commands that use config.NewConfigManager() or
|
||||||
|
// auth.NewSessionManager() will resolve to the temp directory.
|
||||||
|
type e2eTestEnv struct {
|
||||||
|
t *testing.T
|
||||||
|
tempDir string
|
||||||
|
mockServer *mockAPIServer
|
||||||
|
origHome string
|
||||||
|
}
|
||||||
|
|
||||||
|
// mockAPIServer wraps httptest.Server with dynamic handler registration.
|
||||||
|
type mockAPIServer struct {
|
||||||
|
mux *http.ServeMux
|
||||||
|
server *httptest.Server
|
||||||
|
handlers sync.Map
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMockAPIServer(t *testing.T) *mockAPIServer {
|
||||||
|
t.Helper()
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
srv := httptest.NewServer(mux)
|
||||||
|
|
||||||
|
ms := &mockAPIServer{mux: mux, server: srv}
|
||||||
|
|
||||||
|
// Register catch-all patterns for all API endpoints used by the CLI
|
||||||
|
mux.HandleFunc("POST /auth", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /auth/verify", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages/search", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages/{id}", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages/{id}/movetotrash", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages/{id}/delete", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages/{id}/send", ms.resolve)
|
||||||
|
mux.HandleFunc("GET /api/folders", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/folders", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/folders/{id}", ms.resolve)
|
||||||
|
mux.HandleFunc("GET /api/folders/{id}", ms.resolve)
|
||||||
|
mux.HandleFunc("GET /api/labels", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/labels", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/labels/{id}", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages/{id}/setlabel", ms.resolve)
|
||||||
|
mux.HandleFunc("POST /api/messages/{id}/clearlabel", ms.resolve)
|
||||||
|
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ms *mockAPIServer) URL() string { return ms.server.URL }
|
||||||
|
func (ms *mockAPIServer) Close() { ms.server.Close() }
|
||||||
|
|
||||||
|
func (ms *mockAPIServer) Handle(key string, handler http.HandlerFunc) {
|
||||||
|
ms.handlers.Store(key, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ms *mockAPIServer) resolve(w http.ResponseWriter, r *http.Request) {
|
||||||
|
key := r.Method + " " + r.URL.Path
|
||||||
|
if h, loaded := ms.handlers.Load(key); loaded {
|
||||||
|
h.(http.HandlerFunc)(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
fmt.Fprintf(w, `{"Code":404,"Message":"not found"}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setupE2E creates a fresh test environment. Returns a cleanup func.
|
||||||
|
func setupE2E(t *testing.T) *e2eTestEnv {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
tempDir := t.TempDir()
|
||||||
|
|
||||||
|
// Create Pop config directory structure
|
||||||
|
popDir := filepath.Join(tempDir, ".config", "pop")
|
||||||
|
if err := os.MkdirAll(popDir, 0700); err != nil {
|
||||||
|
t.Fatalf("create config dir: %v", err)
|
||||||
|
}
|
||||||
|
keyringDir := filepath.Join(popDir, "keyring")
|
||||||
|
if err := os.MkdirAll(keyringDir, 0700); err != nil {
|
||||||
|
t.Fatalf("create keyring dir: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override HOME so config.NewConfigManager() resolves to our temp dir
|
||||||
|
origHome := os.Getenv("HOME")
|
||||||
|
os.Setenv("HOME", tempDir)
|
||||||
|
t.Cleanup(func() { os.Setenv("HOME", origHome) })
|
||||||
|
|
||||||
|
srv := newMockAPIServer(t)
|
||||||
|
t.Cleanup(srv.Close)
|
||||||
|
|
||||||
|
return &e2eTestEnv{
|
||||||
|
t: t,
|
||||||
|
tempDir: tempDir,
|
||||||
|
mockServer: srv,
|
||||||
|
origHome: origHome,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeEncryptedSession writes an encrypted session to both the keyring file
|
||||||
|
// and session.json, simulating a successful login.
|
||||||
|
func (env *e2eTestEnv) writeEncryptedSession(uid, accessToken, refreshToken string, expiresAt int64) {
|
||||||
|
sessionData, _ := json.Marshal(map[string]interface{}{
|
||||||
|
"uid": uid,
|
||||||
|
"access_token": accessToken,
|
||||||
|
"refresh_token": refreshToken,
|
||||||
|
"expires_at": expiresAt,
|
||||||
|
"two_factor_enabled": false,
|
||||||
|
"mail_passphrase": "test-passphrase",
|
||||||
|
})
|
||||||
|
|
||||||
|
// Encrypt with AES-256-GCM (same scheme as auth.encryptSession)
|
||||||
|
key := make([]byte, 32)
|
||||||
|
for i := range key {
|
||||||
|
key[i] = byte('k' + i%16)
|
||||||
|
}
|
||||||
|
nonce := make([]byte, 12)
|
||||||
|
for i := range nonce {
|
||||||
|
nonce[i] = byte('n' + i%8)
|
||||||
|
}
|
||||||
|
|
||||||
|
block, _ := aes.NewCipher(key)
|
||||||
|
aead, _ := cipher.NewGCM(block)
|
||||||
|
sealed := aead.Seal(nil, nonce, sessionData, nil)
|
||||||
|
|
||||||
|
encrypted := fmt.Sprintf("%s|%s|%s",
|
||||||
|
base64.StdEncoding.EncodeToString(key),
|
||||||
|
base64.StdEncoding.EncodeToString(nonce),
|
||||||
|
base64.StdEncoding.EncodeToString(sealed),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Write to session.json
|
||||||
|
sessionFile := filepath.Join(env.tempDir, ".config", "pop", "session.json")
|
||||||
|
os.WriteFile(sessionFile, []byte(encrypted), 0600)
|
||||||
|
|
||||||
|
// Write to keyring (file-based keyring stores in keyring/ directory)
|
||||||
|
keyringFile := filepath.Join(env.tempDir, ".config", "pop", "keyring", "session")
|
||||||
|
os.WriteFile(keyringFile, []byte(encrypted), 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
// jsonResp writes a JSON response with the given status code.
|
||||||
|
func jsonResp(t *testing.T, w http.ResponseWriter, code int, v interface{}) {
|
||||||
|
t.Helper()
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(code)
|
||||||
|
json.NewEncoder(w).Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runCommand executes a cobra command with the given args, capturing stdout/stderr.
|
||||||
|
func runCommand(root *cobra.Command, args []string) (string, string, error) {
|
||||||
|
bufOut, bufErr := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
root.SetOut(bufOut)
|
||||||
|
root.SetErr(bufErr)
|
||||||
|
root.SetArgs(args)
|
||||||
|
|
||||||
|
// Disable os.Exit on error by setting SilenceErrors
|
||||||
|
root.SilenceErrors = true
|
||||||
|
err := root.Execute()
|
||||||
|
|
||||||
|
return bufOut.String(), bufErr.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// runFreshCommand creates a fresh root command tree and executes the given args.
|
||||||
|
// Captures both cobra output and os.Stdout (since CLI commands use fmt.Printf).
|
||||||
|
func runFreshCommand(args []string) (string, string, error) {
|
||||||
|
stdoutMu.Lock()
|
||||||
|
defer stdoutMu.Unlock()
|
||||||
|
|
||||||
|
root := newRootCmdBase()
|
||||||
|
root.SetArgs(args)
|
||||||
|
root.SilenceErrors = true
|
||||||
|
|
||||||
|
// Capture os.Stdout since CLI commands use fmt.Printf directly
|
||||||
|
origStdout := os.Stdout
|
||||||
|
origStderr := os.Stderr
|
||||||
|
|
||||||
|
rOut, wOut, _ := os.Pipe()
|
||||||
|
os.Stdout = wOut
|
||||||
|
rErr, wErr, _ := os.Pipe()
|
||||||
|
os.Stderr = wErr
|
||||||
|
|
||||||
|
err := root.Execute()
|
||||||
|
|
||||||
|
wOut.Close()
|
||||||
|
wErr.Close()
|
||||||
|
os.Stdout = origStdout
|
||||||
|
os.Stderr = origStderr
|
||||||
|
|
||||||
|
outBytes, _ := io.ReadAll(rOut)
|
||||||
|
errBytes, _ := io.ReadAll(rErr)
|
||||||
|
rOut.Close()
|
||||||
|
rErr.Close()
|
||||||
|
|
||||||
|
return string(outBytes), string(errBytes), err
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/frenocorp/pop/internal/config"
|
"github.com/frenocorp/pop/internal/config"
|
||||||
|
"github.com/frenocorp/pop/internal/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProtonMailClient struct {
|
type ProtonMailClient struct {
|
||||||
@@ -19,6 +21,7 @@ type ProtonMailClient struct {
|
|||||||
rateLimiter *RateLimiter
|
rateLimiter *RateLimiter
|
||||||
authHeader string
|
authHeader string
|
||||||
authMu sync.RWMutex
|
authMu sync.RWMutex
|
||||||
|
sessionRefresher auth.SessionRefresher
|
||||||
}
|
}
|
||||||
|
|
||||||
type RateLimiter struct {
|
type RateLimiter struct {
|
||||||
@@ -28,11 +31,12 @@ type RateLimiter struct {
|
|||||||
window time.Duration
|
window time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProtonMailClient(cfg *config.Config) *ProtonMailClient {
|
func NewProtonMailClient(cfg *config.Config, refresher auth.SessionRefresher) *ProtonMailClient {
|
||||||
return &ProtonMailClient{
|
return &ProtonMailClient{
|
||||||
baseURL: cfg.APIBaseURL,
|
baseURL: cfg.APIBaseURL,
|
||||||
httpClient: &http.Client{Timeout: time.Duration(cfg.TimeoutSec) * time.Second},
|
httpClient: &http.Client{Timeout: time.Duration(cfg.TimeoutSec) * time.Second},
|
||||||
config: cfg,
|
config: cfg,
|
||||||
|
sessionRefresher: refresher,
|
||||||
rateLimiter: &RateLimiter{
|
rateLimiter: &RateLimiter{
|
||||||
requests: make([]time.Time, 0, cfg.RateLimitReq),
|
requests: make([]time.Time, 0, cfg.RateLimitReq),
|
||||||
limit: cfg.RateLimitReq,
|
limit: cfg.RateLimitReq,
|
||||||
@@ -53,6 +57,13 @@ func (c *ProtonMailClient) getAuthHeader() string {
|
|||||||
return c.authHeader
|
return c.authHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ProtonMailClient) refreshAuth() error {
|
||||||
|
if c.sessionRefresher == nil {
|
||||||
|
return fmt.Errorf("no session refresher configured")
|
||||||
|
}
|
||||||
|
return c.sessionRefresher.RefreshToken()
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ProtonMailClient) GetBaseURL() string {
|
func (c *ProtonMailClient) GetBaseURL() string {
|
||||||
return c.baseURL
|
return c.baseURL
|
||||||
}
|
}
|
||||||
@@ -83,11 +94,20 @@ func (rl *RateLimiter) Wait() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ProtonMailClient) Do(req *http.Request) (*http.Response, error) {
|
func (c *ProtonMailClient) Do(req *http.Request) (*http.Response, error) {
|
||||||
|
return c.DoWithContext(context.Background(), req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ProtonMailClient) DoWithContext(ctx context.Context, req *http.Request) (*http.Response, error) {
|
||||||
c.rateLimiter.Wait()
|
c.rateLimiter.Wait()
|
||||||
|
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.getAuthHeader()))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.getAuthHeader()))
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
// Check if request has its own context
|
||||||
|
if ctx != context.Background() {
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -98,7 +118,33 @@ func (c *ProtonMailClient) Do(req *http.Request) (*http.Response, error) {
|
|||||||
c.rateLimiter.requests = append(c.rateLimiter.requests, time.Now())
|
c.rateLimiter.requests = append(c.rateLimiter.requests, time.Now())
|
||||||
c.rateLimiter.mu.Unlock()
|
c.rateLimiter.mu.Unlock()
|
||||||
|
|
||||||
// Check for API errors
|
// Check for 401 and attempt refresh
|
||||||
|
if resp.StatusCode == http.StatusUnauthorized {
|
||||||
|
// Close the current response body
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
// Attempt to refresh the token
|
||||||
|
if err := c.refreshAuth(); err != nil {
|
||||||
|
return resp, fmt.Errorf("401 received and refresh failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retry the request with new token
|
||||||
|
// Clone the request to reset any body position
|
||||||
|
retryReq := req.Clone(ctx)
|
||||||
|
retryReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.getAuthHeader()))
|
||||||
|
|
||||||
|
resp, err = c.httpClient.Do(retryReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record the retry request
|
||||||
|
c.rateLimiter.mu.Lock()
|
||||||
|
c.rateLimiter.requests = append(c.rateLimiter.requests, time.Now())
|
||||||
|
c.rateLimiter.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for other API errors
|
||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
var apiErr APIError
|
var apiErr APIError
|
||||||
@@ -120,3 +166,14 @@ type APIError struct {
|
|||||||
func (e *APIError) Error() string {
|
func (e *APIError) Error() string {
|
||||||
return fmt.Sprintf("API error %d: %s", e.HTTPStatus, e.Message)
|
return fmt.Sprintf("API error %d: %s", e.HTTPStatus, e.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to create a request with context
|
||||||
|
func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|||||||
11
internal/auth/interface.go
Normal file
11
internal/auth/interface.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
// SessionRefresher defines the interface for refreshing authentication tokens.
|
||||||
|
// This allows the API client to automatically refresh tokens on 401 responses.
|
||||||
|
type SessionRefresher interface {
|
||||||
|
RefreshToken() error
|
||||||
|
RefreshTokenWithContext(ctx context.Context) error
|
||||||
|
GetSession() (*Session, error)
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
@@ -54,7 +55,7 @@ func NewSessionManager() (*SessionManager, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SessionManager) LoginWithCredentials(apiBaseURL, email, password, mailPassphrase string) error {
|
func (m *SessionManager) LoginWithCredentials(ctx context.Context, apiBaseURL, email, password, mailPassphrase string) error {
|
||||||
if err := os.MkdirAll(m.configDir, 0700); err != nil {
|
if err := os.MkdirAll(m.configDir, 0700); err != nil {
|
||||||
return fmt.Errorf("failed to create config dir: %w", err)
|
return fmt.Errorf("failed to create config dir: %w", err)
|
||||||
}
|
}
|
||||||
@@ -75,7 +76,7 @@ func (m *SessionManager) LoginWithCredentials(apiBaseURL, email, password, mailP
|
|||||||
return fmt.Errorf("failed to marshal auth payload: %w", err)
|
return fmt.Errorf("failed to marshal auth payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", authURL, bytes.NewBuffer(jsonData))
|
req, err := http.NewRequestWithContext(ctx, "POST", authURL, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create auth request: %w", err)
|
return fmt.Errorf("failed to create auth request: %w", err)
|
||||||
}
|
}
|
||||||
@@ -95,11 +96,11 @@ func (m *SessionManager) LoginWithCredentials(apiBaseURL, email, password, mailP
|
|||||||
}
|
}
|
||||||
|
|
||||||
var authResponse struct {
|
var authResponse struct {
|
||||||
UID string `json:"UID"`
|
UID string `json:"UID"`
|
||||||
AccessToken string `json:"AccessToken"`
|
AccessToken string `json:"AccessToken"`
|
||||||
RefreshToken string `json:"RefreshToken"`
|
RefreshToken string `json:"RefreshToken"`
|
||||||
ExpiresIn int `json:"ExpiresIn"`
|
ExpiresIn int `json:"ExpiresIn"`
|
||||||
TwoFARequired bool `json:"TwoFARequired"`
|
TwoFARequired bool `json:"TwoFARequired"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&authResponse); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&authResponse); err != nil {
|
||||||
@@ -135,7 +136,7 @@ func (m *SessionManager) LoginWithCredentials(apiBaseURL, email, password, mailP
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SessionManager) LoginInteractive(apiBaseURL string) error {
|
func (m *SessionManager) LoginInteractive(ctx context.Context, apiBaseURL string) error {
|
||||||
if err := os.MkdirAll(m.configDir, 0700); err != nil {
|
if err := os.MkdirAll(m.configDir, 0700); err != nil {
|
||||||
return fmt.Errorf("failed to create config dir: %w", err)
|
return fmt.Errorf("failed to create config dir: %w", err)
|
||||||
}
|
}
|
||||||
@@ -192,7 +193,7 @@ func (m *SessionManager) LoginInteractive(apiBaseURL string) error {
|
|||||||
return fmt.Errorf("failed to marshal auth payload: %w", err)
|
return fmt.Errorf("failed to marshal auth payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", authURL, bytes.NewBuffer(jsonData))
|
req, err := http.NewRequestWithContext(ctx, "POST", authURL, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create auth request: %w", err)
|
return fmt.Errorf("failed to create auth request: %w", err)
|
||||||
}
|
}
|
||||||
@@ -263,7 +264,7 @@ func (m *SessionManager) LoginInteractive(apiBaseURL string) error {
|
|||||||
return fmt.Errorf("failed to marshal TOTP payload: %w", err)
|
return fmt.Errorf("failed to marshal TOTP payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
totpReq, err := http.NewRequest("POST", totpURL, bytes.NewBuffer(totpJSON))
|
totpReq, err := http.NewRequestWithContext(ctx, "POST", totpURL, bytes.NewBuffer(totpJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create TOTP request: %w", err)
|
return fmt.Errorf("failed to create TOTP request: %w", err)
|
||||||
}
|
}
|
||||||
@@ -372,6 +373,10 @@ func (m *SessionManager) IsAuthenticated() (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *SessionManager) RefreshToken() error {
|
func (m *SessionManager) RefreshToken() error {
|
||||||
|
return m.RefreshTokenWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *SessionManager) RefreshTokenWithContext(ctx context.Context) error {
|
||||||
session, err := m.GetSession()
|
session, err := m.GetSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get session: %w", err)
|
return fmt.Errorf("failed to get session: %w", err)
|
||||||
@@ -394,7 +399,7 @@ func (m *SessionManager) RefreshToken() error {
|
|||||||
return fmt.Errorf("failed to marshal refresh payload: %w", err)
|
return fmt.Errorf("failed to marshal refresh payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", refreshURL, bytes.NewBuffer(jsonData))
|
req, err := http.NewRequestWithContext(ctx, "POST", refreshURL, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create refresh request: %w", err)
|
return fmt.Errorf("failed to create refresh request: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func newTestClient(t *testing.T, srv *mockServer) *Client {
|
|||||||
RateLimitReq: 100,
|
RateLimitReq: 100,
|
||||||
RateLimitWin: 60,
|
RateLimitWin: 60,
|
||||||
}
|
}
|
||||||
apiClient := api.NewProtonMailClient(cfg)
|
apiClient := api.NewProtonMailClient(cfg, nil)
|
||||||
apiClient.SetAuthHeader("test-token")
|
apiClient.SetAuthHeader("test-token")
|
||||||
return NewClient(apiClient)
|
return NewClient(apiClient)
|
||||||
}
|
}
|
||||||
@@ -294,8 +294,8 @@ func TestListMessages_APIError(t *testing.T) {
|
|||||||
client := newTestClient(t, srv)
|
client := newTestClient(t, srv)
|
||||||
|
|
||||||
srv.Handle("POST /api/messages", func(w http.ResponseWriter, r *http.Request) {
|
srv.Handle("POST /api/messages", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
fmt.Fprintf(w, `{"Code":401,"Message":"invalid token"}`)
|
fmt.Fprintf(w, `{"Code":403,"Message":"invalid token"}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
_, err := client.ListMessages(ListMessagesRequest{
|
_, err := client.ListMessages(ListMessagesRequest{
|
||||||
@@ -304,7 +304,7 @@ func TestListMessages_APIError(t *testing.T) {
|
|||||||
Passphrase: "pass",
|
Passphrase: "pass",
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error for 401 response")
|
t.Fatal("expected error for 403 response")
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), "invalid token") {
|
if !strings.Contains(err.Error(), "invalid token") {
|
||||||
t.Errorf("expected 'invalid token' in error, got: %s", err.Error())
|
t.Errorf("expected 'invalid token' in error, got: %s", err.Error())
|
||||||
@@ -404,6 +404,9 @@ func TestGetMessage_NotFound(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error for 404")
|
t.Fatal("expected error for 404")
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(err.Error(), "message not found") {
|
||||||
|
t.Errorf("expected 'message not found' in error, got: %s", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMessage_DecryptBody(t *testing.T) {
|
func TestGetMessage_DecryptBody(t *testing.T) {
|
||||||
@@ -417,7 +420,7 @@ func TestGetMessage_DecryptBody(t *testing.T) {
|
|||||||
RateLimitReq: 100,
|
RateLimitReq: 100,
|
||||||
RateLimitWin: 60,
|
RateLimitWin: 60,
|
||||||
}
|
}
|
||||||
apiClient := api.NewProtonMailClient(cfg)
|
apiClient := api.NewProtonMailClient(cfg, nil)
|
||||||
apiClient.SetAuthHeader("test-token")
|
apiClient.SetAuthHeader("test-token")
|
||||||
client := NewClient(apiClient)
|
client := NewClient(apiClient)
|
||||||
client.SetPGPService(svc)
|
client.SetPGPService(svc)
|
||||||
@@ -488,7 +491,7 @@ func TestSend_WithPGP(t *testing.T) {
|
|||||||
RateLimitReq: 100,
|
RateLimitReq: 100,
|
||||||
RateLimitWin: 60,
|
RateLimitWin: 60,
|
||||||
}
|
}
|
||||||
apiClient := api.NewProtonMailClient(cfg)
|
apiClient := api.NewProtonMailClient(cfg, nil)
|
||||||
apiClient.SetAuthHeader("test-token")
|
apiClient.SetAuthHeader("test-token")
|
||||||
client := NewClient(apiClient)
|
client := NewClient(apiClient)
|
||||||
client.SetPGPService(svc)
|
client.SetPGPService(svc)
|
||||||
@@ -1089,7 +1092,7 @@ func TestAuthHeader_Propagated(t *testing.T) {
|
|||||||
RateLimitReq: 100,
|
RateLimitReq: 100,
|
||||||
RateLimitWin: 60,
|
RateLimitWin: 60,
|
||||||
}
|
}
|
||||||
apiClient := api.NewProtonMailClient(cfg)
|
apiClient := api.NewProtonMailClient(cfg, nil)
|
||||||
apiClient.SetAuthHeader("my-test-token")
|
apiClient.SetAuthHeader("my-test-token")
|
||||||
client := NewClient(apiClient)
|
client := NewClient(apiClient)
|
||||||
|
|
||||||
@@ -1324,7 +1327,7 @@ func TestListMessages_Timeout(t *testing.T) {
|
|||||||
RateLimitReq: 100,
|
RateLimitReq: 100,
|
||||||
RateLimitWin: 60,
|
RateLimitWin: 60,
|
||||||
}
|
}
|
||||||
apiClient := api.NewProtonMailClient(cfg)
|
apiClient := api.NewProtonMailClient(cfg, nil)
|
||||||
apiClient.SetAuthHeader("test-token")
|
apiClient.SetAuthHeader("test-token")
|
||||||
client := NewClient(apiClient)
|
client := NewClient(apiClient)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user