AX.php 26 KB

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