-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
32b9f02
commit 1a8e6cb
Showing
13 changed files
with
305 additions
and
2 deletions.
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
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,10 @@ | ||
<?php | ||
|
||
namespace App\Contracts; | ||
|
||
interface VerifiableAdapter | ||
{ | ||
public function verify(string $email) : bool; | ||
|
||
public function checkResponse() : bool; | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Contracts\VerifiableAdapter; | ||
use App\Models\User; | ||
use Illuminate\Auth\Events\Registered; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class AdapterController extends Controller | ||
{ | ||
public function __invoke(Request $request, VerifiableAdapter $emailVerifier) | ||
{ | ||
$validated = $request->validate([ | ||
'name' => 'required|string|max:255', | ||
'email' => 'required|string|email|max:255|unique:users', | ||
'password' => ['required'], | ||
]); | ||
|
||
if (! $emailVerifier->verify($validated['email'])) { | ||
return redirect()->back()->withErrors(['El email no es válido']); | ||
} | ||
|
||
$user = User::create($validated); | ||
|
||
event(new Registered($user)); | ||
|
||
Auth::login($user); | ||
|
||
return redirect()->back()->withSuccess('Sesión iniciada correctamente'); | ||
} | ||
} |
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
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,36 @@ | ||
<?php | ||
|
||
namespace App\Services\Adapters; | ||
|
||
use App\Contracts\VerifiableAdapter; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class AbstractApiAdapter implements VerifiableAdapter | ||
{ | ||
private $client; | ||
private $response; | ||
|
||
public function __construct() | ||
{ | ||
$this->client = Http::baseUrl('https://emailvalidation.abstractapi.com'); | ||
} | ||
|
||
public function verify(string $email) : bool | ||
{ | ||
$this->response = $this->client->get('v1', [ | ||
'api_key' => config('external.api_key_abstractapi'), | ||
'email' => $email, | ||
]); | ||
|
||
return $this->checkResponse(); | ||
} | ||
|
||
public function checkResponse() : bool | ||
{ | ||
if (isset($this->response['error'])) { | ||
throw new \Exception('Se produjo un error, revisa si tu API_KEY_ABSTRACTAPI es válida'); | ||
} | ||
|
||
return (bool) $this->response['is_smtp_valid']['value']; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace App\Services\Adapters; | ||
|
||
use App\Contracts\VerifiableAdapter; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class MailboxLayerAdapter implements VerifiableAdapter | ||
{ | ||
private $client; | ||
private $response; | ||
|
||
public function __construct() | ||
{ | ||
$this->client = Http::baseUrl('http://apilayer.net'); | ||
} | ||
|
||
public function verify(string $email) : bool | ||
{ | ||
$this->response = $this->client->get('/api/check', [ | ||
'access_key' => config('external.api_key_apilayer'), | ||
'email' => $email, | ||
'smtp' => 1, | ||
'format' => 1, | ||
]); | ||
|
||
return $this->checkResponse(); | ||
} | ||
|
||
public function checkResponse() : bool | ||
{ | ||
if (isset($this->response['error'])) { | ||
throw new \Exception('Se produjo un error, revisa si tu API_KEY_APILAYER es válida'); | ||
} | ||
|
||
return (bool) $this->response['mx_found']; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Archivo para Credenciales | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Modifica el .env con tus llaves de API | ||
| | ||
| API_KEY_APILAYER={tu propia api_key} | ||
| API_KEY_ABSTRACTAPI={tu propia api_key} | ||
| | ||
*/ | ||
return [ | ||
'api_key_apilayer' => env('API_KEY_APILAYER'), | ||
'api_key_abstractapi' => env('API_KEY_ABSTRACTAPI'), | ||
]; |
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,32 @@ | ||
@extends('layout') | ||
@section('titulo', 'Patrón Adapter') | ||
|
||
@section('contenido') | ||
@foreach ($users as $user) | ||
<ul> | ||
<li> | ||
{{ $user->name }} | ||
</li> | ||
</ul> | ||
@endforeach | ||
|
||
<form action="{{ route('adapter') }}" method="POST"> | ||
@csrf | ||
<div> | ||
<h2>Agregar nueva persona</h2> | ||
</div> | ||
<div> | ||
<label for="name">Nombre:</label> | ||
<input name="name" id="name" type="text" placeholder="Nombre"> | ||
</div> | ||
<div> | ||
<label for="email">Email:</label> | ||
<input name="email" id="email" type="email" placeholder="Email"> | ||
</div> | ||
<div> | ||
<label for="password">Contraseña:</label> | ||
<input name="password" id="password" type="password" placeholder="Contraseña"> | ||
</div> | ||
<button>Agregar</button> | ||
</form> | ||
@endsection |
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,98 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Contracts\VerifiableAdapter; | ||
use App\Services\Adapters\AbstractApiAdapter; | ||
use App\Services\Adapters\MailboxLayerAdapter; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Illuminate\Support\Facades\Http; | ||
use Tests\TestCase; | ||
|
||
class AdapterTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected function forzarMailboxLayer($emailValido = true) | ||
{ | ||
// 💡 Forzar el uso de MailboxLayer (ApiLayer), para simular funcionamiento de AppServiceProvider.php | ||
app()->bind(VerifiableAdapter::class, config('adapter.driver',MailboxLayerAdapter::class)); | ||
|
||
// 😎 Mocking de solicitud Http | ||
Http::fake([ | ||
'apilayer.net/*' => Http::response([ | ||
'mx_found' => $emailValido, | ||
], 200), | ||
]); | ||
} | ||
|
||
protected function forzarAbstractApi($emailValido = true) | ||
{ | ||
// 💡 Forzar el uso de AbstractApi, para simular funcionamiento de AppServiceProvider.php | ||
app()->bind(VerifiableAdapter::class, config('adapter.driver',AbstractApiAdapter::class)); | ||
|
||
// 😎 Mocking de solicitud Http | ||
Http::fake([ | ||
'emailvalidation.abstractapi.com/*' => Http::response([ | ||
'is_smtp_valid' => ['value' => $emailValido] | ||
], 200), | ||
]); | ||
} | ||
|
||
/** @test */ | ||
function puede_crear_un_usuario_usando_el_proveedor_mailboxlayer_con_adapter() | ||
{ | ||
$this->forzarMailboxLayer(true); | ||
|
||
$data = ['name' => 'Nombre que funciona', 'email' => 'existente@dominio.cl', 'password' => 'password']; | ||
|
||
$request = $this->post(route('adapter'), $data); | ||
|
||
$response = $this->get(route('patrones.adapter')); | ||
|
||
$response->assertSee('Nombre que funciona'); | ||
} | ||
|
||
/** @test */ | ||
function puede_crear_un_usuario_usando_el_proveedor_abstractapi_con_adapter() | ||
{ | ||
$this->forzarAbstractApi(true); | ||
|
||
$data = ['name' => 'Nombre que funciona', 'email' => 'existente@dominio.cl', 'password' => 'password']; | ||
|
||
$request = $this->post(route('adapter'), $data); | ||
|
||
$response = $this->get(route('patrones.adapter')); | ||
|
||
$response->assertSee('Nombre que funciona'); | ||
} | ||
|
||
/** @test */ | ||
function no_puede_crear_un_usuario_usando_el_proveedor_mailboxlayer_con_adapter() | ||
{ | ||
$this->forzarMailboxLayer(false); | ||
|
||
$data = ['name' => 'Nombre que no debería aparecer', 'email' => 'repetido@aaaaaa.tk', 'password' => 'password']; | ||
|
||
$request = $this->post(route('adapter'), $data); | ||
|
||
$response = $this->get(route('patrones.adapter')); | ||
|
||
$response->assertDontSee('Nombre que no debería aparecer'); | ||
} | ||
|
||
/** @test */ | ||
function no_puede_crear_un_usuario_usando_el_proveedor_abstractapi_con_adapter() | ||
{ | ||
$this->forzarAbstractApi(false); | ||
|
||
$data = ['name' => 'Nombre que no debería aparecer', 'email' => 'repetido@aaaaaa.tk', 'password' => 'password']; | ||
|
||
$request = $this->post(route('adapter'), $data); | ||
|
||
$response = $this->get(route('patrones.adapter')); | ||
|
||
$response->assertDontSee('Nombre que no debería aparecer'); | ||
} | ||
} |