Skip to content

Commit 6820d01

Browse files
committed
Tests & drop Laravel 7.0 support for this release as it appears we have breaking changes.
1 parent bc3db48 commit 6820d01

File tree

7 files changed

+229
-11
lines changed

7 files changed

+229
-11
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
vendor
22
composer.lock
3-
.todo
3+
.todo
4+
.idea
5+
.phpunit.result.cache

composer.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
],
1212
"prefer-stable": true,
1313
"require": {
14-
"illuminate/contracts": "^6.0|^7.0",
15-
"illuminate/http": "^6.0|^7.0",
16-
"illuminate/database": "^6.0|^7.0",
17-
"illuminate/auth": "^6.0|^7.0",
18-
"illuminate/routing": "^6.0|^7.0",
19-
"illuminate/notifications": "^6.0|^7.0",
20-
"illuminate/bus": "^6.0|^7.0",
21-
"illuminate/cookie": "^6.0|^7.0",
22-
"illuminate/encryption": "^6.0|^7.0",
14+
"php": "^7.2.5|^8.0",
15+
"illuminate/contracts": "^6.0",
16+
"illuminate/http": "^6.0",
17+
"illuminate/database": "^6.0",
18+
"illuminate/auth": "^6.0",
19+
"illuminate/routing": "^6.0",
20+
"illuminate/notifications": "^6.0",
21+
"illuminate/bus": "^6.0",
22+
"illuminate/cookie": "^6.0",
23+
"illuminate/encryption": "^6.0",
2324
"ua-parser/uap-php": "^3.8"
2425
},
2526
"require-dev": {
26-
"orchestra/testbench": "^4.0|^5.0",
27+
"orchestra/testbench": "^4.0",
2728
"symfony/thanks": "^1.0"
2829
},
2930
"autoload": {

phpunit.xml.dist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="false"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
<filter>
19+
<whitelist processUncoveredFilesFromWhitelist="true">
20+
<directory suffix=".php">./src</directory>
21+
<exclude>
22+
<directory>./vendor</directory>
23+
</exclude>
24+
</whitelist>
25+
</filter>
26+
</phpunit>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Tests\Integration;
4+
5+
use BoxedCode\Laravel\Auth\Device\Notifications\AuthorizationRequest;
6+
use Illuminate\Encryption\Encrypter;
7+
8+
class EnforcingMiddlewareTestCase extends TestCase
9+
{
10+
protected $da;
11+
12+
/**
13+
* Setup the test environment.
14+
*/
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->da = app('auth.device.broker');
20+
}
21+
22+
protected function getEnvironmentSetUp($app)
23+
{
24+
parent::getEnvironmentSetUp($app);
25+
26+
// Set the application encryption key.
27+
$app['config']->set('app.key', 'base64:'.base64_encode(
28+
Encrypter::generateKey($app['config']['app.cipher'])
29+
));
30+
}
31+
32+
public function testDefaultFlow()
33+
{
34+
\Notification::fake();
35+
36+
// Trigger auth request as an unverified user.
37+
$response = $this->actingAs($this->testUser)->get('/');
38+
$response->assertRedirect('http://localhost/auth/device/challenge');
39+
40+
// Visit the challenge page which will send us the email.
41+
$response = $this->actingAs($this->testUser)->get('/auth/device/challenge');
42+
$response->assertRedirect('http://localhost/auth/device/challenged');
43+
44+
// Visit the 'challenged' page which instructs the user to check their mail.
45+
$response = $this->actingAs($this->testUser)->get('/auth/device/challenged');
46+
$response->assertSee('We haven\'t seen you using this device before');
47+
48+
$latestAuthorization = $this->testUser->deviceAuthorizations()->latest()->first();
49+
50+
// Verify the notification was sent.
51+
\Notification::assertSentTo($this->testUser, AuthorizationRequest::class, function ($mail) use ($latestAuthorization) {
52+
return ($mail->verifyToken === $latestAuthorization->verify_token &&
53+
$mail->browser === $latestAuthorization->browser &&
54+
$mail->ip === $latestAuthorization->ip);
55+
});
56+
57+
// 'Click' the link in the notification e-mail.
58+
$response = $this->actingAs($this->testUser)
59+
->withoutMiddleware([\Illuminate\Cookie\Middleware\EncryptCookies::class])
60+
->get('/auth/device/verify/' . $latestAuthorization->verify_token);
61+
$response->assertRedirect('/');
62+
63+
$authCookie = collect($response->headers->getCookies())
64+
->filter(function ($cookie) {
65+
return $cookie->getName() === '_la_dat';
66+
})->first();
67+
68+
// Check we can see the homepage.
69+
$response = $this->withUnencryptedCookie($authCookie->getName(), $authCookie->getValue())
70+
->actingAs($this->testUser)->get('/');
71+
72+
$response->assertStatus(200);
73+
$response->assertSeeText('Hello Test User!');
74+
}
75+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests\Integration\Support;
4+
5+
use BoxedCode\Laravel\Auth\Device\Http\Traits\DeviceAuthorization;
6+
use Illuminate\Foundation\Auth\AuthenticatesUsers;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Routing\Controller as BaseController;
9+
10+
class Controller extends BaseController
11+
{
12+
use DeviceAuthorization;
13+
use AuthenticatesUsers;
14+
15+
public function home(Request $request)
16+
{
17+
return 'Hello '.$request->user()->name.'!';
18+
}
19+
}

tests/Integration/Support/User.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tests\Integration\Support;
4+
5+
use Illuminate\Foundation\Auth\User as BaseUser;
6+
7+
class User extends BaseUser implements \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations
8+
{
9+
use \BoxedCode\Laravel\Auth\Device\DeviceAuthorizations,
10+
\Illuminate\Notifications\Notifiable;
11+
12+
protected $table = 'users';
13+
14+
protected $fillable = ['name', 'email', 'password'];
15+
16+
public function canAuthorizeDevice(): bool
17+
{
18+
return true;
19+
}
20+
}

tests/Integration/TestCase.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Tests\Integration;
4+
5+
use Tests\Integration\Support\User;
6+
7+
abstract class TestCase extends \Orchestra\Testbench\TestCase
8+
{
9+
public $testUser;
10+
11+
/**
12+
* Get package providers. At a minimum this is the package being tested, but also
13+
* would include packages upon which our package depends, e.g. Cartalyst/Sentry
14+
* In a normal app environment these would be added to the 'providers' array in
15+
* the config/app.php file.
16+
*
17+
* @param \Illuminate\Foundation\Application $app
18+
*
19+
* @return array
20+
*/
21+
protected function getPackageProviders($app)
22+
{
23+
return [
24+
\BoxedCode\Laravel\Auth\Device\DeviceAuthServiceProvider::class
25+
];
26+
}
27+
28+
/**
29+
* Setup the test environment.
30+
*/
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
35+
// Run the migrations.
36+
$this->loadLaravelMigrations(['--database' => 'testing']);
37+
$this->artisan('migrate', ['--database' => 'testing']);
38+
39+
// Create a test user.
40+
$this->testUser = User::create([
41+
'name' => 'Test User',
42+
'email' => '[email protected]',
43+
'password' => 'password',
44+
]);
45+
46+
\Route::get('/', '\Tests\Integration\Support\Controller@home')->middleware([
47+
'auth',
48+
\BoxedCode\Laravel\Auth\Device\Http\Middleware\RequireDeviceAuthorization::class,
49+
]);
50+
51+
\Route::get('/logout', '\Tests\Integration\Support\Controller@logout')->name('logout');
52+
}
53+
54+
/**
55+
* Define environment setup.
56+
*
57+
* @param \Illuminate\Foundation\Application $app
58+
*
59+
* @return void
60+
*/
61+
protected function getEnvironmentSetUp($app)
62+
{
63+
// Setup default database to use sqlite :memory:
64+
$app['config']->set('database.default', 'testing');
65+
$app['config']->set('database.connections.testing', [
66+
'driver' => 'sqlite',
67+
'database' => ':memory:',
68+
'prefix' => '',
69+
]);
70+
71+
// Set default user model.
72+
$app['config']->set('auth.providers.users.model', User::class);
73+
//$app['config']->set()
74+
}
75+
}

0 commit comments

Comments
 (0)