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_settings.php (533023a653e23ee99071fab131df22ec886e1458) (6,217B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$rg_cache_debug = TRUE;
$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_settings.log");

require_once("common.php");

$_testns = 'http_settings';


prepare_http();

$now = time();

rg_log_enter("Testing if caching works: cache_enable=" . ($rg_cache_enable ? "true" : "false"));
rg_cache_set("test::a", "1", 0);
rg_cache_core_unset("test::a"); // to force "network" access = bypass mem cache
$r = rg_cache_get("test::a");
if (strcmp($r, "1") != 0) {
	rg_log("Main cache (set) is not working!");
	exit(1);
}
rg_cache_unset("test::a", RG_SOCKET_NO_WAIT);
rg_cache_core_unset("test::a"); // to force "network" access = bypass mem cache
$r = rg_cache_get("test::a");
if ($r !== FALSE) {
	rg_log("Main cache (unset) is not working!");
	exit(1);
}
rg_log_exit();

test_restore($db);

rg_test_create_user($db, $rg_ui);
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}

rg_log('');
rg_log_enter("Loading change pass form");
$data = array();
$headers = array();
$r = do_req($test_url . "/op/settings/change_pass?t=load_change_pass_form", $data, $headers);
if (!strstr($r['body'], "action=\"/op/settings/change_pass\"")) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Cannot load change pass form!");
	exit(1);
}
$good_token = $r['tokens']['set_pass'];
rg_log_exit();

rg_log('');
rg_log_enter('Posting change pass form');
$data = array(
	"doit" => 1,
	"token" => $good_token,
	"old_pass" => $rg_ui['pass'],
	"pass1" => "bbbb",
	"pass2" => "bbbb"
	);
$headers = array();
$r = do_req($test_url . "/op/settings/change_pass?t=post_change_pass_form", $data, $headers);
if (!strstr($r['body'], "Password has been successfully updated")) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Cannot change pass!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Now, try to login with the old password');
$r2 = test_login($test_url, $rg_ui);
if ($r2 !== FALSE) {
	rg_log("Seems we were able to login with the old password!");
	exit(1);

}
rg_log_exit();


rg_log('');
rg_log_enter('Change back the password');
$data = array();
$headers = array();
$r = do_req($test_url . "/op/settings/change_pass?t=load_change_pass_form", $data, $headers);
if (!strstr($r['body'], "action=\"/op/settings/change_pass\"")) {
	rg_log("Cannot load change pass form!");
	exit(1);
}
$good_token = $r['tokens']['set_pass'];
$data = array(
	"doit" => 1,
	"token" => $good_token,
	"old_pass" => "bbbb",
	"pass1" => $rg_ui['pass'],
	"pass2" => $rg_ui['pass']
	);
$headers = array();
$r = do_req($test_url . "/op/settings/change_pass?t=change_back_the_password", $data, $headers);
if ($r === FALSE) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Cannot change back the pass to aaaa!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter("Testing edit info section");

rg_log('');
rg_log_enter('Loading edit info form');
$data = array();
$headers = array();
$r = do_req($test_url . "/op/settings/edit_info?t=load_edit_info_form", $data, $headers);
if ($r === FALSE) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Cannot load form!");
	exit(1);
}
rg_log_exit();

rg_log('');
rg_log_enter('Posting edit info form');
$session_time = intval($now / 393956);
$data = array(
	"doit" => 1,
	"token" => $r['tokens']['user_edit_hl'],
	"uid" => 4,
	"username" => $rg_ui['username'],
	"realname" => $rg_ui['realname'],
	"plan_id" => 5,
	"session_time" => $session_time
	);
$headers = array();
$r = do_req($test_url . "/op/settings/edit_info?t=post_edit_info_form", $data, $headers);
if (!strstr($r['body'], "Information has been successfully updated")) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Cannot change account info!");
	exit(1);
}
rg_log_exit();

rg_log('');
rg_log_enter('Verify against database');
$sql = "SELECT * FROM users WHERE username = '" . $rg_ui['username'] . "'";
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
if (strcmp($rg_ui['realname'], $row['realname']) != 0) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log_ml("realname has not been changed: " . print_r($row, TRUE));
	exit(1);
}
if ($row['plan_id'] != 5) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log_ml("plan_id has not been changed: " . print_r($row, TRUE));
	exit(1);
}
if ($row['session_time'] != $session_time) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log_ml("session_time has not been changed: " . print_r($row, TRUE));
	exit(1);
}
rg_log_exit();

rg_log_exit();


rg_log('');
rg_log_enter('Testing SSH keys');
$key = rg_test_upload_ssh_key($db, $rg_ui, 'http_settings', $kn);
$key = trim($key);
// the key upload stuff will change < and > to empty.
$sql = "SELECT * FROM keys WHERE key = '" . $key . "'";
$res = rg_sql_query($db, $sql);
$rows = rg_sql_num_rows($res);
if ($rows > 0)
	$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
if ($rows == 0) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Key has not been uploaded!");
	exit(1);
}
$key_id = $row['key_id'];
rg_log_exit();


rg_log('');
rg_log_enter('Now, testing deletion: key_id=' . $key_id);
rg_log("Loading ssh keys form");
$data = array();
$headers = array();
$r = do_req($test_url . "/op/settings/keys?t=load_key_form_del", $data, $headers);
if ($r === FALSE) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Cannot load ssh key form!");
	exit(1);
}
rg_log("Posting delete keys form");
$data = array("delete" => 1, "token" => $r['tokens']['keys'], "key_delete_ids[$key_id]" => "on");
$headers = array();
$r = do_req($test_url . "/op/settings/keys?t=post_key_form_del", $data, $headers);
if (!strstr($r['body'], "success")) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("Cannot delete key!");
	exit(1);
}
$sql = "SELECT * FROM keys WHERE key_id = $key_id";
$res = rg_sql_query($db, $sql);
$rows = rg_sql_num_rows($res);
rg_sql_free_result($res);
if ($rows == 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log("key $key_id has not been deleted!");
	exit(1);
}
rg_log_exit();

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