WipeControllerTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Tests\Core\Controller;
  8. use OC\Authentication\Exceptions\InvalidTokenException;
  9. use OC\Authentication\Token\RemoteWipe;
  10. use OC\Core\Controller\WipeController;
  11. use OCP\AppFramework\Http;
  12. use OCP\IRequest;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Test\TestCase;
  15. class WipeControllerTest extends TestCase {
  16. /** @var RemoteWipe|MockObject */
  17. private $remoteWipe;
  18. /** @var WipeController */
  19. private $controller;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->remoteWipe = $this->createMock(RemoteWipe::class);
  23. $this->controller = new WipeController(
  24. 'core',
  25. $this->createMock(IRequest::class),
  26. $this->remoteWipe);
  27. }
  28. public function dataTest() {
  29. return [
  30. // valid token, could perform operation, valid result
  31. [ true, true, true],
  32. [ true, false, false],
  33. [false, true, false],
  34. [false, false, false],
  35. ];
  36. }
  37. /**
  38. * @param bool $valid
  39. * @param bool $couldPerform
  40. * @param bool $result
  41. *
  42. * @dataProvider dataTest
  43. */
  44. public function testCheckWipe(bool $valid, bool $couldPerform, bool $result): void {
  45. if (!$valid) {
  46. $this->remoteWipe->method('start')
  47. ->with('mytoken')
  48. ->willThrowException(new InvalidTokenException());
  49. } else {
  50. $this->remoteWipe->method('start')
  51. ->with('mytoken')
  52. ->willReturn($couldPerform);
  53. }
  54. $result = $this->controller->checkWipe('mytoken');
  55. if (!$valid || !$couldPerform) {
  56. $this->assertSame(Http::STATUS_NOT_FOUND, $result->getStatus());
  57. $this->assertSame([], $result->getData());
  58. } else {
  59. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  60. $this->assertSame(['wipe' => true], $result->getData());
  61. }
  62. }
  63. /**
  64. * @param bool $valid
  65. * @param bool $couldPerform
  66. * @param bool $result
  67. *
  68. * @dataProvider dataTest
  69. */
  70. public function testWipeDone(bool $valid, bool $couldPerform, bool $result): void {
  71. if (!$valid) {
  72. $this->remoteWipe->method('finish')
  73. ->with('mytoken')
  74. ->willThrowException(new InvalidTokenException());
  75. } else {
  76. $this->remoteWipe->method('finish')
  77. ->with('mytoken')
  78. ->willReturn($couldPerform);
  79. }
  80. $result = $this->controller->wipeDone('mytoken');
  81. if (!$valid || !$couldPerform) {
  82. $this->assertSame(Http::STATUS_NOT_FOUND, $result->getStatus());
  83. $this->assertSame([], $result->getData());
  84. } else {
  85. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  86. $this->assertSame([], $result->getData());
  87. }
  88. }
  89. }