jsonApi.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. class jsonApiTest extends PHPUnit_Framework_TestCase
  3. {
  4. protected $_model;
  5. public function setUp()
  6. {
  7. /* Setup Routine */
  8. $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
  9. serversalt::setPath(PATH . 'data');
  10. $this->reset();
  11. }
  12. public function tearDown()
  13. {
  14. /* Tear Down Routine */
  15. helper::confRestore();
  16. }
  17. public function reset()
  18. {
  19. $_POST = array();
  20. $_GET = array();
  21. $_SERVER = array();
  22. if ($this->_model->exists(helper::getPasteId()))
  23. $this->_model->delete(helper::getPasteId());
  24. helper::confRestore();
  25. }
  26. /**
  27. * @runInSeparateProcess
  28. */
  29. public function testCreate()
  30. {
  31. $this->reset();
  32. $options = parse_ini_file(CONF, true);
  33. $options['traffic']['limit'] = 0;
  34. helper::confBackup();
  35. helper::createIniFile(CONF, $options);
  36. $_POST = helper::getPaste();
  37. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  38. $_SERVER['REQUEST_METHOD'] = 'POST';
  39. $_SERVER['REMOTE_ADDR'] = '::1';
  40. ob_start();
  41. new zerobin;
  42. $content = ob_get_contents();
  43. $response = json_decode($content, true);
  44. $this->assertEquals(0, $response['status'], 'outputs status');
  45. $this->assertEquals(
  46. hash_hmac('sha1', $response['id'], serversalt::get()),
  47. $response['deletetoken'],
  48. 'outputs valid delete token'
  49. );
  50. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  51. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  52. }
  53. /**
  54. * @runInSeparateProcess
  55. */
  56. public function testPut()
  57. {
  58. $this->reset();
  59. $options = parse_ini_file(CONF, true);
  60. $options['traffic']['limit'] = 0;
  61. helper::confBackup();
  62. helper::createIniFile(CONF, $options);
  63. $paste = helper::getPaste();
  64. unset($paste['meta']);
  65. $file = tempnam(sys_get_temp_dir(), 'FOO');
  66. file_put_contents($file, http_build_query($paste));
  67. request::setInputStream($file);
  68. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  69. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  70. $_SERVER['REQUEST_METHOD'] = 'PUT';
  71. $_SERVER['REMOTE_ADDR'] = '::1';
  72. ob_start();
  73. new zerobin;
  74. $content = ob_get_contents();
  75. $response = json_decode($content, true);
  76. $this->assertEquals(0, $response['status'], 'outputs status');
  77. $this->assertEquals(helper::getPasteId(), $response['id'], 'outputted paste ID matches input');
  78. $this->assertEquals(
  79. hash_hmac('sha1', $response['id'], serversalt::get()),
  80. $response['deletetoken'],
  81. 'outputs valid delete token'
  82. );
  83. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  84. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  85. }
  86. /**
  87. * @runInSeparateProcess
  88. */
  89. public function testDelete()
  90. {
  91. $this->reset();
  92. $this->_model->create(helper::getPasteId(), helper::getPaste());
  93. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  94. $file = tempnam(sys_get_temp_dir(), 'FOO');
  95. file_put_contents($file, http_build_query(array(
  96. 'deletetoken' => hash_hmac('sha1', helper::getPasteId(), serversalt::get()),
  97. )));
  98. request::setInputStream($file);
  99. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  100. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  101. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  102. ob_start();
  103. new zerobin;
  104. $content = ob_get_contents();
  105. $response = json_decode($content, true);
  106. $this->assertEquals(0, $response['status'], 'outputs status');
  107. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  108. }
  109. /**
  110. * @runInSeparateProcess
  111. */
  112. public function testDeleteWithPost()
  113. {
  114. $this->reset();
  115. $this->_model->create(helper::getPasteId(), helper::getPaste());
  116. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  117. $_POST = array(
  118. 'action' => 'delete',
  119. 'deletetoken' => hash_hmac('sha1', helper::getPasteId(), serversalt::get()),
  120. );
  121. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  122. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  123. $_SERVER['REQUEST_METHOD'] = 'POST';
  124. ob_start();
  125. new zerobin;
  126. $content = ob_get_contents();
  127. $response = json_decode($content, true);
  128. $this->assertEquals(0, $response['status'], 'outputs status');
  129. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  130. }
  131. /**
  132. * @runInSeparateProcess
  133. */
  134. public function testRead()
  135. {
  136. $this->reset();
  137. $paste = helper::getPasteWithAttachment();
  138. $this->_model->create(helper::getPasteId(), $paste);
  139. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  140. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  141. ob_start();
  142. new zerobin;
  143. $content = ob_get_contents();
  144. $response = json_decode($content, true);
  145. $this->assertEquals(0, $response['status'], 'outputs success status');
  146. $this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly');
  147. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  148. $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
  149. $this->assertEquals($paste['attachment'], $response['attachment'], 'outputs attachment correctly');
  150. $this->assertEquals($paste['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly');
  151. $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
  152. $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
  153. $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
  154. $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
  155. $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
  156. }
  157. /**
  158. * @runInSeparateProcess
  159. */
  160. public function testJsonLdPaste()
  161. {
  162. $this->reset();
  163. $paste = helper::getPasteWithAttachment();
  164. $this->_model->create(helper::getPasteId(), $paste);
  165. $_GET['jsonld'] = 'paste';
  166. ob_start();
  167. new zerobin;
  168. $content = ob_get_contents();
  169. $this->assertEquals(str_replace(
  170. '?jsonld=',
  171. '/?jsonld=',
  172. file_get_contents(PUBLIC_PATH . '/js/paste.jsonld')
  173. ), $content, 'outputs data correctly');
  174. }
  175. /**
  176. * @runInSeparateProcess
  177. */
  178. public function testJsonLdComment()
  179. {
  180. $this->reset();
  181. $paste = helper::getPasteWithAttachment();
  182. $this->_model->create(helper::getPasteId(), $paste);
  183. $_GET['jsonld'] = 'comment';
  184. ob_start();
  185. new zerobin;
  186. $content = ob_get_contents();
  187. $this->assertEquals(str_replace(
  188. '?jsonld=',
  189. '/?jsonld=',
  190. file_get_contents(PUBLIC_PATH . '/js/comment.jsonld')
  191. ), $content, 'outputs data correctly');
  192. }
  193. /**
  194. * @runInSeparateProcess
  195. */
  196. public function testJsonLdPasteMeta()
  197. {
  198. $this->reset();
  199. $paste = helper::getPasteWithAttachment();
  200. $this->_model->create(helper::getPasteId(), $paste);
  201. $_GET['jsonld'] = 'pastemeta';
  202. ob_start();
  203. new zerobin;
  204. $content = ob_get_contents();
  205. $this->assertEquals(str_replace(
  206. '?jsonld=',
  207. '/?jsonld=',
  208. file_get_contents(PUBLIC_PATH . '/js/pastemeta.jsonld')
  209. ), $content, 'outputs data correctly');
  210. }
  211. /**
  212. * @runInSeparateProcess
  213. */
  214. public function testJsonLdCommentMeta()
  215. {
  216. $this->reset();
  217. $paste = helper::getPasteWithAttachment();
  218. $this->_model->create(helper::getPasteId(), $paste);
  219. $_GET['jsonld'] = 'commentmeta';
  220. ob_start();
  221. new zerobin;
  222. $content = ob_get_contents();
  223. $this->assertEquals(str_replace(
  224. '?jsonld=',
  225. '/?jsonld=',
  226. file_get_contents(PUBLIC_PATH . '/js/commentmeta.jsonld')
  227. ), $content, 'outputs data correctly');
  228. }
  229. /**
  230. * @runInSeparateProcess
  231. */
  232. public function testJsonLdInvalid()
  233. {
  234. $this->reset();
  235. $paste = helper::getPasteWithAttachment();
  236. $this->_model->create(helper::getPasteId(), $paste);
  237. $_GET['jsonld'] = '../cfg/conf.ini';
  238. ob_start();
  239. new zerobin;
  240. $content = ob_get_contents();
  241. $this->assertEquals('{}', $content, 'does not output nasty data');
  242. }
  243. }