Skip to content

Commit 3f61c50

Browse files
committed
test: test doTryTokenLogin method
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
1 parent dd05f6a commit 3f61c50

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tests/lib/User/SessionTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,4 +1336,136 @@ public function testLogClientInThrottlerEmail(): void {
13361336

13371337
$this->assertFalse($userSession->logClientIn('john@foo.bar', 'I-AM-A-PASSWORD', $request, $this->throttler));
13381338
}
1339+
1340+
public function testDoTryTokenLoginSuccess(): void {
1341+
$manager = $this->createMock(Manager::class);
1342+
$session = $this->createMock(ISession::class);
1343+
1344+
$user = $this->createMock(IUser::class);
1345+
$user->method('getUID')->willReturn('testuser');
1346+
$user->method('isEnabled')->willReturn(true);
1347+
1348+
$manager->method('get')
1349+
->with('testuser')
1350+
->willReturn($user);
1351+
1352+
$token = $this->createMock(PublicKeyToken::class);
1353+
$token->method('getUID')->willReturn('testuser');
1354+
$token->method('getLoginName')->willReturn('testuser');
1355+
$token->method('getType')->willReturn(\OCP\Authentication\Token\IToken::PERMANENT_TOKEN);
1356+
$token->method('getLastCheck')->willReturn($this->timeFactory->getTime());
1357+
1358+
$this->tokenProvider->method('getToken')
1359+
->with('valid-token')
1360+
->willReturn($token);
1361+
1362+
$appPasswordSet = false;
1363+
$session->expects($this->atLeastOnce())
1364+
->method('set')
1365+
->willReturnCallback(function ($key, $value) use (&$appPasswordSet) {
1366+
// We expect app_password to be set for permanent tokens
1367+
if ($key === 'app_password') {
1368+
$appPasswordSet = true;
1369+
$this->assertEquals('valid-token', $value);
1370+
}
1371+
return true;
1372+
});
1373+
1374+
/** @var Session $userSession */
1375+
$userSession = $this->getMockBuilder(Session::class)
1376+
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1377+
->onlyMethods(['setMagicInCookie'])
1378+
->getMock();
1379+
1380+
$this->assertTrue($userSession->doTryTokenLogin('valid-token'));
1381+
$this->assertTrue($appPasswordSet, 'app_password should be set for permanent tokens');
1382+
}
1383+
1384+
public function testDoTryTokenLoginInvalidToken(): void {
1385+
$manager = $this->createMock(Manager::class);
1386+
$session = $this->createMock(ISession::class);
1387+
1388+
$this->tokenProvider->method('getToken')
1389+
->with('invalid-token')
1390+
->willThrowException(new InvalidTokenException());
1391+
1392+
/** @var Session $userSession */
1393+
$userSession = $this->getMockBuilder(Session::class)
1394+
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1395+
->onlyMethods(['setMagicInCookie'])
1396+
->getMock();
1397+
1398+
$this->assertFalse($userSession->doTryTokenLogin('invalid-token'));
1399+
}
1400+
1401+
public function testDoTryTokenLoginTemporaryToken(): void {
1402+
$manager = $this->createMock(Manager::class);
1403+
$session = $this->createMock(ISession::class);
1404+
1405+
$user = $this->createMock(IUser::class);
1406+
$user->method('getUID')->willReturn('testuser');
1407+
$user->method('isEnabled')->willReturn(true);
1408+
1409+
$manager->method('get')
1410+
->with('testuser')
1411+
->willReturn($user);
1412+
1413+
$token = $this->createMock(PublicKeyToken::class);
1414+
$token->method('getUID')->willReturn('testuser');
1415+
$token->method('getLoginName')->willReturn('testuser');
1416+
$token->method('getType')->willReturn(\OCP\Authentication\Token\IToken::TEMPORARY_TOKEN);
1417+
$token->method('getLastCheck')->willReturn($this->timeFactory->getTime());
1418+
1419+
$this->tokenProvider->method('getToken')
1420+
->with('temp-token')
1421+
->willReturn($token);
1422+
1423+
// app_password should NOT be set for temporary tokens
1424+
$session->expects($this->atLeastOnce())
1425+
->method('set')
1426+
->willReturnCallback(function ($key, $value) {
1427+
$this->assertNotEquals('app_password', $key, 'app_password should not be set for temporary tokens');
1428+
return true;
1429+
});
1430+
1431+
/** @var Session $userSession */
1432+
$userSession = $this->getMockBuilder(Session::class)
1433+
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1434+
->onlyMethods(['setMagicInCookie'])
1435+
->getMock();
1436+
1437+
$this->assertTrue($userSession->doTryTokenLogin('temp-token'));
1438+
}
1439+
1440+
public function testDoTryTokenLoginDisabledUser(): void {
1441+
$manager = $this->createMock(Manager::class);
1442+
$session = $this->createMock(ISession::class);
1443+
1444+
$user = $this->createMock(IUser::class);
1445+
$user->method('getUID')->willReturn('testuser');
1446+
$user->method('isEnabled')->willReturn(false);
1447+
1448+
$manager->method('get')
1449+
->with('testuser')
1450+
->willReturn($user);
1451+
1452+
$token = $this->createMock(PublicKeyToken::class);
1453+
$token->method('getUID')->willReturn('testuser');
1454+
$token->method('getLoginName')->willReturn('testuser');
1455+
$token->method('getType')->willReturn(\OCP\Authentication\Token\IToken::PERMANENT_TOKEN);
1456+
$token->method('getLastCheck')->willReturn($this->timeFactory->getTime());
1457+
1458+
$this->tokenProvider->method('getToken')
1459+
->with('valid-token')
1460+
->willReturn($token);
1461+
1462+
/** @var Session $userSession */
1463+
$userSession = $this->getMockBuilder(Session::class)
1464+
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1465+
->onlyMethods(['setMagicInCookie'])
1466+
->getMock();
1467+
1468+
$this->expectException(LoginException::class);
1469+
$userSession->doTryTokenLogin('valid-token');
1470+
}
13391471
}

0 commit comments

Comments
 (0)