-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
116 additions
and
1 deletion.
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
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' | ||
] | ||
], | ||
]; | ||
} | ||
} |
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