ulogger-server/.tests/tests/AuthTest.php

136 lines
4.7 KiB
PHP
Raw Normal View History

2017-09-05 09:36:10 +02:00
<?php
require_once(__DIR__ . "/../../helpers/auth.php");
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/config.php");
class AuthTest extends UloggerDatabaseTestCase {
2021-04-22 19:47:52 +02:00
public function setUp(): void {
2017-09-05 09:36:10 +02:00
$_SESSION = [];
parent::setUp();
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testLogin(): void {
2017-09-05 09:36:10 +02:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$auth = new uAuth();
2019-01-24 19:07:41 +01:00
$auth->checkLogin($this->testUser, $this->testPass);
2021-04-22 19:47:52 +02:00
self::assertTrue($auth->isAuthenticated(), "Not authenticated");
self::assertInstanceOf(uUser::class, $auth->user, "User variable not set");
self::assertEquals($this->testUser, $auth->user->login, "Wrong login");
self::assertEquals($_SESSION["user"]->login, $auth->user->login, "Wrong login");
self::assertInstanceOf(uUser::class, $_SESSION["user"], "User not set in session");
2017-09-05 09:36:10 +02:00
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testLoginBadPass(): void {
2017-09-05 09:36:10 +02:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$auth = new uAuth();
2019-01-24 19:07:41 +01:00
$auth->checkLogin($this->testUser, "badPass");
2021-04-22 19:47:52 +02:00
self::assertFalse($auth->isAuthenticated(), "Should not be authenticated");
self::assertNull($auth->user, "User not null");
2017-09-05 09:36:10 +02:00
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testLoginEmptyLogin(): void {
2017-09-05 09:36:10 +02:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$auth = new uAuth();
2019-01-24 19:07:41 +01:00
$auth->checkLogin("", $this->testPass);
2021-04-22 19:47:52 +02:00
self::assertFalse($auth->isAuthenticated(), "Should not be authenticated");
self::assertNull($auth->user, "User not null");
2017-09-05 09:36:10 +02:00
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testLoginNoFormData(): void {
2017-09-05 09:36:10 +02:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$auth = new uAuth();
2021-04-22 19:47:52 +02:00
self::assertFalse($auth->isAuthenticated(), "Should not be authenticated");
self::assertNull($auth->user, "User not null");
2017-09-05 09:36:10 +02:00
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testSessionAuth(): void {
2017-09-05 09:36:10 +02:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$user = new uUser($this->testUser);
2021-04-22 19:47:52 +02:00
self::assertTrue($user->isValid, "User not valid");
2017-09-05 09:36:10 +02:00
session_name("ulogger");
session_start();
$_SESSION["user"] = $user;
unset($user);
@$auth = new uAuth();
2021-04-22 19:47:52 +02:00
self::assertTrue($auth->isAuthenticated(), "Should be authenticated");
self::assertEquals($this->testUser, $auth->user->login, "Wrong login");
2017-09-05 09:36:10 +02:00
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testSessionAndRequest(): void {
2017-09-05 09:36:10 +02:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$user = new uUser($this->testUser);
2021-04-22 19:47:52 +02:00
self::assertTrue($user->isValid, "User not valid");
2017-09-05 09:36:10 +02:00
session_name("ulogger");
session_start();
$_SESSION["user"] = $user;
unset($user);
@$auth = new uAuth();
2019-01-24 19:07:41 +01:00
$auth->checkLogin($this->testUser, $this->testPass);
2021-04-22 19:47:52 +02:00
self::assertTrue($auth->isAuthenticated(), "Should be authenticated");
self::assertEquals($this->testUser, $auth->user->login, "Wrong login");
2017-09-05 09:36:10 +02:00
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testIsNotAdmin(): void {
2017-09-05 09:36:10 +02:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
@$auth = new uAuth();
2019-01-24 19:07:41 +01:00
$auth->checkLogin($this->testUser, $this->testPass);
2021-04-22 19:47:52 +02:00
self::assertTrue($auth->isAuthenticated(), "Should be authenticated");
self::assertFalse($auth->isAdmin(), "Should not be admin");
2017-09-05 09:36:10 +02:00
}
/**
* @runInSeparateProcess
*/
2021-04-22 19:47:52 +02:00
public function testIsAdmin(): void {
2020-02-17 18:51:27 +01:00
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT), true);
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
2017-09-05 09:36:10 +02:00
@$auth = new uAuth();
2019-01-24 19:07:41 +01:00
$auth->checkLogin($this->testUser, $this->testPass);
2021-04-22 19:47:52 +02:00
self::assertTrue($auth->isAuthenticated(), "Should be authenticated");
self::assertTrue($auth->isAdmin(), "Should be admin");
2017-09-05 09:36:10 +02:00
}
}
?>