1
1
package cli_test
2
2
3
3
import (
4
+ "encoding/json"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "net/url"
4
8
"testing"
9
+ "time"
5
10
11
+ "github.com/google/uuid"
6
12
"github.com/stretchr/testify/require"
7
13
8
14
"github.com/coder/coder/v2/cli/clitest"
@@ -16,6 +22,38 @@ import (
16
22
func TestCurrentOrganization (t * testing.T ) {
17
23
t .Parallel ()
18
24
25
+ // This test emulates 2 cases:
26
+ // 1. The user is not a part of the default organization, but only belongs to one.
27
+ // 2. The user is connecting to an older Coder instance.
28
+ t .Run ("no-default" , func (t * testing.T ) {
29
+ t .Parallel ()
30
+
31
+ orgID := uuid .New ()
32
+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
33
+ json .NewEncoder (w ).Encode ([]codersdk.Organization {
34
+ {
35
+ ID : orgID ,
36
+ Name : "not-default" ,
37
+ CreatedAt : time .Now (),
38
+ UpdatedAt : time .Now (),
39
+ IsDefault : false ,
40
+ },
41
+ })
42
+ }))
43
+ defer srv .Close ()
44
+
45
+ client := codersdk .New (must (url .Parse (srv .URL )))
46
+ inv , root := clitest .New (t , "organizations" , "show" , "current" )
47
+ clitest .SetupConfig (t , client , root )
48
+ pty := ptytest .New (t ).Attach (inv )
49
+ errC := make (chan error )
50
+ go func () {
51
+ errC <- inv .Run ()
52
+ }()
53
+ require .NoError (t , <- errC )
54
+ pty .ExpectMatch (orgID .String ())
55
+ })
56
+
19
57
t .Run ("OnlyID" , func (t * testing.T ) {
20
58
t .Parallel ()
21
59
ownerClient := coderdtest .New (t , nil )
@@ -108,3 +146,10 @@ func TestOrganizationSwitch(t *testing.T) {
108
146
pty .ExpectMatch (exp .ID .String ())
109
147
})
110
148
}
149
+
150
+ func must [V any ](v V , err error ) V {
151
+ if err != nil {
152
+ panic (err )
153
+ }
154
+ return v
155
+ }
0 commit comments