Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more unit test #7

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
uses: ramsey/composer-install@v1

- name: Run pint
run: ./vendor/bin/pint
run: ./vendor/bin/pint --test
2 changes: 1 addition & 1 deletion src/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function delete(CanUsePasswordlessAuthenticatable $user): bool

public function deleteExpired(): bool
{
$expiredAt = Carbon::now()->subSeconds($this->expires);
$expiredAt = Carbon::now()->addSeconds($this->expires);

return (bool) $this->getPasswordlessTable()
->where('created_at', '<=', $expiredAt)
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/MagicLinkUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace NorbyBaru\Passwordless\Tests\Unit;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use NorbyBaru\Passwordless\Facades\Passwordless;
use NorbyBaru\Passwordless\MagicLink;
use NorbyBaru\Passwordless\Tests\Fixtures\Models\User as UserModel;
use NorbyBaru\Passwordless\Tests\TestCase;

class MagicLinkUnitTest extends TestCase
{
protected MagicLink $magicLink;

protected UserModel $user;

public function setUp(): void
{
parent::setUp();

$this->magicLink = Passwordless::magicLink();

$this->user = UserModel::create([
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => Hash::make(Str::random(10)),
'remember_token' => Str::random(10),
]);
}

public function test_it_should_create_a_token()
{
$token = $this->magicLink->createToken($this->user);
$this->assertNotEmpty($token);
}

public function test_it_should_created_signed_url()
{
$signedUrl = $this->magicLink->generateUrl($this->user, Str::random(40));

$queryParams = collect(explode('&', substr($signedUrl, strpos($signedUrl, '?') + 1)))
->mapWithKeys(function ($value) {
$values = explode('=', $value);

return [$values[0] => $values[1]];
});
$url = Arr::first(explode('?', $signedUrl));

$this->assertNotNull($queryParams['hash']);
$this->assertNotNull($queryParams['signature']);
$this->assertNotNull($queryParams['expires']);
$this->assertEquals($this->user->email, urldecode($queryParams['email']));
$this->assertEquals(config('app.url').config('passwordless.callback_url'), $url);
$this->assertNotNull($url);
$this->assertNotEmpty($url);
}
}
147 changes: 147 additions & 0 deletions tests/Unit/TokenRepositoryUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace NorbyBaru\Passwordless\Tests\Unit;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use NorbyBaru\Passwordless\Tests\Fixtures\Models\User as UserModel;
use NorbyBaru\Passwordless\Tests\TestCase;
use NorbyBaru\Passwordless\TokenRepository;

class TokenRepositoryUnitTest extends TestCase
{
protected TokenRepository $tokenRepository;

protected UserModel $user;

public function setUp(): void
{
parent::setUp();

$this->tokenRepository = $this->getTokenRepository();

$this->user = UserModel::create([
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => Hash::make(Str::random(10)),
'remember_token' => Str::random(10),
]);
}

public function test_it_should_create_new_token_for_user()
{
Carbon::setTestNow();
$token = $this->tokenRepository->create($this->user);
$this->assertNotNull($token);

$this->assertDatabaseHas(config('passwordless.table'), [
'email' => $this->user->getEmailForMagicLink(),
'created_at' => now(),
]);
}

public function test_it_should_not_create_new_user_token_due_to_throttling()
{
$knowTime = Carbon::now()->addSeconds(config('passwordless.throttle') - 1);
Carbon::setTestNow();
$token = $this->tokenRepository->create($this->user);
$this->assertNotNull($token);

Carbon::setTestNow($knowTime);
$token = $this->tokenRepository->create($this->user);
$this->assertNull($token);
}

public function test_it_should_determine_whether_token_was_recently_created_or_not()
{
Carbon::setTestNow();
$recent = $this->tokenRepository->recentlyCreatedToken($this->user);
$this->assertFalse($recent);

$this->tokenRepository->create($this->user);
$recent = $this->tokenRepository->recentlyCreatedToken($this->user);
$this->assertTrue($recent);
}

public function test_it_should_validate_that_valid_token_exist_and_is_valid()
{
$token = $this->tokenRepository->create($this->user);

$isValid = $this->tokenRepository->exist($this->user, $token);
$this->assertTrue($isValid);
}

public function test_it_should_validate_that_invalid_token_do_not_exist_and_is_invalid()
{
$isValid = $this->tokenRepository->exist($this->user, Str::random(40));
$this->assertFalse($isValid);
}

public function test_it_should_validate_that_expired_token_is_invalid()
{
$knowTime = Carbon::now()->subSeconds(config('passwordless.expire'));
Carbon::setTestNow($knowTime);
$token = $this->tokenRepository->create($this->user);

$knowTime = Carbon::now()->addSeconds(config('passwordless.expire') + 1);
Carbon::setTestNow($knowTime);
$isValid = $this->tokenRepository->exist($this->user, $token);
$this->assertFalse($isValid);
}

public function test_it_should_delete_all_user_tokens()
{
Carbon::setTestNow();
$this->tokenRepository->create($this->user);

$this->assertDatabaseHas(config('passwordless.table'), [
'email' => $this->user->getEmailForMagicLink(),
]);

$this->tokenRepository->delete($this->user);

$this->assertDatabaseMissing(config('passwordless.table'), [
'email' => $this->user->getEmailForMagicLink(),
]);
}

public function test_it_should_delete_all_user_expired_tokens()
{
$knowTime = Carbon::now()->subSeconds(config('passwordless.expire'));
Carbon::setTestNow($knowTime);
$this->tokenRepository->create($this->user);
$this->assertDatabaseCount(config('passwordless.table'), 1);

$knowTime = Carbon::now();
Carbon::setTestNow($knowTime);
$this->tokenRepository->deleteExpired();

$this->assertDatabaseCount(config('passwordless.table'), 0);
}

private function getTokenRepository(): TokenRepository
{
$key = $this->app['config']['app.key'];

if (Str::startsWith($key, 'base64:')) {
$key = base64_decode(substr($key, 7));
}

$config = $this->getPasswordlessConfig();

return new TokenRepository(
$this->app['db']->connection(),
$config['table'],
$key,
$config['expire'],
$config['throttle']
);
}

private function getPasswordlessConfig(): array
{
return $this->app['config']->get('passwordless');
}
}