ProviderLoaderTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  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 lib\Authentication\TwoFactorAuth;
  25. use Exception;
  26. use OC\Authentication\TwoFactorAuth\ProviderLoader;
  27. use OCP\App\IAppManager;
  28. use OCP\Authentication\TwoFactorAuth\IProvider;
  29. use PHPUnit_Framework_MockObject_MockObject;
  30. use Test\TestCase;
  31. class ProviderLoaderTest extends TestCase {
  32. /** @var IAppManager|PHPUnit_Framework_MockObject_MockObject */
  33. private $appManager;
  34. /** @var IUser|PHPUnit_Framework_MockObject_MockObject */
  35. private $user;
  36. /** @var ProviderLoader */
  37. private $loader;
  38. protected function setUp() {
  39. parent::setUp();
  40. $this->appManager = $this->createMock(IAppManager::class);
  41. $this->user = $this->createMock(\OCP\IUser::class);
  42. $this->loader = new ProviderLoader($this->appManager);
  43. }
  44. /**
  45. * @expectedException Exception
  46. * @expectedExceptionMessage Could not load two-factor auth provider \OCA\MyFaulty2faApp\DoesNotExist
  47. */
  48. public function testFailHardIfProviderCanNotBeLoaded() {
  49. $this->appManager->expects($this->once())
  50. ->method('getEnabledAppsForUser')
  51. ->with($this->user)
  52. ->willReturn(['mail', 'twofactor_totp']);
  53. $this->appManager
  54. ->method('getAppInfo')
  55. ->will($this->returnValueMap([
  56. ['mail', false, null, []],
  57. ['twofactor_totp', false, null, [
  58. 'two-factor-providers' => [
  59. '\\OCA\\MyFaulty2faApp\\DoesNotExist',
  60. ],
  61. ]],
  62. ]));
  63. $this->loader->getProviders($this->user);
  64. }
  65. public function testGetProviders() {
  66. $provider = $this->createMock(IProvider::class);
  67. $provider->method('getId')->willReturn('test');
  68. \OC::$server->registerService('\\OCA\\TwoFactorTest\\Provider', function () use ($provider) {
  69. return $provider;
  70. });
  71. $this->appManager->expects($this->once())
  72. ->method('getEnabledAppsForUser')
  73. ->with($this->user)
  74. ->willReturn(['twofactor_test']);
  75. $this->appManager
  76. ->method('getAppInfo')
  77. ->with('twofactor_test')
  78. ->willReturn(['two-factor-providers' => [
  79. '\\OCA\\TwoFactorTest\\Provider',
  80. ]]);
  81. $providers = $this->loader->getProviders($this->user);
  82. $this->assertCount(1, $providers);
  83. $this->assertArrayHasKey('test', $providers);
  84. $this->assertSame($provider, $providers['test']);
  85. }
  86. }