JsonApiTest.php 10 KB

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