From 5cb948d0f3fc8a314b6e49ab9195aeba3f4ca221 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 25 Dec 2017 09:00:09 +0100 Subject: [PATCH] Currency code test. --- app/Support/Binder/CurrencyCode.php | 8 ++- tests/Unit/Middleware/BinderTest.php | 97 +++++++++++++++++++++------- 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/app/Support/Binder/CurrencyCode.php b/app/Support/Binder/CurrencyCode.php index 0245fa4405..a8887c6466 100644 --- a/app/Support/Binder/CurrencyCode.php +++ b/app/Support/Binder/CurrencyCode.php @@ -38,9 +38,11 @@ class CurrencyCode implements BinderInterface */ public static function routeBinder($value, $route) { - $currency = TransactionCurrency::where('code', $value)->first(); - if (null !== $currency) { - return $currency; + if (auth()->check()) { + $currency = TransactionCurrency::where('code', $value)->first(); + if (null !== $currency) { + return $currency; + } } throw new NotFoundHttpException; } diff --git a/tests/Unit/Middleware/BinderTest.php b/tests/Unit/Middleware/BinderTest.php index b92460dcf2..d9a394c28a 100644 --- a/tests/Unit/Middleware/BinderTest.php +++ b/tests/Unit/Middleware/BinderTest.php @@ -373,6 +373,62 @@ class BinderTest extends TestCase $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); } + /** + * @covers \FireflyIII\Http\Middleware\Binder::handle + * @covers \FireflyIII\Http\Middleware\Binder::__construct + * @covers \FireflyIII\Http\Middleware\Binder::performBinding + * @covers \FireflyIII\Support\Binder\CurrencyCode::routeBinder + */ + public function testCurrencyCode() + { + Route::middleware(Binder::class)->any( + '/_test/binder/{fromCurrencyCode}', function () { + return 'OK'; + } + ); + + $this->be($this->user()); + $response = $this->get('/_test/binder/USD'); + $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); + } + + /** + * @covers \FireflyIII\Http\Middleware\Binder::handle + * @covers \FireflyIII\Http\Middleware\Binder::__construct + * @covers \FireflyIII\Http\Middleware\Binder::performBinding + * @covers \FireflyIII\Support\Binder\CurrencyCode::routeBinder + */ + public function testCurrencyCodeNotFound() + { + Route::middleware(Binder::class)->any( + '/_test/binder/{fromCurrencyCode}', function () { + return 'OK'; + } + ); + + $this->be($this->user()); + $response = $this->get('/_test/binder/ABC'); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + /** + * @covers \FireflyIII\Http\Middleware\Binder::handle + * @covers \FireflyIII\Http\Middleware\Binder::__construct + * @covers \FireflyIII\Http\Middleware\Binder::performBinding + * @covers \FireflyIII\Support\Binder\CurrencyCode::routeBinder + */ + public function testCurrencyCodeNotLoggedIn() + { + Route::middleware(Binder::class)->any( + '/_test/binder/{fromCurrencyCode}', function () { + return 'OK'; + } + ); + + $response = $this->get('/_test/binder/EUR'); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + /** * @covers \FireflyIII\Http\Middleware\Binder::handle * @covers \FireflyIII\Http\Middleware\Binder::__construct @@ -448,6 +504,24 @@ class BinderTest extends TestCase $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); } + /** + * @covers \FireflyIII\Http\Middleware\Binder::handle + * @covers \FireflyIII\Http\Middleware\Binder::__construct + * @covers \FireflyIII\Http\Middleware\Binder::performBinding + * @covers \FireflyIII\Models\ImportJob::routeBinder + */ + public function testImportJobBadStatus() + { + Route::middleware(Binder::class)->any( + '/_test/binder/{importJob}', function () { + return 'OK'; + } + ); + $this->be($this->user()); + $response = $this->get('/_test/binder/bad-status'); + $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode()); + } + /** * @covers \FireflyIII\Http\Middleware\Binder::handle * @covers \FireflyIII\Http\Middleware\Binder::__construct @@ -485,27 +559,6 @@ class BinderTest extends TestCase $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); } - - /** - * @covers \FireflyIII\Http\Middleware\Binder::handle - * @covers \FireflyIII\Http\Middleware\Binder::__construct - * @covers \FireflyIII\Http\Middleware\Binder::performBinding - * @covers \FireflyIII\Models\ImportJob::routeBinder - */ - public function testImportJobBadStatus() - { - Route::middleware(Binder::class)->any( - '/_test/binder/{importJob}', function () { - return 'OK'; - } - ); - $this->be($this->user()); - $response = $this->get('/_test/binder/bad-status'); - $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode()); - } - - - /** * @covers \FireflyIII\Http\Middleware\Binder::handle * @covers \FireflyIII\Http\Middleware\Binder::__construct @@ -1010,7 +1063,5 @@ class BinderTest extends TestCase $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); } - - } \ No newline at end of file