-
-
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.
- Loading branch information
1 parent
44dfa9d
commit 9d965dc
Showing
7 changed files
with
145 additions
and
43 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
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 |
---|---|---|
|
@@ -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(); | ||
|
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 |
---|---|---|
|
@@ -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) { | ||
|
@@ -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(); | ||
|
@@ -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'] | ||
]); | ||
|
@@ -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' => [] | ||
]); | ||
|
@@ -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'); | ||
}); |
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 |
---|---|---|
|
@@ -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(); | ||
|
@@ -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]); | ||
|
||
|
@@ -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(); | ||
|
@@ -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); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -152,4 +194,4 @@ | |
|
||
expect($auth->user())->toBeNull(); | ||
expect($_SESSION['auth']['user']['username'] ?? null)->toBeNull(); | ||
})->with('test-user'); | ||
}); |
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 |
---|---|---|
|
@@ -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())); | ||
|
@@ -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); | ||
|
Oops, something went wrong.