Company Logo upload test

This commit is contained in:
David Bomba 2019-10-03 10:17:29 +10:00
parent db40a4ab71
commit 4e51256b51
5 changed files with 79 additions and 3 deletions

View File

@ -12,6 +12,7 @@
namespace App\Exceptions; namespace App\Exceptions;
use Exception; use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
@ -78,6 +79,10 @@ class Handler extends ExceptionHandler
{ {
return response()->json(['message'=>'Fatal error', 500]); return response()->json(['message'=>'Fatal error', 500]);
} }
else if($exception instanceof AuthorizationException)
{
return response()->json(['message'=>'You are not authorized to view or perform this action',401]);
}
else if ($exception instanceof \Illuminate\Session\TokenMismatchException) else if ($exception instanceof \Illuminate\Session\TokenMismatchException)
{ {
return redirect() return redirect()

View File

@ -106,6 +106,7 @@ class CompanyController extends BaseController
if($request->file('logo')) if($request->file('logo'))
{ {
\Log::error('logo exists');
$path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key); $path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key);
if($path){ if($path){
@ -185,6 +186,7 @@ class CompanyController extends BaseController
if($request->file('logo')) if($request->file('logo'))
{ {
\Log::error('logo exists');
$path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key); $path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key);
if($path){ if($path){

View File

@ -62,7 +62,7 @@ class CompanyPolicy extends EntityPolicy
*/ */
public function edit(User $user, $entity) : bool public function edit(User $user, $entity) : bool
{ {
return ($user->isAdmin() && $entity->id == $user->companyId()) return ($user->isAdmin() && $entity->id == $user->companyId())
|| ($user->hasPermission('edit_' . strtolower(class_basename($entity))) && $entity->id == $user->companyId()) || ($user->hasPermission('edit_' . strtolower(class_basename($entity))) && $entity->id == $user->companyId())
|| $user->owns($entity); || $user->owns($entity);

View File

@ -58,8 +58,8 @@ class PaymentTransformer extends EntityTransformer
'amount' => (float) $payment->amount, 'amount' => (float) $payment->amount,
'transaction_reference' => $payment->transaction_reference ?: '', 'transaction_reference' => $payment->transaction_reference ?: '',
'payment_date' => $payment->payment_date ?: '', 'payment_date' => $payment->payment_date ?: '',
'updated_at' => $this->getTimestamp($payment->updated_at), 'updated_at' => $payment->updated_at,
'archived_at' => $this->getTimestamp($payment->deleted_at), 'archived_at' => $payment->deleted_at,
'is_deleted' => (bool) $payment->is_deleted, 'is_deleted' => (bool) $payment->is_deleted,
'payment_type_id' => (string) $payment->payment_type_id ?: '', 'payment_type_id' => (string) $payment->payment_type_id ?: '',
'invitation_id' => (string) $payment->invitation_id ?: '', 'invitation_id' => (string) $payment->invitation_id ?: '',

View File

@ -0,0 +1,69 @@
<?php
namespace Tests\Integration;
use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Invoice\MarkInvoicePaid;
use App\Jobs\Util\UploadFile;
use App\Models\Account;
use App\Models\Activity;
use App\Models\Company;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\User;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class UploadLogoTest extends TestCase
{
use MockAccountData;
use DatabaseTransactions;
use MakesHash;
public function setUp() :void
{
parent::setUp();
$this->makeTestData();
}
public function testLogoUploadWorks()
{
Storage::fake('avatars');
$data = [
'logo' => UploadedFile::fake()->image('avatar.jpg'),
'name' => 'TestCompany'
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $data);
$response->assertStatus(200);
$acc = $response->json();
$logo = $acc['data']['logo'];
$logo_file = Storage::url($logo);
$this->assertNotNull($logo_file);
}
}