/tests/unit/WindowsAzure/ServiceBus/Internal/WrapTokenManagerTest.php

https://bitbucket.org/skudatech/azure-sdk-for-php · PHP · 184 lines · 105 code · 25 blank · 54 comment · 0 complexity · 396c61cd52cf07453deba3f2176dac2c MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package Tests\Unit\WindowsAzure\ServiceBus\Internal
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/WindowsAzure/azure-sdk-for-php
  22. */
  23. namespace Tests\Unit\WindowsAzure\ServiceBus\Internal;
  24. use WindowsAzure\Common\ServicesBuilder;
  25. use WindowsAzure\Common\Models\ServiceProperties;
  26. use Tests\Framework\TestResources;
  27. use WindowsAzure\Common\Configuration;
  28. use WindowsAzure\Common\ServiceException;
  29. use WindowsAzure\Common\Internal\WindowsAzureUtilities;
  30. use WindowsAzure\ServiceBus\WrapRestProxy;
  31. use WindowsAzure\ServiceBus\Internal\WrapTokenManager;
  32. use WindowsAzure\Common\Internal\Resources;
  33. /**
  34. * Unit tests for WrapRestProxy class
  35. *
  36. * @category Microsoft
  37. * @package Tests\Unit\WindowsAzure\ServiceBus\Internal
  38. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  39. * @copyright 2012 Microsoft Corporation
  40. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  41. * @version Release: @package_version@
  42. * @link https://github.com/WindowsAzure/azure-sdk-for-php
  43. */
  44. class WrapTokenManagerTest extends \PHPUnit_Framework_TestCase
  45. {
  46. private $_wrapRestProxy;
  47. public function setUp()
  48. {
  49. $builder = new ServicesBuilder();
  50. $wrapUri = 'https://' . TestResources::serviceBusNamespace() .'-sb.accesscontrol.windows.net/WRAPv0.9';
  51. $wrapBuilder = new \ReflectionMethod($builder, 'createWrapService');
  52. $wrapBuilder->setAccessible(true);
  53. $this->_wrapRestProxy = $wrapBuilder->invoke($builder, $wrapUri);
  54. }
  55. /**
  56. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::__construct
  57. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::getAccessToken
  58. */
  59. public function testGetAccessTokenSuccess()
  60. {
  61. // Setup
  62. $wrapUri = 'https://'
  63. .TestResources::serviceBusNamespace()
  64. .'-sb.accesscontrol.windows.net/WRAPv0.9';
  65. $wrapUserName = TestResources::wrapAuthenticationName();
  66. $wrapPassword = TestResources::wrapPassword();
  67. $scope = "https://"
  68. .TestResources::serviceBusNameSpace()
  69. .'.servicebus.windows.net';
  70. // Execute
  71. $wrapTokenManager = new WrapTokenManager(
  72. $wrapUri,
  73. $wrapUserName,
  74. $wrapPassword,
  75. $this->_wrapRestProxy
  76. );
  77. // Assert
  78. $accessToken = $wrapTokenManager->getAccessToken($scope);
  79. parse_str($accessToken, $parsedAccessToken);
  80. $this->assertNotNull($accessToken);
  81. $this->assertTrue(is_array($parsedAccessToken));
  82. }
  83. /**
  84. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::__construct
  85. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::getAccessToken
  86. */
  87. public function testGetAccessTokenFailedWithInvalidWrapUri()
  88. {
  89. $this->setExpectedException(get_class(
  90. new \InvalidArgumentException(''))
  91. );
  92. $wrapUri = 'IamNotAValidUri';
  93. $wrapUserName = TestResources::wrapAuthenticationName();
  94. $wrapPassword = TestResources::wrapPassword();
  95. $scope = "http://"
  96. .TestResources::serviceBusNameSpace()
  97. .'.servicebus.windows.net';
  98. $wrapTokenManager = new WrapTokenManager(
  99. $wrapUri,
  100. $wrapUserName,
  101. $wrapPassword,
  102. $this->_wrapRestProxy
  103. );
  104. // Test
  105. $wrapTokenManager->getAccessToken($scope);
  106. }
  107. /**
  108. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::__construct
  109. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::getAccessToken
  110. */
  111. public function testGetAccessTokenFailedWithInvalidUserName()
  112. {
  113. $this->setExpectedException(get_class(
  114. new ServiceException(''))
  115. );
  116. $wrapUri = 'https://'
  117. .TestResources::serviceBusNamespace()
  118. .'-sb.accesscontrol.windows.net/WRAPv0.9';
  119. $wrapUserName = 'IAmNotAGoodUserName';
  120. $wrapPassword = TestResources::wrapPassword();
  121. $scope = "http://"
  122. .TestResources::serviceBusNameSpace()
  123. .'.servicebus.windows.net';
  124. $wrapTokenManager = new WrapTokenManager(
  125. $wrapUri,
  126. $wrapUserName,
  127. $wrapPassword,
  128. $this->_wrapRestProxy
  129. );
  130. // Test
  131. $wrapTokenManager->getAccessToken($scope);
  132. }
  133. /**
  134. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::__construct
  135. * @covers WindowsAzure\ServiceBus\Internal\WrapTokenManager::getAccessToken
  136. */
  137. public function testGetAccesTokenFailedWithInvalidPassword()
  138. {
  139. $this->setExpectedException(get_class(
  140. new ServiceException(''))
  141. );
  142. $wrapUri = 'https://'
  143. .TestResources::serviceBusNamespace()
  144. .'-sb.accesscontrol.windows.net/WRAPv0.9';
  145. $wrapUserName = TestResources::wrapAuthenticationName();
  146. $wrapPassword = 'IAmNotACorrectPassword';
  147. $scope = "http://"
  148. .TestResources::serviceBusNameSpace()
  149. .'.servicebus.windows.net';
  150. $wrapTokenManager = new WrapTokenManager(
  151. $wrapUri,
  152. $wrapUserName,
  153. $wrapPassword,
  154. $this->_wrapRestProxy
  155. );
  156. // Test
  157. $wrapTokenManager->getAccessToken($scope);
  158. }
  159. }