xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / http_bug.php (6e83c6b0f1cf98d4eee4e44ff6bad3d958c64980) (8,302B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$test_normal = TRUE;

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/util.inc.php");
require_once("helpers.inc.php");
require_once("http.inc.php");

rg_log_set_file("http_bug.log");

require_once("common.php");

$_testns = 'http_bug';


prepare_http();

rg_test_create_user($db, $u1);
rg_test_create_user($db, $u2);
rg_test_create_repo($db, $u1, $repo);

$r = test_login($test_url, $u1);
if ($r === FALSE)
	exit(1);


rg_log('');
rg_log_enter("Loading bug form...");
$url = "/user/" . $u1['username'] . "/" . $repo['name'] . "/bug/add";
$headers = array();
$data = array();
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load add bug form.');
	exit(1);
}
$token = $r['tokens']['bug_edit_hl'];
if (empty($token)) {
	rg_log("Seems I cannot load a token. See above.");
	exit(1);
}
rg_log_exit();

rg_log('');
rg_log_enter("Posting bug form (token=$token)...");
$labels = array('a/b', 'uu::bb', '<xss>'); sort($labels);
$data = array(
	'doit' => 1,
	'edit' => 1,
	'token' => $token,
	'title' => 'Title1 space<xss>',
	'body' => 'aasasasassa<xss>',
	'state' => 1,
	'assigned_to' => $u2['username'],
	'labels' => implode(' ', $labels),
	'_test' => 'add bug');
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot post bug request.');
	exit(1);
}
$sql = 'SELECT * FROM bugs WHERE repo_id = ' . $repo['repo_id'];
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
if ($row['bug_id'] != 1) {
	rg_log("Seems the bugs do not start at 1, but " . $row['bug_id']);
	exit(1);
}
if ($row['assigned_uid'] != $u2['uid']) {
	rg_log('assigned_uid is not ' . $u2['uid']
		. ', but ' . $row['assigned_uid'] . '!');
	exit(1);
}
if ($row['state'] != 1) {
	rg_log('State is not 1 but ' . $row['state'] . '!');
	exit(1);
}
$sql = 'SELECT * FROM bug_labels WHERE repo_id = ' . $repo['repo_id']
	. ' AND bug_id = 1';
$res = rg_sql_query($db, $sql);
$g = array();
while (($row = rg_sql_fetch_array($res))) {
	$g[] = $row['label'];
}
rg_sql_free_result($res);
sort($g);
$p1 = implode(' ', $labels);
$p2 = implode(' ', $g);
if (strcmp($p1, $p2) != 0) {
	rg_log('Labels are different [' . $p1 . '] != [' . $p2 . ']!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter("Testing adding a note");

rg_log_enter("Loading bug/note form...");
$url = "/user/" . $u1['username'] . "/" . $repo['name'] . "/bug/1";
$data = array('_test' => 'adding a note');
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log("Cannot load bug page.");
	exit(1);
}
if (!isset($r['tokens']['note_add'])) {
	rg_log('note_add token not present!');
	exit(1);
}
$token = $r['tokens']['note_add'];
rg_log_exit();

rg_log_enter("Posting note add form (token=$token)...");
$note = 'This is a note<xss>';
$data = array('note_add_doit' => 1, 'token' => $token,
	'note' => $note,
	'_test' => 'adding a note');
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot post bug request.');
	exit(1);
}
$sql = 'SELECT * FROM bug_notes WHERE repo_id = ' . $repo['repo_id']
	. ' AND bug_id = 1';
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
if ($row['note'] != $note) {
	rg_log('Note is not ok [' . $note . '] != [' . $row['note'] . ']!');
	exit(1);
}
if ($row['uid'] != $u1['uid']) {
	rg_log('uid is not ok [' . $row['uid'] . '] != [' . $u1['uid'] . ']!');
	exit(1);
}
rg_log_exit();

rg_log_exit();


$token = $r['tokens']['close_reopen'];
rg_log('');
rg_log_enter("Posting close bug form (token=$token)...");
$data = array('token' => $token,
	'close_reopen' => 1,
	'state' => 2,
	'_test' => 'closing a bug');
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot post bug request.');
	exit(1);
}
$sql = 'SELECT * FROM bugs WHERE repo_id = ' . $repo['repo_id']
	. ' AND bug_id = 1';
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
if ($row['state'] != 2) {
	rg_log('Seems we could not close the bug; state=[' . $row['state'] . ']');
	exit(1);
}
rg_log_exit();

$token = $r['tokens']['close_reopen'];
rg_log('');
rg_log_enter("Posting re-open bug form (token=$token)...");
$data = array('token' => $token,
	'close_reopen' => 1,
	'state'=> 1,
	'_test' => 're-opn bug');
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot post bug request (check http_bug.out for last answer).');
	exit(1);
}
$sql = 'SELECT * FROM bugs WHERE repo_id = ' . $repo['repo_id']
	. ' AND bug_id = 1';
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
if ($row['state'] != 1) {
	rg_log('Seems we could not re-open the bug;'
		. ' state=[' . $row['state'] . ']!');
	exit(1);
}
rg_log_exit();

rg_log('');
rg_log_enter("Testing deleting a bug");
for ($i = 0; $i <= 1; $i++) {
	$token = $r['tokens']['del_undel'];
	rg_log("Posting delete bug form (token=$token)...");
	$data = array('token' => $token,
		'del_doit' => 1,
		'del_undel' => $i + 1,
		'_test' => 'deleting a bug (i=' . $i . ')');
	$r = do_req($test_url . $url, $data, $headers);
	if ($r === FALSE) {
		rg_log('Cannot post bug request.');
		exit(1);
	}
	$sql = 'SELECT * FROM bugs WHERE repo_id = ' . $repo['repo_id']
		. ' AND bug_id = 1';
	$res = rg_sql_query($db, $sql);
	$row = rg_sql_fetch_array($res);
	rg_sql_free_result($res);
	if ($i == 0)
		$test = $row['deleted'] > 0;
	else
		$test = $row['deleted'] == 0;
	if (!$test) {
		rg_log('i=' . $i . ': Seems we could not delete/undelete the bug;'
			. ' deleted=[' . $row['deleted'] . ']!');
		exit(1);
	}
	if ($row['deleted_who'] != $u1['uid']) {
		rg_log('i=' . $i . ': Seems the deleted_who has not been updated;'
			. ' deleted_who=[' . $row['deleted_who'] . ']!');
		exit(1);
	}
}
rg_log_exit();


rg_log('');
rg_log_enter("Test watching a bug - we need to login as a different user");
$r = test_login($test_url, $u2);
if ($r === FALSE)
	exit(1);
$headers = array();

// We are already in the watch list because the asignee is automatically added
for ($i = 0; $i <= 1; $i++) {
	rg_log("Loading bug form (i=$i)...");
	$data = array('_test' => 'load-form-watch-' . $i);
	$r = do_req($test_url . $url, $data, $headers);
	if ($r === FALSE) {
		rg_log('Cannot load bug form.');
		exit(1);
	}
	$token = $r['tokens']['watch_bug'];

	rg_log("Posting (un)watch bug form...");
	$data = array('token' => $token, 'watch_type' => 'bug',
		'watch_doit' => 1, 'watch' => $i,
		'_test' => 'post-watch-' . $i);
	$r = do_req($test_url . $url, $data, $headers);
	if ($r === FALSE) {
		rg_log('Cannot post watch bug request.');
		exit(1);
	}
	$sql = 'SELECT uid FROM watch_bug WHERE repo_id = ' . $repo['repo_id']
		. ' AND bug_id = 1';
	$res = rg_sql_query($db, $sql);
	$rows = rg_sql_num_rows($res);
	$row = array();
	while (($r = rg_sql_fetch_array($res)))
		$row[] = $r;
	rg_sql_free_result($res);

	if ($i == 0) { // unwatch - only rg_ui[uid] should be present
		if (($rows != 1) || ($row[0]['uid'] != $u1['uid'])) {
			rg_log_ml('Seems we could not watch the bug; row:'
				. print_r($row, TRUE));
			exit(1);
		}
	} else {
		$_l = array($u1['uid'], $u2['uid']);
		for ($j = 0; $j <= 1; $j++) {
			if (!in_array($row[$j]['uid'], $_l)) {
				rg_log('Seems I cannot unwatch a bug'
					. ' (' . $row[$j]['uid']
					. ' is not in the expected list)!');
				exit(1);
			}
		}
	}
}
rg_log_exit();


rg_log_enter('Search bugs: loading form...');
$url = '/user/' . $u1['username'] . '/' . $repo['name'] . '/bug/search';
$headers = array(); $data = array();
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load bug search page!');
	exit(1);
}
rg_log_exit();


rg_log_enter('Search bugs: ad a search...');
$url = '/user/' . $u1['username'] . '/' . $repo['name'] . '/bug/search';
$headers = array(); $data = array('doit' => '1');
$r = do_req($test_url . $url, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot do bug search!');
	exit(1);
}
if (!strstr($r['body'], 'Title1 space&lt;xss&gt;')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Search did not returned our bug!');
	exit(1);
}
rg_log_exit();


rg_prof_log();
rg_log('OK!');
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master