JsonApiTest.php 11 KB

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