@@ -374,6 +374,103 @@ 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 , err := adminClient .CreateUser (ctx , codersdk.CreateUserRequest {
428
+ OrganizationID : firstUser .OrganizationID ,
429
+ Email : "user-admin@user.org" ,
430
+ Username : "mr-user-admin" ,
431
+ Password : "SomeSecurePassword!" ,
432
+ })
433
+ require .NoError (t , err )
434
+
435
+ _ , err = adminClient .UpdateUserRoles (ctx , userAdmin .Username , codersdk.UpdateRoles {
436
+ Roles : []string {
437
+ rbac .RoleUserAdmin ().String (),
438
+ },
439
+ })
440
+ require .NoError (t , err )
441
+
442
+ member , err := adminClient .CreateUser (ctx , codersdk.CreateUserRequest {
443
+ OrganizationID : firstUser .OrganizationID ,
444
+ Email : "another@user.org" ,
445
+ Username : "someone-else" ,
446
+ Password : "SomeSecurePassword!" ,
447
+ })
448
+ require .NoError (t , err )
449
+
450
+ // when
451
+ err = adminClient .DeleteUser (context .Background (), member .ID )
452
+ require .NoError (t , err )
453
+
454
+ // then
455
+ require .Len (t , notifyEnq .Sent , 5 )
456
+ // notifyEnq.Sent[0]: "User admin" account created, "owner" notified
457
+ // notifyEnq.Sent[1]: "Member" account created, "owner" notified
458
+ // notifyEnq.Sent[2]: "Member" account created, "user admin" notified
459
+
460
+ // "Member" account deleted, "owner" notified
461
+ require .Equal (t , notifications .TemplateUserAccountDeleted , notifyEnq .Sent [3 ].TemplateID )
462
+ require .Equal (t , firstUser .UserID , notifyEnq .Sent [3 ].UserID )
463
+ require .Contains (t , notifyEnq .Sent [3 ].Targets , member .ID )
464
+ require .Equal (t , member .Username , notifyEnq .Sent [3 ].Labels ["deleted_account_name" ])
465
+
466
+ // "Member" account deleted, "user admin" notified
467
+ require .Equal (t , notifications .TemplateUserAccountDeleted , notifyEnq .Sent [4 ].TemplateID )
468
+ require .Equal (t , userAdmin .ID , notifyEnq .Sent [4 ].UserID )
469
+ require .Contains (t , notifyEnq .Sent [4 ].Targets , member .ID )
470
+ require .Equal (t , member .Username , notifyEnq .Sent [4 ].Labels ["deleted_account_name" ])
471
+ })
472
+ }
473
+
377
474
func TestPostLogout (t * testing.T ) {
378
475
t .Parallel ()
379
476
0 commit comments