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_confirm.php (9b64bf221062c22476098b63a58a2aa5693d1cbe) (3,148B) (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_confirm.log");

require_once("common.php");

$_testns = 'http_confirm';


prepare_http();

$uniq = rg_id(10);

rg_log_enter('Loading confirmation form...');
// First we need to load the form so we can get the token
$r = do_req($test_url . "/op/create_account", $data, $headers);
if ($r === FALSE) {
	rg_log("Cannot load create_account page!");
	exit(1);
}
$good_token = $r['tokens']['user_edit_hl'];
$gen = $r['inputs']['gen'];
rg_log_exit();


rg_log_enter('Adding an account...');
$data['token'] = $r['tokens']['user_edit_hl'];
$data['username'] = 'http_confirm-' . rg_id(16);
$data['email'] = 'http_confirm-' . $uniq . '@a.b';
$data['pass'] = $uniq;
$data['pass2'] = $uniq;
$data['plan_id'] = 0;
$data['tos'] = 1;
$data['force_confirm'] = 1;
$data['gen'] = $gen;
$data['doit'] = 1;
$headers = array();
sleep(3); // we need to sleep, else we will trigger the bot protection
$r = do_req($test_url . '/op/create_account?t=create_account', $data, $headers);
if (!strstr($r['body'], 'Your account has been successfully created')) {
	rg_log_ml('body: ' . print_r($r['body'], TRUE));
	rg_log('Cannot create account!');
	exit(1);
}
rg_log_exit();


rg_log_enter('Finding uid...');
$sql = 'SELECT uid FROM users WHERE username = \'' . $data['username'] . '\'';
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
	rg_log('Query error!');
	exit(1);
}
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$uid = $row['uid'];
rg_log_exit();


rg_log_enter('Finding confirmation link...');
$key = 'DEBUG::' . $uid . '::mail';
$c = test_wait_cache($key);
rg_log_ml('c: ' . print_r($c, TRUE));
$c = $c['mail/user/welcome'];
$t = explode('below:', $c['body']);
if (!isset($t[1])) {
	rg_log_ml('r: ' . print_r($c['body'], TRUE));
	rg_log('seems I cannot explode by \'below:\'!');
	exit(1);
}
$t = ltrim($t[1]);
$t = explode("\n", $t);
$link = $t[0];
rg_log('Link=' . $link);
rg_log_exit();


rg_log_enter('Trying with the wrong link...');
$data = array(); $headers = array();
$r = do_req($link . 'XXX?t=http_confirm', $data, $headers);
if (!strstr($r['body'], 'Invalid token')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Something wrong with the token!');
	exit(1);
}
rg_log_exit();


rg_log_enter('Trying with the good link (first time)...');
$r = do_req($link . '?t=http_confirm1', $data, $headers);
if (!strstr($r['body'], 'Your e-mail is confirmed now.')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Something wrong with the token!');
	exit(1);
}
rg_log_exit();


rg_log_enter('Trying with the good link (second time)...');
$r = do_req($link . '?t=http_confirm2', $data, $headers);
if (!strstr($r['body'], 'Invalid token.')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Reusing a confirmation token must not work!');
	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