2017-09-24 16:45:07 +00:00
|
|
|
<?php
|
2017-11-17 01:51:03 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-09-24 16:45:07 +00:00
|
|
|
declare(strict_types=1);
|
2021-04-14 15:17:01 +00:00
|
|
|
|
2018-11-23 15:01:17 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Database;
|
2017-09-24 16:45:07 +00:00
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
|
|
|
|
|
|
|
trait SeriesSession {
|
2020-01-20 18:52:48 +00:00
|
|
|
protected function setUpSeriesSession(): void {
|
2018-01-01 17:31:42 +00:00
|
|
|
// set up the configuration
|
2018-11-25 05:03:56 +00:00
|
|
|
static::setConf([
|
2018-01-01 17:31:42 +00:00
|
|
|
'userSessionTimeout' => "PT1H",
|
|
|
|
'userSessionLifetime' => "PT24H",
|
|
|
|
]);
|
2017-09-24 16:45:07 +00:00
|
|
|
// set up the test data
|
2020-03-01 20:16:50 +00:00
|
|
|
$past = gmdate("Y-m-d H:i:s", strtotime("now - 1 minute"));
|
2017-09-24 16:45:07 +00:00
|
|
|
$future = gmdate("Y-m-d H:i:s", strtotime("now + 1 minute"));
|
|
|
|
$faroff = gmdate("Y-m-d H:i:s", strtotime("now + 1 hour"));
|
|
|
|
$old = gmdate("Y-m-d H:i:s", strtotime("now - 2 days"));
|
|
|
|
$this->data = [
|
|
|
|
'arsse_users' => [
|
|
|
|
'columns' => [
|
|
|
|
'id' => 'str',
|
|
|
|
'password' => 'str',
|
2020-10-30 19:25:22 +00:00
|
|
|
'num' => 'int',
|
2017-09-24 16:45:07 +00:00
|
|
|
],
|
|
|
|
'rows' => [
|
2020-10-30 19:25:22 +00:00
|
|
|
["jane.doe@example.com", "",1],
|
|
|
|
["john.doe@example.com", "",2],
|
2017-09-24 16:45:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arsse_sessions' => [
|
|
|
|
'columns' => [
|
|
|
|
'id' => "str",
|
|
|
|
'user' => "str",
|
|
|
|
'created' => "datetime",
|
|
|
|
'expires' => "datetime",
|
|
|
|
],
|
|
|
|
'rows' => [
|
|
|
|
["80fa94c1a11f11e78667001e673b2560", "jane.doe@example.com", $past, $faroff],
|
|
|
|
["27c6de8da13311e78667001e673b2560", "jane.doe@example.com", $past, $past], // expired
|
|
|
|
["ab3b3eb8a13311e78667001e673b2560", "jane.doe@example.com", $old, $future], // too old
|
|
|
|
["da772f8fa13c11e78667001e673b2560", "john.doe@example.com", $past, $future],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
protected function tearDownSeriesSession(): void {
|
2018-11-25 05:03:56 +00:00
|
|
|
unset($this->data);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testResumeAValidSession(): void {
|
2017-09-24 16:45:07 +00:00
|
|
|
$exp1 = [
|
2020-03-01 20:16:50 +00:00
|
|
|
'id' => "80fa94c1a11f11e78667001e673b2560",
|
|
|
|
'user' => "jane.doe@example.com",
|
2017-09-24 16:45:07 +00:00
|
|
|
];
|
|
|
|
$exp2 = [
|
2020-03-01 20:16:50 +00:00
|
|
|
'id' => "da772f8fa13c11e78667001e673b2560",
|
|
|
|
'user' => "john.doe@example.com",
|
2017-09-24 16:45:07 +00:00
|
|
|
];
|
|
|
|
$this->assertArraySubset($exp1, Arsse::$db->sessionResume("80fa94c1a11f11e78667001e673b2560"));
|
|
|
|
$this->assertArraySubset($exp2, Arsse::$db->sessionResume("da772f8fa13c11e78667001e673b2560"));
|
|
|
|
$now = time();
|
|
|
|
// sessions near timeout should be refreshed automatically
|
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_sessions' => ["id", "created", "expires", "user"]]);
|
|
|
|
$state['arsse_sessions']['rows'][3][2] = Date::transform(Date::add(Arsse::$conf->userSessionTimeout, $now), "sql");
|
2019-06-21 22:52:27 +00:00
|
|
|
$this->compareExpectations(static::$drv, $state);
|
2017-09-24 16:45:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testResumeAMissingSession(): void {
|
2017-09-24 16:45:07 +00:00
|
|
|
$this->assertException("invalid", "User", "ExceptionSession");
|
|
|
|
Arsse::$db->sessionResume("thisSessionDoesNotExist");
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testResumeAnExpiredSession(): void {
|
2017-09-24 16:45:07 +00:00
|
|
|
$this->assertException("invalid", "User", "ExceptionSession");
|
|
|
|
Arsse::$db->sessionResume("27c6de8da13311e78667001e673b2560");
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testResumeAStaleSession(): void {
|
2017-09-24 16:45:07 +00:00
|
|
|
$this->assertException("invalid", "User", "ExceptionSession");
|
|
|
|
Arsse::$db->sessionResume("ab3b3eb8a13311e78667001e673b2560");
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testCreateASession(): void {
|
2017-09-24 16:45:07 +00:00
|
|
|
$user = "jane.doe@example.com";
|
|
|
|
$id = Arsse::$db->sessionCreate($user);
|
|
|
|
$now = time();
|
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_sessions' => ["id", "created", "expires", "user"]]);
|
|
|
|
$state['arsse_sessions']['rows'][] = [$id, Date::transform($now, "sql"), Date::transform(Date::add(Arsse::$conf->userSessionTimeout, $now), "sql"), $user];
|
2019-06-21 22:52:27 +00:00
|
|
|
$this->compareExpectations(static::$drv, $state);
|
2017-09-24 16:45:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testDestroyASession(): void {
|
2017-09-24 16:45:07 +00:00
|
|
|
$user = "jane.doe@example.com";
|
|
|
|
$id = "80fa94c1a11f11e78667001e673b2560";
|
|
|
|
$this->assertTrue(Arsse::$db->sessionDestroy($user, $id));
|
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_sessions' => ["id", "created", "expires", "user"]]);
|
|
|
|
unset($state['arsse_sessions']['rows'][0]);
|
2019-06-21 22:52:27 +00:00
|
|
|
$this->compareExpectations(static::$drv, $state);
|
2017-09-24 16:45:07 +00:00
|
|
|
// destroying a session which does not exist is not an error
|
|
|
|
$this->assertFalse(Arsse::$db->sessionDestroy($user, $id));
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testDestroyAllSessions(): void {
|
2019-07-26 02:34:58 +00:00
|
|
|
$user = "jane.doe@example.com";
|
|
|
|
$this->assertTrue(Arsse::$db->sessionDestroy($user));
|
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_sessions' => ["id", "created", "expires", "user"]]);
|
|
|
|
unset($state['arsse_sessions']['rows'][0]);
|
|
|
|
unset($state['arsse_sessions']['rows'][1]);
|
|
|
|
unset($state['arsse_sessions']['rows'][2]);
|
|
|
|
$this->compareExpectations(static::$drv, $state);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testDestroyASessionForTheWrongUser(): void {
|
2017-09-24 16:45:07 +00:00
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$id = "80fa94c1a11f11e78667001e673b2560";
|
|
|
|
$this->assertFalse(Arsse::$db->sessionDestroy($user, $id));
|
|
|
|
}
|
|
|
|
}
|