Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Oct 31, 2024
1 parent 44dfa9d commit 9d965dc
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: json, zip, dom, curl, libxml, mbstring
extensions: json, zip, dom, curl, libxml, mbstring, PDO_PGSQL
tools: composer:v2
coverage: xdebug

Expand Down
2 changes: 1 addition & 1 deletion alchemy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ actions:
- macos-latest
- windows-latest
php:
extensions: json, zip, dom, curl, libxml, mbstring
extensions: json, zip, dom, curl, libxml, mbstring, PDO_PGSQL
versions:
- '8.3'
- '8.2'
Expand Down
20 changes: 16 additions & 4 deletions tests/login.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,38 @@
dbInstance()->delete('users')->execute();
});

test('user can login', function (array $testUser) {
test('user can login', function () {
$auth = authInstance();

$testUser = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->login($testUser);

expect($success)->toBeTrue();
expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class);
expect($auth->user()->username)->toBe($testUser['username']);
})->with('test-user');
});

test('login generates tokens on success', function (array $testUser) {
test('login generates tokens on success', function () {
$auth = authInstance();

$testUser = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->login($testUser);

expect($success)->toBeTrue();
expect($auth->data())->not()->toBeNull();
expect($auth->data()->accessToken)->toBeString();
expect($auth->data()->refreshToken)->toBeString();
})->with('test-user');
});

test('login fails with incorrect password', function () {
$auth = authInstance();
Expand Down
40 changes: 32 additions & 8 deletions tests/register.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
dbInstance()->delete('users')->execute();
});

test('user can register an account', function (array $userData) {
test('user can register an account', function () {
$auth = authInstance();

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->register($userData);

if (!$success) {
Expand All @@ -21,11 +27,17 @@
expect($success)->toBeTrue();
expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class);
expect($auth->user()->username)->toBe($userData['username']);
})->with('test-user');
});

test('user can login after registering', function (array $userData) {
test('user can login after registering', function () {
$auth = authInstance();

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$registerSuccess = $auth->register($userData);

expect($registerSuccess)->toBeTrue();
Expand All @@ -35,11 +47,17 @@
expect($loginSuccess)->toBeTrue();
expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class);
expect($auth->user()->username)->toBe($userData['username']);
})->with('test-user');
});

test('user can only sign up once', function (array $userData) {
test('user can only sign up once', function () {
$auth = authInstance();

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$auth->config([
'unique' => ['email', 'username']
]);
Expand All @@ -56,11 +74,17 @@
'email' => 'email already exists',
'username' => 'username already exists',
]);
})->with('test-user');
});

test('register passwords are encrypted', function (array $userData) {
test('register passwords are encrypted', function () {
$auth = authInstance();

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$auth->config([
'hidden' => []
]);
Expand All @@ -70,4 +94,4 @@
expect($registerSuccess)->toBeTrue();
expect($auth->user()->password)->not()->toBe($userData['password']);
expect(password_verify($userData['password'], $auth->user()->password))->toBeTrue();
})->with('test-user');
});
74 changes: 58 additions & 16 deletions tests/session.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
dbInstance()->delete('users')->execute();
});

test('register should create a new session when session => true', function (array $userData) {
test('register should create a new session when session => true', function () {
$auth = authInstance();
$auth->config(['session' => true]);

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->register($userData);

expect($success)->toBeTrue();
Expand All @@ -28,9 +34,9 @@

expect(session_status())->toBe(PHP_SESSION_ACTIVE);
expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']);
})->with('test-user');
});

test('register should not create a new session when session => false', function (array $userData) {
test('register should not create a new session when session => false', function () {
$auth = authInstance();
$auth->config(['session' => false]);

Expand All @@ -47,12 +53,18 @@

expect(session_status())->toBe(PHP_SESSION_NONE);
expect($_SESSION['auth']['user']['username'] ?? null)->toBeNull();
})->with('test-user');
});

test('login should create session when session => true', function (array $userData) {
test('login should create session when session => true', function () {
$auth = authInstance();
$auth->config(['session' => true]);

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->login($userData);

expect($success)->toBeTrue();
Expand All @@ -61,12 +73,18 @@

expect(session_status())->toBe(PHP_SESSION_ACTIVE);
expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']);
})->with('test-user');
});

test('session should create auth.ttl when session.lifetime is not 0', function (array $userData) {
test('session should create auth.ttl when session.lifetime is not 0', function () {
$auth = authInstance();
$auth->config(['session' => true, 'session.lifetime' => 2]);

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$timeBeforeLogin = time();

$success = $auth->login($userData);
Expand All @@ -79,12 +97,18 @@
expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']);

expect($_SESSION['auth']['ttl'])->toBeGreaterThan($timeBeforeLogin);
})->with('test-user');
});

test('session should not create auth.ttl when session.lifetime is 0', function (array $userData) {
test('session should not create auth.ttl when session.lifetime is 0', function () {
$auth = authInstance();
$auth->config(['session' => true, 'session.lifetime' => 0]);

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->login($userData);

expect($success)->toBeTrue();
Expand All @@ -95,12 +119,18 @@
expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']);

expect($_SESSION['auth']['ttl'] ?? null)->toBeNull();
})->with('test-user');
});

test('session should expire after session.lifetime', function (array $userData) {
test('session should expire after session.lifetime', function () {
$auth = authInstance();
$auth->config(['session' => true, 'session.lifetime' => 2]);

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->login($userData);

expect($success)->toBeTrue();
Expand All @@ -114,16 +144,22 @@

expect($auth->id())->toBeNull();
expect($auth->user())->toBeNull();
})->with('test-user');
});

test('login should regenerate session id when session => true and session is already active', function (array $userData) {
test('login should regenerate session id when session => true and session is already active', function () {
$auth = authInstance();
$auth->config(['session' => true]);

session_start();

$sessionId = session_id();

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->login($userData);

$newSessionId = session_id();
Expand All @@ -136,12 +172,18 @@
expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']);

expect($newSessionId)->not()->toBe($sessionId);
})->with('test-user');
});

test('logout should remove auth info from session when session => true', function (array $userData) {
test('logout should remove auth info from session when session => true', function () {
$auth = authInstance();
$auth->config(['session' => true]);

$userData = [
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
];

$success = $auth->login($userData);

expect($success)->toBeTrue();
Expand All @@ -152,4 +194,4 @@

expect($auth->user())->toBeNull();
expect($_SESSION['auth']['user']['username'] ?? null)->toBeNull();
})->with('test-user');
});
30 changes: 21 additions & 9 deletions tests/table.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,49 @@
dbInstance()->delete('myusers')->execute();
});

test('register should save user in user defined table', function (array $testUser) {
test('register should save user in user defined table', function () {
$auth = authInstance();
$auth->config(['session' => false, 'db.table' => 'myusers']);

$success = $auth->register($testUser);
$success = $auth->register([
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
]);

if (!$success) {
$this->fail(json_encode($auth->errors()));
}

expect($auth->user()->username)->toBe('test-user');
})->with('test-user');
});

test('login should work with user defined table', function (array $testUser) {
test('login should work with user defined table', function () {
$auth = authInstance();
$auth->config(['session' => false, 'db.table' => 'myusers']);

$success = $auth->login($testUser);
$success = $auth->login([
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
]);

if (!$success) {
$this->fail(json_encode($auth->errors()));
}

expect($auth->user()->username)->toBe('test-user');
})->with('test-user');
});

test('update should work with user defined table', function (array $testUser) {
test('update should work with user defined table', function () {
$auth = authInstance();
$auth->config(['session' => true, 'db.table' => 'myusers', 'session.lifetime' => '1 day']);

$success = $auth->login($testUser);
$success = $auth->login([
'username' => 'test-user',
'email' => '[email protected]',
'password' => 'password'
]);

if (!$success) {
$this->fail(json_encode($auth->errors()));
Expand All @@ -58,7 +70,7 @@

expect($response['user']['username'])->toBe('test-user55');
expect($response['user']['email'])->toBe('[email protected]');
})->with('test-user')->skip();
})->skip();

test('user table can use uuid as id', function () {
createUsersTable('uuid_users', true);
Expand Down
Loading

0 comments on commit 9d965dc

Please sign in to comment.