Visitar URL original
Strengthen login and logout test assertions (#24959) · pulumi/pulumi@2a41c68 · GitHub
Skip to content

Commit 2a41c68

Browse files
authored
Strengthen login and logout test assertions (#24959)
Do a pass over the login and logout tests In preparation for the work on the credentials helper process.
1 parent 289cf7a commit 2a41c68

2 files changed

Lines changed: 59 additions & 21 deletions

File tree

‎pkg/cmd/pulumi/auth/login_test.go‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,20 @@ func TestLoginURLResolution(t *testing.T) {
4646
type capturedLogin struct {
4747
url string
4848
oidcToken string
49+
calls int
4950
}
5051

5152
credsF := func() (workspace.Credentials, error) {
5253
return workspace.Credentials{Current: "https://stored-creds.example.com"}, nil
5354
}
55+
configuredWorkspace := &pkgWorkspace.MockContext{
56+
ReadProjectF: func(string) (*workspace.Project, string, error) {
57+
return &workspace.Project{
58+
Backend: &workspace.ProjectBackend{URL: "https://project-backend.example.com"},
59+
}, "", nil
60+
},
61+
GetStoredCredentialsF: credsF,
62+
}
5463

5564
tests := []struct {
5665
name string
@@ -63,25 +72,34 @@ func TestLoginURLResolution(t *testing.T) {
6372
expectedError string
6473
}{
6574
{
66-
name: "command argument takes precedence",
67-
args: []string{"s3://my-bucket"},
68-
ws: &pkgWorkspace.MockContext{},
75+
name: "command argument takes precedence",
76+
args: []string{"s3://my-bucket"},
77+
ws: configuredWorkspace,
78+
envVars: map[string]string{
79+
"PULUMI_BACKEND_URL": "https://env-backend.example.com",
80+
},
6981
expectedURL: "s3://my-bucket",
7082
},
7183
{
7284
name: "cloud-url flag takes precedence",
7385
flags: map[string]string{
7486
"cloud-url": "https://custom.example.com",
7587
},
76-
ws: &pkgWorkspace.MockContext{},
88+
ws: configuredWorkspace,
89+
envVars: map[string]string{
90+
"PULUMI_BACKEND_URL": "https://env-backend.example.com",
91+
},
7792
expectedURL: "https://custom.example.com",
7893
},
7994
{
8095
name: "local flag sets file:// backend",
8196
flags: map[string]string{
8297
"local": "true",
8398
},
84-
ws: &pkgWorkspace.MockContext{},
99+
ws: configuredWorkspace,
100+
envVars: map[string]string{
101+
"PULUMI_BACKEND_URL": "https://env-backend.example.com",
102+
},
85103
expectedURL: "file://~",
86104
},
87105
{
@@ -106,24 +124,15 @@ func TestLoginURLResolution(t *testing.T) {
106124
},
107125
{
108126
name: "environment variable used when no explicit URL",
109-
ws: &pkgWorkspace.MockContext{
110-
GetStoredCredentialsF: credsF,
111-
},
127+
ws: configuredWorkspace,
112128
envVars: map[string]string{
113129
"PULUMI_BACKEND_URL": "https://env-backend.example.com",
114130
},
115131
expectedURL: "https://env-backend.example.com",
116132
},
117133
{
118-
name: "project backend URL used when no env var",
119-
ws: &pkgWorkspace.MockContext{
120-
ReadProjectF: func(string) (*workspace.Project, string, error) {
121-
return &workspace.Project{
122-
Backend: &workspace.ProjectBackend{URL: "https://project-backend.example.com"},
123-
}, "", nil
124-
},
125-
GetStoredCredentialsF: credsF,
126-
},
134+
name: "project backend URL used when no env var",
135+
ws: configuredWorkspace,
127136
expectedURL: "https://project-backend.example.com",
128137
},
129138
{
@@ -190,6 +199,7 @@ func TestLoginURLResolution(t *testing.T) {
190199
insecure bool,
191200
color colors.Colorization,
192201
) (pkgBackend.Backend, error) {
202+
captured.calls++
193203
captured.url = url
194204
return &pkgBackend.MockBackend{
195205
URLF: func() string { return url },
@@ -208,6 +218,7 @@ func TestLoginURLResolution(t *testing.T) {
208218
insecure bool,
209219
authContext workspace.AuthContext,
210220
) (pkgBackend.Backend, error) {
221+
captured.calls++
211222
captured.url = url
212223
captured.oidcToken = authContext.Token
213224
return &pkgBackend.MockBackend{
@@ -241,13 +252,16 @@ func TestLoginURLResolution(t *testing.T) {
241252

242253
if tt.expectError {
243254
require.Error(t, err)
255+
assert.Zero(t, captured.calls)
244256
if tt.expectedError != "" {
245257
require.ErrorContains(t, err, tt.expectedError)
246258
}
247259
} else {
248260
require.NoError(t, err)
261+
assert.Equal(t, 1, captured.calls)
249262
assert.Equal(t, tt.expectedURL, captured.url,
250263
"login should be called with expected URL")
264+
assert.Equal(t, tt.flags["oidc-token"], captured.oidcToken)
251265
}
252266
})
253267
}

‎pkg/cmd/pulumi/auth/logout_test.go‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,37 @@ func TestDeleteAccountSkipsAgentFallbackWhenExplicitPathSet(t *testing.T) {
7777
t.Setenv("CODEX_SANDBOX", "1")
7878
t.Setenv(workspace.PulumiCredentialsPathEnvVar, credsDir)
7979

80+
const cloudURL = "https://api.logout-explicit.example.com"
81+
require.NoError(t, workspace.StoreAgentAccount(cloudURL, workspace.Account{AccessToken: "agent-token"}, true))
8082
err := workspace.StoreCredentials(workspace.Credentials{
8183
Accounts: map[string]workspace.Account{
82-
"https://api.logout-explicit.example.com": {AccessToken: "default-token"},
84+
cloudURL: {AccessToken: "default-token"},
8385
},
8486
})
8587
require.NoError(t, err)
8688

87-
err = deleteAccount("https://api.logout-explicit.example.com")
89+
err = deleteAccount(cloudURL)
8890
require.NoError(t, err)
8991

9092
creds, err := workspace.GetStoredCredentials()
9193
require.NoError(t, err)
92-
assert.NotContains(t, creds.Accounts, "https://api.logout-explicit.example.com")
94+
assert.NotContains(t, creds.Accounts, cloudURL)
95+
96+
// Logging out again must leave the agent account alone
97+
require.NoError(t, deleteAccount(cloudURL))
98+
agentAccount, err := workspace.GetAgentAccount(cloudURL)
99+
require.NoError(t, err)
100+
assert.Equal(t, "agent-token", agentAccount.AccessToken)
93101
}
94102

95103
func TestDeleteAllAccountsSkipsAgentFallbackOutsideAgentMode(t *testing.T) {
96104
credsDir := ptesting.IsolateCredentials(t).Home
97105
t.Setenv("PULUMI_TEST_ALLOW_AGENT_FALLBACK", "true")
98106
credsPath := filepath.Join(credsDir, "credentials.json")
99107

108+
const agentURL = "https://api.agent.example.com"
109+
require.NoError(t, workspace.StoreAgentAccount(agentURL, workspace.Account{AccessToken: "agent-token"}, true))
110+
100111
err := workspace.StoreCredentials(workspace.Credentials{
101112
Accounts: map[string]workspace.Account{
102113
"https://api.logout-all.example.com": {AccessToken: "default-token"},
@@ -109,6 +120,9 @@ func TestDeleteAllAccountsSkipsAgentFallbackOutsideAgentMode(t *testing.T) {
109120

110121
_, err = os.Stat(credsPath)
111122
assert.True(t, os.IsNotExist(err))
123+
agentAccount, err := workspace.GetAgentAccount(agentURL)
124+
require.NoError(t, err)
125+
assert.Equal(t, "agent-token", agentAccount.AccessToken)
112126
}
113127

114128
//nolint:paralleltest // IsolateCredentials changes process-wide environment variables.
@@ -117,6 +131,7 @@ func TestLogoutCommandAll(t *testing.T) {
117131
require.NoError(t, workspace.StoreCredentials(workspace.Credentials{
118132
Accounts: map[string]workspace.Account{
119133
"https://api.logout-command-all.example.com": {AccessToken: "default-token"},
134+
"https://api.other.example.com": {AccessToken: "other-token"},
120135
},
121136
}))
122137

@@ -128,6 +143,10 @@ func TestLogoutCommandAll(t *testing.T) {
128143
err := cmd.Execute()
129144
require.NoError(t, err)
130145
assert.Contains(t, output.String(), "Logged out of everything")
146+
creds, err := workspace.GetStoredCredentials()
147+
require.NoError(t, err)
148+
assert.Empty(t, creds.Current)
149+
assert.Empty(t, creds.Accounts)
131150
}
132151

133152
func TestLogoutCommandDeleteCredentialsKeyRequiresAll(t *testing.T) {
@@ -148,7 +167,8 @@ func TestLogoutCommandCloudURL(t *testing.T) {
148167
cloudURL := "https://api.logout-command.example.com"
149168
require.NoError(t, workspace.StoreCredentials(workspace.Credentials{
150169
Accounts: map[string]workspace.Account{
151-
cloudURL: {AccessToken: "default-token"},
170+
cloudURL: {AccessToken: "default-token"},
171+
"https://api.other.example.com": {AccessToken: "other-token"},
152172
},
153173
}))
154174

@@ -160,6 +180,10 @@ func TestLogoutCommandCloudURL(t *testing.T) {
160180
err := cmd.Execute()
161181
require.NoError(t, err)
162182
assert.Contains(t, output.String(), "Logged out of "+cloudURL)
183+
creds, err := workspace.GetStoredCredentials()
184+
require.NoError(t, err)
185+
assert.NotContains(t, creds.Accounts, cloudURL)
186+
assert.Equal(t, "other-token", creds.Accounts["https://api.other.example.com"].AccessToken)
163187
}
164188

165189
func TestLogoutCommandFallsBackToAgentCurrentCloud(t *testing.T) {

0 commit comments

Comments
 (0)