-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from crosa7/create-auth-tests-structure
Added tests infrastructure, session lifetime and cookie params config
- Loading branch information
Showing
11 changed files
with
411 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: Run Tests | ||
|
||
on: ['push', 'pull_request'] | ||
|
||
env: | ||
MYSQL_DATABASE: leaf | ||
DB_USER: root | ||
DB_PASSWORD: root | ||
|
||
jobs: | ||
ci: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
php: ['7.4', '8.0', '8.1', '8.2'] | ||
|
||
name: PHP ${{ matrix.php }} - ${{ matrix.os }} | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Initialize MySQL | ||
run: sudo systemctl start mysql.service | ||
|
||
- name: Initialize first database | ||
run: | | ||
mysql -e 'CREATE DATABASE ${{ env.MYSQL_DATABASE }};' \ | ||
-u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }} | ||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
tools: composer:v2 | ||
coverage: xdebug | ||
|
||
- name: Install PHP dependencies | ||
run: composer update --no-interaction --no-progress | ||
|
||
- name: All Tests | ||
run: composer run-script test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./app</directory> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
beforeEach(function () { | ||
createUsersTable(); | ||
haveRegisteredUser('login-user', 'login-pass'); | ||
}); | ||
|
||
test('login should set user session', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig()); | ||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
$session = new \Leaf\Http\Session(false); | ||
$user = $session->get('AUTH_USER'); | ||
|
||
expect($user['username'])->toBe('login-user'); | ||
}); | ||
|
||
test('login should set session ttl', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig()); | ||
|
||
$timeBeforeLogin = time(); | ||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
$session = new \Leaf\Http\Session(false); | ||
$sessionTtl = $session->get('SESSION_TTL'); | ||
|
||
expect($sessionTtl > $timeBeforeLogin)->toBeTrue(); | ||
}); | ||
|
||
test('login should set regenerate session id', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig()); | ||
|
||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
$originalSessionId = session_id(); | ||
|
||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
expect(session_id())->not()->toBe($originalSessionId); | ||
}); | ||
|
||
test('login should set secure session cookie params', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig()); | ||
|
||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
$cookieParams = session_get_cookie_params(); | ||
|
||
expect($cookieParams['secure'])->toBeTrue(); | ||
expect($cookieParams['httponly'])->toBeTrue(); | ||
expect($cookieParams['samesite'])->toBe('lax'); | ||
}); | ||
|
||
test('register should set session ttl on login', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig()); | ||
|
||
$timeBeforeLogin = time(); | ||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
$session = new \Leaf\Http\Session(false); | ||
$sessionTtl = $session->get('SESSION_TTL'); | ||
|
||
expect($sessionTtl > $timeBeforeLogin)->toBeTrue(); | ||
}); | ||
|
||
test('Session should expire when fetching user, and then login is possible again', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig(['SESSION_LIFETIME' => 2])); | ||
|
||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
$user = $auth::user(); | ||
expect($user)->not()->toBeNull(); | ||
expect($user['username'])->toBe('login-user'); | ||
|
||
sleep(1); | ||
expect($auth::user())->not()->toBeNull(); | ||
|
||
sleep(2); | ||
expect($auth::user())->toBeNull(); | ||
|
||
$userAfterReLogin = $auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
expect($userAfterReLogin)->not()->toBeNull(); | ||
expect($userAfterReLogin['user']['username'])->toBe('login-user'); | ||
}); | ||
|
||
test('Session should not expire when fetching user if session lifetime is 0', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig(['SESSION_LIFETIME' => 0])); | ||
|
||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
$user = $auth::user(); | ||
expect($user)->not()->toBeNull(); | ||
expect($user['username'])->toBe('login-user'); | ||
|
||
sleep(2); | ||
expect($auth::user())->not()->toBeNull(); | ||
}); | ||
|
||
test('Session should expire when fetching user id', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig(['SESSION_LIFETIME' => 2])); | ||
|
||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
expect($auth::id())->not()->toBeNull(); | ||
|
||
sleep(1); | ||
expect($auth::id())->not()->toBeNull(); | ||
|
||
sleep(2); | ||
expect($auth::id())->toBeNull(); | ||
}); | ||
|
||
test('Session should expire when fetching status', function () { | ||
$auth = new \Leaf\Auth(); | ||
$auth::config(getAuthConfig(['SESSION_LIFETIME' => 2])); | ||
$auth::login(['username' => 'login-user', 'password' => 'login-pass']); | ||
|
||
expect($auth::status())->not()->toBeNull(); | ||
|
||
sleep(1); | ||
expect($auth::status())->not()->toBeNull(); | ||
|
||
sleep(2); | ||
expect($auth::status())->toBeFalse(); | ||
}); | ||
|
||
afterEach(function () { | ||
deleteUser('login-user'); | ||
}); |
Oops, something went wrong.