Skip to content

Commit

Permalink
DOCS(AuthTest): unit test comments updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovsiankina committed Apr 23, 2024
1 parent d7274de commit daace23
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 197 deletions.
48 changes: 24 additions & 24 deletions orif/user/Controllers/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public function prepare_mail_form(): string {
$correspondingEmail = $correspondingUser['email'];
}


// send correspondance if found for the auto complete
$output = array(
'title' => lang('user_lang.title_page_login'),
Expand Down Expand Up @@ -394,19 +395,8 @@ public function verify_verification_code() {
if ($_SESSION['new_user'] == true) {

// A new user needs to be created in the db
// Receive array $user from register_user()
$new_user = $this->register_user();
return $this->register_user();

// insert this new user
$this->user_model->insert($new_user);

// Force user to change password on next 'normal' login

$_SESSION['logged_in'] = (bool)true;

// TODO: Afficher formulaire creation user avec infos pré-remplies (save_user)
// TODO : Route differente, remplacer after_login_redirect

} else {

// User already in DB => Update azure_mail in DB
Expand Down Expand Up @@ -452,6 +442,10 @@ public function verify_verification_code() {
return $this->reset_session();
}

/**
* This method has been moved to Profile::update()
*/

public function register_user() {

$user_type_model = new User_type_model();
Expand All @@ -468,27 +462,33 @@ public function register_user() {
$new_username = explode('@', $_SESSION['azure_mail']);
$new_username = substr($new_username[0], 0, $username_max_length);

// Generate random password ?? why

// Generating a random password
$password_max_lenght = $user_config->password_max_length;
$new_password = '';
$characters = '0123456789abcdefghijklmnopqrstuvwxyz'
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-={}[]|:;"<>,.?/~`';
// $password_max_lenght = $user_config->password_max_length;
// $new_password = '';
// $characters = '0123456789abcdefghijklmnopqrstuvwxyz'
// .'ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-={}[]|:;"<>,.?/~`';

// for ($i = 0; $i < $password_max_lenght; $i++) {
// $new_password .= $characters[rand(0, strlen($characters) - 1)];
// }

for ($i = 0; $i < $password_max_lenght; $i++) {
$new_password .= $characters[rand(0, strlen($characters) - 1)];
}
$reset_password = True;
$new_user = array(
'fk_user_type' => $new_user_type['id'],
'username' => $new_username,
'password' => $new_password,
'password_confirm' => $new_password,
'reset_password' => $reset_password,
'email' => $_SESSION['form_email'],
'azure_mail' => $_SESSION['azure_mail'],
'title' => lang('user_lang.title_user_info'),
);

return $new_user;
// Insert new user in DB
// $this->user_model->insert($new_user);

return $this->display_view(
'\User\auth\azure_update_form',
$new_user
);
}

public function reset_session() {
Expand Down
13 changes: 13 additions & 0 deletions orif/user/Controllers/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use CodeIgniter\HTTP\RedirectResponse;
use User\Models\User_type_model;

class Profile extends BaseController {

Expand Down Expand Up @@ -93,5 +94,17 @@ public function change_password(): Response|string|RedirectResponse {
return $this->display_view('\User\auth\change_password', $output);

}

/**
* Displays a form to let user change his password
*
* @return void
*/
public function update_form() {
$username = $this->request->getPost('username');
$password = $this->request->getPost('password_new');
$password_c = $this->request->getPost('password_confirm');

}
}
?>
3 changes: 3 additions & 0 deletions orif/user/Language/fr/user_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'title_administration' => 'Administration',
'title_register_account' => 'Enregistrer votre compte',
'title_email_validation' => 'Validation de l\'e-mail',
'title_user_info' => 'Vos informations personnelles',

// Buttons
'btn_admin' => 'Administration',
Expand All @@ -36,6 +37,7 @@
'field_username' => 'Identifiant',
'field_password' => 'Mot de passe',
'field_email' => 'Adresse e-mail',
'field_microsoft_email' => 'Adresse e-mail microsoft',
'field_old_password' => 'Ancien mot de passe',
'field_new_password' => 'Nouveau mot de passe',
'field_password_confirm' => 'Confirmer le mot de passe',
Expand Down Expand Up @@ -72,6 +74,7 @@
'user_delete_himself' => 'Vous ne pouvez pas désactiver ou supprimer votre propre compte. Cette opération doit être faite par un autre administrateur.',
'page_my_password_change' => 'Modification de mon mot de passe',
'page_force_password_change' => 'Veuillez modifier votre mot de passe avant de poursuivre.',
'page_username_choice' => 'Il vous ait possible de choisir un identifiant différent.<br>',

// Azure

Expand Down
134 changes: 134 additions & 0 deletions orif/user/Views/auth/azure_update_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* change_password view
*
* @author Orif (ViDi,HeMa)
* @link https://github.com/OrifInformatique
* @copyright Copyright (c), Orif (https://www.orif.ch)
*/

?>
<div class="container">
<div class="row">
<div class="col-md-10 well">
<?php
$validation=\Config\Services::validation();
$attributes = array("class" => "form-horizontal",
"id" => "update_form",
"name" => "update_form");
echo form_open("user/profile/update_form", $attributes);
?>
<fieldset>
<legend><?= $title; ?></legend>

<!-- ERROR MESSAGES -->
<?php if(isset($errors)) {
foreach ($errors as $error) { ?>
<div class="alert alert-danger" role="alert">
<?= $error ?>
</div>
<?php } ?>
<?php } ?>

<div class="alert alert-info">
<?= lang('user_lang.page_username_choice'); ?>
</div>

<!-- Possibility to change username -->
<div class="form-group">
<div class="row colbox">
<div class="col-md-4">
<label for="username" class="control-label">
<?= lang('user_lang.field_username'); ?>
</label>
</div>
<div class="col-md-8">
<input
id="username"
name="username"
type="text"
class="form-control"
placeholder="<?= lang('user_lang.field_username'); ?>"
value="<?= $username, set_value('new_username'); ?>"
>
</div>
</div>
</br>

<!-- Display user info -->
<div class="row colbox">
<div class="col-md-4">
<label for="mail" class="control-label">
<?= lang('user_lang.field_email'); ?>
</label>
</div>
<div class="col-md-8">
<input
id="email"
name="email"
type="email"
class="form-control"
placeholder="<?= lang('user_lang.field_email'); ?>"
value="<?= $email; ?>"
readonly
>
</div>
</div>
</br>
<div class="row colbox">
<div class="col-md-4">
<label for="mail" class="control-label">
<?= lang('user_lang.field_microsoft_email'); ?>
</label>
</div>
<div class="col-md-8">
<input
id="ms_email"
name="ms_email"
type="mail"
class="form-control"
placeholder="<?= lang('user_lang.field_microsoft_email'); ?>"
value="<?= $azure_mail; ?>"
readonly
>
</div>
</div>
</br>

<!-- PASSWORD -->
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<?= form_label(lang('user_lang.field_new_password'), 'password_new', ['class' => 'form-label']); ?>
<?= form_password('password_new', '', [
'class' => 'form-control', 'id' => 'password_new',
'maxlength' => config('\User\Config\UserConfig')->password_max_length
]); ?>
</div>
</div>
</br>
<div class="col-sm-6">
<div class="form-group">
<?= form_label(lang('user_lang.field_password_confirm'), 'password_confirm', ['class' => 'form-label']); ?>
<?= form_password('password_confirm', '', [
'class' => 'form-control', 'id' => 'password_confirm',
'maxlength' => config('\User\Config\UserConfig')->password_max_length
]); ?>
</div>
</div>
</div>
</div>

<!-- Submit button-->
<div class="form-group">
<div class="col-md-12 text-right">
<a id="btn_cancel" class="btn btn-secondary" href="<?= base_url(); ?>"><?= lang('common_lang.btn_cancel'); ?></a>
<input id="btn_update_form" name="btn_update_form" type="submit" class="btn btn-primary" value="<?= lang('common_lang.btn_save'); ?>" />
</div>
</div>

</fieldset>
<?= form_close(); ?>
</div>
</div>
</div>
6 changes: 3 additions & 3 deletions orif/user/Views/auth/verification_code_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
echo form_open("user/auth/verify_verification_code", $attributes);
?>
<fieldset>
<!-- S tatus messages -->
<!-- Status messages -->
<?php if(!is_null($session->getFlashdata('message-danger'))){ ?>
<div class="alert alert-danger text-center"><?= $session->getFlashdata('message-danger'); ?></div>
<?php } ?>
Expand Down Expand Up @@ -75,7 +75,7 @@ function startCountdown(timeRemaining, display) {

</script>
</div>

<div class="form-group">
<input class="form-control" id="user_verification_code" name="user_verification_code" placeholder="<?= lang('user_lang.field_verification_code'); ?>" type="text" value="<?= set_value('username'); ?>" />
</div>
Expand Down Expand Up @@ -103,4 +103,4 @@ function startCountdown(timeRemaining, display) {
<?= form_close(); ?>
</div>
</div>
</div>
</div>
18 changes: 18 additions & 0 deletions tests/orif/user/Controllers/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ private function get_session_data()
return $data;
}

/**
* Custom assertions
*/

/**
* Function which asserts if a response (HTTP) exists and that it
* has a header.
*/
private function get_response_and_assert(TestResponse $result)
: Response
{
Expand All @@ -50,6 +58,12 @@ private function get_response_and_assert(TestResponse $result)
return $result->response();
}

/*
* Function which asserts that get_response_and_assert() returns an object
* that is an insance of the class `Response`.
*
* Assert the response (HTTP) has a body.
*/
private function assert_reponse(TestResponse $result): void
{
$response = $this->get_response_and_assert($result);
Expand All @@ -58,6 +72,10 @@ private function assert_reponse(TestResponse $result): void

}

/**
* Function which asserts that a response (HTTP) is an object of type
* RedirectResponse and that it has an empty body
*/
private function assert_redirect(TestResponse $result): void
{
$response = $this->get_response_and_assert($result);
Expand Down
Loading

0 comments on commit daace23

Please sign in to comment.