-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgateway_test.go
More file actions
160 lines (140 loc) · 4.73 KB
/
gateway_test.go
File metadata and controls
160 lines (140 loc) · 4.73 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cmd
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/hookdeck/hookdeck-cli/pkg/config"
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRequireGatewayProject(t *testing.T) {
t.Run("no API key", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.ProjectId = "proj_1"
cfg.Profile.ProjectType = config.ProjectTypeGateway
err := requireGatewayProject(cfg)
require.Error(t, err)
assert.Contains(t, err.Error(), "authenticated")
})
t.Run("no project selected", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.APIKey = "sk_xxx"
cfg.Profile.ProjectId = ""
err := requireGatewayProject(cfg)
require.Error(t, err)
assert.Contains(t, err.Error(), "no project selected")
})
t.Run("Gateway type passes", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.APIKey = "sk_xxx"
cfg.Profile.ProjectId = "proj_1"
cfg.Profile.ProjectType = config.ProjectTypeGateway
err := requireGatewayProject(cfg)
assert.NoError(t, err)
})
t.Run("Console type passes", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.APIKey = "sk_xxx"
cfg.Profile.ProjectId = "proj_1"
cfg.Profile.ProjectType = config.ProjectTypeConsole
err := requireGatewayProject(cfg)
assert.NoError(t, err)
})
t.Run("inbound mode passes when type empty", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.APIKey = "sk_xxx"
cfg.Profile.ProjectId = "proj_1"
cfg.Profile.ProjectMode = "inbound"
err := requireGatewayProject(cfg)
assert.NoError(t, err)
})
t.Run("outbound mode passes when type empty (same as inbound)", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.APIKey = "sk_xxx"
cfg.Profile.ProjectId = "proj_1"
cfg.Profile.ProjectMode = "outbound"
err := requireGatewayProject(cfg)
assert.NoError(t, err)
})
t.Run("Outpost type fails", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.APIKey = "sk_xxx"
cfg.Profile.ProjectId = "proj_1"
cfg.Profile.ProjectType = config.ProjectTypeOutpost
err := requireGatewayProject(cfg)
require.Error(t, err)
assert.Contains(t, err.Error(), "requires a Gateway project")
assert.Contains(t, err.Error(), "Outpost")
assert.Contains(t, err.Error(), "hookdeck project use")
})
t.Run("unknown type fails", func(t *testing.T) {
cfg := &config.Config{}
cfg.Profile.APIKey = "sk_xxx"
cfg.Profile.ProjectId = "proj_1"
cfg.Profile.ProjectMode = "outpost"
err := requireGatewayProject(cfg)
require.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "requires a Gateway project") || strings.Contains(err.Error(), "Outpost"))
})
}
func TestRequireGatewayProject_resolveFromValidate(t *testing.T) {
config.ResetAPIClientForTesting()
t.Cleanup(config.ResetAPIClientForTesting)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != hookdeck.APIPathPrefix+"/cli-auth/validate" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(hookdeck.ValidateAPIKeyResponse{
ProjectID: "team_from_validate",
ProjectMode: "inbound",
})
}))
t.Cleanup(server.Close)
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
toml := `profile = "default"
[default]
api_key = "sk_test_123456789012"
project_id = "stale_team_should_be_replaced"
guest_url = "https://guest.example/keep-me"
`
require.NoError(t, os.WriteFile(path, []byte(toml), 0600))
cfg, err := config.LoadConfigFromFile(path)
require.NoError(t, err)
cfg.APIBaseURL = server.URL
err = requireGatewayProject(cfg)
require.NoError(t, err)
require.Equal(t, "team_from_validate", cfg.Profile.ProjectId)
require.Equal(t, config.ProjectTypeGateway, cfg.Profile.ProjectType)
require.Equal(t, "inbound", cfg.Profile.ProjectMode)
require.Equal(t, "https://guest.example/keep-me", cfg.Profile.GuestURL, "gateway validate path must not clear guest_url")
}
func TestGatewayPersistentPreRunE_MCP(t *testing.T) {
old := Config
t.Cleanup(func() { Config = old })
gw := newGatewayCmd().cmd
mcpLeaf, _, err := gw.Find([]string{"mcp"})
require.NoError(t, err)
require.True(t, isGatewayMCPLeafCommand(mcpLeaf))
t.Run("no API key skips requireGatewayProject", func(t *testing.T) {
Config = config.Config{}
Config.Profile.ProjectId = ""
err := gatewayPersistentPreRunE(mcpLeaf, nil)
assert.NoError(t, err)
})
t.Run("with API key and no project fails", func(t *testing.T) {
Config = config.Config{}
Config.Profile.APIKey = "sk_test_123456789012"
Config.Profile.ProjectId = ""
err := gatewayPersistentPreRunE(mcpLeaf, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "no project selected")
})
}