Skip to content

Commit

Permalink
stubbing twofactor class implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
begimov committed May 23, 2019
1 parent 7831a20 commit 69c36df
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 21 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="true">
<testsuites>
<testsuite name="AwesIO Auth Package">
<directory suffix=".php">./tests/</directory>
Expand Down
22 changes: 10 additions & 12 deletions src/Services/AuthyTwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace AwesIO\Auth\Services;

use App\User;
use GuzzleHttp\Client;
use AwesIO\Auth\Services\Contracts\TwoFactor;

Expand All @@ -23,10 +22,10 @@ public function __construct(Client $client)
/**
* Register user on Authy
*
* @param \App\User $user
* @param $user
* @return boolean|object
*/
public function register(User $user)
public function register($user)
{
try {
$response = $this->client->request(
Expand All @@ -45,11 +44,11 @@ public function register(User $user)
/**
* Verify if 2FA token is valid
*
* @param \App\User $user
* @param $user
* @param string $token
* @return boolean
*/
public function verifyToken(User $user, $token)
public function verifyToken($user, $token)
{
try {
$response = $this->client->request(
Expand All @@ -70,10 +69,10 @@ public function verifyToken(User $user, $token)
/**
* Remove user from Authy
*
* @param \App\User $user
* @param $user
* @return boolean
*/
public function remove(User $user)
public function remove($user)
{
try {
$response = $this->client->request(
Expand All @@ -91,12 +90,11 @@ public function remove(User $user)
/**
* Request QR code link.
*
* @param string $authy_id User's id stored in your database
* @param array $opts Array of options, for example: array("qr_size" => 300)
* @param array $user
*
* @return mixed
*/
public function qrCode(User $user)
public function qrCode($user)
{
try {
$response = $this->client->request(
Expand All @@ -114,10 +112,10 @@ public function qrCode(User $user)
/**
* Get data needed for user registration on Authy
*
* @param \App\User $user
* @param $user
* @return array
*/
protected function getUserRegistrationPayload(User $user)
protected function getUserRegistrationPayload($user)
{
return [
'user' => [
Expand Down
23 changes: 15 additions & 8 deletions src/Services/Contracts/TwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,41 @@

namespace AwesIO\Auth\Services\Contracts;

use App\User;

interface TwoFactor
{
/**
* Register user on 2FA service
*
* @param \App\User $user
* @param $user
* @return boolean|object
*/
public function register(User $user);
public function register($user);

/**
* Verify if 2FA token is valid
*
* @param \App\User $user
* @param $user
* @param string $token
* @return boolean
*/
public function verifyToken(User $user, $token);
public function verifyToken($user, $token);

/**
* Remove user from 2FA service
*
* @param \App\User $user
* @param $user
* @return boolean
*/
public function remove(User $user);
public function remove($user);

/**
* Request QR code link.
*
* @param array $user
*
* @return mixed
*/
public function qrCode($user);

// public function sendSMS(User $user);
}
24 changes: 24 additions & 0 deletions tests/Feature/TwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ public function it_stores_new_user_two_factor_record()
'dial_code' => $code
]);
}

/** @test */
public function it_destroys_user_two_factor_record()
{
$user = factory(User::class)->create();

$this->actingAs($user)->post('twofactor', [
'phone' => ($code = '+7') . ' ' . ($phone = '999 999-99-99'),
]);

$this->assertDatabaseHas('two_factor', [
'user_id' => $user->id,
'phone' => $phone,
'dial_code' => $code,
]);

$this->actingAs($user)->delete('twofactor');

$this->assertDatabaseMissing('two_factor', [
'user_id' => $user->id,
'phone' => $phone,
'dial_code' => $code,
]);
}
}
67 changes: 67 additions & 0 deletions tests/Stubs/TwoFactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace AwesIO\Auth\Tests\Stubs;

use AwesIO\Auth\Services\Contracts\TwoFactor as TwoFactorInterface;

class TwoFactor implements TwoFactorInterface
{
/**
* Http client
*
* @var \GuzzleHttp\Client
*/
protected $client;

public function __construct()
{
//
}

/**
* Register user on Authy
*
* @param \App\User $user
* @return boolean|object
*/
public function register($user)
{
return false;
}

/**
* Verify if 2FA token is valid
*
* @param \App\User $user
* @param string $token
* @return boolean
*/
public function verifyToken($user, $token)
{
return true;
}

/**
* Remove user from Authy
*
* @param \App\User $user
* @return boolean
*/
public function remove($user)
{
return true;
}

/**
* Request QR code link.
*
* @param string $authy_id User's id stored in your database
* @param array $opts Array of options, for example: array("qr_size" => 300)
*
* @return mixed
*/
public function qrCode($user)
{
return json_decode([]);
}
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use AwesIO\Auth\Facades\Auth;
use AwesIO\Auth\Tests\Stubs\User;
use AwesIO\Auth\AuthServiceProvider;
use AwesIO\Auth\Tests\Stubs\TwoFactor;
use Illuminate\Database\Schema\Blueprint;
use AwesIO\Auth\Services\Contracts\TwoFactor as TwoFactorContract;

abstract class TestCase extends \Orchestra\Testbench\TestCase
{
Expand All @@ -23,6 +25,8 @@ protected function setUp()
$this->assignRouteActionMiddlewares();

$this->withFactories(__DIR__ . '/../database/factories');

$this->app->singleton(TwoFactorContract::class, TwoFactor::class);
}

/**
Expand Down

0 comments on commit 69c36df

Please sign in to comment.