1
0

WipeControllerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Tests\Core\Controller;
  25. use OC\Authentication\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Token\RemoteWipe;
  27. use OC\Core\Controller\WipeController;
  28. use OCP\AppFramework\Http;
  29. use OCP\IRequest;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Test\TestCase;
  32. class WipeControllerTest extends TestCase {
  33. /** @var RemoteWipe|MockObject */
  34. private $remoteWipe;
  35. /** @var WipeController */
  36. private $controller;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->remoteWipe = $this->createMock(RemoteWipe::class);
  40. $this->controller = new WipeController(
  41. 'core',
  42. $this->createMock(IRequest::class),
  43. $this->remoteWipe);
  44. }
  45. public function dataTest() {
  46. return [
  47. // valid token, could perform operation, valid result
  48. [ true, true, true],
  49. [ true, false, false],
  50. [false, true, false],
  51. [false, false, false],
  52. ];
  53. }
  54. /**
  55. * @param bool $valid
  56. * @param bool $couldPerform
  57. * @param bool $result
  58. *
  59. * @dataProvider dataTest
  60. */
  61. public function testCheckWipe(bool $valid, bool $couldPerform, bool $result) {
  62. if (!$valid) {
  63. $this->remoteWipe->method('start')
  64. ->with('mytoken')
  65. ->willThrowException(new InvalidTokenException());
  66. } else {
  67. $this->remoteWipe->method('start')
  68. ->with('mytoken')
  69. ->willReturn($couldPerform);
  70. }
  71. $result = $this->controller->checkWipe('mytoken');
  72. if (!$valid || !$couldPerform) {
  73. $this->assertSame(Http::STATUS_NOT_FOUND, $result->getStatus());
  74. $this->assertSame([], $result->getData());
  75. } else {
  76. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  77. $this->assertSame(['wipe' => true], $result->getData());
  78. }
  79. }
  80. /**
  81. * @param bool $valid
  82. * @param bool $couldPerform
  83. * @param bool $result
  84. *
  85. * @dataProvider dataTest
  86. */
  87. public function testWipeDone(bool $valid, bool $couldPerform, bool $result) {
  88. if (!$valid) {
  89. $this->remoteWipe->method('finish')
  90. ->with('mytoken')
  91. ->willThrowException(new InvalidTokenException());
  92. } else {
  93. $this->remoteWipe->method('finish')
  94. ->with('mytoken')
  95. ->willReturn($couldPerform);
  96. }
  97. $result = $this->controller->wipeDone('mytoken');
  98. if (!$valid || !$couldPerform) {
  99. $this->assertSame(Http::STATUS_NOT_FOUND, $result->getStatus());
  100. $this->assertSame([], $result->getData());
  101. } else {
  102. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  103. $this->assertSame([], $result->getData());
  104. }
  105. }
  106. }