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 (20b84b88b84b6bb30f76fe7f5e80b23151ddb6f6) (3,496B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

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

$rg_sql_debug = 1;

rg_log_set_file("user.log");

$rg_user_max_len = 20;
$rg_session_time = 3600;
$rg_user_allow = '/^[A-Za-z0-9_.-]*$/';
$rg_admin_email = "admin@localhost";
$rg_admin_name = "RocketGit Admin";
$_SERVER['HTTP_HOST'] = "fake.tld";


$db = rg_sql_open("dbname=trg");
if ($db === FALSE) {
	echo "Cannot create a database (" . rg_sql_error() . ")!\n";
	exit(1);
}

$r = rg_sql_struct_update($db, RG_DROP_TABLES);
if ($r !== TRUE) {
	echo "Cannot create structure (" . rg_sql_error() . ")!\n";
	exit(1);
}

// Clean userA
$sql = "DELETE FROM users WHERE username = 'userA'";
$res = rg_sql_query($db, $sql);
rg_sql_free_result($res);

// add user
$_u['uid'] = 0;
$_u['realname'] = "userA real name";
$_u['username'] = "userA";
$_u['email'] = "rg@localhost";
$_u['pass'] = "pass1";
$_u['is_admin'] = 1;
$_u['disk_quota_mb'] = 100;
$_u['rights'] = "C";
$_u['session_time'] = 3600;
$_u['confirm_token'] = "";
$r = rg_user_edit($db, $_u);
if ($r !== TRUE) {
	echo "Cannot add user (" . rg_user_error() . ")!\n";
	exit(1);
}
$uid = rg_sql_last_id($db);

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

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

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

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

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

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

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

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

// test name2path
$rg_repos = "/base"; $user = "b"; $e = "/base/users/b/_/b";
$rr = array("prefix" => "/" . $user, "user" => $user);
$r = rg_user_name2path($rr);
if (strcmp($r, $e) != 0) {
	echo "name2path: e=[$e] != r=[$r]!\n";
	exit(1);
}

echo "user: OK!\n";
?>
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