@@ -8,9 +8,12 @@ import (
8
8
"net/http"
9
9
"testing"
10
10
11
+ "github.com/golang-jwt/jwt/v4"
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
13
14
15
+ "github.com/coder/coder/v2/coderd/coderdtest"
16
+ "github.com/coder/coder/v2/coderd/coderdtest/oidctest"
14
17
"github.com/coder/coder/v2/codersdk"
15
18
"github.com/coder/coder/v2/cryptorand"
16
19
"github.com/coder/coder/v2/enterprise/coderd"
@@ -338,5 +341,68 @@ func TestScim(t *testing.T) {
338
341
require .Len (t , userRes .Users , 1 )
339
342
assert .Equal (t , codersdk .UserStatusSuspended , userRes .Users [0 ].Status )
340
343
})
344
+
345
+ // Create a user via SCIM, which starts as dormant.
346
+ // Log in as the user, making them active.
347
+ // Then patch the user again and the user should still be active.
348
+ t .Run ("ActiveIsActive" , func (t * testing.T ) {
349
+ t .Parallel ()
350
+
351
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
352
+ defer cancel ()
353
+
354
+ scimAPIKey := []byte ("hi" )
355
+
356
+ fake := oidctest .NewFakeIDP (t , oidctest .WithServing ())
357
+ client , _ := coderdenttest .New (t , & coderdenttest.Options {
358
+ Options : & coderdtest.Options {
359
+ OIDCConfig : fake .OIDCConfig (t , []string {}),
360
+ },
361
+ SCIMAPIKey : scimAPIKey ,
362
+ AuditLogging : true ,
363
+ LicenseOptions : & coderdenttest.LicenseOptions {
364
+ AccountID : "coolin" ,
365
+ Features : license.Features {
366
+ codersdk .FeatureSCIM : 1 ,
367
+ },
368
+ },
369
+ })
370
+
371
+ // User is dormant on create
372
+ sUser := makeScimUser (t )
373
+ res , err := client .Request (ctx , "POST" , "/scim/v2/Users" , sUser , setScimAuth (scimAPIKey ))
374
+ require .NoError (t , err )
375
+ defer res .Body .Close ()
376
+ assert .Equal (t , http .StatusOK , res .StatusCode )
377
+
378
+ err = json .NewDecoder (res .Body ).Decode (& sUser )
379
+ require .NoError (t , err )
380
+
381
+ // Verify the user is dormant
382
+ scimUser , err := client .User (ctx , sUser .UserName )
383
+ require .NoError (t , err )
384
+ require .Equal (t , codersdk .UserStatusDormant , scimUser .Status , "user starts as dormant" )
385
+
386
+ // Log in as the user, making them active
387
+ //nolint:bodyclose
388
+ scimUserClient , _ := fake .Login (t , client , jwt.MapClaims {
389
+ "email" : sUser .Emails [0 ].Value ,
390
+ })
391
+ scimUser , err = scimUserClient .User (ctx , codersdk .Me )
392
+ require .NoError (t , err )
393
+ require .Equal (t , codersdk .UserStatusActive , scimUser .Status , "user should now be active" )
394
+
395
+ // Patch the user
396
+ res , err = client .Request (ctx , "PATCH" , "/scim/v2/Users/" + sUser .ID , sUser , setScimAuth (scimAPIKey ))
397
+ require .NoError (t , err )
398
+ _ , _ = io .Copy (io .Discard , res .Body )
399
+ _ = res .Body .Close ()
400
+ assert .Equal (t , http .StatusOK , res .StatusCode )
401
+
402
+ // Verify the user is still active.
403
+ scimUser , err = client .User (ctx , sUser .UserName )
404
+ require .NoError (t , err )
405
+ require .Equal (t , codersdk .UserStatusActive , scimUser .Status , "user is still active" )
406
+ })
341
407
})
342
408
}
0 commit comments