|
13 | 13 | use OCA\Libresign\Db\FileElement; |
14 | 14 | use OCA\Libresign\Db\FileElementMapper; |
15 | 15 | use OCA\Libresign\Db\FileMapper; |
| 16 | +use OCA\Libresign\Db\IdentifyMethod; |
16 | 17 | use OCA\Libresign\Db\IdentifyMethodMapper; |
17 | 18 | use OCA\Libresign\Db\SignRequest; |
18 | 19 | use OCA\Libresign\Db\SignRequestMapper; |
| 20 | +use OCA\Libresign\Exception\LibresignException; |
19 | 21 | use OCA\Libresign\Handler\DocMdpHandler; |
20 | 22 | use OCA\Libresign\Helper\FileUploadHelper; |
21 | 23 | use OCA\Libresign\Helper\ValidateHelper; |
@@ -299,23 +301,267 @@ public function testValidateSuccess():void { |
299 | 301 | 'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))], |
300 | 302 | 'name' => 'test', |
301 | 303 | 'signers' => [ |
302 | | - ['identify' => ['email' => 'jhondoe@test.coop']] |
| 304 | + ['identifyMethods' => [['method' => 'email', 'value' => 'jhondoe@test.coop']]] |
303 | 305 | ], |
304 | 306 | 'userManager' => $this->user |
305 | 307 | ]); |
306 | 308 | $this->assertNull($actual); |
307 | 309 | } |
308 | 310 |
|
309 | | - public function testValidateSignersAllowsValidSignerPayload(): void { |
| 311 | + public function testValidateSignersAllowsIdentifyMethodsPayload(): void { |
310 | 312 | $service = $this->getService(); |
311 | 313 | $service->validateSigners([ |
312 | 314 | 'signers' => [ |
313 | | - ['identify' => ['email' => 'test@example.com']], |
| 315 | + [ |
| 316 | + 'identifyMethods' => [ |
| 317 | + ['method' => 'email', 'value' => 'test@example.com'], |
| 318 | + ], |
| 319 | + ], |
314 | 320 | ], |
315 | 321 | ]); |
316 | 322 | $this->addToAssertionCount(1); |
317 | 323 | } |
318 | 324 |
|
| 325 | + public function testValidateSignersRejectsLegacyIdentifyPayload(): void { |
| 326 | + $this->expectExceptionMessage('No identify methods for signer'); |
| 327 | + $this->validateHelper |
| 328 | + ->method('validateIdentifySigners') |
| 329 | + ->willThrowException(new LibresignException('No identify methods for signer')); |
| 330 | + |
| 331 | + $service = $this->getService(); |
| 332 | + $service->validateSigners([ |
| 333 | + 'signers' => [ |
| 334 | + ['identify' => ['email' => 'test@example.com']], |
| 335 | + ], |
| 336 | + ]); |
| 337 | + } |
| 338 | + |
| 339 | + public function testAssociateToSignersCreatesSignRequestsUsingIdentifyMethods(): void { |
| 340 | + $file = new \OCA\Libresign\Db\File(); |
| 341 | + $file->setId(77); |
| 342 | + |
| 343 | + $data = [ |
| 344 | + 'status' => 9, |
| 345 | + 'signers' => [[ |
| 346 | + 'displayName' => 'John Doe', |
| 347 | + 'description' => 'Needs review', |
| 348 | + 'notify' => 0, |
| 349 | + 'status' => 4, |
| 350 | + 'signingOrder' => 3, |
| 351 | + 'identifyMethods' => [ |
| 352 | + ['method' => 'email', 'value' => 'john@example.com'], |
| 353 | + ['method' => 'account', 'value' => 'john'], |
| 354 | + ], |
| 355 | + ]], |
| 356 | + ]; |
| 357 | + |
| 358 | + $this->validateHelper |
| 359 | + ->method('normalizeRequestSigners') |
| 360 | + ->willReturnCallback(static fn (array $signers): array => $signers); |
| 361 | + |
| 362 | + $this->signRequestMapper |
| 363 | + ->method('getByFileId') |
| 364 | + ->with(77) |
| 365 | + ->willReturn([]); |
| 366 | + |
| 367 | + $this->identifyMethodService |
| 368 | + ->expects($this->once()) |
| 369 | + ->method('clearCache'); |
| 370 | + |
| 371 | + $this->sequentialSigningService |
| 372 | + ->expects($this->once()) |
| 373 | + ->method('resetOrderCounter'); |
| 374 | + |
| 375 | + $this->sequentialSigningService |
| 376 | + ->expects($this->once()) |
| 377 | + ->method('determineSigningOrder') |
| 378 | + ->with(3) |
| 379 | + ->willReturn(3); |
| 380 | + |
| 381 | + $expectedCalls = [ |
| 382 | + [['email' => 'john@example.com'], 'John Doe', 'Needs review', false, 77, 3, 9, 4], |
| 383 | + [['account' => 'john'], 'John Doe', 'Needs review', false, 77, 3, 9, 4], |
| 384 | + ]; |
| 385 | + |
| 386 | + $this->signRequestService |
| 387 | + ->expects($this->exactly(2)) |
| 388 | + ->method('createOrUpdateSignRequest') |
| 389 | + ->willReturnCallback(function ( |
| 390 | + array $identifyMethods, |
| 391 | + string $displayName, |
| 392 | + string $description, |
| 393 | + bool $notify, |
| 394 | + int $fileId, |
| 395 | + int $signingOrder, |
| 396 | + ?int $fileStatus, |
| 397 | + ?int $signerStatus, |
| 398 | + ) use (&$expectedCalls): SignRequest { |
| 399 | + $expectedCall = array_shift($expectedCalls); |
| 400 | + $this->assertNotNull($expectedCall); |
| 401 | + [$expectedIdentifyMethods, $expectedDisplayName, $expectedDescription, $expectedNotify, $expectedFileId, $expectedSigningOrder, $expectedFileStatus, $expectedSignerStatus] = $expectedCall; |
| 402 | + $this->assertSame($expectedIdentifyMethods, $identifyMethods); |
| 403 | + $this->assertSame($expectedDisplayName, $displayName); |
| 404 | + $this->assertSame($expectedDescription, $description); |
| 405 | + $this->assertSame($expectedNotify, $notify); |
| 406 | + $this->assertSame($expectedFileId, $fileId); |
| 407 | + $this->assertSame($expectedSigningOrder, $signingOrder); |
| 408 | + $this->assertSame($expectedFileStatus, $fileStatus); |
| 409 | + $this->assertSame($expectedSignerStatus, $signerStatus); |
| 410 | + |
| 411 | + return new SignRequest(); |
| 412 | + }); |
| 413 | + |
| 414 | + $actual = self::invokePrivate($this->getService(), 'associateToSigners', [$data, $file]); |
| 415 | + |
| 416 | + $this->assertCount(2, $actual); |
| 417 | + $this->assertSame([], $expectedCalls); |
| 418 | + } |
| 419 | + |
| 420 | + public function testDeleteIdentifyMethodIfNotExitsKeepsMatchingIdentifyMethods(): void { |
| 421 | + $file = new \OCA\Libresign\Db\File(); |
| 422 | + $file->setId(77); |
| 423 | + |
| 424 | + $signRequest = new SignRequest(); |
| 425 | + $signRequest->setId(501); |
| 426 | + |
| 427 | + $entity = new IdentifyMethod(); |
| 428 | + $entity->setIdentifierKey('email'); |
| 429 | + $entity->setIdentifierValue('john@example.com'); |
| 430 | + |
| 431 | + $identifyMethod = $this->createMock(\OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod::class); |
| 432 | + $identifyMethod->method('getEntity')->willReturn($entity); |
| 433 | + |
| 434 | + $this->validateHelper |
| 435 | + ->expects($this->once()) |
| 436 | + ->method('normalizeRequestSigners') |
| 437 | + ->with([['identifyMethods' => [['method' => 'email', 'value' => 'john@example.com']]]]) |
| 438 | + ->willReturn([['identifyMethods' => [['method' => 'email', 'value' => 'john@example.com']]]]); |
| 439 | + |
| 440 | + $this->signRequestMapper |
| 441 | + ->expects($this->once()) |
| 442 | + ->method('getByFileId') |
| 443 | + ->with(77) |
| 444 | + ->willReturn([$signRequest]); |
| 445 | + |
| 446 | + $this->identifyMethodService |
| 447 | + ->expects($this->once()) |
| 448 | + ->method('getIdentifyMethodsFromSignRequestId') |
| 449 | + ->with(501) |
| 450 | + ->willReturn(['email' => [$identifyMethod]]); |
| 451 | + |
| 452 | + $service = $this->getMockBuilder(RequestSignatureService::class) |
| 453 | + ->setConstructorArgs([ |
| 454 | + $this->fileService, |
| 455 | + $this->l10n, |
| 456 | + $this->identifyMethodService, |
| 457 | + $this->signRequestMapper, |
| 458 | + $this->userManager, |
| 459 | + $this->fileMapper, |
| 460 | + $this->identifyMethodMapper, |
| 461 | + $this->pdfMetadataExtractor, |
| 462 | + $this->fileElementService, |
| 463 | + $this->fileElementMapper, |
| 464 | + $this->folderService, |
| 465 | + $this->mimeTypeDetector, |
| 466 | + $this->validateHelper, |
| 467 | + $this->client, |
| 468 | + $this->docMdpHandler, |
| 469 | + $this->loggerInterface, |
| 470 | + $this->sequentialSigningService, |
| 471 | + $this->appConfig, |
| 472 | + $this->eventDispatcher, |
| 473 | + $this->fileStatusService, |
| 474 | + $this->docMdpConfigService, |
| 475 | + $this->envelopeService, |
| 476 | + $this->envelopeFileRelocator, |
| 477 | + $this->uploadHelper, |
| 478 | + $this->signRequestService, |
| 479 | + ]) |
| 480 | + ->onlyMethods(['unassociateToUser']) |
| 481 | + ->getMock(); |
| 482 | + |
| 483 | + $service->expects($this->never()) |
| 484 | + ->method('unassociateToUser'); |
| 485 | + |
| 486 | + self::invokePrivate($service, 'deleteIdentifyMethodIfNotExits', [ |
| 487 | + [['identifyMethods' => [['method' => 'email', 'value' => 'john@example.com']]]], |
| 488 | + $file, |
| 489 | + ]); |
| 490 | + } |
| 491 | + |
| 492 | + public function testDeleteIdentifyMethodIfNotExitsRemovesMissingIdentifyMethods(): void { |
| 493 | + $file = new \OCA\Libresign\Db\File(); |
| 494 | + $file->setId(77); |
| 495 | + |
| 496 | + $signRequest = new SignRequest(); |
| 497 | + $signRequest->setId(501); |
| 498 | + |
| 499 | + $entity = new IdentifyMethod(); |
| 500 | + $entity->setIdentifierKey('email'); |
| 501 | + $entity->setIdentifierValue('old@example.com'); |
| 502 | + |
| 503 | + $identifyMethod = $this->createMock(\OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod::class); |
| 504 | + $identifyMethod->method('getEntity')->willReturn($entity); |
| 505 | + |
| 506 | + $this->validateHelper |
| 507 | + ->expects($this->once()) |
| 508 | + ->method('normalizeRequestSigners') |
| 509 | + ->with([['identifyMethods' => [['method' => 'email', 'value' => 'john@example.com']]]]) |
| 510 | + ->willReturn([['identifyMethods' => [['method' => 'email', 'value' => 'john@example.com']]]]); |
| 511 | + |
| 512 | + $this->signRequestMapper |
| 513 | + ->expects($this->once()) |
| 514 | + ->method('getByFileId') |
| 515 | + ->with(77) |
| 516 | + ->willReturn([$signRequest]); |
| 517 | + |
| 518 | + $this->identifyMethodService |
| 519 | + ->expects($this->once()) |
| 520 | + ->method('getIdentifyMethodsFromSignRequestId') |
| 521 | + ->with(501) |
| 522 | + ->willReturn(['email' => [$identifyMethod]]); |
| 523 | + |
| 524 | + $service = $this->getMockBuilder(RequestSignatureService::class) |
| 525 | + ->setConstructorArgs([ |
| 526 | + $this->fileService, |
| 527 | + $this->l10n, |
| 528 | + $this->identifyMethodService, |
| 529 | + $this->signRequestMapper, |
| 530 | + $this->userManager, |
| 531 | + $this->fileMapper, |
| 532 | + $this->identifyMethodMapper, |
| 533 | + $this->pdfMetadataExtractor, |
| 534 | + $this->fileElementService, |
| 535 | + $this->fileElementMapper, |
| 536 | + $this->folderService, |
| 537 | + $this->mimeTypeDetector, |
| 538 | + $this->validateHelper, |
| 539 | + $this->client, |
| 540 | + $this->docMdpHandler, |
| 541 | + $this->loggerInterface, |
| 542 | + $this->sequentialSigningService, |
| 543 | + $this->appConfig, |
| 544 | + $this->eventDispatcher, |
| 545 | + $this->fileStatusService, |
| 546 | + $this->docMdpConfigService, |
| 547 | + $this->envelopeService, |
| 548 | + $this->envelopeFileRelocator, |
| 549 | + $this->uploadHelper, |
| 550 | + $this->signRequestService, |
| 551 | + ]) |
| 552 | + ->onlyMethods(['unassociateToUser']) |
| 553 | + ->getMock(); |
| 554 | + |
| 555 | + $service->expects($this->once()) |
| 556 | + ->method('unassociateToUser') |
| 557 | + ->with(77, 501); |
| 558 | + |
| 559 | + self::invokePrivate($service, 'deleteIdentifyMethodIfNotExits', [ |
| 560 | + [['identifyMethods' => [['method' => 'email', 'value' => 'john@example.com']]]], |
| 561 | + $file, |
| 562 | + ]); |
| 563 | + } |
| 564 | + |
319 | 565 | /** |
320 | 566 | * @dataProvider dataGetFileMetadata |
321 | 567 | */ |
|
0 commit comments