2015-10-11 12:50:48 -04:00
|
|
|
<?php
|
2016-07-21 11:09:48 -04:00
|
|
|
|
|
|
|
use PrivateBin\data\data;
|
|
|
|
use PrivateBin\privatebin;
|
|
|
|
use PrivateBin\request;
|
|
|
|
use PrivateBin\serversalt;
|
|
|
|
|
2015-10-11 12:50:48 -04:00
|
|
|
class jsonApiTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
protected $_model;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-07-21 11:09:48 -04:00
|
|
|
$this->_model = data::getInstance(array('dir' => PATH . 'data'));
|
2015-10-11 12:50:48 -04:00
|
|
|
serversalt::setPath(PATH . 'data');
|
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
helper::confRestore();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
$_POST = array();
|
|
|
|
$_GET = array();
|
|
|
|
$_SERVER = array();
|
2016-07-26 02:19:35 -04:00
|
|
|
if ($this->_model->exists(helper::getPasteId())) {
|
2015-10-11 12:50:48 -04:00
|
|
|
$this->_model->delete(helper::getPasteId());
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2015-10-11 12:50:48 -04:00
|
|
|
helper::confRestore();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testCreate()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$options = parse_ini_file(CONF, true);
|
|
|
|
$options['traffic']['limit'] = 0;
|
|
|
|
helper::confBackup();
|
|
|
|
helper::createIniFile(CONF, $options);
|
|
|
|
$_POST = helper::getPaste();
|
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '::1';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-11 12:50:48 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-11 12:50:48 -04:00
|
|
|
$response = json_decode($content, true);
|
|
|
|
$this->assertEquals(0, $response['status'], 'outputs status');
|
2016-07-06 05:37:13 -04:00
|
|
|
$this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
|
|
|
|
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
|
|
|
|
$paste = $this->_model->read($response['id']);
|
2015-10-11 12:50:48 -04:00
|
|
|
$this->assertEquals(
|
2016-07-06 05:37:13 -04:00
|
|
|
hash_hmac('sha256', $response['id'], $paste->meta->salt),
|
2015-10-11 12:50:48 -04:00
|
|
|
$response['deletetoken'],
|
|
|
|
'outputs valid delete token'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testPut()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$options = parse_ini_file(CONF, true);
|
|
|
|
$options['traffic']['limit'] = 0;
|
|
|
|
helper::confBackup();
|
|
|
|
helper::createIniFile(CONF, $options);
|
|
|
|
$paste = helper::getPaste();
|
|
|
|
unset($paste['meta']);
|
2015-10-11 15:22:00 -04:00
|
|
|
$file = tempnam(sys_get_temp_dir(), 'FOO');
|
2015-10-11 12:50:48 -04:00
|
|
|
file_put_contents($file, http_build_query($paste));
|
|
|
|
request::setInputStream($file);
|
|
|
|
$_SERVER['QUERY_STRING'] = helper::getPasteId();
|
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '::1';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-11 12:50:48 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-11 12:50:48 -04:00
|
|
|
$response = json_decode($content, true);
|
|
|
|
$this->assertEquals(0, $response['status'], 'outputs status');
|
|
|
|
$this->assertEquals(helper::getPasteId(), $response['id'], 'outputted paste ID matches input');
|
2016-07-06 05:37:13 -04:00
|
|
|
$this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
|
|
|
|
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
|
|
|
|
$paste = $this->_model->read($response['id']);
|
2015-10-11 12:50:48 -04:00
|
|
|
$this->assertEquals(
|
2016-07-06 05:37:13 -04:00
|
|
|
hash_hmac('sha256', $response['id'], $paste->meta->salt),
|
2015-10-11 12:50:48 -04:00
|
|
|
$response['deletetoken'],
|
|
|
|
'outputs valid delete token'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-11 15:22:00 -04:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testDelete()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$this->_model->create(helper::getPasteId(), helper::getPaste());
|
|
|
|
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
|
2016-07-06 05:37:13 -04:00
|
|
|
$paste = $this->_model->read(helper::getPasteId());
|
2015-10-11 15:22:00 -04:00
|
|
|
$file = tempnam(sys_get_temp_dir(), 'FOO');
|
|
|
|
file_put_contents($file, http_build_query(array(
|
2016-07-06 05:37:13 -04:00
|
|
|
'deletetoken' => hash_hmac('sha256', helper::getPasteId(), $paste->meta->salt),
|
2015-10-11 15:22:00 -04:00
|
|
|
)));
|
|
|
|
request::setInputStream($file);
|
|
|
|
$_SERVER['QUERY_STRING'] = helper::getPasteId();
|
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-11 15:22:00 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-11 15:22:00 -04:00
|
|
|
$response = json_decode($content, true);
|
|
|
|
$this->assertEquals(0, $response['status'], 'outputs status');
|
|
|
|
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testDeleteWithPost()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$this->_model->create(helper::getPasteId(), helper::getPaste());
|
|
|
|
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
|
2016-07-06 05:37:13 -04:00
|
|
|
$paste = $this->_model->read(helper::getPasteId());
|
2015-10-11 15:22:00 -04:00
|
|
|
$_POST = array(
|
|
|
|
'action' => 'delete',
|
2016-07-06 05:37:13 -04:00
|
|
|
'deletetoken' => hash_hmac('sha256', helper::getPasteId(), $paste->meta->salt),
|
2015-10-11 15:22:00 -04:00
|
|
|
);
|
|
|
|
$_SERVER['QUERY_STRING'] = helper::getPasteId();
|
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-11 15:22:00 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-11 15:22:00 -04:00
|
|
|
$response = json_decode($content, true);
|
|
|
|
$this->assertEquals(0, $response['status'], 'outputs status');
|
|
|
|
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
|
|
|
|
}
|
|
|
|
|
2015-10-15 16:04:57 -04:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testRead()
|
|
|
|
{
|
|
|
|
$this->reset();
|
2015-10-18 05:08:28 -04:00
|
|
|
$paste = helper::getPasteWithAttachment();
|
|
|
|
$this->_model->create(helper::getPasteId(), $paste);
|
2015-10-15 16:04:57 -04:00
|
|
|
$_SERVER['QUERY_STRING'] = helper::getPasteId();
|
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-15 16:04:57 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-15 16:04:57 -04:00
|
|
|
$response = json_decode($content, true);
|
|
|
|
$this->assertEquals(0, $response['status'], 'outputs success status');
|
2015-10-18 05:08:28 -04:00
|
|
|
$this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly');
|
|
|
|
$this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
|
|
|
|
$this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
|
|
|
|
$this->assertEquals($paste['attachment'], $response['attachment'], 'outputs attachment correctly');
|
|
|
|
$this->assertEquals($paste['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly');
|
|
|
|
$this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
|
|
|
|
$this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
|
|
|
|
$this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
|
|
|
|
$this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
|
|
|
|
$this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
|
2015-10-15 16:04:57 -04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:37:58 -04:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testJsonLdPaste()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$paste = helper::getPasteWithAttachment();
|
|
|
|
$this->_model->create(helper::getPasteId(), $paste);
|
|
|
|
$_GET['jsonld'] = 'paste';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-18 08:37:58 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-18 08:46:07 -04:00
|
|
|
$this->assertEquals(str_replace(
|
|
|
|
'?jsonld=',
|
|
|
|
'/?jsonld=',
|
|
|
|
file_get_contents(PUBLIC_PATH . '/js/paste.jsonld')
|
|
|
|
), $content, 'outputs data correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testJsonLdComment()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$paste = helper::getPasteWithAttachment();
|
|
|
|
$this->_model->create(helper::getPasteId(), $paste);
|
|
|
|
$_GET['jsonld'] = 'comment';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-18 08:46:07 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-18 08:46:07 -04:00
|
|
|
$this->assertEquals(str_replace(
|
|
|
|
'?jsonld=',
|
|
|
|
'/?jsonld=',
|
|
|
|
file_get_contents(PUBLIC_PATH . '/js/comment.jsonld')
|
|
|
|
), $content, 'outputs data correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testJsonLdPasteMeta()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$paste = helper::getPasteWithAttachment();
|
|
|
|
$this->_model->create(helper::getPasteId(), $paste);
|
|
|
|
$_GET['jsonld'] = 'pastemeta';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-18 08:46:07 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-18 08:46:07 -04:00
|
|
|
$this->assertEquals(str_replace(
|
|
|
|
'?jsonld=',
|
|
|
|
'/?jsonld=',
|
|
|
|
file_get_contents(PUBLIC_PATH . '/js/pastemeta.jsonld')
|
|
|
|
), $content, 'outputs data correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testJsonLdCommentMeta()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$paste = helper::getPasteWithAttachment();
|
|
|
|
$this->_model->create(helper::getPasteId(), $paste);
|
|
|
|
$_GET['jsonld'] = 'commentmeta';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-18 08:46:07 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-18 08:46:07 -04:00
|
|
|
$this->assertEquals(str_replace(
|
|
|
|
'?jsonld=',
|
|
|
|
'/?jsonld=',
|
|
|
|
file_get_contents(PUBLIC_PATH . '/js/commentmeta.jsonld')
|
|
|
|
), $content, 'outputs data correctly');
|
2015-10-18 08:37:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function testJsonLdInvalid()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$paste = helper::getPasteWithAttachment();
|
|
|
|
$this->_model->create(helper::getPasteId(), $paste);
|
|
|
|
$_GET['jsonld'] = '../cfg/conf.ini';
|
|
|
|
ob_start();
|
2016-07-11 05:58:15 -04:00
|
|
|
new privatebin;
|
2015-10-18 08:37:58 -04:00
|
|
|
$content = ob_get_contents();
|
2016-08-02 04:29:25 -04:00
|
|
|
ob_end_clean();
|
2015-10-18 08:37:58 -04:00
|
|
|
$this->assertEquals('{}', $content, 'does not output nasty data');
|
|
|
|
}
|
2016-07-06 05:37:13 -04:00
|
|
|
}
|