Repository navigation
Expand file tree
/
Copy pathconnection_token_test.go
More file actions
122 lines (106 loc) · 3.38 KB
/
Copy pathconnection_token_test.go
File metadata and controls
122 lines (106 loc) · 3.38 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package e2e
import (
"fmt"
"strings"
"testing"
copilot "github.com/github/copilot-sdk/go"
"github.com/github/copilot-sdk/go/internal/e2e/testharness"
)
func TestConnectionToken(t *testing.T) {
t.Run("explicit token round-trips successfully", func(t *testing.T) {
ctx := testharness.NewTestContext(t)
client := ctx.NewClient(func(opts *copilot.ClientOptions) {
opts.Connection = copilot.TCPConnection{
Path: ctx.CLIPath,
ConnectionToken: "right-token",
}
})
t.Cleanup(func() { client.ForceStop() })
if err := client.Start(t.Context()); err != nil {
t.Fatalf("Start failed: %v", err)
}
resp, err := client.Ping(t.Context(), "hi")
if err != nil {
t.Fatalf("Ping failed: %v", err)
}
if resp.Message != "pong: hi" {
t.Errorf("expected message 'pong: hi', got %q", resp.Message)
}
})
t.Run("auto-generated token round-trips successfully", func(t *testing.T) {
ctx := testharness.NewTestContext(t)
client := ctx.NewClient(func(opts *copilot.ClientOptions) {
opts.Connection = copilot.TCPConnection{Path: ctx.CLIPath}
})
t.Cleanup(func() { client.ForceStop() })
if err := client.Start(t.Context()); err != nil {
t.Fatalf("Start failed: %v", err)
}
resp, err := client.Ping(t.Context(), "hi")
if err != nil {
t.Fatalf("Ping failed: %v", err)
}
if resp.Message != "pong: hi" {
t.Errorf("expected message 'pong: hi', got %q", resp.Message)
}
})
t.Run("sibling client with wrong token is rejected", func(t *testing.T) {
ctx := testharness.NewTestContext(t)
good := ctx.NewClient(func(opts *copilot.ClientOptions) {
opts.Connection = copilot.TCPConnection{
Path: ctx.CLIPath,
ConnectionToken: "right-token",
}
})
t.Cleanup(func() { good.ForceStop() })
if err := good.Start(t.Context()); err != nil {
t.Fatalf("good client Start failed: %v", err)
}
port := good.RuntimePort()
if port == 0 {
t.Fatalf("expected non-zero port from TCP mode client")
}
bad := copilot.NewClient(&copilot.ClientOptions{
Connection: copilot.URIConnection{
URL: fmt.Sprintf("localhost:%d", port),
ConnectionToken: "wrong",
},
})
t.Cleanup(func() { bad.ForceStop() })
err := bad.Start(t.Context())
if err == nil {
t.Fatalf("expected sibling client with wrong token to fail")
}
if !strings.Contains(err.Error(), "AUTHENTICATION_FAILED") {
t.Errorf("expected AUTHENTICATION_FAILED error, got: %v", err)
}
})
t.Run("sibling client with no token is rejected", func(t *testing.T) {
ctx := testharness.NewTestContext(t)
good := ctx.NewClient(func(opts *copilot.ClientOptions) {
opts.Connection = copilot.TCPConnection{
Path: ctx.CLIPath,
ConnectionToken: "right-token",
}
})
t.Cleanup(func() { good.ForceStop() })
if err := good.Start(t.Context()); err != nil {
t.Fatalf("good client Start failed: %v", err)
}
port := good.RuntimePort()
if port == 0 {
t.Fatalf("expected non-zero port from TCP mode client")
}
none := copilot.NewClient(&copilot.ClientOptions{
Connection: copilot.URIConnection{URL: fmt.Sprintf("localhost:%d", port)},
})
t.Cleanup(func() { none.ForceStop() })
err := none.Start(t.Context())
if err == nil {
t.Fatalf("expected sibling client with no token to fail")
}
if !strings.Contains(err.Error(), "AUTHENTICATION_FAILED") {
t.Errorf("expected AUTHENTICATION_FAILED error, got: %v", err)
}
})
}