@@ -5,9 +5,11 @@ import (
5
5
"fmt"
6
6
"net/http"
7
7
"net/http/httptest"
8
+ "os"
8
9
"runtime"
9
10
"testing"
10
11
12
+ "github.com/google/uuid"
11
13
"github.com/stretchr/testify/assert"
12
14
"github.com/stretchr/testify/require"
13
15
@@ -304,4 +306,48 @@ func TestLogin(t *testing.T) {
304
306
// This **should not be equal** to the token we passed in.
305
307
require .NotEqual (t , client .SessionToken (), sessionFile )
306
308
})
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
+ })
307
353
}
0 commit comments