You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.5 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @author Morris Jobke <hey@morrisjobke.de>
  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 OCA\Talk\Tests\BackgroundJob;
  25. use OCA\Talk\BackgroundJob\CheckHostedSignalingServer;
  26. use OCA\Talk\Service\HostedSignalingServerService;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\IConfig;
  29. use OCP\IGroup;
  30. use OCP\IGroupManager;
  31. use OCP\IURLGenerator;
  32. use OCP\Notification\IManager;
  33. use PHPUnit\Framework\Assert;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Psr\Log\LoggerInterface;
  36. use Test\TestCase;
  37. class CheckHostedSignalingServerTest extends TestCase {
  38. protected ITimeFactory&MockObject $timeFactory;
  39. protected HostedSignalingServerService&MockObject $hostedSignalingServerService;
  40. protected IConfig&MockObject $config;
  41. protected IManager&MockObject $notificationManager;
  42. protected IGroupManager&MockObject $groupManager;
  43. protected IURLGenerator&MockObject $urlGenerator;
  44. protected LoggerInterface&MockObject $logger;
  45. public function setUp(): void {
  46. parent::setUp();
  47. $this->timeFactory = $this->createMock(ITimeFactory::class);
  48. $this->hostedSignalingServerService = $this->createMock(HostedSignalingServerService::class);
  49. $this->config = $this->createMock(IConfig::class);
  50. $this->notificationManager = $this->createMock(IManager::class);
  51. $this->groupManager = $this->createMock(IGroupManager::class);
  52. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  53. $this->logger = $this->createMock(LoggerInterface::class);
  54. }
  55. public function getBackgroundJob(): CheckHostedSignalingServer {
  56. return new CheckHostedSignalingServer(
  57. $this->timeFactory,
  58. $this->hostedSignalingServerService,
  59. $this->config,
  60. $this->notificationManager,
  61. $this->groupManager,
  62. $this->urlGenerator,
  63. $this->logger
  64. );
  65. }
  66. public function testRunWithNoChange(): void {
  67. $backgroundJob = $this->getBackgroundJob();
  68. $this->config
  69. ->method('getAppValue')
  70. ->will($this->returnValueMap([
  71. ['spreed', 'hosted-signaling-server-account-id', '', 'my-account-id'],
  72. ['spreed', 'hosted-signaling-server-account', '{}', '{"status": "pending"}']
  73. ]));
  74. $this->hostedSignalingServerService->expects($this->once())
  75. ->method('fetchAccountInfo')
  76. ->willReturn(['status' => 'pending']);
  77. self::invokePrivate($backgroundJob, 'run', ['']);
  78. }
  79. public function testRunWithPendingToActiveChange(): void {
  80. $backgroundJob = $this->getBackgroundJob();
  81. $newStatus = [
  82. 'status' => 'active',
  83. 'signaling' => [
  84. 'url' => 'signaling-url',
  85. 'secret' => 'signaling-secret',
  86. ],
  87. ];
  88. $this->config
  89. ->method('getAppValue')
  90. ->willReturnMap([
  91. ['spreed', 'hosted-signaling-server-account-id', '', 'my-account-id'],
  92. ['spreed', 'hosted-signaling-server-account', '{}', '{"status": "pending"}']
  93. ]);
  94. $this->config->expects($this->once())
  95. ->method('deleteAppValue')
  96. ->with('spreed', 'signaling_mode');
  97. $expectedCalls = [
  98. ['spreed', 'signaling_servers', '{"servers":[{"server":"signaling-url","verify":true}],"secret":"signaling-secret"}'],
  99. ['spreed', 'hosted-signaling-server-account', json_encode($newStatus)],
  100. ];
  101. $i = 0;
  102. $this->config->expects($this->exactly(count($expectedCalls)))
  103. ->method('setAppValue')
  104. ->willReturnCallback(function () use ($expectedCalls, &$i) {
  105. Assert::assertArrayHasKey($i, $expectedCalls);
  106. Assert::assertSame($expectedCalls[$i], func_get_args());
  107. $i++;
  108. });
  109. $group = $this->createMock(IGroup::class);
  110. $this->groupManager->expects($this->once())
  111. ->method('get')
  112. ->with('admin')
  113. ->willReturn($group);
  114. $group->expects($this->once())
  115. ->method('getUsers')
  116. ->willReturn([]);
  117. $this->hostedSignalingServerService->expects($this->once())
  118. ->method('fetchAccountInfo')
  119. ->willReturn($newStatus);
  120. self::invokePrivate($backgroundJob, 'run', ['']);
  121. }
  122. }