/tests/OpenID/Auth/RequestTest.php

https://github.com/shupp/openid · PHP · 269 lines · 151 code · 32 blank · 86 comment · 0 complexity · 808bd8224d44adc21675c7080d6d26f0 MD5 · raw file

  1. <?php
  2. /**
  3. * OpenID_Auth_RequestTest
  4. *
  5. * PHP Version 5.2.0+
  6. *
  7. * @uses PHPUnit_Framework_TestCase
  8. * @category Auth
  9. * @package OpenID
  10. * @author Bill Shupp <hostmaster@shupp.org>
  11. * @copyright 2009 Bill Shupp
  12. * @license http://www.opensource.org/licenses/bsd-license.php FreeBSD
  13. * @link http://github.com/shupp/openid
  14. */
  15. require_once 'OpenID/Auth/Request.php';
  16. require_once 'OpenID/Discover.php';
  17. require_once 'OpenID/Discover/Mock.php';
  18. require_once 'OpenID/ServiceEndpoint.php';
  19. require_once 'OpenID/Extension.php';
  20. require_once 'OpenID/Extension/UI.php';
  21. require_once 'OpenID/Store/Mock.php';
  22. require_once 'OpenID/Nonce.php';
  23. /**
  24. * OpenID_Auth_RequestTest
  25. *
  26. * @uses PHPUnit_Framework_TestCase
  27. * @category Auth
  28. * @package OpenID
  29. * @author Bill Shupp <hostmaster@shupp.org>
  30. * @copyright 2009 Bill Shupp
  31. * @license http://www.opensource.org/licenses/bsd-license.php FreeBSD
  32. * @link http://github.com/shupp/openid
  33. */
  34. class OpenID_Auth_RequestTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected $authRequest = null;
  37. protected $identifier = 'http://user.example.com/';
  38. protected $returnTo = 'http://examplerp.com';
  39. protected $opURL = 'http://exampleop.com';
  40. protected $realm = 'http://example.com';
  41. protected $discover = null;
  42. protected $assocHandle = '12345';
  43. /**
  44. * setUp
  45. *
  46. * @return void
  47. */
  48. public function setUp()
  49. {
  50. OpenID_Discover::$discoveryOrder = array(0 => 'Mock');
  51. $opEndpoint = new OpenID_ServiceEndpoint();
  52. $opEndpoint->setVersion(OpenID::SERVICE_2_0_SERVER);
  53. $opEndpoint->setTypes(array(OpenID::SERVICE_2_0_SERVER));
  54. $opEndpoint->setURIs(array($this->opURL));
  55. OpenID_Discover_Mock::$opEndpoint = $opEndpoint;
  56. $this->setObjects();
  57. }
  58. /**
  59. * setObjects
  60. *
  61. * @return void
  62. */
  63. protected function setObjects()
  64. {
  65. $this->discover = new OpenID_Discover($this->identifier);
  66. $this->discover->discover();
  67. $this->authRequest = new OpenID_Auth_Request($this->discover,
  68. $this->returnTo,
  69. $this->realm,
  70. $this->assocHandle);
  71. }
  72. /**
  73. * tearDown
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. OpenID_Discover::$discoveryOrder = array(
  80. 0 => OpenID_Discover::TYPE_YADIS,
  81. 10 => OpenID_Discover::TYPE_HTML
  82. );
  83. OpenID_Discover_Mock::$opEndpoint = null;
  84. $this->discover = null;
  85. $this->authRequest = null;
  86. }
  87. /**
  88. * testAddExtension
  89. *
  90. * @return void
  91. */
  92. public function testAddExtension()
  93. {
  94. $ui = new OpenID_Extension_UI(OpenID_Extension::REQUEST);
  95. $this->authRequest->addExtension($ui);
  96. }
  97. /**
  98. * testSetModeFail
  99. *
  100. * @expectedException OpenID_Auth_Exception
  101. * @return void
  102. */
  103. public function testSetModeFail()
  104. {
  105. $this->authRequest->setMode('foo');
  106. }
  107. /**
  108. * testSetModeSuccess
  109. *
  110. * @return void
  111. */
  112. public function testSetModeSuccess()
  113. {
  114. $mode = OpenID::MODE_CHECKID_IMMEDIATE;
  115. $this->authRequest->setMode($mode);
  116. $this->assertSame($mode, $this->authRequest->getMode());
  117. }
  118. /**
  119. * testGetAuthorizeURL
  120. *
  121. * @return void
  122. */
  123. public function testGetAuthorizeURL()
  124. {
  125. $url = $this->authRequest->getAuthorizeURL();
  126. $split = preg_split('/\?/', $url);
  127. $message = new OpenID_Message($split[1], OpenID_Message::FORMAT_HTTP);
  128. $this->assertSame($this->returnTo, $message->get('openid.return_to'));
  129. $this->assertSame(OpenID::NS_2_0_ID_SELECT,
  130. $message->get('openid.identity'));
  131. $this->assertSame(OpenID::NS_2_0_ID_SELECT,
  132. $message->get('openid.claimed_id'));
  133. $this->assertSame($this->opURL, $split[0]);
  134. }
  135. /**
  136. * testGetAuthorizeURLWithQueryString
  137. *
  138. * @return void
  139. */
  140. public function testGetAuthorizeURLWithQueryString()
  141. {
  142. $originalURL = $this->opURL;
  143. $newURL = 'http://exampleop.com/foobar?foo=bar';
  144. $this->opURL = $newURL;
  145. $this->setUp();
  146. $url = $this->authRequest->getAuthorizeURL();
  147. $split = preg_split('/\?/', $url);
  148. $message = new OpenID_Message($split[1], OpenID_Message::FORMAT_HTTP);
  149. $this->assertSame($this->returnTo, $message->get('openid.return_to'));
  150. $this->assertSame(OpenID::NS_2_0_ID_SELECT,
  151. $message->get('openid.identity'));
  152. $this->assertSame(OpenID::NS_2_0_ID_SELECT,
  153. $message->get('openid.claimed_id'));
  154. $this->assertSame('bar', $message->get('foo'));
  155. $this->opURL = $originalURL;
  156. }
  157. /**
  158. * testGetAuthorizeURLSignon
  159. *
  160. * @return void
  161. */
  162. public function testGetAuthorizeURLSignon()
  163. {
  164. $opEndpoint = new OpenID_ServiceEndpoint();
  165. $opEndpoint->setVersion(OpenID::SERVICE_2_0_SIGNON);
  166. $opEndpoint->setTypes(array(OpenID::SERVICE_2_0_SIGNON));
  167. $opEndpoint->setURIs(array($this->opURL));
  168. OpenID_Discover_Mock::$opEndpoint = $opEndpoint;
  169. $this->setObjects();
  170. $url = $this->authRequest->getAuthorizeURL();
  171. $split = preg_split('/\?/', $url);
  172. $message = new OpenID_Message($split[1], OpenID_Message::FORMAT_HTTP);
  173. $this->assertSame($this->returnTo, $message->get('openid.return_to'));
  174. $this->assertSame($this->identifier, $message->get('openid.identity'));
  175. $this->assertSame($this->identifier, $message->get('openid.claimed_id'));
  176. $this->assertSame($this->opURL, $split[0]);
  177. }
  178. /**
  179. * testGetAuthorizeURLSignonLocalID
  180. *
  181. * @return void
  182. */
  183. public function testGetAuthorizeURLSignonLocalID()
  184. {
  185. $opEndpoint = new OpenID_ServiceEndpoint();
  186. $opEndpoint->setVersion(OpenID::SERVICE_2_0_SIGNON);
  187. $opEndpoint->setTypes(array(OpenID::SERVICE_2_0_SIGNON));
  188. $opEndpoint->setLocalID($this->identifier);
  189. $opEndpoint->setURIs(array($this->opURL));
  190. OpenID_Discover_Mock::$opEndpoint = $opEndpoint;
  191. $this->setObjects();
  192. $url = $this->authRequest->getAuthorizeURL();
  193. $split = preg_split('/\?/', $url);
  194. $message = new OpenID_Message($split[1], OpenID_Message::FORMAT_HTTP);
  195. $this->assertSame($this->returnTo, $message->get('openid.return_to'));
  196. $this->assertSame($this->identifier, $message->get('openid.identity'));
  197. $this->assertSame($this->identifier, $message->get('openid.claimed_id'));
  198. $this->assertSame($this->opURL, $split[0]);
  199. }
  200. /**
  201. * testGetAuthorizeURLSignonLocalIDOneOne
  202. *
  203. * @return void
  204. */
  205. public function testGetAuthorizeURLSignonLocalIDOneOne()
  206. {
  207. $opEndpoint = new OpenID_ServiceEndpoint();
  208. $opEndpoint->setVersion(OpenID::SERVICE_1_1_SIGNON);
  209. $opEndpoint->setTypes(array(OpenID::SERVICE_1_1_SIGNON));
  210. $opEndpoint->setLocalID($this->identifier);
  211. $opEndpoint->setURIs(array($this->opURL));
  212. OpenID_Discover_Mock::$opEndpoint = $opEndpoint;
  213. $this->setObjects();
  214. $url = $this->authRequest->getAuthorizeURL();
  215. $split = preg_split('/\?/', $url);
  216. $message = new OpenID_Message($split[1], OpenID_Message::FORMAT_HTTP);
  217. $this->assertNotSame($this->returnTo, $message->get('openid.return_to'));
  218. $this->assertSame($this->identifier, $message->get('openid.identity'));
  219. $this->assertSame(null, $message->get('openid.claimed_id'));
  220. $this->assertSame($this->opURL, $split[0]);
  221. // Mock nonce/store rather than have a new one created
  222. $store = new OpenID_Store_Mock();
  223. $nonce = new OpenID_Nonce($this->opURL, null, $store);
  224. $this->authRequest->setNonce($nonce);
  225. $url = $this->authRequest->getAuthorizeURL();
  226. }
  227. /**
  228. * testGetDiscover
  229. *
  230. * @return void
  231. */
  232. public function testGetDiscover()
  233. {
  234. $this->assertSame($this->discover, $this->authRequest->getDiscover());
  235. }
  236. }
  237. ?>