mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
fix: handle CLI default organization when none exists in <v2.9.0 coderd (#12594)
* chore: 'coder org set' help message was incorrect * fix: handler coder cli against older versions of Coder
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/cli/clitest"
|
||||
@@ -16,6 +22,38 @@ import (
|
||||
func TestCurrentOrganization(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// This test emulates 2 cases:
|
||||
// 1. The user is not a part of the default organization, but only belongs to one.
|
||||
// 2. The user is connecting to an older Coder instance.
|
||||
t.Run("no-default", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
orgID := uuid.New()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode([]codersdk.Organization{
|
||||
{
|
||||
ID: orgID,
|
||||
Name: "not-default",
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
IsDefault: false,
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
client := codersdk.New(must(url.Parse(srv.URL)))
|
||||
inv, root := clitest.New(t, "organizations", "show", "current")
|
||||
clitest.SetupConfig(t, client, root)
|
||||
pty := ptytest.New(t).Attach(inv)
|
||||
errC := make(chan error)
|
||||
go func() {
|
||||
errC <- inv.Run()
|
||||
}()
|
||||
require.NoError(t, <-errC)
|
||||
pty.ExpectMatch(orgID.String())
|
||||
})
|
||||
|
||||
t.Run("OnlyID", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ownerClient := coderdtest.New(t, nil)
|
||||
@@ -108,3 +146,10 @@ func TestOrganizationSwitch(t *testing.T) {
|
||||
pty.ExpectMatch(exp.ID.String())
|
||||
})
|
||||
}
|
||||
|
||||
func must[V any](v V, err error) V {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user