JsonApiTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. use PrivateBin\Controller;
  3. use PrivateBin\Data\Filesystem;
  4. use PrivateBin\Persistence\ServerSalt;
  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 Controller;
  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. $_GET[Helper::getPasteId()] = '';
  78. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  79. $_SERVER['REQUEST_METHOD'] = 'PUT';
  80. $_SERVER['REMOTE_ADDR'] = '::1';
  81. ob_start();
  82. new Controller;
  83. $content = ob_get_contents();
  84. ob_end_clean();
  85. unlink($file);
  86. $response = json_decode($content, true);
  87. $this->assertEquals(0, $response['status'], 'outputs status');
  88. $this->assertEquals(Helper::getPasteId(), $response['id'], 'outputted paste ID matches input');
  89. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  90. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  91. $paste = $this->_model->read($response['id']);
  92. $this->assertEquals(
  93. hash_hmac('sha256', $response['id'], $paste->meta->salt),
  94. $response['deletetoken'],
  95. 'outputs valid delete token'
  96. );
  97. }
  98. /**
  99. * @runInSeparateProcess
  100. */
  101. public function testDelete()
  102. {
  103. $this->_model->create(Helper::getPasteId(), Helper::getPaste());
  104. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
  105. $paste = $this->_model->read(Helper::getPasteId());
  106. $file = tempnam(sys_get_temp_dir(), 'FOO');
  107. file_put_contents($file, http_build_query(array(
  108. 'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste->meta->salt),
  109. )));
  110. Request::setInputStream($file);
  111. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  112. $_GET[Helper::getPasteId()] = '';
  113. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  114. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  115. ob_start();
  116. new Controller;
  117. $content = ob_get_contents();
  118. ob_end_clean();
  119. unlink($file);
  120. $response = json_decode($content, true);
  121. $this->assertEquals(0, $response['status'], 'outputs status');
  122. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  123. }
  124. /**
  125. * @runInSeparateProcess
  126. */
  127. public function testDeleteWithPost()
  128. {
  129. $this->_model->create(Helper::getPasteId(), Helper::getPaste());
  130. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
  131. $paste = $this->_model->read(Helper::getPasteId());
  132. $_POST = array(
  133. 'pasteid' => Helper::getPasteId(),
  134. 'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste->meta->salt),
  135. );
  136. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  137. $_SERVER['REQUEST_METHOD'] = 'POST';
  138. ob_start();
  139. new Controller;
  140. $content = ob_get_contents();
  141. ob_end_clean();
  142. $response = json_decode($content, true);
  143. $this->assertEquals(0, $response['status'], 'outputs status');
  144. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  145. }
  146. /**
  147. * @runInSeparateProcess
  148. */
  149. public function testRead()
  150. {
  151. $paste = Helper::getPasteWithAttachment();
  152. $paste['meta']['attachment'] = $paste['attachment'];
  153. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  154. unset($paste['attachment']);
  155. unset($paste['attachmentname']);
  156. $this->_model->create(Helper::getPasteId(), $paste);
  157. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  158. $_GET[Helper::getPasteId()] = '';
  159. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  160. ob_start();
  161. new Controller;
  162. $content = ob_get_contents();
  163. ob_end_clean();
  164. $response = json_decode($content, true);
  165. $this->assertEquals(0, $response['status'], 'outputs success status');
  166. $this->assertEquals(Helper::getPasteId(), $response['id'], 'outputs data correctly');
  167. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  168. $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
  169. $this->assertEquals($paste['meta']['attachment'], $response['attachment'], 'outputs attachment correctly');
  170. $this->assertEquals($paste['meta']['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly');
  171. $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
  172. $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
  173. $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
  174. $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
  175. $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
  176. }
  177. /**
  178. * @runInSeparateProcess
  179. */
  180. public function testJsonLdPaste()
  181. {
  182. $paste = Helper::getPasteWithAttachment();
  183. $this->_model->create(Helper::getPasteId(), $paste);
  184. $_GET['jsonld'] = 'paste';
  185. ob_start();
  186. new Controller;
  187. $content = ob_get_contents();
  188. ob_end_clean();
  189. $this->assertEquals(str_replace(
  190. '?jsonld=',
  191. '/?jsonld=',
  192. file_get_contents(PUBLIC_PATH . '/js/paste.jsonld')
  193. ), $content, 'outputs data correctly');
  194. }
  195. /**
  196. * @runInSeparateProcess
  197. */
  198. public function testJsonLdComment()
  199. {
  200. $paste = Helper::getPasteWithAttachment();
  201. $this->_model->create(Helper::getPasteId(), $paste);
  202. $_GET['jsonld'] = 'comment';
  203. ob_start();
  204. new Controller;
  205. $content = ob_get_contents();
  206. ob_end_clean();
  207. $this->assertEquals(str_replace(
  208. '?jsonld=',
  209. '/?jsonld=',
  210. file_get_contents(PUBLIC_PATH . '/js/comment.jsonld')
  211. ), $content, 'outputs data correctly');
  212. }
  213. /**
  214. * @runInSeparateProcess
  215. */
  216. public function testJsonLdPasteMeta()
  217. {
  218. $paste = Helper::getPasteWithAttachment();
  219. $this->_model->create(Helper::getPasteId(), $paste);
  220. $_GET['jsonld'] = 'pastemeta';
  221. ob_start();
  222. new Controller;
  223. $content = ob_get_contents();
  224. ob_end_clean();
  225. $this->assertEquals(str_replace(
  226. '?jsonld=',
  227. '/?jsonld=',
  228. file_get_contents(PUBLIC_PATH . '/js/pastemeta.jsonld')
  229. ), $content, 'outputs data correctly');
  230. }
  231. /**
  232. * @runInSeparateProcess
  233. */
  234. public function testJsonLdCommentMeta()
  235. {
  236. $paste = Helper::getPasteWithAttachment();
  237. $this->_model->create(Helper::getPasteId(), $paste);
  238. $_GET['jsonld'] = 'commentmeta';
  239. ob_start();
  240. new Controller;
  241. $content = ob_get_contents();
  242. ob_end_clean();
  243. $this->assertEquals(str_replace(
  244. '?jsonld=',
  245. '/?jsonld=',
  246. file_get_contents(PUBLIC_PATH . '/js/commentmeta.jsonld')
  247. ), $content, 'outputs data correctly');
  248. }
  249. /**
  250. * @runInSeparateProcess
  251. */
  252. public function testJsonLdInvalid()
  253. {
  254. $paste = Helper::getPasteWithAttachment();
  255. $this->_model->create(Helper::getPasteId(), $paste);
  256. $_GET['jsonld'] = CONF;
  257. ob_start();
  258. new Controller;
  259. $content = ob_get_contents();
  260. ob_end_clean();
  261. $this->assertEquals('{}', $content, 'does not output nasty data');
  262. }
  263. }