fix: Adjust and add new tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2026-01-12 11:07:44 +01:00
parent 80f6856ce7
commit f4acd8a7ab
No known key found for this signature in database
GPG key ID: F72FA5B49FFA96B0
5 changed files with 115 additions and 22 deletions

View file

@ -19,6 +19,8 @@ use OCA\Settings\Controller\AuthSettingsController;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserSession;
@ -35,7 +37,9 @@ class AuthSettingsControllerTest extends TestCase {
private IUserSession&MockObject $userSession;
private ISecureRandom&MockObject $secureRandom;
private IManager&MockObject $activityManager;
private IAppConfig&MockObject $appConfig;
private RemoteWipe&MockObject $remoteWipe;
private IConfig&MockObject $serverConfig;
private string $uid = 'jane';
private AuthSettingsController $controller;
@ -48,7 +52,9 @@ class AuthSettingsControllerTest extends TestCase {
$this->userSession = $this->createMock(IUserSession::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->activityManager = $this->createMock(IManager::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->remoteWipe = $this->createMock(RemoteWipe::class);
$this->serverConfig = $this->createMock(IConfig::class);
/** @var LoggerInterface&MockObject $logger */
$logger = $this->createMock(LoggerInterface::class);
@ -61,8 +67,10 @@ class AuthSettingsControllerTest extends TestCase {
$this->uid,
$this->userSession,
$this->activityManager,
$this->appConfig,
$this->remoteWipe,
$logger
$logger,
$this->serverConfig,
);
}
@ -72,6 +80,9 @@ class AuthSettingsControllerTest extends TestCase {
$deviceToken = $this->createMock(IToken::class);
$password = '123456';
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$this->session->expects($this->once())
->method('getId')
->willReturn('sessionid');
@ -115,6 +126,30 @@ class AuthSettingsControllerTest extends TestCase {
$this->assertEquals($expected, $response->getData());
}
public function testCreateDisabledBySystemConfig(): void {
$name = 'Nexus 4';
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(false);
$this->session->expects($this->once())
->method('getId')
->willReturn('sessionid');
$this->tokenProvider->expects($this->never())
->method('getToken');
$this->tokenProvider->expects($this->never())
->method('getPassword');
$this->tokenProvider->expects($this->never())
->method('generateToken');
$expected = new JSONResponse();
$expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
$this->assertEquals($expected, $this->controller->create($name));
}
public function testCreateSessionNotAvailable(): void {
$name = 'personal phone';
@ -131,6 +166,9 @@ class AuthSettingsControllerTest extends TestCase {
public function testCreateInvalidToken(): void {
$name = 'Company IPhone';
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$this->session->expects($this->once())
->method('getId')
->willReturn('sessionid');

View file

@ -14,6 +14,7 @@ use OCA\Settings\Settings\Personal\Security\Authtokens;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Authentication\Token\IToken;
use OCP\IConfig;
use OCP\ISession;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
@ -24,6 +25,7 @@ class AuthtokensTest extends TestCase {
private ISession&MockObject $session;
private IUserSession&MockObject $userSession;
private IInitialState&MockObject $initialState;
private IConfig&MockObject $serverConfig;
private string $uid;
private Authtokens $section;
@ -34,6 +36,7 @@ class AuthtokensTest extends TestCase {
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->initialState = $this->createMock(IInitialState::class);
$this->serverConfig = $this->createMock(IConfig::class);
$this->uid = 'test123';
$this->section = new Authtokens(
@ -41,7 +44,8 @@ class AuthtokensTest extends TestCase {
$this->session,
$this->userSession,
$this->initialState,
$this->uid
$this->serverConfig,
$this->uid,
);
}
@ -57,6 +61,9 @@ class AuthtokensTest extends TestCase {
$sessionToken = new PublicKeyToken();
$sessionToken->setId(100);
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$this->authTokenProvider->expects($this->once())
->method('getTokenByUser')
->with($this->uid)

View file

@ -24,7 +24,6 @@ use OCP\AppFramework\Db\TTransactional;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Token\IToken as OCPIToken;
use OCP\EventDispatcher\GenericEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\NotPermittedException;

View file

@ -20,6 +20,7 @@ use OCP\Authentication\Exceptions\PasswordUnavailableException;
use OCP\Authentication\LoginCredentials\ICredentials;
use OCP\Authentication\LoginCredentials\IStore;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserManager;
@ -55,6 +56,7 @@ class AppPasswordControllerTest extends TestCase {
/** @var IThrottler|MockObject */
private $throttler;
private IConfig&MockObject $serverConfig;
/** @var AppPasswordController */
private $controller;
@ -71,6 +73,7 @@ class AppPasswordControllerTest extends TestCase {
$this->userSession = $this->createMock(Session::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->throttler = $this->createMock(IThrottler::class);
$this->serverConfig = $this->createMock(IConfig::class);
$this->controller = new AppPasswordController(
'core',
@ -82,33 +85,56 @@ class AppPasswordControllerTest extends TestCase {
$this->eventDispatcher,
$this->userSession,
$this->userManager,
$this->throttler
$this->throttler,
$this->serverConfig,
);
}
public function testGetAppPasswordWithAppPassword(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$this->session->method('exists')
->with('app_password')
->willReturn(true);
$this->tokenProvider->expects($this->never())
->method('generateToken');
$this->eventDispatcher->expects($this->never())
->method('dispatchTyped');
$this->expectException(OCSForbiddenException::class);
$this->controller->getAppPassword();
}
public function testGetAppPasswordNoLoginCreds(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$this->session->method('exists')
->with('app_password')
->willReturn(false);
$this->credentialStore->method('getLoginCredentials')
->willThrowException(new CredentialsUnavailableException());
$this->tokenProvider->expects($this->never())
->method('generateToken');
$this->eventDispatcher->expects($this->never())
->method('dispatchTyped');
$this->expectException(OCSForbiddenException::class);
$this->controller->getAppPassword();
}
public function testGetAppPassword(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$credentials = $this->createMock(ICredentials::class);
$this->session->method('exists')
@ -150,6 +176,10 @@ class AppPasswordControllerTest extends TestCase {
}
public function testGetAppPasswordNoPassword(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$credentials = $this->createMock(ICredentials::class);
$this->session->method('exists')
@ -190,6 +220,22 @@ class AppPasswordControllerTest extends TestCase {
$this->controller->getAppPassword();
}
public function testGetAppPasswordDisabledBySystemConfig(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(false);
$this->tokenProvider->expects($this->never())
->method('generateToken');
$this->eventDispatcher->expects($this->never())
->method('dispatchTyped');
$this->expectException(OCSForbiddenException::class);
$this->controller->getAppPassword();
}
public function testDeleteAppPasswordNoAppPassword(): void {
$this->session->method('exists')
->with('app_password')

View file

@ -485,12 +485,13 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->onlyMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->onlyMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$userSession->expects($this->once())
->method('isTokenPassword')
->willReturn(true);
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('I-AM-AN-APP-PASSWORD')
->willReturn($this->createMock(IToken::class));
$userSession->expects($this->once())
->method('login')
->with('john', 'I-AM-AN-APP-PASSWORD')
@ -1234,15 +1235,16 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->onlyMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->onlyMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$userSession->expects($this->once())
->method('isTokenPassword')
->willReturn(true);
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('I-AM-A-PASSWORD')
->willReturn($this->createMock(IToken::class));
$userSession->expects($this->once())
->method('login')
->with('john', 'I-AM-AN-PASSWORD')
->with('john', 'I-AM-A-PASSWORD')
->willReturn(false);
$session->expects($this->never())
@ -1267,9 +1269,9 @@ class SessionTest extends \Test\TestCase {
$this->dispatcher
->expects($this->once())
->method('dispatchTyped')
->with(new LoginFailed('john', 'I-AM-AN-PASSWORD'));
->with(new LoginFailed('john', 'I-AM-A-PASSWORD'));
$this->assertFalse($userSession->logClientIn('john', 'I-AM-AN-PASSWORD', $request, $this->throttler));
$this->assertFalse($userSession->logClientIn('john', 'I-AM-A-PASSWORD', $request, $this->throttler));
}
public function testLogClientInThrottlerEmail(): void {
@ -1280,15 +1282,16 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->onlyMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->onlyMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$userSession->expects($this->once())
->method('isTokenPassword')
->willReturn(false);
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('I-AM-A-PASSWORD')
->willThrowException(new InvalidTokenException());
$userSession->expects($this->once())
->method('login')
->with('john@foo.bar', 'I-AM-AN-PASSWORD')
->with('john@foo.bar', 'I-AM-A-PASSWORD')
->willReturn(false);
$manager
->method('getByEmail')
@ -1317,8 +1320,8 @@ class SessionTest extends \Test\TestCase {
$this->dispatcher
->expects($this->once())
->method('dispatchTyped')
->with(new LoginFailed('john@foo.bar', 'I-AM-AN-PASSWORD'));
->with(new LoginFailed('john@foo.bar', 'I-AM-A-PASSWORD'));
$this->assertFalse($userSession->logClientIn('john@foo.bar', 'I-AM-AN-PASSWORD', $request, $this->throttler));
$this->assertFalse($userSession->logClientIn('john@foo.bar', 'I-AM-A-PASSWORD', $request, $this->throttler));
}
}