diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index bb529320e07..a02f132fdd7 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -115,7 +115,7 @@ public function createSessionForAttendee(Attendee $attendee, string $forceSessio break; } catch (Exception $e) { // 255 chars are not unique? Try again... - if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { throw $e; } } diff --git a/tests/php/Service/SessionServiceTest.php b/tests/php/Service/SessionServiceTest.php new file mode 100644 index 00000000000..7e89e22033a --- /dev/null +++ b/tests/php/Service/SessionServiceTest.php @@ -0,0 +1,122 @@ +sessionMapper = \OCP\Server::get(SessionMapper::class); + $this->secureRandom = $this->createMock(ISecureRandom::class); + $this->service = new SessionService( + $this->sessionMapper, + \OCP\Server::get(IDBConnection::class), + $this->secureRandom, + ); + } + + public function tearDown(): void { + foreach ($this->attendeeIds as $attendeeId) { + try { + $this->sessionMapper->deleteByAttendeeId($attendeeId); + } catch (DoesNotExistException $exception) { + } + } + + parent::tearDown(); + } + + public function testCreateSessionForAttendee() { + $attendee = new Attendee(); + $attendee->setId(42); + $attendee->setActorType(Attendee::ACTOR_USERS); + $attendee->setActorId('test'); + $this->attendeeIds[] = $attendee->getId(); + + $random = self::RANDOM_254 . 'x'; + + $this->secureRandom->expects($this->once()) + ->method('generate') + ->with(255) + ->willReturn($random); + + $session = $this->service->createSessionForAttendee($attendee); + + self::assertEquals($random, $session->getSessionId()); + } + + public function testCreateSessionForAttendeeWithDuplicatedSessionId() { + $attendee1 = new Attendee(); + $attendee1->setId(42); + $attendee1->setActorType(Attendee::ACTOR_USERS); + $attendee1->setActorId('test1'); + $this->attendeeIds[] = $attendee1->getId(); + + $attendee2 = new Attendee(); + $attendee2->setId(108); + $attendee2->setActorType(Attendee::ACTOR_USERS); + $attendee2->setActorId('test2'); + $this->attendeeIds[] = $attendee2->getId(); + + $random1 = self::RANDOM_254 . 'x'; + $random2 = self::RANDOM_254 . 'y'; + + $this->secureRandom->expects($this->exactly(3)) + ->method('generate') + ->with(255) + ->willReturn( + $random1, + $random1, + $random2, + ); + + $session1 = $this->service->createSessionForAttendee($attendee1); + $session2 = $this->service->createSessionForAttendee($attendee2); + + self::assertEquals($random1, $session1->getSessionId()); + self::assertEquals($random2, $session2->getSessionId()); + } + + public function testCreateSessionForAttendeeWithoutId() { + $attendee = new Attendee(); + $attendee->setActorType(Attendee::ACTOR_USERS); + $attendee->setActorId('test'); + + $random = self::RANDOM_254 . 'x'; + + $this->secureRandom->expects($this->once()) + ->method('generate') + ->with(255) + ->willReturn($random); + + $this->expectException(\OC\DB\Exceptions\DbalException::class); + + $session = $this->service->createSessionForAttendee($attendee); + } +}