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

LoginAction: only store auth identifier instead of user class #59

Merged
merged 6 commits into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test support
  • Loading branch information
cesargb committed Jan 8, 2022
commit 0b571b2bb2a5c8946e4d84643719d00764d96610
26 changes: 25 additions & 1 deletion tests/Actions/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace MagicLink\Test\Actions;

use Illuminate\Support\Facades\Auth;
use MagicLink\Actions\LoginAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestCase;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\CustomAutenticable;
use MagicLink\Test\TestSupport\CustomUserProvider;
use MagicLink\Test\TestSupport\User;

class LoginTest extends TestCase
{
Expand All @@ -19,4 +22,25 @@ public function test_auth()

$this->assertAuthenticatedAs(User::first());
}

public function test_auth_custom()
{
Auth::provider('custom_provider', function ($app, array $config) {
return new CustomUserProvider($app, $config);
});

config()->set('auth.providers.custom', [
'driver' => 'custom_provider',
]);

config()->set('auth.guards.web.provider', 'custom');

$magiclink = MagicLink::create(new LoginAction(new CustomAutenticable('user_1')));

$this->get($magiclink->url)
->assertStatus(302)
->assertRedirect('/');

$this->assertAuthenticatedAs(new CustomAutenticable('user_1'));
}
}
2 changes: 1 addition & 1 deletion tests/Actions/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use MagicLink\Actions\ResponseAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestCase;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\User;

class ResponseTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Actions/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use MagicLink\Actions\ViewAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestCase;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\User;

class ViewTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions tests/MagicLinkDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MagicLink\Actions\LoginAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestSupport\User;

class MagicLinkDeleteTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions tests/MagicLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MagicLink\Actions\LoginAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestSupport\User;

class MagicLinkTest extends TestCase
{
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Schema\Blueprint;
use MagicLink\MagicLinkServiceProvider;
use MagicLink\Test\TestSupport\User;
use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
Expand Down Expand Up @@ -35,7 +36,7 @@ protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.key', 'base64:mJlbzP1TMXUPouK3KK6e9zS/VvxtWTfzfVlkn1JTqpM=');

$app['config']->set('auth.providers.users.model', 'MagicLink\Test\User');
$app['config']->set('auth.providers.users.model', 'MagicLink\Test\TestSupport\User');

$app['config']->set('view.paths', [__DIR__.'/stubs/resources/views']);

Expand Down
46 changes: 46 additions & 0 deletions tests/TestSupport/CustomAutenticable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace MagicLink\Test\TestSupport;

use Illuminate\Contracts\Auth\Authenticatable;


class CustomAutenticable implements Authenticatable
{
private $id;

public function __construct($id)
{
$this->id = $id;
}

public function getAuthIdentifierName()
{

}

public function getAuthIdentifier()
{
return $this->id;
}

public function getAuthPassword()
{

}

public function getRememberToken()
{

}

public function setRememberToken($value)
{

}

public function getRememberTokenName()
{

}
}
36 changes: 36 additions & 0 deletions tests/TestSupport/CustomUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace MagicLink\Test\TestSupport;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;

class CustomUserProvider implements UserProvider
{
public function retrieveById($identifier)
{
return new CustomAutenticable($identifier);
}

public function retrieveByToken($identifier, $token)
{
print_r([__LINE__ => $identifier]);
}

public function updateRememberToken(Authenticatable $user, $token)
{
print_r([__LINE__ => $user]);
}

public function retrieveByCredentials(array $credentials)
{
print_r([__LINE__ => $credentials]);

//return User::where('username', $username)->first();
}

public function validateCredentials(Authenticatable $user, array $credentials)
{
print_r([__LINE__ => $credentials]);
}
}
2 changes: 1 addition & 1 deletion tests/User.php → tests/TestSupport/User.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MagicLink\Test;
namespace MagicLink\Test\TestSupport;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
Expand Down
2 changes: 1 addition & 1 deletion tests/stubs/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use MagicLink\Actions\LoginAction;
use MagicLink\Actions\ResponseAction;
use MagicLink\MagicLink;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\User;

Route::get('/create/login', function () {
return MagicLink::create(new LoginAction(User::first()))->url;
Expand Down