@@ -134,13 +134,21 @@ func TestEnvVarOverridesAccounts(t *testing.T) {
134
134
},
135
135
},
136
136
}
137
+
138
+ // Configure a default org to skip mocking a client call for default org
139
+ backendConfig := make (map [string ]pulumi_workspace.BackendConfig , len (creds .Accounts ))
140
+ for url := range creds .Accounts {
141
+ backendConfig [url ] = pulumi_workspace.BackendConfig {DefaultOrg : "test-user-org" }
142
+ }
143
+
137
144
esc := & escCommand {
138
145
command : "esc" ,
139
146
login : & testLoginManager {creds : creds },
140
147
newClient : func (userAgent , backendURL , accessToken string , insecure bool ) client.Client {
141
148
return client .New (userAgent , backendURL , accessToken , insecure )
142
149
},
143
- workspace : workspace .New (testFS {}, & testPulumiWorkspace {}),
150
+ workspace : workspace .New (testFS {}, & testPulumiWorkspace {
151
+ config : pulumi_workspace.PulumiConfig {BackendConfig : backendConfig }}),
144
152
}
145
153
146
154
// Verify default
@@ -162,3 +170,102 @@ func TestEnvVarOverridesAccounts(t *testing.T) {
162
170
assert .NoError (t , err )
163
171
assert .Equal (t , esc .client .URL (), "https://api.pulumi.com" )
164
172
}
173
+
174
+ func TestDefaultOrgConfiguration (t * testing.T ) {
175
+ username := "test-user"
176
+ backend := "https://api.pulumi.com"
177
+ creds := pulumi_workspace.Credentials {
178
+ Current : backend ,
179
+ Accounts : map [string ]pulumi_workspace.Account {
180
+ backend : {
181
+ Username : username ,
182
+ AccessToken : "access-token" ,
183
+ },
184
+ },
185
+ }
186
+
187
+ t .Run ("prefers user configuration" , func (t * testing.T ) {
188
+ // GIVEN
189
+ // The user has configured a default org:
190
+ userConfiguredDefaultOrg := "my-default-org"
191
+ testWorkspace := workspace .New (testFS {}, & testPulumiWorkspace {
192
+ config : pulumi_workspace.PulumiConfig {
193
+ BackendConfig : map [string ]pulumi_workspace.BackendConfig {
194
+ backend : {
195
+ DefaultOrg : userConfiguredDefaultOrg ,
196
+ },
197
+ },
198
+ },
199
+ })
200
+
201
+ testClient := testPulumiClient {}
202
+ esc := & escCommand {
203
+ command : "esc" ,
204
+ login : & testLoginManager {creds : creds },
205
+ newClient : func (userAgent , backendURL , accessToken string , insecure bool ) client.Client {
206
+ return & testClient
207
+ },
208
+ workspace : testWorkspace ,
209
+ }
210
+
211
+ // WHEN
212
+ err := esc .getCachedClient (context .Background ())
213
+
214
+ // THEN
215
+ assert .NoError (t , err )
216
+ assert .Equal (t , userConfiguredDefaultOrg , esc .account .DefaultOrg )
217
+ })
218
+
219
+ t .Run ("falls back to backend client configuration" , func (t * testing.T ) {
220
+ // GIVEN
221
+ // The user has not configured a default org:
222
+ testWorkspace := workspace .New (testFS {}, & testPulumiWorkspace {})
223
+
224
+ // But the backend has an opinion on the default org:
225
+ serviceDefaultOrg := "service-default-org"
226
+ testClient := testPulumiClient {
227
+ defaultOrg : serviceDefaultOrg ,
228
+ }
229
+
230
+ esc := & escCommand {
231
+ command : "esc" ,
232
+ login : & testLoginManager {creds : creds },
233
+ newClient : func (userAgent , backendURL , accessToken string , insecure bool ) client.Client {
234
+ return & testClient
235
+ },
236
+ workspace : testWorkspace ,
237
+ }
238
+
239
+ // WHEN
240
+ err := esc .getCachedClient (context .Background ())
241
+
242
+ // THEN
243
+ assert .NoError (t , err )
244
+ assert .Equal (t , serviceDefaultOrg , esc .account .DefaultOrg )
245
+ })
246
+
247
+ t .Run ("falls back to individual org as last resort" , func (t * testing.T ) {
248
+ // GIVEN
249
+ // The user has not configured a default org:
250
+ testWorkspace := workspace .New (testFS {}, & testPulumiWorkspace {})
251
+
252
+ // And the service has no opinion:
253
+ testClient := testPulumiClient {defaultOrg : "" }
254
+
255
+ esc := & escCommand {
256
+ command : "esc" ,
257
+ login : & testLoginManager {creds : creds },
258
+ newClient : func (userAgent , backendURL , accessToken string , insecure bool ) client.Client {
259
+ return & testClient
260
+ },
261
+ workspace : testWorkspace ,
262
+ }
263
+
264
+ // WHEN
265
+ err := esc .getCachedClient (context .Background ())
266
+
267
+ // THEN
268
+ assert .NoError (t , err )
269
+ assert .Equal (t , username , esc .account .DefaultOrg )
270
+ })
271
+ }
0 commit comments