48
48
use OCA \Settings \Mailer \NewUserMailHelper ;
49
49
use OCP \App \IAppManager ;
50
50
use OCP \AppFramework \Http \DataResponse ;
51
+ use OCP \EventDispatcher \IEventDispatcher ;
51
52
use OCP \IConfig ;
52
53
use OCP \IGroup ;
53
54
use OCP \IL10N ;
58
59
use OCP \IUserSession ;
59
60
use OCP \L10N \IFactory ;
60
61
use OCP \Mail \IEMailTemplate ;
62
+ use OCP \Security \Events \GenerateSecurePasswordEvent ;
61
63
use OCP \Security \ISecureRandom ;
62
64
use OCP \UserInterface ;
63
65
use PHPUnit \Framework \MockObject \MockObject ;
@@ -94,6 +96,8 @@ class UsersControllerTest extends TestCase {
94
96
private $ secureRandom ;
95
97
/** @var RemoteWipe|MockObject */
96
98
private $ remoteWipe ;
99
+ /** @var IEventDispatcher */
100
+ private $ eventDispatcher ;
97
101
98
102
protected function setUp (): void {
99
103
parent ::setUp ();
@@ -111,6 +115,7 @@ protected function setUp(): void {
111
115
$ this ->federatedShareProviderFactory = $ this ->createMock (FederatedShareProviderFactory::class);
112
116
$ this ->secureRandom = $ this ->createMock (ISecureRandom::class);
113
117
$ this ->remoteWipe = $ this ->createMock (RemoteWipe::class);
118
+ $ this ->eventDispatcher = $ this ->createMock (IEventDispatcher::class);
114
119
115
120
$ this ->api = $ this ->getMockBuilder (UsersController::class)
116
121
->setConstructorArgs ([
@@ -128,6 +133,7 @@ protected function setUp(): void {
128
133
$ this ->federatedShareProviderFactory ,
129
134
$ this ->secureRandom ,
130
135
$ this ->remoteWipe ,
136
+ $ this ->eventDispatcher ,
131
137
])
132
138
->setMethods (['fillStorageInfo ' ])
133
139
->getMock ();
@@ -389,7 +395,8 @@ public function testAddUserSuccessfulWithDisplayName() {
389
395
$ this ->newUserMailHelper ,
390
396
$ this ->federatedShareProviderFactory ,
391
397
$ this ->secureRandom ,
392
- $ this ->remoteWipe
398
+ $ this ->remoteWipe ,
399
+ $ this ->eventDispatcher ,
393
400
])
394
401
->setMethods (['editUser ' ])
395
402
->getMock ();
@@ -486,6 +493,46 @@ public function testAddUserSuccessfulGenerateUserID() {
486
493
));
487
494
}
488
495
496
+ public function testAddUserSuccessfulGeneratePassword () {
497
+ $ this ->userManager
498
+ ->expects ($ this ->once ())
499
+ ->method ('userExists ' )
500
+ ->with ('NewUser ' )
501
+ ->willReturn (false );
502
+ $ this ->userManager
503
+ ->expects ($ this ->once ())
504
+ ->method ('createUser ' );
505
+ $ this ->logger
506
+ ->expects ($ this ->once ())
507
+ ->method ('info ' )
508
+ ->with ('Successful addUser call with userid: NewUser ' , ['app ' => 'ocs_api ' ]);
509
+ $ loggedInUser = $ this ->getMockBuilder (IUser::class)
510
+ ->disableOriginalConstructor ()
511
+ ->getMock ();
512
+ $ loggedInUser
513
+ ->expects ($ this ->once ())
514
+ ->method ('getUID ' )
515
+ ->willReturn ('adminUser ' );
516
+ $ this ->userSession
517
+ ->expects ($ this ->once ())
518
+ ->method ('getUser ' )
519
+ ->willReturn ($ loggedInUser );
520
+ $ this ->groupManager
521
+ ->expects ($ this ->once ())
522
+ ->method ('isAdmin ' )
523
+ ->with ('adminUser ' )
524
+ ->willReturn (true );
525
+ $ this ->eventDispatcher
526
+ ->expects ($ this ->once ())
527
+ ->method ('dispatchTyped ' )
528
+ ->with (new GenerateSecurePasswordEvent ());
529
+
530
+ $ this ->assertTrue (key_exists (
531
+ 'id ' ,
532
+ $ this ->api ->addUser ('NewUser ' , '' , '' , 'foo@bar ' )->getData ()
533
+ ));
534
+ }
535
+
489
536
490
537
public function testAddUserFailedToGenerateUserID () {
491
538
$ this ->expectException (\OCP \AppFramework \OCS \OCSException::class);
@@ -3126,6 +3173,7 @@ public function testGetCurrentUserLoggedIn() {
3126
3173
$ this ->federatedShareProviderFactory ,
3127
3174
$ this ->secureRandom ,
3128
3175
$ this ->remoteWipe ,
3176
+ $ this ->eventDispatcher ,
3129
3177
])
3130
3178
->setMethods (['getUserData ' ])
3131
3179
->getMock ();
@@ -3190,6 +3238,7 @@ public function testGetUser() {
3190
3238
$ this ->federatedShareProviderFactory ,
3191
3239
$ this ->secureRandom ,
3192
3240
$ this ->remoteWipe ,
3241
+ $ this ->eventDispatcher ,
3193
3242
])
3194
3243
->setMethods (['getUserData ' ])
3195
3244
->getMock ();
0 commit comments