From 7cb0a96ab55f94936ea66b2b4d059e018b3e2b56 Mon Sep 17 00:00:00 2001 From: Warren Summerton Date: Thu, 19 May 2022 17:01:20 +0100 Subject: [PATCH 1/3] Added first test, ready for writing more tests --- .../AuthenticationLogSubscriberTest.php | 41 ++++++++++++++ tests/Models/User.php | 56 +++++++++++++++++++ tests/TestCase.php | 1 + tests/Unit/AuthenticationLogRecordTest.php | 1 + 4 files changed, 99 insertions(+) create mode 100644 tests/Feature/AuthenticationLogSubscriberTest.php create mode 100644 tests/Models/User.php diff --git a/tests/Feature/AuthenticationLogSubscriberTest.php b/tests/Feature/AuthenticationLogSubscriberTest.php new file mode 100644 index 0000000..1a0362a --- /dev/null +++ b/tests/Feature/AuthenticationLogSubscriberTest.php @@ -0,0 +1,41 @@ +id = 1; + + $event = new Login('web', $user, false); + + Event::dispatch($event); + + $this->assertDatabaseHas('authentication_log_records', + ['authenticatable_id' => $user->id, + 'authenticatable_type' => get_class($user) + ]); + } +} \ No newline at end of file diff --git a/tests/Models/User.php b/tests/Models/User.php new file mode 100644 index 0000000..005b811 --- /dev/null +++ b/tests/Models/User.php @@ -0,0 +1,56 @@ + 1, 'authenticatable_type' => "Test Type", + 'eventType' => 'testEvent', 'recorded_at' => $timeLog ]; From f3e7531b727de787be53e6c2a53909a657fb41c1 Mon Sep 17 00:00:00 2001 From: Warren Summerton Date: Fri, 20 May 2022 13:25:40 +0100 Subject: [PATCH 2/3] Added tests and all are passing --- CHANGELOG.md | 1 + .../AuthenticationLogSubscriberTest.php | 43 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 077ec7f..23f6b89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Can select events to ignore in the Config File - Can select credentials to be omitted from the database in the Config File, such as passwords - Developers can now add authentication events easily through the `AuthenticationLogSubscriber` +- Tests have been implemented to ensure the package works correctly throughout development ## v0.1 diff --git a/tests/Feature/AuthenticationLogSubscriberTest.php b/tests/Feature/AuthenticationLogSubscriberTest.php index 1a0362a..9a6769a 100644 --- a/tests/Feature/AuthenticationLogSubscriberTest.php +++ b/tests/Feature/AuthenticationLogSubscriberTest.php @@ -3,7 +3,9 @@ namespace LangleyFoxall\LaravelAuthenticationLog\Tests\Feature; use Illuminate\Auth\Authenticatable; +use Illuminate\Auth\Events\Failed; use Illuminate\Auth\Events\Login; +use Illuminate\Auth\Events\Logout; use Illuminate\Contracts\Auth\Authenticatable as AuthAuthenticatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -16,7 +18,7 @@ use Orchestra\Testbench\Factories\UserFactory; class AuthenticationLogSubscriberTest extends TestCase -{ +{ use RefreshDatabase; public function setUp() : void @@ -33,9 +35,42 @@ public function test_a_log_record_is_created_when_a_login_event_is_fired() Event::dispatch($event); - $this->assertDatabaseHas('authentication_log_records', - ['authenticatable_id' => $user->id, - 'authenticatable_type' => get_class($user) + $this->assertDatabaseHas('authentication_log_records', [ + 'authenticatable_id' => $user->id, + 'authenticatable_type' => get_class($user), + 'eventType' => get_class($event) + ]); + } + + public function test_a_log_record_is_created_when_a_logout_event_is_fired() + { + $user = new User; + $user->id = 1; + + $event = new Logout('web', $user); + + Event::dispatch($event); + + $this->assertDatabaseHas('authentication_log_records', [ + 'authenticatable_id' => $user->id, + 'authenticatable_type' => get_class($user), + 'eventType' => get_class($event) + ]); + } + + public function test_a_log_record_is_created_when_a_failed_event_is_fired() + { + $credentials = [ + 'name' => 'notJohn', + ]; + + $event = new Failed('web', null, $credentials); + + Event::dispatch($event); + + $this->assertDatabaseHas('authentication_log_records', [ + 'credentials' => json_encode($credentials), + 'eventType' => get_class($event) ]); } } \ No newline at end of file From a6241fe4cdaef2c5a433cf67660ecf5b4b360444 Mon Sep 17 00:00:00 2001 From: Warren Summerton Date: Fri, 20 May 2022 13:38:49 +0100 Subject: [PATCH 3/3] Submit to PR suggestions --- tests/Models/User.php | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/tests/Models/User.php b/tests/Models/User.php index 005b811..85b9743 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -9,47 +9,13 @@ class User implements Authenticatable { public $id; - /** - * Get the name of the unique identifier for the user. - * - * @return string - */ + // no-op -- method definitions given to comply with Authenticatable Interface, + // purely for testing purposes only public function getAuthIdentifierName(){} - - /** - * Get the unique identifier for the user. - * - * @return mixed - */ public function getAuthIdentifier(){} - - /** - * Get the password for the user. - * - * @return string - */ public function getAuthPassword(){} - - /** - * Get the token value for the "remember me" session. - * - * @return string - */ public function getRememberToken(){} - - /** - * Set the token value for the "remember me" session. - * - * @param string $value - * @return void - */ public function setRememberToken($value){} - - /** - * Get the column name for the "remember me" token. - * - * @return string - */ public function getRememberTokenName(){}