-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_test.go
More file actions
281 lines (238 loc) · 8.66 KB
/
create_test.go
File metadata and controls
281 lines (238 loc) · 8.66 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package cmd
import (
"encoding/json"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func buildCreateResponse(r createResp, resp interface{}) {
if r.AppUrl == "" {
r.AppUrl = "https://test-app.example.com"
}
if r.ID == "" {
r.ID = "test-app-id"
}
*resp.(*createResp) = r
}
func TestRunCreate_NoAppOrName(t *testing.T) {
client := &mockRESTClient{}
_, err := runCreate(client, createCmdFlags{})
require.ErrorContains(t, err, "either --app or --name flag is required")
}
func TestRunCreate_InvalidEnvVarFormat(t *testing.T) {
client := &mockRESTClient{}
_, err := runCreate(client, createCmdFlags{app: "my-app", environmentVariables: []string{"BADFORMAT"}})
require.ErrorContains(t, err, "invalid environment variable format")
}
func TestRunCreate_InvalidSecretFormat(t *testing.T) {
client := &mockRESTClient{}
_, err := runCreate(client, createCmdFlags{app: "my-app", secrets: []string{"NOSEPARATOR"}})
require.ErrorContains(t, err, "invalid secret format")
}
func TestRunCreate_APIError(t *testing.T) {
client := &mockRESTClient{
putFunc: mockPutError("server error"),
}
_, err := runCreate(client, createCmdFlags{app: "my-app", environmentVariables: []string{"K=V"}})
require.ErrorContains(t, err, "error creating app")
}
func TestRunCreate_Success(t *testing.T) {
var capturedPath string
var capturedBody []byte
client := &mockRESTClient{
putFunc: func(path string, body io.Reader, resp interface{}) error {
capturedPath = path
capturedBody, _ = io.ReadAll(body)
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com"}, resp)
return nil
},
}
resp, err := runCreate(client, createCmdFlags{
app: "my-app",
environmentVariables: []string{"KEY1=val1", "KEY2=val2"},
secrets: []string{"SECRET=sval"},
})
require.NoError(t, err)
assert.Equal(t, "https://my-app.example.com", resp.AppUrl)
assert.Equal(t, "runtime/my-app/deployment", capturedPath)
var req createReq
json.Unmarshal(capturedBody, &req)
assert.Equal(t, "val1", req.EnvironmentVariables["KEY1"])
assert.Equal(t, "sval", req.Secrets["SECRET"])
}
func TestRunCreate_WithRevisionName(t *testing.T) {
var capturedPath string
client := &mockRESTClient{
putFunc: func(path string, _ io.Reader, resp interface{}) error {
capturedPath = path
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com"}, resp)
return nil
},
}
_, err := runCreate(client, createCmdFlags{app: "my-app", revisionName: "v2"})
require.NoError(t, err)
assert.Contains(t, capturedPath, "revision_name=v2")
}
func TestRunCreate_WithInit(t *testing.T) {
tmp := t.TempDir()
origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmp))
defer os.Chdir(origDir)
client := &mockRESTClient{
putFunc: func(_ string, _ io.Reader, resp interface{}) error {
buildCreateResponse(createResp{AppUrl: "https://init-app.example.com", ID: "init-app-id"}, resp)
return nil
},
}
resp, err := runCreate(client, createCmdFlags{app: "init-app", init: true})
require.NoError(t, err)
assert.Equal(t, "https://init-app.example.com", resp.AppUrl)
data, err := os.ReadFile("runtime.config.json")
require.NoError(t, err, "expected runtime.config.json to be created")
assert.Contains(t, string(data), "init-app-id")
}
func TestRunCreate_EnvVarWithEqualsInValue(t *testing.T) {
var capturedBody []byte
client := &mockRESTClient{
putFunc: func(_ string, body io.Reader, resp interface{}) error {
capturedBody, _ = io.ReadAll(body)
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com"}, resp)
return nil
},
}
_, err := runCreate(client, createCmdFlags{app: "my-app", environmentVariables: []string{"KEY=val=with=equals"}})
require.NoError(t, err)
var req createReq
json.Unmarshal(capturedBody, &req)
assert.Equal(t, "val=with=equals", req.EnvironmentVariables["KEY"])
}
func TestRunCreate_WithName(t *testing.T) {
var capturedPath string
var capturedBody []byte
client := &mockRESTClient{
putFunc: func(path string, body io.Reader, resp interface{}) error {
capturedPath = path
capturedBody, _ = io.ReadAll(body)
buildCreateResponse(createResp{AppUrl: "https://my-new-app.example.com", ID: "abc-123"}, resp)
return nil
},
}
resp, err := runCreate(client, createCmdFlags{name: "my-new-app"})
require.NoError(t, err)
assert.Equal(t, "https://my-new-app.example.com", resp.AppUrl)
assert.Equal(t, "abc-123", resp.ID)
assert.Equal(t, "runtime", capturedPath)
var req createReq
json.Unmarshal(capturedBody, &req)
assert.Equal(t, "my-new-app", req.Name)
}
func TestRunCreate_WithNameAndApp(t *testing.T) {
var capturedPath string
var capturedBody []byte
client := &mockRESTClient{
putFunc: func(path string, body io.Reader, resp interface{}) error {
capturedPath = path
capturedBody, _ = io.ReadAll(body)
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com", ID: "app-123"}, resp)
return nil
},
}
resp, err := runCreate(client, createCmdFlags{app: "my-app", name: "my-new-name"})
require.NoError(t, err)
assert.Equal(t, "https://my-app.example.com", resp.AppUrl)
assert.Equal(t, "runtime/my-app/deployment", capturedPath)
var req createReq
json.Unmarshal(capturedBody, &req)
assert.Equal(t, "my-new-name", req.Name)
}
func TestRunCreate_WithNameAndInit(t *testing.T) {
tmp := t.TempDir()
origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmp))
defer os.Chdir(origDir)
var capturedBody []byte
client := &mockRESTClient{
putFunc: func(_ string, body io.Reader, resp interface{}) error {
capturedBody, _ = io.ReadAll(body)
buildCreateResponse(createResp{AppUrl: "https://named-app.example.com", ID: "def-456"}, resp)
return nil
},
}
_, err = runCreate(client, createCmdFlags{name: "named-app", init: true})
require.NoError(t, err)
var req createReq
json.Unmarshal(capturedBody, &req)
assert.Equal(t, "named-app", req.Name)
data, err := os.ReadFile("runtime.config.json")
require.NoError(t, err)
assert.Contains(t, string(data), "def-456")
}
func TestRunCreate_WithVisibility(t *testing.T) {
var capturedBody []byte
client := &mockRESTClient{
putFunc: func(_ string, body io.Reader, resp interface{}) error {
capturedBody, _ = io.ReadAll(body)
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com"}, resp)
return nil
},
}
_, err := runCreate(client, createCmdFlags{app: "my-app", visibility: "github"})
require.NoError(t, err)
var req createReq
json.Unmarshal(capturedBody, &req)
assert.Equal(t, "github", req.Visibility)
}
func TestRunCreate_OrgWithoutSelectedOrgsVisibility(t *testing.T) {
client := &mockRESTClient{}
_, err := runCreate(client, createCmdFlags{app: "my-app", org: "my-org"})
require.ErrorContains(t, err, "--org can only be used with --visibility=selected_orgs")
_, err = runCreate(client, createCmdFlags{app: "my-app", visibility: "github", org: "my-org"})
require.ErrorContains(t, err, "--org can only be used with --visibility=selected_orgs")
}
func TestRunCreate_SelectedOrgsVisibilityWithoutOrg(t *testing.T) {
client := &mockRESTClient{}
_, err := runCreate(client, createCmdFlags{app: "my-app", visibility: "selected_orgs"})
require.ErrorContains(t, err, "--org is required when --visibility=selected_orgs")
}
func TestRunCreate_WithOrgAndSelectedOrgsVisibility(t *testing.T) {
var capturedBody []byte
client := &mockRESTClient{
putFunc: func(_ string, body io.Reader, resp interface{}) error {
capturedBody, _ = io.ReadAll(body)
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com"}, resp)
return nil
},
}
_, err := runCreate(client, createCmdFlags{app: "my-app", visibility: "selected_orgs", org: "my-org"})
require.NoError(t, err)
var req createReq
json.Unmarshal(capturedBody, &req)
assert.Equal(t, "selected_orgs", req.Visibility)
assert.Equal(t, "my-org", req.OrganizationLogin)
}
func TestRunCreate_ResponseWithID(t *testing.T) {
client := &mockRESTClient{
putFunc: func(_ string, _ io.Reader, resp interface{}) error {
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com", ID: "xyz-789"}, resp)
return nil
},
}
resp, err := runCreate(client, createCmdFlags{app: "my-app"})
require.NoError(t, err)
assert.Equal(t, "https://my-app.example.com", resp.AppUrl)
assert.Equal(t, "xyz-789", resp.ID)
}
func TestRunCreate_InitWithoutIDInResponse(t *testing.T) {
client := &mockRESTClient{
putFunc: func(_ string, _ io.Reader, resp interface{}) error {
b, _ := json.Marshal(createResp{AppUrl: "https://my-app.example.com"})
return json.Unmarshal(b, resp)
},
}
_, err := runCreate(client, createCmdFlags{app: "my-app", init: true})
require.ErrorContains(t, err, "server did not return an app ID")
}