@@ -22,6 +22,8 @@ type mockLsStore struct {
2222 orgs []entity.Organization
2323 workspaces []entity.Workspace
2424 accessToken string
25+ authTokens * entity.AuthTokens
26+ workspaceOrgID string
2527 currentUserCalls int
2628 getOrganizationsCall int
2729}
@@ -38,6 +40,10 @@ func (m *mockLsStore) GetAccessToken() (string, error) {
3840 return "tok" , nil
3941}
4042
43+ func (m * mockLsStore ) GetAuthTokens () (* entity.AuthTokens , error ) {
44+ return m .authTokens , nil
45+ }
46+
4147func (m * mockLsStore ) GetWorkspace (_ string ) (* entity.Workspace , error ) {
4248 return nil , nil
4349}
@@ -50,7 +56,8 @@ func (m *mockLsStore) GetCachedActiveOrganizationOrNil() (*entity.Organization,
5056 return m .org , nil
5157}
5258
53- func (m * mockLsStore ) GetWorkspaces (_ string , _ * store.GetWorkspacesOptions ) ([]entity.Workspace , error ) {
59+ func (m * mockLsStore ) GetWorkspaces (orgID string , _ * store.GetWorkspacesOptions ) ([]entity.Workspace , error ) {
60+ m .workspaceOrgID = orgID
5461 return m .workspaces , nil
5562}
5663
@@ -90,6 +97,7 @@ func newTestStore() *mockLsStore {
9097func TestRunLs_APIKeyJSONSkipsUserAndOrgList (t * testing.T ) {
9198 s := newTestStore ()
9299 s .accessToken = "brev_api_test-key"
100+ s .authTokens = & entity.AuthTokens {APIKey : "brev_api_test-key" , APIKeyOrgID : "org1" }
93101 s .workspaces = []entity.Workspace {
94102 {
95103 ID : "ws1" ,
@@ -130,6 +138,55 @@ func TestRunLs_APIKeyJSONSkipsUserAndOrgList(t *testing.T) {
130138 }
131139}
132140
141+ func TestRunLs_APIKeyUsesCredentialOrgNotCachedActiveOrg (t * testing.T ) {
142+ s := newTestStore ()
143+ s .accessToken = "brev_api_test-key"
144+ s .authTokens = & entity.AuthTokens {APIKey : "brev_api_test-key" , APIKeyOrgID : "org-login" }
145+ s .org = & entity.Organization {ID : "org-set" , Name : "set-org" }
146+ s .workspaces = []entity.Workspace {
147+ {
148+ ID : "ws1" ,
149+ Name : "api-key-instance" ,
150+ Status : entity .Running ,
151+ CreatedByUserID : "other-user" ,
152+ },
153+ }
154+ term := terminal .New ()
155+
156+ out := captureStdout (t , func () {
157+ err := RunLs (term , s , nil , "" , false , true )
158+ if err != nil {
159+ t .Fatalf ("RunLs returned error: %v" , err )
160+ }
161+ })
162+
163+ if ! strings .Contains (out , "api-key-instance" ) {
164+ t .Fatalf ("expected workspace output, got %s" , out )
165+ }
166+ if s .workspaceOrgID != "org-login" {
167+ t .Fatalf ("expected API key credential org org-login, got %s" , s .workspaceOrgID )
168+ }
169+ }
170+
171+ func TestRunLs_APIKeyRequiresCredentialOrg (t * testing.T ) {
172+ s := newTestStore ()
173+ s .accessToken = "brev_api_test-key"
174+ s .authTokens = & entity.AuthTokens {APIKey : "brev_api_test-key" }
175+ term := terminal .New ()
176+
177+ err := RunLs (term , s , nil , "" , false , true )
178+
179+ if err == nil {
180+ t .Fatal ("expected missing API key org error, got nil" )
181+ }
182+ if ! strings .Contains (err .Error (), "api key auth requires an org id" ) {
183+ t .Fatalf ("expected API key org validation error, got %v" , err )
184+ }
185+ if s .workspaceOrgID != "" {
186+ t .Fatalf ("expected no workspace call, got org %s" , s .workspaceOrgID )
187+ }
188+ }
189+
133190// captureStdout runs fn while capturing stdout and returns the output.
134191func captureStdout (t * testing.T , fn func ()) string {
135192 t .Helper ()
0 commit comments