2626 */
2727namespace OCA \OAuth2 \Tests \Controller ;
2828
29+ use OC \Authentication \Token \IToken ;
30+ use OCP \Authentication \Token \IProvider as IAuthTokenProvider ;
2931use OCA \OAuth2 \Controller \SettingsController ;
3032use OCA \OAuth2 \Db \AccessTokenMapper ;
3133use OCA \OAuth2 \Db \Client ;
3436use OCP \AppFramework \Http \JSONResponse ;
3537use OCP \IL10N ;
3638use OCP \IRequest ;
39+ use OCP \IUser ;
40+ use OCP \IUserManager ;
3741use OCP \Security \ISecureRandom ;
3842use Test \TestCase ;
3943
44+ /**
45+ * @group DB
46+ */
4047class SettingsControllerTest extends TestCase {
4148 /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
4249 private $ request ;
@@ -46,8 +53,14 @@ class SettingsControllerTest extends TestCase {
4653 private $ secureRandom ;
4754 /** @var AccessTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
4855 private $ accessTokenMapper ;
56+ /** @var IAuthTokenProvider|\PHPUnit\Framework\MockObject\MockObject */
57+ private $ authTokenProvider ;
58+ /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
59+ private $ userManager ;
4960 /** @var SettingsController */
5061 private $ settingsController ;
62+ /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
63+ private $ l ;
5164
5265 protected function setUp (): void {
5366 parent ::setUp ();
@@ -56,18 +69,22 @@ protected function setUp(): void {
5669 $ this ->clientMapper = $ this ->createMock (ClientMapper::class);
5770 $ this ->secureRandom = $ this ->createMock (ISecureRandom::class);
5871 $ this ->accessTokenMapper = $ this ->createMock (AccessTokenMapper::class);
59- $ l = $ this ->createMock (IL10N ::class);
60- $ l ->method ('t ' )
72+ $ this ->authTokenProvider = $ this ->createMock (IAuthTokenProvider::class);
73+ $ this ->userManager = $ this ->createMock (IUserManager::class);
74+ $ this ->l = $ this ->createMock (IL10N ::class);
75+ $ this ->l ->method ('t ' )
6176 ->willReturnArgument (0 );
62-
6377 $ this ->settingsController = new SettingsController (
6478 'oauth2 ' ,
6579 $ this ->request ,
6680 $ this ->clientMapper ,
6781 $ this ->secureRandom ,
6882 $ this ->accessTokenMapper ,
69- $ l
83+ $ this ->l ,
84+ $ this ->authTokenProvider ,
85+ $ this ->userManager
7086 );
87+
7188 }
7289
7390 public function testAddClient () {
@@ -113,6 +130,26 @@ public function testAddClient() {
113130 }
114131
115132 public function testDeleteClient () {
133+
134+ $ userManager = \OC ::$ server ->getUserManager ();
135+ // count other users in the db before adding our own
136+ $ count = 0 ;
137+ $ function = function (IUser $ user ) use (&$ count ) {
138+ if ($ user ->getLastLogin () > 0 ) {
139+ $ count ++;
140+ }
141+ };
142+ $ userManager ->callForAllUsers ($ function );
143+ $ user1 = $ userManager ->createUser ('test101 ' , 'test101 ' );
144+ $ user1 ->updateLastLoginTimestamp ();
145+ $ tokenProviderMock = $ this ->getMockBuilder (IAuthTokenProvider::class)->getMock ();
146+
147+ // expect one call per user and ensure the correct client name
148+ $ tokenProviderMock
149+ ->expects ($ this ->exactly ($ count + 1 ))
150+ ->method ('invalidateTokensOfUser ' )
151+ ->with ($ this ->isType ('string ' ), 'My Client Name ' );
152+
116153 $ client = new Client ();
117154 $ client ->setId (123 );
118155 $ client ->setName ('My Client Name ' );
@@ -129,12 +166,26 @@ public function testDeleteClient() {
129166 ->method ('deleteByClientId ' )
130167 ->with (123 );
131168 $ this ->clientMapper
169+ ->expects ($ this ->once ())
132170 ->method ('delete ' )
133171 ->with ($ client );
134172
135- $ result = $ this ->settingsController ->deleteClient (123 );
173+ $ settingsController = new SettingsController (
174+ 'oauth2 ' ,
175+ $ this ->request ,
176+ $ this ->clientMapper ,
177+ $ this ->secureRandom ,
178+ $ this ->accessTokenMapper ,
179+ $ this ->l ,
180+ $ tokenProviderMock ,
181+ $ userManager
182+ );
183+
184+ $ result = $ settingsController ->deleteClient (123 );
136185 $ this ->assertInstanceOf (JSONResponse::class, $ result );
137186 $ this ->assertEquals ([], $ result ->getData ());
187+
188+ $ user1 ->delete ();
138189 }
139190
140191 public function testInvalidRedirectUri () {
0 commit comments