6 Commits e433262c4c ... 2e45ccfde7

Author SHA1 Message Date
  Felix Freeman 2e45ccfde7 Complete core exceptions test 4 years ago
  Felix Freeman 558953b002 Bugfix: ModelObjectNotFoundException message when class name is not absolute 4 years ago
  Felix Freeman 9d4defb592 Comment out use of HttpResponseException on exceptions handler. Couldn't find a real use case 4 years ago
  Felix Freeman 34abe943d0 Use getStatusCode() instead of plain $status on Exceptions 4 years ago
  Felix Freeman d246ce73f2 Add unauthorized whoAmI test to AuthController in order to test Authorize middleware 4 years ago
  Felix Freeman 077209acba HomeController test 4 years ago

+ 3 - 0
src/Exceptions/Handler.php

@@ -50,9 +50,12 @@ class Handler extends ExceptionHandler
      */
     public function render($request, Exception $exception)
     {
+        /*
+        // I don't know when it would be used, so I'll just comment it
         if ($exception instanceof HttpResponseException) {
             return $exception->getResponse();
         }
+        */
 
         return JsonExceptionRenderer::render($exception);
     }

+ 1 - 1
src/Exceptions/JsonExceptionRenderer.php

@@ -53,7 +53,7 @@ class JsonExceptionRenderer
         if ($this->exception->getCode() === 0) {
             return null;
         }
-        
+
         return $this->exception->getCode();
     }
 

+ 2 - 1
src/Exceptions/ModelObjectNotFoundException.php

@@ -25,7 +25,8 @@ class ModelObjectNotFoundException extends RuntimeException
         parent::__construct(
             sprintf( // message
                 "%s %s %s could not be found",
-                substr(strrchr($model, '\\'), 1),
+                (strpos($model, '\\') === false
+                 ? $model : substr(strrchr($model, '\\'), 1)),
                 implode(' or ', $keys),
                 htmlspecialchars($value)
             ),

+ 8 - 3
src/Exceptions/UnknownForeignObjectException.php

@@ -9,8 +9,8 @@ use Exception;
 
 class UnknownForeignObjectException extends Exception
 {
-    private $detail = [];
-    public $status;
+    protected $detail = [];
+    protected $statusCode;
 
     public function __construct($model, $attribute)
     {
@@ -19,12 +19,17 @@ class UnknownForeignObjectException extends Exception
             2 // code
         );
 
+        $this->statusCode = 400;
+
         $this->detail = [
             'model' => $model,
             'attribute' => $attribute
         ];
+    }
 
-        $this->status = 400;
+    public function getStatusCode(): int
+    {
+        return $this->statusCode;
     }
 
     public function getDetail()

+ 7 - 2
src/Exceptions/WrongCredentialsException.php

@@ -9,7 +9,7 @@ use RuntimeException;
 
 class WrongCredentialsException extends RuntimeException
 {
-    public $status;
+    protected $statusCode;
 
     public function __construct($model, $identifier)
     {
@@ -20,6 +20,11 @@ class WrongCredentialsException extends RuntimeException
             4 // code
         );
 
-        $this->status = 403;
+        $this->statusCode = 403;
+    }
+
+    public function getStatusCode(): int
+    {
+        return $this->statusCode;
     }
 }

+ 8 - 1
tests/AuthControllerTest.php

@@ -5,7 +5,6 @@
 
 namespace Hawese\Tests;
 
-// use Hawese\Core\Http\Controllers\AuthController; // ??
 use Hawese\Core\User;
 use Laravel\Lumen\Testing\DatabaseTransactions;
 
@@ -170,6 +169,14 @@ class AuthControllerTest extends TestCase
         );
     }
 
+    public function testUnauthorizedWhoAmI()
+    {
+        $this->assertSame(
+            \Illuminate\Http\Response::HTTP_UNAUTHORIZED,
+            $this->request('GET', '/auth/whoami')->getStatusCode()
+        );
+    }
+
     public function testLogout()
     {
         $this->user->login();

+ 165 - 0
tests/ExceptionsTest.php

@@ -0,0 +1,165 @@
+<?php
+
+// Copyright 2019 Hackware SpA <human@hackware.cl>
+// Part of "Hackware Web Services Core", released under the MIT License terms.
+
+namespace Hawese\Tests;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+use Illuminate\Validation\ValidationException;
+use Hawese\Core\Exceptions
+    \{Handler, JsonExceptionRenderer, ModelValidationException,
+    ModelObjectNotFoundException, UnknownForeignObjectException,
+    WrongCredentialsException};
+use Exception;
+
+class ExceptionsTest extends TestCase
+{
+    public function testBuildCode()
+    {
+        $renderer = new JsonExceptionRenderer(new Exception());
+        $this->assertArrayNotHasKey('code', $renderer->build());
+
+        $renderer = new JsonExceptionRenderer(new Exception('', 42));
+        $this->assertSame(42, $renderer->build()['code']);
+    }
+
+    public function testBuildMessage()
+    {
+        $renderer = new JsonExceptionRenderer(
+            (new HttpException(
+                Response::HTTP_BAD_REQUEST
+            ))
+        );
+        $this->assertSame(
+            Response::$statusTexts[Response::HTTP_BAD_REQUEST],
+            $renderer->build()['message']
+        );
+
+        $renderer = new JsonExceptionRenderer(
+            (new HttpException(
+                Response::HTTP_BAD_REQUEST,
+                'Custom message'
+            ))
+        );
+        $this->assertSame(
+            'Custom message',
+            $renderer->build()['message']
+        );
+    }
+
+    public function testBuildDetail()
+    {
+        $renderer = new JsonExceptionRenderer(new Exception());
+        $this->assertArrayNotHasKey('detail', $renderer->build());
+
+        $renderer = new JsonExceptionRenderer(
+            new UnknownForeignObjectException('model', 'attribute')
+        );
+        $this->assertEqualsCanonicalizing(
+            ['model' => 'model', 'attribute' => 'attribute'],
+            $renderer->build()['detail']
+        );
+
+        // ValidationException instance tested on testModelValidationException
+    }
+
+    public function testBuildDebug()
+    {
+        $renderer = new JsonExceptionRenderer((new Exception()));
+        $this->assertSame('Exception', $renderer->build()['debug']['exception']);
+
+        $_ENV['APP_DEBUG'] = false;
+        $this->assertArrayNotHasKey('debug', $renderer->build());
+    }
+
+    public function testResponseAndRender()
+    {
+        $response = JsonExceptionRenderer::render(new Exception());
+        $this->assertObjectHasAttribute('error', $response->getData());
+
+        $this->assertSame(
+            Response::HTTP_INTERNAL_SERVER_ERROR,
+            $response->getStatusCode()
+        );
+
+        $response = JsonExceptionRenderer::render(
+            (new HttpException(
+                Response::HTTP_BAD_REQUEST
+            ))
+        );
+        $this->assertSame(
+            Response::HTTP_BAD_REQUEST,
+            $response->getStatusCode()
+        );
+    }
+
+    public function testModelObjectNotFoundException()
+    {
+        $response = JsonExceptionRenderer::render(
+            new ModelObjectNotFoundException('model', 'key', 'value')
+        );
+        $this->assertSame(
+            'model key value could not be found',
+            $response->getData()->error->message
+        );
+        $response = JsonExceptionRenderer::render(
+            new ModelObjectNotFoundException('model', ['key1', 'key2'], 'value')
+        );
+        $this->assertSame(
+            'model key1 or key2 value could not be found',
+            $response->getData()->error->message
+        );
+    }
+
+    public function testModelValidationException()
+    {
+        $validator = app('validator')->make([], ['attr' => 'required']);
+        $response = JsonExceptionRenderer::render(
+            new ModelValidationException('model', $validator)
+        );
+        $this->assertSame(
+            Response::HTTP_UNPROCESSABLE_ENTITY,
+            $response->getStatusCode()
+        );
+        $this->assertStringContainsString(
+            'Failed',
+            $response->getData()->error->message
+        );
+        $this->assertStringContainsString(
+            'required',
+            $response->getData()->error->detail->attributes->attr[0]
+        );
+    }
+
+    public function testUnknownForeignObjectException()
+    {
+        $response = JsonExceptionRenderer::render(
+            new UnknownForeignObjectException('model', 'attribute')
+        );
+        $this->assertSame(
+            Response::HTTP_BAD_REQUEST,
+            $response->getStatusCode()
+        );
+        $this->assertEqualsCanonicalizing(
+            (object) ['model' => 'model', 'attribute' => 'attribute'],
+            $response->getData()->error->detail
+        );
+    }
+
+    public function testWrongCredentialsException()
+    {
+        $response = JsonExceptionRenderer::render(
+            new WrongCredentialsException('model', 'identifier')
+        );
+        $this->assertSame(
+            Response::HTTP_FORBIDDEN,
+            $response->getStatusCode()
+        );
+        $this->assertSame(
+            'Wrong secret for model identifier',
+            $response->getData()->error->message
+        );
+    }
+}

+ 23 - 0
tests/HomeControllerTest.php

@@ -0,0 +1,23 @@
+<?php
+
+// Copyright 2019 Hackware SpA <human@hackware.cl>
+// "Hackware Web Services Core" is released under the MIT License terms.
+
+namespace Hawese\Tests;
+
+class HomeControllerTest extends TestCase
+{
+    public function testIndex()
+    {
+        $response = $this->request('GET', '/');
+
+        $this->assertSame(
+            env('WALLET_SOURCE_URL'),
+            $response->getData()->links->wallet_sourcecode
+        );
+        $this->assertSame(
+            env('PAYMENT_SOURCE_URL'),
+            $response->getData()->links->payment_sourcecode
+        );
+    }
+}