Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Aug 23, 2023
1 parent 54afad8 commit 22252df
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function generateOtp(Request $request, Otp $otp)
{
$identifier = (string) $request->get('identifier');

$token = $otp->generate(
$token = $otp->generate(
$identifier,
config('services.otp.digits'),
config('services.otp.validity')
Expand Down
113 changes: 113 additions & 0 deletions tests/Feature/OtpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace Feature;

use App\Mail\OtpWasGenerated;
use Faker\Factory;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use TestCase;

class OtpTest extends TestCase
{
/**
* @test
*/
public function it_can_generate_otp_success()
{
Mail::fake();

$faker = Factory::create();

$this
->json('POST', '/api/v1/otp/generate', [
'identifier' => $faker->email
])->assertStatus(Response::HTTP_OK);

Mail::assertSent(OtpWasGenerated::class);
}

/**
* @test
*/
public function it_can_generate_otp_fails()
{
$randStr = Str::random(10);

$this
->json('POST', '/api/v1/otp/generate', [
'identifier' => $randStr
])
->assertExactJson([
"message" => "There was an error processing this request. Please try again."
]);

Log::shouldReceive('debug')
->with(
'Error sending OTP email',
[
'identifier' => $randStr,
'errorMsg' => 'zazu'
]
);
}

/**
* @dataProvider OtpValidationDataProvider
* @test
*/
public function it_can_validate_a_token(?string $identifier, ?string $token, array $response)
{
$this
->json('POST', '/api/v1/otp/validate', [
'identifier' => $identifier,
'token' => $token
])
->assertStatus(Response::HTTP_OK)
->assertExactJson($response);
}

public static function OtpValidationDataProvider(): array
{
$faker = Factory::create();

return [
'With null identifier and token' => [
null,
null,
[
'status' => false,
'message' => 'OTP does not exist'
]
],
'With null token only' => [
$faker->email,
null,
[
'status' => false,
'message' => 'OTP does not exist'
]
],
'With null identifier only' => [
null,
(string)$faker->randomNumber(6),
[
'status' => false,
'message' => 'OTP does not exist'
]
],
'Identifier and token not matching db' => [
$faker->email,
(string)$faker->randomNumber(6),
[
'status' => false,
'message' => 'OTP does not exist'
]
],
];
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\WithFaker;

abstract class TestCase extends BaseTestCase
{
use RefreshDatabase;
use WithFaker;

public function createApplication()
{
Expand Down

0 comments on commit 22252df

Please sign in to comment.