|
| 1 | +<?php |
| 2 | + |
| 3 | +use SolutionForest\MathCaptcha\Contracts\CaptchaGenerator; |
| 4 | +use SolutionForest\MathCaptcha\MathCaptcha; |
| 5 | + |
| 6 | +beforeEach(function () { |
| 7 | + $this->app->singleton(CaptchaGenerator::class, function ($app) { |
| 8 | + return new MathCaptcha([ |
| 9 | + 'cache_prefix' => 'test_controller', |
| 10 | + ]); |
| 11 | + }); |
| 12 | +}); |
| 13 | + |
| 14 | +describe('CaptchaController', function () { |
| 15 | + describe('generate()', function () { |
| 16 | + it('returns a JSON response', function () { |
| 17 | + $response = $this->get('/captcha'); |
| 18 | + |
| 19 | + $response->assertStatus(200) |
| 20 | + ->assertHeader('Content-Type', 'application/json'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns response with token and image keys', function () { |
| 24 | + $response = $this->get('/captcha'); |
| 25 | + |
| 26 | + $response->assertStatus(200) |
| 27 | + ->assertJsonStructure(['token', 'image']); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns a 32 character token', function () { |
| 31 | + $response = $this->get('/captcha'); |
| 32 | + |
| 33 | + $data = $response->json(); |
| 34 | + |
| 35 | + expect($data['token'])->toBeString() |
| 36 | + ->toHaveLength(32); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns a valid base64 PNG image', function () { |
| 40 | + $response = $this->get('/captcha'); |
| 41 | + |
| 42 | + $data = $response->json(); |
| 43 | + |
| 44 | + expect($data['image'])->toBeString() |
| 45 | + ->toStartWith('data:image/png;base64,'); |
| 46 | + |
| 47 | + // Verify we can decode it |
| 48 | + $base64Data = str_replace('data:image/png;base64,', '', $data['image']); |
| 49 | + $decodedImage = base64_decode($base64Data, true); |
| 50 | + |
| 51 | + expect($decodedImage)->not->toBeFalse(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('stores the answer in cache', function () { |
| 55 | + $response = $this->get('/captcha'); |
| 56 | + |
| 57 | + $data = $response->json(); |
| 58 | + $cacheKey = "test_controller:{$data['token']}"; |
| 59 | + |
| 60 | + expect(cache()->has($cacheKey))->toBeTrue(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('generates different tokens on each request', function () { |
| 64 | + $response1 = $this->get('/captcha'); |
| 65 | + $response2 = $this->get('/captcha'); |
| 66 | + $response3 = $this->get('/captcha'); |
| 67 | + |
| 68 | + $token1 = $response1->json('token'); |
| 69 | + $token2 = $response2->json('token'); |
| 70 | + $token3 = $response3->json('token'); |
| 71 | + |
| 72 | + expect($token1)->not->toBe($token2) |
| 73 | + ->and($token2)->not->toBe($token3) |
| 74 | + ->and($token1)->not->toBe($token3); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('route configuration', function () { |
| 79 | + it('responds on configured URI', function () { |
| 80 | + config(['math-captcha.route.uri' => '/custom-captcha']); |
| 81 | + |
| 82 | + // Re-register routes with new config |
| 83 | + $this->app['router']->get('/custom-captcha', [\SolutionForest\MathCaptcha\Http\Controllers\CaptchaController::class, 'generate']) |
| 84 | + ->name('custom.captcha.generate'); |
| 85 | + |
| 86 | + $response = $this->get('/custom-captcha'); |
| 87 | + |
| 88 | + $response->assertStatus(200) |
| 89 | + ->assertJsonStructure(['token', 'image']); |
| 90 | + }); |
| 91 | + |
| 92 | + it('has named route', function () { |
| 93 | + expect(route('captcha.generate'))->toEndWith('/captcha'); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments