@@ -374,6 +374,90 @@ func TestDeleteUser(t *testing.T) {
374
374
})
375
375
}
376
376
377
+ func TestNotifyDeletedUser (t * testing.T ) {
378
+ t .Parallel ()
379
+
380
+ t .Run ("OwnerNotified" , func (t * testing.T ) {
381
+ t .Parallel ()
382
+
383
+ // given
384
+ notifyEnq := & testutil.FakeNotificationsEnqueuer {}
385
+ adminClient := coderdtest .New (t , & coderdtest.Options {
386
+ NotificationsEnqueuer : notifyEnq ,
387
+ })
388
+ firstUser := coderdtest .CreateFirstUser (t , adminClient )
389
+
390
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
391
+ defer cancel ()
392
+
393
+ user , err := adminClient .CreateUser (ctx , codersdk.CreateUserRequest {
394
+ OrganizationID : firstUser .OrganizationID ,
395
+ Email : "another@user.org" ,
396
+ Username : "someone-else" ,
397
+ Password : "SomeSecurePassword!" ,
398
+ })
399
+ require .NoError (t , err )
400
+
401
+ // when
402
+ err = adminClient .DeleteUser (context .Background (), user .ID )
403
+ require .NoError (t , err )
404
+
405
+ // then
406
+ require .Len (t , notifyEnq .Sent , 2 )
407
+ // notifyEnq.Sent[0] is create account event
408
+ require .Equal (t , notifications .TemplateUserAccountDeleted , notifyEnq .Sent [1 ].TemplateID )
409
+ require .Equal (t , firstUser .UserID , notifyEnq .Sent [1 ].UserID )
410
+ require .Contains (t , notifyEnq .Sent [1 ].Targets , user .ID )
411
+ require .Equal (t , user .Username , notifyEnq .Sent [1 ].Labels ["deleted_account_name" ])
412
+ })
413
+
414
+ t .Run ("UserAdminNotified" , func (t * testing.T ) {
415
+ t .Parallel ()
416
+
417
+ // given
418
+ notifyEnq := & testutil.FakeNotificationsEnqueuer {}
419
+ adminClient := coderdtest .New (t , & coderdtest.Options {
420
+ NotificationsEnqueuer : notifyEnq ,
421
+ })
422
+ firstUser := coderdtest .CreateFirstUser (t , adminClient )
423
+
424
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
425
+ defer cancel ()
426
+
427
+ _ , userAdmin := coderdtest .CreateAnotherUser (t , adminClient , firstUser .OrganizationID , rbac .RoleUserAdmin ())
428
+
429
+ member , err := adminClient .CreateUser (ctx , codersdk.CreateUserRequest {
430
+ OrganizationID : firstUser .OrganizationID ,
431
+ Email : "another@user.org" ,
432
+ Username : "someone-else" ,
433
+ Password : "SomeSecurePassword!" ,
434
+ })
435
+ require .NoError (t , err )
436
+
437
+ // when
438
+ err = adminClient .DeleteUser (context .Background (), member .ID )
439
+ require .NoError (t , err )
440
+
441
+ // then
442
+ require .Len (t , notifyEnq .Sent , 5 )
443
+ // notifyEnq.Sent[0]: "User admin" account created, "owner" notified
444
+ // notifyEnq.Sent[1]: "Member" account created, "owner" notified
445
+ // notifyEnq.Sent[2]: "Member" account created, "user admin" notified
446
+
447
+ // "Member" account deleted, "owner" notified
448
+ require .Equal (t , notifications .TemplateUserAccountDeleted , notifyEnq .Sent [3 ].TemplateID )
449
+ require .Equal (t , firstUser .UserID , notifyEnq .Sent [3 ].UserID )
450
+ require .Contains (t , notifyEnq .Sent [3 ].Targets , member .ID )
451
+ require .Equal (t , member .Username , notifyEnq .Sent [3 ].Labels ["deleted_account_name" ])
452
+
453
+ // "Member" account deleted, "user admin" notified
454
+ require .Equal (t , notifications .TemplateUserAccountDeleted , notifyEnq .Sent [4 ].TemplateID )
455
+ require .Equal (t , userAdmin .ID , notifyEnq .Sent [4 ].UserID )
456
+ require .Contains (t , notifyEnq .Sent [4 ].Targets , member .ID )
457
+ require .Equal (t , member .Username , notifyEnq .Sent [4 ].Labels ["deleted_account_name" ])
458
+ })
459
+ }
460
+
377
461
func TestPostLogout (t * testing.T ) {
378
462
t .Parallel ()
379
463
0 commit comments