-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcontext_test.go
More file actions
96 lines (81 loc) · 2.95 KB
/
Copy pathcontext_test.go
File metadata and controls
96 lines (81 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
clierrors "github.com/heygen-com/heygen-cli/internal/errors"
)
func TestEnrichAuthHint_EnvSource_401(t *testing.T) {
srv := setupTestServer(t, map[string]testHandler{
"GET /v3/videos": {
StatusCode: 401,
Body: `{"error":{"code":"unauthorized","message":"invalid API key"}}`,
},
})
defer srv.Close()
res := runCommand(t, srv.URL, "bad-key", "video", "list")
if res.ExitCode != clierrors.ExitAuth {
t.Fatalf("ExitCode = %d, want %d\nstderr: %s", res.ExitCode, clierrors.ExitAuth, res.Stderr)
}
if !strings.Contains(res.Stderr, "HEYGEN_API_KEY environment variable") {
t.Fatalf("stderr should mention env var source:\n%s", res.Stderr)
}
if !strings.Contains(res.Stderr, "app.heygen.com/settings?nav=API") {
t.Fatalf("stderr should contain key generation URL:\n%s", res.Stderr)
}
}
func TestEnrichAuthHint_FileSource_401(t *testing.T) {
srv := setupTestServer(t, map[string]testHandler{
"GET /v3/videos": {
StatusCode: 401,
Body: `{"error":{"code":"unauthorized","message":"invalid API key"}}`,
},
})
defer srv.Close()
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "credentials"), []byte("bad-key"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv("HEYGEN_CONFIG_DIR", dir)
t.Setenv("HEYGEN_API_KEY", "")
res := runCommand(t, srv.URL, "", "video", "list")
if res.ExitCode != clierrors.ExitAuth {
t.Fatalf("ExitCode = %d, want %d\nstderr: %s", res.ExitCode, clierrors.ExitAuth, res.Stderr)
}
if !strings.Contains(res.Stderr, "heygen auth login") {
t.Fatalf("stderr should mention auth login for file source:\n%s", res.Stderr)
}
pathJSON, _ := json.Marshal(filepath.Join(dir, "credentials"))
escapedPath := string(pathJSON[1 : len(pathJSON)-1])
if !strings.Contains(res.Stderr, escapedPath) {
t.Fatalf("stderr should mention the resolved credentials path:\n%s", res.Stderr)
}
}
func TestEnrichAuthHint_NonAuthError_NoMutation(t *testing.T) {
srv := setupTestServer(t, map[string]testHandler{
"GET /v3/videos": {
StatusCode: 500,
Body: `{"error":{"code":"internal_error","message":"server error"}}`,
},
})
defer srv.Close()
res := runCommand(t, srv.URL, "test-key", "video", "list")
if res.ExitCode != clierrors.ExitGeneral {
t.Fatalf("ExitCode = %d, want %d", res.ExitCode, clierrors.ExitGeneral)
}
if strings.Contains(res.Stderr, "HEYGEN_API_KEY environment variable") {
t.Fatalf("non-auth error should not get auth hint:\n%s", res.Stderr)
}
}
func TestEnrichAuthHint_ExistingHint_Preserved(t *testing.T) {
t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir())
res := runCommand(t, "http://example.invalid", "", "video", "list")
if res.ExitCode != clierrors.ExitAuth {
t.Fatalf("ExitCode = %d, want %d\nstderr: %s", res.ExitCode, clierrors.ExitAuth, res.Stderr)
}
if !strings.Contains(res.Stderr, "Three ways to provide your API key") {
t.Fatalf("no-key error should preserve authGuidance hint:\n%s", res.Stderr)
}
}