Skip to content

Commit e4333c0

Browse files
authored
chore: 'coder login' reset cli organization context (coder#13646)
Cli organization context is reset on `coder login` if the organization selected is an invalid organization.
1 parent 8ccdf05 commit e4333c0

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

cli/login.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ func (r *RootCmd) login() *serpent.Command {
336336
return xerrors.Errorf("write server url: %w", err)
337337
}
338338

339+
// If the current organization cannot be fetched, then reset the organization context.
340+
// Otherwise, organization cli commands will fail.
341+
_, err = CurrentOrganization(r, inv, client)
342+
if err != nil {
343+
_ = config.Organization().Delete()
344+
}
345+
339346
_, _ = fmt.Fprintf(inv.Stdout, Caret+"Welcome to Coder, %s! You're authenticated.\n", pretty.Sprint(cliui.DefaultStyles.Keyword, resp.Username))
340347
return nil
341348
},

cli/login_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8+
"os"
89
"runtime"
910
"testing"
1011

12+
"github.com/google/uuid"
1113
"github.com/stretchr/testify/assert"
1214
"github.com/stretchr/testify/require"
1315

@@ -304,4 +306,48 @@ func TestLogin(t *testing.T) {
304306
// This **should not be equal** to the token we passed in.
305307
require.NotEqual(t, client.SessionToken(), sessionFile)
306308
})
309+
310+
// Login should reset the configured organization if the user is not a member
311+
t.Run("ResetOrganization", func(t *testing.T) {
312+
t.Parallel()
313+
client := coderdtest.New(t, nil)
314+
coderdtest.CreateFirstUser(t, client)
315+
root, cfg := clitest.New(t, "login", client.URL.String(), "--token", client.SessionToken())
316+
317+
notRealOrg := uuid.NewString()
318+
err := cfg.Organization().Write(notRealOrg)
319+
require.NoError(t, err, "write bad org to config")
320+
321+
err = root.Run()
322+
require.NoError(t, err)
323+
sessionFile, err := cfg.Session().Read()
324+
require.NoError(t, err)
325+
require.NotEqual(t, client.SessionToken(), sessionFile)
326+
327+
// Organization config should be deleted since the org does not exist
328+
selected, err := cfg.Organization().Read()
329+
require.ErrorIs(t, err, os.ErrNotExist)
330+
require.NotEqual(t, selected, notRealOrg)
331+
})
332+
333+
t.Run("KeepOrganizationContext", func(t *testing.T) {
334+
t.Parallel()
335+
client := coderdtest.New(t, nil)
336+
first := coderdtest.CreateFirstUser(t, client)
337+
root, cfg := clitest.New(t, "login", client.URL.String(), "--token", client.SessionToken())
338+
339+
err := cfg.Organization().Write(first.OrganizationID.String())
340+
require.NoError(t, err, "write bad org to config")
341+
342+
err = root.Run()
343+
require.NoError(t, err)
344+
sessionFile, err := cfg.Session().Read()
345+
require.NoError(t, err)
346+
require.NotEqual(t, client.SessionToken(), sessionFile)
347+
348+
// Organization config should be deleted since the org does not exist
349+
selected, err := cfg.Organization().Read()
350+
require.NoError(t, err)
351+
require.Equal(t, selected, first.OrganizationID.String())
352+
})
307353
}

0 commit comments

Comments
 (0)