diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 20ff0a71..40f1b2b6 100755 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -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') diff --git a/tests/Feature/OtpTest.php b/tests/Feature/OtpTest.php new file mode 100644 index 00000000..87780fff --- /dev/null +++ b/tests/Feature/OtpTest.php @@ -0,0 +1,113 @@ +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' + ] + ], + ]; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index dc77c3de..fc616d16 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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() {