-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
7 changed files
with
169 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,12 @@ | |
|
||
namespace Cone\Root\Tests\Http; | ||
|
||
use Cone\Root\Notifications\AuthCodeNotification; | ||
use Cone\Root\Tests\TestCase; | ||
use Cone\Root\Tests\User; | ||
use Illuminate\Auth\Events\Login; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Notification; | ||
|
||
class AuthLoginControllerTest extends TestCase | ||
{ | ||
|
@@ -63,6 +65,23 @@ public function test_login_controller_login(): void | |
$this->assertAuthenticatedAs($user); | ||
} | ||
|
||
public function test_login_controller_two_factor_login(): void | ||
{ | ||
Notification::fake([ | ||
AuthCodeNotification::class, | ||
]); | ||
|
||
$user = User::factory()->create(['email' => '[email protected]']); | ||
|
||
$this->post('/root/login', [ | ||
'email' => $user->email, | ||
'password' => 'password', | ||
])->assertRedirect() | ||
->assertSessionHas('status', __('The two factor authentication link has been sent!')); | ||
|
||
Notification::assertSentTo($user, AuthCodeNotification::class); | ||
} | ||
|
||
public function test_login_controller_logout(): void | ||
{ | ||
$this->app['auth']->login($this->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
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,66 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Tests\Http; | ||
|
||
use Cone\Root\Models\AuthCode; | ||
use Cone\Root\Notifications\AuthCodeNotification; | ||
use Cone\Root\Tests\TestCase; | ||
use Cone\Root\Tests\User; | ||
use Illuminate\Support\Facades\Notification; | ||
|
||
class AuthTwoFactorControllerTest extends TestCase | ||
{ | ||
protected User $user; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = User::factory()->create(['email' => '[email protected]']); | ||
} | ||
|
||
public function test_two_factor_controller_show(): void | ||
{ | ||
$this->actingAs($this->user) | ||
->get('/root/two-factor') | ||
->assertOk() | ||
->assertViewIs('root::auth.two-factor'); | ||
} | ||
|
||
public function test_two_factor_controller_resend(): void | ||
{ | ||
Notification::fake(); | ||
|
||
$this->actingAs($this->user) | ||
->post('/root/two-factor/resend') | ||
->assertRedirect('/root/two-factor') | ||
->assertSessionHas('status', __('The authentication code has been sent!')); | ||
|
||
Notification::assertSentTo($this->user, AuthCodeNotification::class); | ||
} | ||
|
||
public function test_two_factor_controller_verify(): void | ||
{ | ||
$code = AuthCode::factory()->for($this->user)->create(); | ||
|
||
$this->actingAs($this->user) | ||
->post('/root/two-factor', [ | ||
'code' => 000000, | ||
]) | ||
->assertRedirect('/root/two-factor') | ||
->assertSessionHasErrors([ | ||
'code' => __('The authentication code is not valid!'), | ||
]); | ||
|
||
$this->actingAs($this->user) | ||
->post('/root/two-factor', [ | ||
'code' => $code->code, | ||
'trust' => true, | ||
]) | ||
->assertRedirect('/root') | ||
->assertCookie('device_token') | ||
->assertSessionHas('root.auth.two-factor', true); | ||
|
||
$this->assertDatabaseMissing('root_auth_codes', ['id' => $code->getKey()]); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Tests\Models; | ||
|
||
use Cone\Root\Models\AuthCode; | ||
use Cone\Root\Tests\TestCase; | ||
use Cone\Root\Tests\User; | ||
|
||
class AuthCodeTest extends TestCase | ||
{ | ||
protected User $user; | ||
|
||
protected AuthCode $code; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = User::factory()->create(); | ||
|
||
$this->code = AuthCode::factory()->for($this->user)->create(); | ||
} | ||
|
||
public function test_an_auth_code_belongs_to_a_user(): void | ||
{ | ||
$this->assertTrue($this->code->user->is($this->user)); | ||
} | ||
|
||
public function test_an_auth_code_can_be_expired(): void | ||
{ | ||
$this->assertTrue($this->code->active()); | ||
$this->assertFalse($this->code->expired()); | ||
|
||
$expired = AuthCode::factory()->for($this->user)->expired()->create(); | ||
|
||
$this->assertFalse($expired->active()); | ||
$this->assertTrue($expired->expired()); | ||
} | ||
|
||
public function test_an_auth_code_has_active_query_scope(): void | ||
{ | ||
$this->assertSame( | ||
'select * from "root_auth_codes" where "root_auth_codes"."expires_at" > ?', | ||
AuthCode::query()->active()->toSql() | ||
); | ||
} | ||
|
||
public function test_an_auth_code_has_expired_query_scope(): void | ||
{ | ||
$this->assertSame( | ||
'select * from "root_auth_codes" where "root_auth_codes"."expires_at" <= ?', | ||
AuthCode::query()->expired()->toSql() | ||
); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use Illuminate\Database\Eloquent\Relations\MorphTo; | ||
use Illuminate\Database\Eloquent\Relations\MorphToMany; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Illuminate\Http\Request; | ||
|
||
class User extends Model implements MustVerifyEmail | ||
{ | ||
|
@@ -68,4 +69,9 @@ public function employer(): MorphTo | |
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
public function shouldTwoFactorAuthenticate(Request $request): bool | ||
{ | ||
return $this->email === '[email protected]'; | ||
} | ||
} |