AX.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. /*
  3. * Tests for the attribute exchange extension module
  4. */
  5. require_once "Auth/OpenID/AX.php";
  6. require_once "Auth/OpenID/Message.php";
  7. require_once "Auth/OpenID/Consumer.php";
  8. require_once "Auth/OpenID/Server.php";
  9. class BogusAXMessage extends Auth_OpenID_AX_Message {
  10. var $mode = 'bogus';
  11. function getExtensionArgs()
  12. {
  13. return $this->_newArgs();
  14. }
  15. }
  16. class AXMessageTest extends PHPUnit_Framework_TestCase {
  17. function setUp()
  18. {
  19. $this->bax = new BogusAXMessage();
  20. }
  21. function test_checkMode()
  22. {
  23. $result = $this->bax->_checkMode(array());
  24. $this->assertTrue(Auth_OpenID_AX::isError($result));
  25. $result = $this->bax->_checkMode(array('mode' => 'fetch_request'));
  26. $this->assertTrue(Auth_OpenID_AX::isError($result));
  27. // does not raise an exception when the mode is right
  28. $result = $this->bax->_checkMode(array('mode' => $this->bax->mode));
  29. $this->assertTrue($result === true);
  30. }
  31. /*
  32. * _newArgs generates something that has the correct mode
  33. */
  34. function test_checkMode_newArgs()
  35. {
  36. $result = $this->bax->_checkMode($this->bax->_newArgs());
  37. $this->assertTrue($result === true);
  38. }
  39. }
  40. class AttrInfoTest extends PHPUnit_Framework_TestCase {
  41. function test_construct()
  42. {
  43. $type_uri = 'a uri';
  44. $ainfo = Auth_OpenID_AX_AttrInfo::make($type_uri);
  45. $this->assertEquals($type_uri, $ainfo->type_uri);
  46. $this->assertEquals(1, $ainfo->count);
  47. $this->assertFalse($ainfo->required);
  48. $this->assertTrue($ainfo->alias === null);
  49. }
  50. }
  51. class ToTypeURIsTest extends PHPUnit_Framework_TestCase {
  52. function setUp()
  53. {
  54. $this->aliases = new Auth_OpenID_NamespaceMap();
  55. }
  56. function test_empty()
  57. {
  58. foreach (array(null, '') as $empty) {
  59. $uris = Auth_OpenID_AX_toTypeURIs($this->aliases, $empty);
  60. $this->assertEquals(array(), $uris);
  61. }
  62. }
  63. function test_undefined()
  64. {
  65. $result = Auth_OpenID_AX_toTypeURIs($this->aliases,
  66. 'http://janrain.com/');
  67. $this->assertTrue(Auth_OpenID_AX::isError($result));
  68. }
  69. function test_one()
  70. {
  71. $uri = 'http://janrain.com/';
  72. $alias = 'openid_hackers';
  73. $this->aliases->addAlias($uri, $alias);
  74. $uris = Auth_OpenID_AX_toTypeURIs($this->aliases, $alias);
  75. $this->assertEquals(array($uri), $uris);
  76. }
  77. function test_two()
  78. {
  79. $uri1 = 'http://janrain.com/';
  80. $alias1 = 'openid_hackers';
  81. $this->aliases->addAlias($uri1, $alias1);
  82. $uri2 = 'http://jyte.com/';
  83. $alias2 = 'openid_hack';
  84. $this->aliases->addAlias($uri2, $alias2);
  85. $uris = Auth_OpenID_AX_toTypeURIs($this->aliases,
  86. implode(',', array($alias1, $alias2)));
  87. $this->assertEquals(array($uri1, $uri2), $uris);
  88. }
  89. }
  90. class ParseAXValuesTest extends PHPUnit_Framework_TestCase {
  91. function failUnlessAXKeyError($ax_args)
  92. {
  93. $msg = new Auth_OpenID_AX_KeyValueMessage();
  94. $result = $msg->parseExtensionArgs($ax_args);
  95. $this->assertTrue(Auth_OpenID_AX::isError($result));
  96. $this->assertTrue($result->message);
  97. }
  98. function failUnlessAXValues($ax_args, $expected_args)
  99. {
  100. $msg = new Auth_OpenID_AX_KeyValueMessage();
  101. $msg->parseExtensionArgs($ax_args);
  102. $this->assertEquals($expected_args, $msg->data);
  103. }
  104. function test_emptyIsValid()
  105. {
  106. $this->failUnlessAXValues(array(), array());
  107. }
  108. function test_invalidAlias()
  109. {
  110. $types = array(
  111. 'Auth_OpenID_AX_KeyValueMessage',
  112. 'Auth_OpenID_AX_FetchRequest'
  113. );
  114. $inputs = array(
  115. array('type.a.b' => 'urn:foo',
  116. 'count.a.b' => '1'),
  117. array('type.a,b' => 'urn:foo',
  118. 'count.a,b' => '1'),
  119. );
  120. foreach ($types as $typ) {
  121. foreach ($inputs as $input) {
  122. $msg = new $typ();
  123. $result = $msg->parseExtensionArgs($input);
  124. $this->assertTrue(Auth_OpenID_AX::isError($result));
  125. }
  126. }
  127. }
  128. function test_missingValueForAliasExplodes()
  129. {
  130. $this->failUnlessAXKeyError(array('type.foo' => 'urn:foo'));
  131. }
  132. function test_countPresentButNotValue()
  133. {
  134. $this->failUnlessAXKeyError(array('type.foo' => 'urn:foo',
  135. 'count.foo' => '1'));
  136. }
  137. function test_invalidCountValue()
  138. {
  139. $msg = new Auth_OpenID_AX_FetchRequest();
  140. $result = $msg->parseExtensionArgs(
  141. array('type.foo' => 'urn:foo',
  142. 'count.foo' => 'bogus'));
  143. $this->assertTrue(Auth_OpenID_AX::isError($result));
  144. }
  145. function test_requestUnlimitedValues()
  146. {
  147. $msg = new Auth_OpenID_AX_FetchRequest();
  148. $result = $msg->parseExtensionArgs(
  149. array('mode' => 'fetch_request',
  150. 'required' => 'foo',
  151. 'type.foo' => 'urn:foo',
  152. 'count.foo' => Auth_OpenID_AX_UNLIMITED_VALUES));
  153. $attrs = $msg->iterAttrs();
  154. $foo = $attrs[0];
  155. $this->assertTrue($foo->count == Auth_OpenID_AX_UNLIMITED_VALUES);
  156. $this->assertTrue($foo->wantsUnlimitedValues());
  157. }
  158. function test_longAlias()
  159. {
  160. // Spec minimum length is 32 characters. This is a silly test
  161. // for this library, but it's here for completeness.
  162. $alias = str_repeat('x', Auth_OpenID_AX_MINIMUM_SUPPORTED_ALIAS_LENGTH);
  163. $msg = new Auth_OpenID_AX_KeyValueMessage();
  164. $result = $msg->parseExtensionArgs(
  165. array('type.' . $alias => 'urn:foo',
  166. 'count.' . $alias => '1',
  167. 'value.'.$alias.'.1' => 'first')
  168. );
  169. $this->assertFalse(Auth_OpenID_AX::isError($result));
  170. }
  171. function test_countPresentAndIsZero()
  172. {
  173. $this->failUnlessAXValues(
  174. array('type.foo' => 'urn:foo',
  175. 'count.foo' => '0',
  176. ), array('urn:foo' => array()));
  177. }
  178. function test_singletonEmpty()
  179. {
  180. $this->failUnlessAXValues(
  181. array('type.foo' => 'urn:foo',
  182. 'value.foo' => '',
  183. ), array('urn:foo' => array()));
  184. }
  185. function test_doubleAlias()
  186. {
  187. $this->failUnlessAXKeyError(
  188. array('type.foo' => 'urn:foo',
  189. 'value.foo' => '',
  190. 'type.bar' => 'urn:foo',
  191. 'value.bar' => '',
  192. ));
  193. }
  194. function test_doubleSingleton()
  195. {
  196. $this->failUnlessAXValues(
  197. array('type.foo' => 'urn:foo',
  198. 'value.foo' => '',
  199. 'type.bar' => 'urn:bar',
  200. 'value.bar' => '',
  201. ), array('urn:foo' => array(), 'urn:bar' => array()));
  202. }
  203. function test_singletonValue()
  204. {
  205. $this->failUnlessAXValues(
  206. array('type.foo' => 'urn:foo',
  207. 'value.foo' => 'Westfall',
  208. ), array('urn:foo' => array('Westfall')));
  209. }
  210. }
  211. class FetchRequestTest extends PHPUnit_Framework_TestCase {
  212. function setUp()
  213. {
  214. $this->msg = new Auth_OpenID_AX_FetchRequest();
  215. $this->type_a = 'http://janrain.example.com/a';
  216. $this->alias_a = 'a';
  217. }
  218. function test_mode()
  219. {
  220. $this->assertEquals($this->msg->mode, 'fetch_request');
  221. }
  222. function test_construct()
  223. {
  224. $this->assertEquals(array(), $this->msg->requested_attributes);
  225. $this->assertEquals(null, $this->msg->update_url);
  226. $msg = new Auth_OpenID_AX_FetchRequest('hailstorm');
  227. $this->assertEquals(array(), $msg->requested_attributes);
  228. $this->assertEquals('hailstorm', $msg->update_url);
  229. }
  230. function test_add()
  231. {
  232. $uri = 'mud://puddle';
  233. // Not yet added:
  234. $this->assertFalse(in_array($uri, $this->msg->iterTypes()));
  235. $attr = Auth_OpenID_AX_AttrInfo::make($uri);
  236. $this->msg->add($attr);
  237. // Present after adding
  238. $this->assertTrue(in_array($uri, $this->msg->iterTypes()));
  239. }
  240. function test_addTwice()
  241. {
  242. $uri = 'lightning://storm';
  243. $attr = Auth_OpenID_AX_AttrInfo::make($uri);
  244. $this->msg->add($attr);
  245. $this->assertTrue(Auth_OpenID_AX::isError($this->msg->add($attr)));
  246. }
  247. function test_getExtensionArgs_empty()
  248. {
  249. $expected_args = array(
  250. 'mode' =>'fetch_request',
  251. );
  252. $this->assertEquals($expected_args, $this->msg->getExtensionArgs());
  253. }
  254. function test_getExtensionArgs_noAlias()
  255. {
  256. $attr = Auth_OpenID_AX_AttrInfo::make('type://of.transportation');
  257. $this->msg->add($attr);
  258. $ax_args = $this->msg->getExtensionArgs();
  259. $found = false;
  260. $alias = null;
  261. foreach ($ax_args as $k => $v) {
  262. if (($v == $attr->type_uri) && (strpos($k, 'type.') === 0)) {
  263. $alias = substr($k, 5);
  264. $found = true;
  265. break;
  266. }
  267. }
  268. if (!$found) {
  269. $this->fail("Didn't find the type definition");
  270. return;
  271. }
  272. $this->failUnlessExtensionArgs(array(
  273. 'type.' . $alias => $attr->type_uri,
  274. 'if_available' => $alias));
  275. }
  276. function test_getExtensionArgs_alias_if_available()
  277. {
  278. $attr = Auth_OpenID_AX_AttrInfo::make(
  279. 'type://of.transportation', 1, false,
  280. 'transport');
  281. $this->msg->add($attr);
  282. $this->failUnlessExtensionArgs(array(
  283. 'type.' . $attr->alias => $attr->type_uri,
  284. 'if_available' => $attr->alias));
  285. }
  286. function test_getExtensionArgs_alias_req()
  287. {
  288. $attr = Auth_OpenID_AX_AttrInfo::make(
  289. 'type://of.transportation',
  290. 1, true, 'transport');
  291. $this->msg->add($attr);
  292. $this->failUnlessExtensionArgs(array(
  293. 'type.' . $attr->alias => $attr->type_uri,
  294. 'required' => $attr->alias));
  295. }
  296. /*
  297. * Make sure that getExtensionArgs has the expected result
  298. *
  299. * This method will fill in the mode.
  300. */
  301. function failUnlessExtensionArgs($expected_args)
  302. {
  303. $expected_args['mode'] = $this->msg->mode;
  304. $this->assertEquals($expected_args, $this->msg->getExtensionArgs());
  305. }
  306. function test_isIterable()
  307. {
  308. $this->assertEquals(array(), $this->msg->iterAttrs());
  309. $this->assertEquals(array(), $this->msg->iterTypes());
  310. }
  311. function test_getRequiredAttrs_empty()
  312. {
  313. $this->assertEquals(array(), $this->msg->getRequiredAttrs());
  314. }
  315. function test_parseExtensionArgs_extraType()
  316. {
  317. $extension_args = array(
  318. 'mode' => 'fetch_request',
  319. 'type.' . $this->alias_a => $this->type_a);
  320. $this->assertTrue(Auth_OpenID_AX::isError(
  321. $this->msg->parseExtensionArgs($extension_args)));
  322. }
  323. function test_parseExtensionArgs()
  324. {
  325. $extension_args = array(
  326. 'mode' => 'fetch_request',
  327. 'type.' . $this->alias_a => $this->type_a,
  328. 'if_available' => $this->alias_a);
  329. $this->msg->parseExtensionArgs($extension_args);
  330. $this->assertEquals(array($this->type_a), $this->msg->iterTypes());
  331. $attr_info = Auth_OpenID::arrayGet($this->msg->requested_attributes,
  332. $this->type_a);
  333. $this->assertTrue($attr_info);
  334. $this->assertFalse($attr_info->required);
  335. $this->assertEquals($this->type_a, $attr_info->type_uri);
  336. $this->assertEquals($this->alias_a, $attr_info->alias);
  337. $this->assertEquals(array($attr_info),
  338. $this->msg->iterAttrs());
  339. }
  340. function test_extensionArgs_idempotent()
  341. {
  342. $extension_args = array(
  343. 'mode' => 'fetch_request',
  344. 'type.' . $this->alias_a => $this->type_a,
  345. 'if_available' => $this->alias_a);
  346. $this->msg->parseExtensionArgs($extension_args);
  347. $this->assertEquals($extension_args, $this->msg->getExtensionArgs());
  348. $attr = $this->msg->requested_attributes[$this->type_a];
  349. $this->assertFalse($attr->required);
  350. }
  351. function test_extensionArgs_idempotent_count_required()
  352. {
  353. $extension_args = array(
  354. 'mode' => 'fetch_request',
  355. 'type.' . $this->alias_a => $this->type_a,
  356. 'count.' . $this->alias_a => '2',
  357. 'required' => $this->alias_a);
  358. $this->msg->parseExtensionArgs($extension_args);
  359. $this->assertEquals($extension_args, $this->msg->getExtensionArgs());
  360. $attr = $this->msg->requested_attributes[$this->type_a];
  361. $this->assertTrue($attr->required);
  362. }
  363. function test_extensionArgs_count1()
  364. {
  365. $extension_args = array(
  366. 'mode' => 'fetch_request',
  367. 'type.' . $this->alias_a => $this->type_a,
  368. 'count.' . $this->alias_a => '1',
  369. 'if_available' => $this->alias_a);
  370. $extension_args_norm = array(
  371. 'mode' => 'fetch_request',
  372. 'type.' . $this->alias_a => $this->type_a,
  373. 'if_available' => $this->alias_a);
  374. $this->msg->parseExtensionArgs($extension_args);
  375. $this->assertEquals($extension_args_norm, $this->msg->getExtensionArgs());
  376. }
  377. function test_openidNoRealm()
  378. {
  379. $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
  380. 'mode' => 'checkid_setup',
  381. 'ns' => Auth_OpenID_OPENID2_NS,
  382. 'ns.ax' => Auth_OpenID_AX_NS_URI,
  383. 'ax.update_url' => 'http://different.site/path',
  384. 'ax.mode' => 'fetch_request',
  385. ));
  386. $openid_req = new Auth_OpenID_Request();
  387. $openid_req->message =& $openid_req_msg;
  388. $result = Auth_OpenID_AX_FetchRequest::fromOpenIDRequest(
  389. $openid_req);
  390. $this->assertTrue(Auth_OpenID_AX::isError($result));
  391. }
  392. function test_openidUpdateURLVerificationError()
  393. {
  394. $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
  395. 'mode' => 'checkid_setup',
  396. 'ns' => Auth_OpenID_OPENID2_NS,
  397. 'realm' => 'http://example.com/realm',
  398. 'ns.ax' => Auth_OpenID_AX_NS_URI,
  399. 'ax.update_url' => 'http://different.site/path',
  400. 'ax.mode' => 'fetch_request',
  401. ));
  402. $openid_req = new Auth_OpenID_Request();
  403. $openid_req->message =& $openid_req_msg;
  404. $result = Auth_OpenID_AX_FetchRequest::fromOpenIDRequest($openid_req);
  405. $this->assertTrue(Auth_OpenID_AX::isError($result));
  406. }
  407. function test_openidUpdateURLVerificationSuccess()
  408. {
  409. $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
  410. 'mode' => 'checkid_setup',
  411. 'ns' => Auth_OpenID_OPENID2_NS,
  412. 'realm' => 'http://example.com/realm',
  413. 'ns.ax' => Auth_OpenID_AX_NS_URI,
  414. 'ax.update_url' => 'http://example.com/realm/update_path',
  415. 'ax.mode' => 'fetch_request',
  416. ));
  417. $openid_req = new Auth_OpenID_Request();
  418. $openid_req->message =& $openid_req_msg;
  419. $fr = Auth_OpenID_AX_FetchRequest::fromOpenIDRequest($openid_req);
  420. $this->assertFalse(Auth_OpenID_AX::isError($fr));
  421. }
  422. function test_openidUpdateURLVerificationSuccessReturnTo()
  423. {
  424. $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
  425. 'mode' => 'checkid_setup',
  426. 'ns' => Auth_OpenID_OPENID2_NS,
  427. 'return_to' => 'http://example.com/realm',
  428. 'ns.ax' => Auth_OpenID_AX_NS_URI,
  429. 'ax.update_url' => 'http://example.com/realm/update_path',
  430. 'ax.mode' => 'fetch_request',
  431. ));
  432. $openid_req = new Auth_OpenID_Request();
  433. $openid_req->message =& $openid_req_msg;
  434. $fr = Auth_OpenID_AX_FetchRequest::fromOpenIDRequest($openid_req);
  435. $this->assertFalse(Auth_OpenID_AX::isError($fr));
  436. }
  437. }
  438. class FauxEndpoint {
  439. function FauxEndpoint() {
  440. $this->claimed_id = 'http://some.url/';
  441. }
  442. }
  443. class FetchResponseTest extends PHPUnit_Framework_TestCase {
  444. function setUp()
  445. {
  446. $this->msg = new Auth_OpenID_AX_FetchResponse();
  447. $this->value_a = 'monkeys';
  448. $this->type_a = 'http://phone.home/';
  449. $this->alias_a = 'robocop';
  450. $this->request_update_url = 'http://update.bogus/';
  451. }
  452. function test_construct()
  453. {
  454. $this->assertTrue($this->msg->update_url === null);
  455. $this->assertEquals(array(), $this->msg->data);
  456. }
  457. function test_getExtensionArgs_empty()
  458. {
  459. $expected_args = array(
  460. 'mode' => 'fetch_response',
  461. );
  462. $req = null;
  463. $this->assertEquals($expected_args, $this->msg->getExtensionArgs($req));
  464. }
  465. function test_getExtensionArgs_empty_request()
  466. {
  467. $expected_args = array(
  468. 'mode' => 'fetch_response',
  469. );
  470. $req = new Auth_OpenID_AX_FetchRequest();
  471. $this->assertEquals($expected_args, $this->msg->getExtensionArgs($req));
  472. }
  473. function test_getExtensionArgs_empty_request_some()
  474. {
  475. $uri = 'http://not.found/';
  476. $alias = 'ext0';
  477. $expected_args = array(
  478. 'mode' => 'fetch_response',
  479. 'type.' . $alias => $uri,
  480. 'count.' . $alias => '0'
  481. );
  482. $req = new Auth_OpenID_AX_FetchRequest();
  483. $req->add(Auth_OpenID_AX_AttrInfo::make('http://not.found/'));
  484. $this->assertEquals($expected_args, $this->msg->getExtensionArgs($req));
  485. }
  486. function test_updateUrlInResponse()
  487. {
  488. $uri = 'http://not.found/';
  489. $alias = 'ext0';
  490. $expected_args = array(
  491. 'mode' => 'fetch_response',
  492. 'update_url' => $this->request_update_url,
  493. 'type.' . $alias => $uri,
  494. 'count.' . $alias => '0'
  495. );
  496. $req = new Auth_OpenID_AX_FetchRequest($this->request_update_url);
  497. $req->add(Auth_OpenID_AX_AttrInfo::make($uri));
  498. $this->assertEquals($expected_args, $this->msg->getExtensionArgs($req));
  499. }
  500. function test_getExtensionArgs_some_request()
  501. {
  502. $expected_args = array(
  503. 'mode' => 'fetch_response',
  504. 'type.' . $this->alias_a => $this->type_a,
  505. 'value.' . $this->alias_a . '.1' => $this->value_a,
  506. 'count.' . $this->alias_a => '1'
  507. );
  508. $req = new Auth_OpenID_AX_FetchRequest();
  509. $req->add(Auth_OpenID_AX_AttrInfo::make($this->type_a, 1, false, $this->alias_a));
  510. $this->msg->addValue($this->type_a, $this->value_a);
  511. $result = $this->msg->getExtensionArgs($req);
  512. $this->assertEquals($expected_args, $result);
  513. }
  514. function test_getExtensionArgs_some_not_request()
  515. {
  516. $req = new Auth_OpenID_AX_FetchRequest();
  517. $this->msg->addValue($this->type_a, $this->value_a);
  518. $this->assertTrue(Auth_OpenID_AX::isError($this->msg->getExtensionArgs($req)));
  519. }
  520. function test_getSingle_success()
  521. {
  522. $req = new Auth_OpenID_AX_FetchRequest();
  523. $this->msg->addValue($this->type_a, $this->value_a);
  524. $this->assertEquals($this->value_a, $this->msg->getSingle($this->type_a));
  525. }
  526. function test_getSingle_none()
  527. {
  528. $this->assertEquals(null, $this->msg->getSingle($this->type_a));
  529. }
  530. function test_getSingle_extra()
  531. {
  532. $data = array('x', 'y');
  533. $this->msg->setValues($this->type_a, $data);
  534. $this->assertTrue(Auth_OpenID_AX::isError($this->msg->getSingle($this->type_a)));
  535. }
  536. function test_get()
  537. {
  538. $this->assertTrue(Auth_OpenID_AX::isError($this->msg->get($this->type_a)));
  539. }
  540. function test_fromSuccessResponseWithoutExtension()
  541. {
  542. $args = array(
  543. 'mode' => 'id_res',
  544. 'ns' => Auth_OpenID_OPENID2_NS
  545. );
  546. $sf = array();
  547. foreach (array_keys($args) as $k) {
  548. array_push($sf, $k);
  549. }
  550. $msg = Auth_OpenID_Message::fromOpenIDArgs($args);
  551. $e = new FauxEndpoint();
  552. $resp = new Auth_OpenID_SuccessResponse($e, $msg, $sf);
  553. $ax_resp = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($resp);
  554. $this->assertTrue($ax_resp === null);
  555. }
  556. function test_fromSuccessResponseWithoutData()
  557. {
  558. $args = array(
  559. 'mode' => 'id_res',
  560. 'ns' => Auth_OpenID_OPENID2_NS,
  561. 'ns.ax' => Auth_OpenID_AX_NS_URI,
  562. 'ax.mode' => 'fetch_response',
  563. );
  564. $sf = array();
  565. foreach (array_keys($args) as $k) {
  566. array_push($sf, $k);
  567. }
  568. $msg = Auth_OpenID_Message::fromOpenIDArgs($args);
  569. $e = new FauxEndpoint();
  570. $resp = new Auth_OpenID_SuccessResponse($e, $msg, $sf);
  571. $ax_resp = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($resp);
  572. $this->assertTrue($ax_resp === null);
  573. }
  574. function test_fromSuccessResponse()
  575. {
  576. $name = "ziggy";
  577. $value = "stardust";
  578. $uri = "http://david.bowie.name/";
  579. $args = array(
  580. 'mode' => 'id_res',
  581. 'ns' => Auth_OpenID_OPENID2_NS,
  582. 'ns.ax' => Auth_OpenID_AX_NS_URI,
  583. 'ax.mode' => 'fetch_response',
  584. 'ax.update_url' => 'http://example.com/realm/update_path',
  585. 'ax.type.'.$name => $uri,
  586. 'ax.count.'.$name => '1',
  587. 'ax.value.'.$name.'.1' => $value,
  588. );
  589. $sf = array();
  590. foreach (array_keys($args) as $k) {
  591. array_push($sf, $k);
  592. }
  593. $msg = Auth_OpenID_Message::fromOpenIDArgs($args);
  594. $e = new FauxEndpoint();
  595. $resp = new Auth_OpenID_SuccessResponse($e, $msg, $sf);
  596. $ax_resp = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($resp, false);
  597. $this->assertFalse($ax_resp === null);
  598. $this->assertTrue(is_a($ax_resp, 'Auth_OpenID_AX_FetchResponse'));
  599. $values = $ax_resp->get($uri);
  600. $this->assertEquals(array($value), $values);
  601. }
  602. }
  603. class StoreRequestTest extends PHPUnit_Framework_TestCase {
  604. function setUp()
  605. {
  606. $this->msg = new Auth_OpenID_AX_StoreRequest();
  607. $this->type_a = 'http://three.count/';
  608. $this->alias_a = 'juggling';
  609. }
  610. function test_construct()
  611. {
  612. $this->assertEquals(array(), $this->msg->data);
  613. }
  614. function test_getExtensionArgs_empty()
  615. {
  616. $args = $this->msg->getExtensionArgs();
  617. $expected_args = array(
  618. 'mode' => 'store_request',
  619. );
  620. $this->assertEquals($expected_args, $args);
  621. }
  622. function test_getExtensionArgs_nonempty()
  623. {
  624. $data = array('foo', 'bar');
  625. $this->msg->setValues($this->type_a, $data);
  626. $aliases = new Auth_OpenID_NamespaceMap();
  627. $aliases->addAlias($this->type_a, $this->alias_a);
  628. $args = $this->msg->getExtensionArgs($aliases);
  629. $expected_args = array(
  630. 'mode' => 'store_request',
  631. 'type.' . $this->alias_a => $this->type_a,
  632. 'count.' . $this->alias_a => '2',
  633. sprintf('value.%s.1', $this->alias_a) => 'foo',
  634. sprintf('value.%s.2', $this->alias_a) => 'bar',
  635. );
  636. $this->assertEquals($expected_args, $args);
  637. }
  638. }
  639. class StoreResponseTest extends PHPUnit_Framework_TestCase {
  640. function test_success()
  641. {
  642. $msg = new Auth_OpenID_AX_StoreResponse();
  643. $this->assertTrue($msg->succeeded());
  644. $this->assertFalse($msg->error_message);
  645. $this->assertEquals(array('mode' => 'store_response_success'),
  646. $msg->getExtensionArgs());
  647. }
  648. function test_fail_nomsg()
  649. {
  650. $msg = new Auth_OpenID_AX_StoreResponse(false);
  651. $this->assertFalse($msg->succeeded());
  652. $this->assertFalse($msg->error_message);
  653. $this->assertEquals(array('mode' => 'store_response_failure'),
  654. $msg->getExtensionArgs());
  655. }
  656. function test_fail_msg()
  657. {
  658. $reason = 'no reason, really';
  659. $msg = new Auth_OpenID_AX_StoreResponse(false, $reason);
  660. $this->assertFalse($msg->succeeded());
  661. $this->assertEquals($reason, $msg->error_message);
  662. $this->assertEquals(array('mode' => 'store_response_failure',
  663. 'error' => $reason), $msg->getExtensionArgs());
  664. }
  665. }
  666. class Tests_Auth_OpenID_AX extends PHPUnit_Framework_TestSuite {
  667. function getName()
  668. {
  669. return "Tests_Auth_OpenID_AX";
  670. }
  671. function Tests_Auth_OpenID_AX()
  672. {
  673. $this->addTestSuite('StoreResponseTest');
  674. $this->addTestSuite('StoreRequestTest');
  675. $this->addTestSuite('FetchResponseTest');
  676. $this->addTestSuite('FetchRequestTest');
  677. $this->addTestSuite('ParseAXValuesTest');
  678. $this->addTestSuite('ToTypeURIsTest');
  679. $this->addTestSuite('AttrInfoTest');
  680. $this->addTestSuite('AXMessageTest');
  681. }
  682. }