. */ namespace SP\Tests\Core\Crypt; use Klein\DataCollection\DataCollection; use Klein\Request; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use SP\Core\Crypt\Hash; use SP\Core\Crypt\UuidCookie; use SP\Domain\Core\Bootstrap\UriContextInterface; use SP\Domain\Http\Ports\RequestService; use SP\Tests\UnitaryTestCase; /** * Class UuidCookieTest * */ #[Group('unitary')] class UuidCookieTest extends UnitaryTestCase { private RequestService|MockObject $requestInterface; private UriContextInterface|MockObject $uriContext; /** * @throws Exception */ public function testLoad() { $key = self::$faker->sha1; $message = base64_encode('test'); $data = sprintf('%s;%s', Hash::signMessage($message, $key), $message); $cookies = $this->createMock(DataCollection::class); $cookies->expects(self::once()) ->method('get') ->with('SYSPASS_UUID', false) ->willReturn($data); $request = $this->createMock(Request::class); $request->expects(self::once()) ->method('cookies') ->willReturn($cookies); $this->requestInterface ->expects(self::once()) ->method('getRequest') ->willReturn($request); $cookie = UuidCookie::factory($this->requestInterface, $this->uriContext); self::assertEquals('test', $cookie->load($key)); } /** * @throws Exception */ public function testLoadWithNoData() { $key = self::$faker->sha1; $cookies = $this->createMock(DataCollection::class); $cookies->expects(self::once()) ->method('get') ->with('SYSPASS_UUID', false) ->willReturn(false); $request = $this->createMock(Request::class); $request->expects(self::once()) ->method('cookies') ->willReturn($cookies); $this->requestInterface ->expects(self::once()) ->method('getRequest') ->willReturn($request); $cookie = UuidCookie::factory($this->requestInterface, $this->uriContext); self::assertFalse($cookie->load($key)); } /** * @throws Exception */ public function testLoadWithInvalidData() { $key = self::$faker->sha1; $data = self::$faker->text; $cookies = $this->createMock(DataCollection::class); $cookies->expects(self::once()) ->method('get') ->with('SYSPASS_UUID', false) ->willReturn($data); $request = $this->createMock(Request::class); $request->expects(self::once()) ->method('cookies') ->willReturn($cookies); $this->requestInterface ->expects(self::once()) ->method('getRequest') ->willReturn($request); $cookie = UuidCookie::factory($this->requestInterface, $this->uriContext); self::assertFalse($cookie->load($key)); } /** * @throws Exception */ public function testLoadWithInvalidSignature() { $key = self::$faker->sha1; $data = sprintf('%s;%s', Hash::signMessage(base64_encode('invalid'), $key), base64_encode('test')); $cookies = $this->createMock(DataCollection::class); $cookies->expects(self::once()) ->method('get') ->with('SYSPASS_UUID', false) ->willReturn($data); $request = $this->createMock(Request::class); $request->expects(self::once()) ->method('cookies') ->willReturn($cookies); $this->requestInterface ->expects(self::once()) ->method('getRequest') ->willReturn($request); $cookie = UuidCookie::factory($this->requestInterface, $this->uriContext); self::assertFalse($cookie->load($key)); } public function testCreate() { $uuidCookie = UuidCookie::factory($this->requestInterface, $this->uriContext); $key = self::$faker->sha1; $cookie = $uuidCookie->create($key); self::assertNotEmpty($cookie); } public function testSign() { $key = self::$faker->sha1; $uuidCookie = UuidCookie::factory($this->requestInterface, $this->uriContext); $cookieData = $uuidCookie->sign('test', $key); $out = $uuidCookie->getCookieData($cookieData, $key); self::assertEquals('test', $out); } protected function setUp(): void { parent::setUp(); $this->requestInterface = $this->createMock(RequestService::class); $this->uriContext = $this->createMock(UriContextInterface::class); } }