diff options
author | Joas Schilling <coding@schilljs.com> | 2022-04-29 11:47:53 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-04-30 13:09:32 +0200 |
commit | e8cf4671684ea6f3d5762dce8d4f34da982d5fb2 (patch) | |
tree | 2a50e3f269376727f4d56d4c93a93a027fadaf8f /tests | |
parent | 01fb3a6c834d548916032a8441e563b8eb18e147 (diff) |
Move Room::verifyPassword() to the RoomService
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/php/Chat/SystemMessage/ListenerTest.php | 2 | ||||
-rw-r--r-- | tests/php/Controller/SignalingControllerTest.php | 8 | ||||
-rw-r--r-- | tests/php/RoomTest.php | 94 | ||||
-rw-r--r-- | tests/php/Service/RoomServiceTest.php | 77 | ||||
-rw-r--r-- | tests/php/Signaling/BackendNotifierTest.php | 35 |
5 files changed, 112 insertions, 104 deletions
diff --git a/tests/php/Chat/SystemMessage/ListenerTest.php b/tests/php/Chat/SystemMessage/ListenerTest.php index 448a676fb..8e53679bd 100644 --- a/tests/php/Chat/SystemMessage/ListenerTest.php +++ b/tests/php/Chat/SystemMessage/ListenerTest.php @@ -63,7 +63,7 @@ class ListenerTest extends TestCase { protected ?array $handlers = null; protected ?\DateTime $dummyTime = null; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->request = $this->createMock(IRequest::class); diff --git a/tests/php/Controller/SignalingControllerTest.php b/tests/php/Controller/SignalingControllerTest.php index 53234ce49..e5879629c 100644 --- a/tests/php/Controller/SignalingControllerTest.php +++ b/tests/php/Controller/SignalingControllerTest.php @@ -35,6 +35,7 @@ use OCA\Talk\Model\SessionMapper; use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; +use OCA\Talk\Service\RoomService; use OCA\Talk\Service\SessionService; use OCA\Talk\Signaling\Messages; use OCA\Talk\TalkSession; @@ -1059,9 +1060,12 @@ class SignalingControllerTest extends TestCase { ->willReturn($this->userId); $room = $this->manager->createRoom(Room::TYPE_PUBLIC); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); // The user joined the room. - $oldParticipant = $participantService->joinRoom($room, $testUser, ''); + $oldParticipant = $participantService->joinRoom($roomService, $room, $testUser, ''); $oldSessionId = $oldParticipant->getSession()->getSessionId(); $this->performBackendRequest([ 'type' => 'room', @@ -1077,7 +1081,7 @@ class SignalingControllerTest extends TestCase { // The user is reloading the browser which will join him with another // session id. - $newParticipant = $participantService->joinRoom($room, $testUser, ''); + $newParticipant = $participantService->joinRoom($roomService, $room, $testUser, ''); $newSessionId = $newParticipant->getSession()->getSessionId(); $this->performBackendRequest([ 'type' => 'room', diff --git a/tests/php/RoomTest.php b/tests/php/RoomTest.php deleted file mode 100644 index 07b00ccff..000000000 --- a/tests/php/RoomTest.php +++ /dev/null @@ -1,94 +0,0 @@ -<?php - -declare(strict_types=1); -/** - * @copyright Copyright (c) 2018 Peter Edens <petere@conceiva.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -namespace OCA\Talk\Tests\php; - -use OC\EventDispatcher\EventDispatcher; -use OCA\Talk\Events\VerifyRoomPasswordEvent; -use OCA\Talk\Manager; -use OCA\Talk\Model\Attendee; -use OCA\Talk\Participant; -use OCA\Talk\Room; -use OCA\Talk\Webinary; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IDBConnection; -use OCP\Security\IHasher; -use Psr\Log\LoggerInterface; -use Test\TestCase; - -class RoomTest extends TestCase { - public function testVerifyPassword() { - $dispatcher = new EventDispatcher( - new \Symfony\Component\EventDispatcher\EventDispatcher(), - \OC::$server, - $this->createMock(LoggerInterface::class) - ); - $dispatcher->addListener(Room::EVENT_PASSWORD_VERIFY, static function (VerifyRoomPasswordEvent $event) { - $password = $event->getPassword(); - - if ($password === '1234') { - $event->setIsPasswordValid(true); - $event->setRedirectUrl(''); - } else { - $event->setIsPasswordValid(false); - $event->setRedirectUrl('https://test'); - } - }); - - $room = new Room( - $this->createMock(Manager::class), - $this->createMock(IDBConnection::class), - $dispatcher, - $this->createMock(ITimeFactory::class), - $this->createMock(IHasher::class), - 1, - Room::TYPE_PUBLIC, - Room::READ_WRITE, - Room::LISTABLE_NONE, - Webinary::LOBBY_NONE, - 0, - null, - 'foobar', - 'Test', - 'description', - 'passy', - '', - '', - 0, - Attendee::PERMISSIONS_DEFAULT, - Attendee::PERMISSIONS_DEFAULT, - Participant::FLAG_DISCONNECTED, - null, - null, - 0, - null, - null, - '', - '' - ); - $verificationResult = $room->verifyPassword('1234'); - $this->assertSame($verificationResult, ['result' => true, 'url' => '']); - $verificationResult = $room->verifyPassword('4321'); - $this->assertSame($verificationResult, ['result' => false, 'url' => 'https://test']); - $this->assertSame('passy', $room->getPassword()); - } -} diff --git a/tests/php/Service/RoomServiceTest.php b/tests/php/Service/RoomServiceTest.php index faf324a78..342997543 100644 --- a/tests/php/Service/RoomServiceTest.php +++ b/tests/php/Service/RoomServiceTest.php @@ -24,16 +24,24 @@ declare(strict_types=1); namespace OCA\Talk\Tests\php\Service; use InvalidArgumentException; +use OC\EventDispatcher\EventDispatcher; +use OCA\Talk\Events\VerifyRoomPasswordEvent; use OCA\Talk\Exceptions\RoomNotFoundException; use OCA\Talk\Manager; +use OCA\Talk\Model\Attendee; use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; use OCA\Talk\Service\RoomService; +use OCA\Talk\Webinary; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IDBConnection; use OCP\IUser; +use OCP\Security\IHasher; use OCP\Share\IManager as IShareManager; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class RoomServiceTest extends TestCase { @@ -44,6 +52,8 @@ class RoomServiceTest extends TestCase { protected $participantService; /** @var IShareManager|MockObject */ protected $shareManager; + /** @var IHasher|MockObject */ + protected $hasher; /** @var IEventDispatcher|MockObject */ protected $dispatcher; private ?RoomService $service = null; @@ -55,11 +65,13 @@ class RoomServiceTest extends TestCase { $this->manager = $this->createMock(Manager::class); $this->participantService = $this->createMock(ParticipantService::class); $this->shareManager = $this->createMock(IShareManager::class); + $this->hasher = $this->createMock(IHasher::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->service = new RoomService( $this->manager, $this->participantService, $this->shareManager, + $this->hasher, $this->dispatcher ); } @@ -295,4 +307,69 @@ class RoomServiceTest extends TestCase { public function testPrepareConversationName(string $input, string $expected): void { $this->assertSame($expected, $this->service->prepareConversationName($input)); } + + public function testVerifyPassword(): void { + $dispatcher = new EventDispatcher( + new \Symfony\Component\EventDispatcher\EventDispatcher(), + \OC::$server, + $this->createMock(LoggerInterface::class) + ); + $dispatcher->addListener(Room::EVENT_PASSWORD_VERIFY, static function (VerifyRoomPasswordEvent $event) { + $password = $event->getPassword(); + + if ($password === '1234') { + $event->setIsPasswordValid(true); + $event->setRedirectUrl(''); + } else { + $event->setIsPasswordValid(false); + $event->setRedirectUrl('https://test'); + } + }); + + $service = new RoomService( + $this->manager, + $this->participantService, + $this->shareManager, + $this->hasher, + $dispatcher + ); + + $room = new Room( + $this->createMock(Manager::class), + $this->createMock(IDBConnection::class), + $dispatcher, + $this->createMock(ITimeFactory::class), + $this->createMock(IHasher::class), + 1, + Room::TYPE_PUBLIC, + Room::READ_WRITE, + Room::LISTABLE_NONE, + Webinary::LOBBY_NONE, + 0, + null, + 'foobar', + 'Test', + 'description', + 'passy', + '', + '', + 0, + Attendee::PERMISSIONS_DEFAULT, + Attendee::PERMISSIONS_DEFAULT, + Participant::FLAG_DISCONNECTED, + null, + null, + 0, + null, + null, + '', + '' + ); + + $verificationResult = $service->verifyPassword($room, '1234'); + $this->assertSame($verificationResult, ['result' => true, 'url' => '']); + $verificationResult = $service->verifyPassword($room, '4321'); + $this->assertSame($verificationResult, ['result' => false, 'url' => 'https://test']); + $this->assertSame('passy', $room->getPassword()); + } } diff --git a/tests/php/Signaling/BackendNotifierTest.php b/tests/php/Signaling/BackendNotifierTest.php index 3bb2fc386..e0c4c1cdb 100644 --- a/tests/php/Signaling/BackendNotifierTest.php +++ b/tests/php/Signaling/BackendNotifierTest.php @@ -33,6 +33,7 @@ use OCA\Talk\Model\SessionMapper; use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; +use OCA\Talk\Service\RoomService; use OCA\Talk\Signaling\BackendNotifier; use OCA\Talk\TalkSession; use OCA\Talk\Webinary; @@ -312,12 +313,16 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + $room = $this->manager->createRoom(Room::TYPE_PUBLIC); $this->participantService->addUsers($room, [[ 'actorType' => 'users', 'actorId' => $this->userId, ]]); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $this->controller->clearRequests(); $this->participantService->leaveRoomAsSession($room, $participant); @@ -331,8 +336,12 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + $room = $this->manager->createRoom(Room::TYPE_PUBLIC); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $this->controller->clearRequests(); $this->participantService->leaveRoomAsSession($room, $participant); @@ -360,8 +369,12 @@ class BackendNotifierTest extends TestCase { } public function testRoomDisinviteOnLeaveOfGuest() { + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + $room = $this->manager->createRoom(Room::TYPE_PUBLIC); - $participant = $this->participantService->joinRoomAsNewGuest($room, ''); + $participant = $this->participantService->joinRoomAsNewGuest($roomService, $room, ''); $this->controller->clearRequests(); $this->participantService->leaveRoomAsSession($room, $participant); @@ -587,7 +600,11 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $userSession = $participant->getSession()->getSessionId(); $participant = $room->getParticipantBySession($userSession); @@ -624,7 +641,7 @@ class BackendNotifierTest extends TestCase { $this->controller->clearRequests(); - $guestParticipant = $this->participantService->joinRoomAsNewGuest($room, ''); + $guestParticipant = $this->participantService->joinRoomAsNewGuest($roomService, $room, ''); $guestSession = $guestParticipant->getSession()->getSessionId(); $guestParticipant = $room->getParticipantBySession($guestSession); $this->participantService->changeInCall($room, $guestParticipant, Participant::FLAG_IN_CALL); @@ -745,7 +762,11 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $userSession = $participant->getSession()->getSessionId(); $participant = $room->getParticipantBySession($userSession); @@ -780,7 +801,7 @@ class BackendNotifierTest extends TestCase { $this->controller->clearRequests(); - $guestParticipant = $this->participantService->joinRoomAsNewGuest($room, ''); + $guestParticipant = $this->participantService->joinRoomAsNewGuest($roomService, $room, ''); $guestSession = $guestParticipant->getSession()->getSessionId(); $guestParticipant = $room->getParticipantBySession($guestSession); |