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 / user.php (f516f82892251279f68bdfeacf487bdd8f79717e) (4,870B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/user.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/struct.inc.php");
require_once($INC . "/fixes.inc.php");

rg_log_set_file("user.log");

require_once("common.php");

$rg_sql_debug = 1;


// Defaults
$rg_repos = "ubase";
$rg_user_max_len = 20;
$rg_session_time = 3600;
$rg_user_allow = 'A-Za-z0-9_.-';
$rg_admin_email = "rg@embedromix.ro";
$rg_admin_name = "RocketGit Admin";
$_SERVER['HTTP_HOST'] = "fake.tld";


$r = rg_exec("rm -rf ubase");
if ($r['ok'] != 1) {
	rg_log("Cannot remove ubase dir (" . $r['errmsg'] . ")!");
	exit(1);
}

$sql = "DELETE FROM users";
$res = rg_sql_query($db, $sql);
rg_sql_free_result($res);

// Test if empty uid/user/email returns error
$_ui = rg_user_info($db, 0, "", "");
if ($_ui['ok'] == 1) {
	rg_log("Seems that invalid user does not return error!");
	exit(1);
}


// add user
$_u['uid'] = 0;
$_u['realname'] = "userA real name";
$_u['username'] = "userA";
$_u['email'] = "rg@localhost";
$_u['pass'] = "pass1";
$_u['pass2'] = "pass1";
$_u['is_admin'] = 1;
$_u['rights'] = "C";
$_u['session_time'] = 3600;
$_u['confirm_token'] = "";
$_u['confirmed'] = 0;
$_u['plan_id'] = 1000;
$uid = rg_user_edit($db, $_u);
if ($uid === FALSE) {
	rg_log("Cannot add user (" . rg_user_error() . ")!");
	exit(1);
}

// Simulate event: link_by_name
$ev = array('ui' => array('uid' => $uid, 'username' => $_u['username']));
$r = rg_user_link_by_name($db, $ev);
if ($r === FALSE) {
	rg_log("Cannot link by name (" . rg_user_error() . ")!");
	exit(1);
}

$v = rg_user_forgot_pass_mail_prepare($db, "rg@localhost");
if (empty($v['token'])) {
	print_r($v);
	rg_log("Error(rg_user_forgot_pass_mail): " . rg_user_error() . "!");
	exit(1);
}

$_ui = rg_user_info($db, $uid, "", "");
if ($_ui['exists'] != 1) {
	rg_log("Seems that user with $uid does not exists!");
	exit(1);
}
$pass = $_ui['pass'];

// edit user - empty pass
$_u['uid'] = $uid;
$_u['pass'] = '';
$r = rg_user_edit($db, $_u);
if ($r === FALSE) {
	rg_log("Cannot edit user with empty pass (" . rg_user_error() . ")!");
	exit(1);
}
// the pass should not be changed here
$_ui = rg_user_info($db, $uid, '', '');
if ($_ui['exists'] != 1) {
	rg_log("Seems that user with $uid does not exists!");
	exit(1);
}
if (strcmp($pass, $_ui['pass']) != 0) {
	rg_log("Password has been changed! Bad!");
	exit(1);
}

// edit user - no empty pass
$_u['pass'] = "pass2";
$_u['pass2'] = "pass2";
$r = rg_user_edit($db, $_u);
if ($r === FALSE) {
	rg_log("Cannot edit user with not empty pass (" . rg_user_error() . ")!");
	exit(1);
}
// the pass should be changed here
$salt = $_ui['salt'];
$_ui = rg_user_info($db, $uid, "", "");
if ($_ui['exists'] != 1) {
	rg_log("Seems that user with $uid does not exists!");
	exit(1);
}
if (strcmp($salt, $_ui['salt']) == 0) {
	rg_log("Salt has not been changed ($salt)!");
	exit(1);
}
if (strcmp($pass, $_ui['pass']) == 0) {
	rg_log("Password has not been changed!");
	exit(1);
}

// change password
$r = rg_user_set_pass($db, $uid, "pass3");
if ($r !== TRUE) {
	rg_log("Cannot change pass!");
	exit(1);
}

// get token for e-mail forgot pass feature
$r = rg_user_forgot_pass_mail_prepare($db, "rg@localhost");
if (empty($r['token'])) {
	rg_log("Could not get token (" . rg_user_error() . ")!");
	exit(1);
}

$r = rg_user_forgot_pass_uid($db, $r['token']);
if ($r['ok'] != 1) {
	rg_log("Cannot find uid based on token (" . rg_user_error() . ")!");
	exit(1);
}

if ($r['uid'] != $uid) {
	rg_log("Token returned does not belong to the proper user!");
	exit(1);
}

// test rg_user_path
$x = sprintf("%08X", $uid);
$e = "ubase/by_id/" . substr($x, 0, 2) . "/" . substr($x, 2, 2)
	. "/" . substr($x, 4, 2) . "/" . substr($x, 6, 2) . "/" . $x;
$r = rg_user_path_by_id($uid);
if (strcmp($r, $e) != 0) {
	rg_log("path_by_id: e=[$e] != r=[$r]!");
	exit(1);
}

// test renames
$r = rg_user_rename($db, $_ui, "userA2");
if ($r === FALSE) {
	rg_log("Cannot rename user (" . rg_user_error() . ")!");
	exit(1);
}

rg_log("Testing fixes...");
// add user5
$_u['uid'] = 0;
$_u['realname'] = "user5 real name";
$_u['username'] = "user5";
$_u['email'] = "rg@localhost";
$_u['pass'] = "pass1";
$_u['pass2'] = "pass1";
$_u['is_admin'] = 1;
$_u['rights'] = "C";
$_u['session_time'] = 3600;
$_u['confirm_token'] = "";
$_u['confirmed'] = 0;
$_u['plan_id'] = 1000;
$uid5 = rg_user_edit($db, $_u);
if ($uid5 === FALSE) {
	rg_log("Cannot add user5 (" . rg_user_error() . ")!");
	exit(1);
}

$r = system("mkdir -p ubase/users/u/s/user5");
if ($r === FALSE) {
	rg_log("Cannot make fixes simulation dir ($php_errormsg)!");
	exit(1);
}
$r = rg_fixes_user_index_by_id($db);
if ($r === FALSE) {
	rg_log("cannot re-index by id!");
	exit(1);
}

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