ConnectionTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\User_LDAP\Tests;
  26. use OCA\User_LDAP\Connection;
  27. use OCA\User_LDAP\ILDAPWrapper;
  28. /**
  29. * Class Test_Connection
  30. *
  31. * @group DB
  32. *
  33. * @package OCA\User_LDAP\Tests
  34. */
  35. class ConnectionTest extends \Test\TestCase {
  36. /** @var \OCA\User_LDAP\ILDAPWrapper */
  37. protected $ldap;
  38. /** @var Connection */
  39. protected $connection;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->ldap = $this->createMock(ILDAPWrapper::class);
  43. // we use a mock here to replace the cache mechanism, due to missing DI in LDAP backend.
  44. $this->connection = $this->getMockBuilder('OCA\User_LDAP\Connection')
  45. ->setMethods(['getFromCache', 'writeToCache'])
  46. ->setConstructorArgs([$this->ldap, '', null])
  47. ->getMock();
  48. $this->ldap->expects($this->any())
  49. ->method('areLDAPFunctionsAvailable')
  50. ->will($this->returnValue(true));
  51. }
  52. public function testOriginalAgentUnchangedOnClone() {
  53. //background: upon login a bind is done with the user credentials
  54. //which is valid for the whole LDAP resource. It needs to be reset
  55. //to the agent's credentials
  56. $lw = $this->createMock(ILDAPWrapper::class);
  57. $connection = new Connection($lw, '', null);
  58. $agent = array(
  59. 'ldapAgentName' => 'agent',
  60. 'ldapAgentPassword' => '123456',
  61. );
  62. $connection->setConfiguration($agent);
  63. $testConnection = clone $connection;
  64. $user = array(
  65. 'ldapAgentName' => 'user',
  66. 'ldapAgentPassword' => 'password',
  67. );
  68. $testConnection->setConfiguration($user);
  69. $agentName = $connection->ldapAgentName;
  70. $agentPawd = $connection->ldapAgentPassword;
  71. $this->assertSame($agentName, $agent['ldapAgentName']);
  72. $this->assertSame($agentPawd, $agent['ldapAgentPassword']);
  73. }
  74. public function testUseBackupServer() {
  75. $mainHost = 'ldap://nixda.ldap';
  76. $backupHost = 'ldap://fallback.ldap';
  77. $config = [
  78. 'ldapConfigurationActive' => true,
  79. 'ldapHost' => $mainHost,
  80. 'ldapPort' => 389,
  81. 'ldapBackupHost' => $backupHost,
  82. 'ldapBackupPort' => 389,
  83. 'ldapAgentName' => 'uid=agent',
  84. 'ldapAgentPassword' => 'SuchASecret'
  85. ];
  86. $this->connection->setIgnoreValidation(true);
  87. $this->connection->setConfiguration($config);
  88. $this->ldap->expects($this->any())
  89. ->method('isResource')
  90. ->will($this->returnValue(true));
  91. $this->ldap->expects($this->any())
  92. ->method('setOption')
  93. ->will($this->returnValue(true));
  94. $this->ldap->expects($this->exactly(3))
  95. ->method('connect')
  96. ->will($this->returnValue('ldapResource'));
  97. // Not called often enough? Then, the fallback to the backup server is broken.
  98. $this->connection->expects($this->exactly(4))
  99. ->method('getFromCache')
  100. ->with('overrideMainServer')
  101. ->will($this->onConsecutiveCalls(false, false, true, true));
  102. $this->connection->expects($this->once())
  103. ->method('writeToCache')
  104. ->with('overrideMainServer', true);
  105. $isThrown = false;
  106. $this->ldap->expects($this->exactly(3))
  107. ->method('bind')
  108. ->will($this->returnCallback(function () use (&$isThrown) {
  109. if(!$isThrown) {
  110. $isThrown = true;
  111. throw new \OC\ServerNotAvailableException();
  112. }
  113. return true;
  114. }));
  115. $this->connection->init();
  116. $this->connection->resetConnectionResource();
  117. // with the second init() we test whether caching works
  118. $this->connection->init();
  119. }
  120. }