33namespace OCA \Text \Tests ;
44
55use OCA \Text \Controller \ISessionAwareController ;
6+ use OCA \Text \Db \Document ;
7+ use OCA \Text \Db \Session ;
8+ use OCA \Text \Exception \AccountDisabledException ;
69use OCA \Text \Exception \InvalidSessionException ;
710use OCA \Text \Middleware \SessionMiddleware ;
811use OCA \Text \Service \DocumentService ;
912use OCA \Text \Service \SessionService ;
13+ use OCP \AppFramework \Http ;
14+ use OCP \AppFramework \Http \JSONResponse ;
1015use OCP \Constants ;
1116use OCP \Files \File ;
1217use OCP \Files \Folder ;
1520use OCP \IRequest ;
1621use OCP \ISession ;
1722use OCP \IUser ;
23+ use OCP \IUserManager ;
1824use OCP \IUserSession ;
1925use OCP \Share \Exceptions \ShareNotFound ;
2026use OCP \Share \IManager ;
@@ -28,6 +34,9 @@ class SessionMiddlewareTest extends TestCase {
2834 private IUserSession $ userSession ;
2935 private IRootFolder $ rootFolder ;
3036 private IManager $ shareManager ;
37+ private SessionService $ sessionService ;
38+ private DocumentService $ documentService ;
39+ private IUserManager $ userManager ;
3140
3241 protected function setUp (): void {
3342 parent ::setUp ();
@@ -37,16 +46,20 @@ protected function setUp(): void {
3746 $ this ->userSession = $ this ->createMock (IUserSession::class);
3847 $ this ->rootFolder = $ this ->createMock (IRootFolder::class);
3948 $ this ->shareManager = $ this ->createMock (IManager::class);
49+ $ this ->sessionService = $ this ->createMock (SessionService::class);
50+ $ this ->documentService = $ this ->createMock (DocumentService::class);
51+ $ this ->userManager = $ this ->createMock (IUserManager::class);
4052
4153 $ this ->middleware = new SessionMiddleware (
4254 $ this ->request ,
43- $ this ->createMock (SessionService::class) ,
44- $ this ->createMock (DocumentService::class) ,
55+ $ this ->sessionService ,
56+ $ this ->documentService ,
4557 $ this ->session ,
4658 $ this ->userSession ,
4759 $ this ->rootFolder ,
4860 $ this ->shareManager ,
4961 $ this ->createMock (IL10N ::class),
62+ $ this ->userManager ,
5063 );
5164 }
5265
@@ -138,6 +151,90 @@ public function testLoggedInUserWithValidTokenUnauthenticated(): void {
138151 $ this ->invokeMiddleware ($ share , $ user );
139152 }
140153
154+ public function testDocumentSessionWithEnabledUserAllowed (): void {
155+ $ session = new Session ();
156+ $ session ->setUserId ('alice ' );
157+
158+ $ user = $ this ->createMock (IUser::class);
159+ $ user ->method ('isEnabled ' )->willReturn (true );
160+
161+ $ this ->sessionService ->method ('getValidSession ' )->willReturn ($ session );
162+ $ this ->userManager ->method ('get ' )->with ('alice ' )->willReturn ($ user );
163+ $ this ->documentService ->method ('getDocument ' )->willReturn ($ this ->createMock (Document::class));
164+
165+ $ controller = $ this ->createMock (ISessionAwareController::class);
166+ $ controller ->expects ($ this ->once ())->method ('setUserId ' )->with ('alice ' );
167+
168+ $ this ->invokeAssertDocumentSession ($ controller );
169+ $ this ->assertTrue (true );
170+ }
171+
172+ public function testDocumentSessionWithDisabledUserBlocked (): void {
173+ $ this ->expectException (AccountDisabledException::class);
174+
175+ $ session = new Session ();
176+ $ session ->setUserId ('alice ' );
177+
178+ $ user = $ this ->createMock (IUser::class);
179+ $ user ->method ('isEnabled ' )->willReturn (false );
180+
181+ $ this ->sessionService ->method ('getValidSession ' )->willReturn ($ session );
182+ $ this ->userManager ->method ('get ' )->with ('alice ' )->willReturn ($ user );
183+
184+ $ controller = $ this ->createMock (ISessionAwareController::class);
185+ $ controller ->expects ($ this ->never ())->method ('setUserId ' );
186+
187+ $ this ->invokeAssertDocumentSession ($ controller );
188+ }
189+
190+ public function testDocumentSessionWithNonexistentUserBlocked (): void {
191+ $ this ->expectException (AccountDisabledException::class);
192+
193+ $ session = new Session ();
194+ $ session ->setUserId ('alice ' );
195+
196+ $ this ->sessionService ->method ('getValidSession ' )->willReturn ($ session );
197+ $ this ->userManager ->method ('get ' )->with ('alice ' )->willReturn (null );
198+
199+ $ controller = $ this ->createMock (ISessionAwareController::class);
200+ $ controller ->expects ($ this ->never ())->method ('setUserId ' );
201+
202+ $ this ->invokeAssertDocumentSession ($ controller );
203+ }
204+
205+ public function testDocumentSessionGuestSessionSkipsUserCheck (): void {
206+ $ session = new Session ();
207+
208+ $ this ->sessionService ->method ('getValidSession ' )->willReturn ($ session );
209+ $ this ->userManager ->expects ($ this ->never ())->method ('get ' );
210+ $ this ->documentService ->method ('getDocument ' )->willReturn ($ this ->createMock (Document::class));
211+
212+ $ controller = $ this ->createMock (ISessionAwareController::class);
213+
214+ $ this ->invokeAssertDocumentSession ($ controller , 'shareToken123 ' );
215+ $ this ->assertTrue (true );
216+ }
217+
218+ public function testAfterExceptionMapsAccountDisabledToForbidden (): void {
219+ $ controller = $ this ->createMock (ISessionAwareController::class);
220+
221+ $ response = $ this ->middleware ->afterException ($ controller , 'push ' , new AccountDisabledException ());
222+
223+ $ this ->assertInstanceOf (JSONResponse::class, $ response );
224+ $ this ->assertSame (Http::STATUS_FORBIDDEN , $ response ->getStatus ());
225+ }
226+
227+ private function invokeAssertDocumentSession (ISessionAwareController $ controller , ?string $ shareToken = null ): void {
228+ $ this ->request ->method ('getParam ' )->willReturnMap ([
229+ ['documentId ' , null , 999 ],
230+ ['sessionId ' , null , 1 ],
231+ ['sessionToken ' , null , 'sessionToken ' ],
232+ ['token ' , null , $ shareToken ],
233+ ]);
234+
235+ self ::invokePrivate ($ this ->middleware , 'assertDocumentSession ' , [$ controller ]);
236+ }
237+
141238 private function createPasswordProtectedShare (string $ id ): IShare {
142239 $ share = $ this ->createMock (IShare::class);
143240 $ share ->method ('getId ' )->willReturn ($ id );
0 commit comments