mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange.git
synced 2026-08-19 12:24:09 +00:00
Merge branch 'too/validatefilesize' into 'master'
Too/validatefilesize See merge request CIEFWorldwideSdnBhd/exchange!33
This commit is contained in:
@@ -6,5 +6,5 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ChinaBankSlip extends Model
|
||||
{
|
||||
protected $fillable = ['actual_transfer_amount','date','details','china_bank_slips'];
|
||||
protected $fillable = ['actual_transfer_amount','date','details','china_bank_slip_path'];
|
||||
}
|
||||
|
||||
@@ -284,7 +284,7 @@ class BookingController extends Controller
|
||||
|
||||
return response()->json(['book_id'=>$book_id],200);
|
||||
}
|
||||
|
||||
//upload
|
||||
public function uploadbankslip(Request $request, $id)
|
||||
{
|
||||
$booking = Booking::where('user_id',Auth::user()->id)->where('id',$id)->first();
|
||||
@@ -304,6 +304,14 @@ class BookingController extends Controller
|
||||
return response()->json(['message'=>'Incorrect format'],200);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'file' => 'max:3072'
|
||||
]);
|
||||
|
||||
if($validator->fails())
|
||||
{
|
||||
return response()->json(['message'=>'Image too large, upload files up to 3 MB'], 400);
|
||||
}
|
||||
|
||||
if(!$bankslip_file = $request->file('file'))
|
||||
{
|
||||
@@ -361,6 +369,15 @@ class BookingController extends Controller
|
||||
return response()->json(['message'=>'Incorrect format'],200);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'file' => 'max:3072'
|
||||
]);
|
||||
|
||||
if($validator->fails())
|
||||
{
|
||||
return response()->json(['message'=>'Image too large, upload files up to 3 MB'], 400);
|
||||
}
|
||||
|
||||
if(!$user_input_file = $request->file('file'))
|
||||
{
|
||||
return response()->json(['message' => 'No file detected'], 400);
|
||||
@@ -523,7 +540,7 @@ class BookingController extends Controller
|
||||
|
||||
return response()->json(['message'=>'Success'],200);
|
||||
}
|
||||
|
||||
//upload
|
||||
public function uploadInvoice(Request $request, $id)
|
||||
{
|
||||
$booking = Booking::where('user_id',Auth::user()->id)->where('id',$id)->first();
|
||||
@@ -543,6 +560,15 @@ class BookingController extends Controller
|
||||
return response()->json(['message'=>'Incorrect format'],200);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'invoice_path' => 'max:3072'
|
||||
]);
|
||||
|
||||
if($validator->fails())
|
||||
{
|
||||
return response()->json(['message'=>'Image too large, upload files up to 3 MB'], 400);
|
||||
}
|
||||
|
||||
if(!$input_file = $request->file('invoice_path'))
|
||||
{
|
||||
return response()->json(['message'=>'No file detected'], 400);
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\ChinaBankSlip;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Validator;
|
||||
|
||||
class ChinaBankSlipController extends Controller
|
||||
{
|
||||
@@ -45,6 +47,16 @@ class ChinaBankSlipController extends Controller
|
||||
$extension = $request->file('china_bank_slips')->getClientOriginalExtension();
|
||||
// Filename to store
|
||||
$fileNameToStore= $filename.'_'.time().'.'.$extension;
|
||||
|
||||
// File size checking
|
||||
$validator = Validator::make($request->all(), [
|
||||
'china_bank_slips' => 'max:3072'
|
||||
]);
|
||||
|
||||
if($validator->fails())
|
||||
{
|
||||
return response()->json(['message'=>'Image too large, upload files up to 3 MB'], 400);
|
||||
}
|
||||
// Upload Image
|
||||
$path = $request->file('china_bank_slips')->storeAs('public/bankslip', $fileNameToStore);
|
||||
} else {
|
||||
@@ -55,8 +67,9 @@ class ChinaBankSlipController extends Controller
|
||||
$post->actual_transfer_amount = $request->input('actual_transfer_amount');
|
||||
$post->date = $request->input('date');
|
||||
$post->details = $request->input('details');
|
||||
$post->china_bank_slips = $fileNameToStore;
|
||||
$post->save();;
|
||||
$post->china_bank_slip_path = $fileNameToStore;
|
||||
$post->save();
|
||||
return response()->json(['message'=>$post], 200);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class RenameBankslipUrlToChinabanksliPathInChinaBankSlipTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('china_bank_slips', function($table)
|
||||
{
|
||||
$table->renameColumn('bankslip_url', 'china_bank_slip_path');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
{
|
||||
|
||||
@@ -83,17 +83,21 @@ class BookingTest extends TestCase
|
||||
public function testuploadbankslip()
|
||||
{
|
||||
Storage::fake('file');
|
||||
$bankslip_path = UploadedFile::fake()->image('comp.jpg');
|
||||
|
||||
if(!file_exists($bankslip_path)){
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
$size_in_kb = 3072; // 3072KB = 3MB
|
||||
$bankslip_path = UploadedFile::fake()->image('comp.jpg')->size($size_in_kb);
|
||||
|
||||
$response =
|
||||
$this->actingAs($this->user)
|
||||
->json('POST', '/api/booking/' . $this->booking->id . '/upload-user-bankslip', [
|
||||
'file' => $bankslip_path
|
||||
])
|
||||
->assertSuccessful();
|
||||
]);
|
||||
|
||||
if(!file_exists($bankslip_path) || $size_in_kb > 3072){
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
else{
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
}
|
||||
|
||||
public function testUpdateBankslipAmount()
|
||||
@@ -108,18 +112,22 @@ class BookingTest extends TestCase
|
||||
public function testuploadPO()
|
||||
{
|
||||
Storage::fake('file');
|
||||
$image_path = UploadedFile::fake()->image('comp.jpg');
|
||||
|
||||
if(!file_exists($image_path)){
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
$size_in_kb = 3072; // 3072KB = 3MB
|
||||
$image_path = UploadedFile::fake()->image('comp.jpg')->size($size_in_kb);
|
||||
|
||||
$response =
|
||||
$this->actingAs($this->user)
|
||||
->json('POST', '/api/booking/' . $this->booking->id . '/upload-po', [
|
||||
'file' => $image_path,
|
||||
'amount' => '12345'
|
||||
])
|
||||
->assertSuccessful();
|
||||
]);
|
||||
|
||||
if(!file_exists($image_path) || $size_in_kb > 3072){
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
else{
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
}
|
||||
|
||||
public function testCancel()
|
||||
@@ -152,18 +160,22 @@ class BookingTest extends TestCase
|
||||
$this->user->roles()->sync([]);
|
||||
$this->user->attachRole(Role::Where('name','admin')->first());
|
||||
|
||||
$size_in_kb = 3072;
|
||||
Storage::fake('invoice_path');
|
||||
$invoice_path = UploadedFile::fake()->create('document.pdf');
|
||||
|
||||
if(!file_exists($invoice_path)){
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
$invoice_path = UploadedFile::fake()->create('document.pdf',$size_in_kb);
|
||||
|
||||
$response =
|
||||
$this->actingAs($this->user)
|
||||
->json('POST', '/api/booking/' . $this->booking->id . '/upload-invoice', [
|
||||
'invoice_path' => $invoice_path,
|
||||
'amount' => '12345'
|
||||
])
|
||||
->assertSuccessful();
|
||||
]);
|
||||
|
||||
if(!file_exists($invoice_path) || $size_in_kb > 3072){
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
else{
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Artisan;
|
||||
|
||||
class ChinaBankSlipTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testStore(){
|
||||
Storage::fake('file');
|
||||
$size_in_kb = 3072; // 3072KB = 3MB
|
||||
$china_bank_slips_path = UploadedFile::fake()->image('comp.jpg')->size($size_in_kb);
|
||||
|
||||
$response =
|
||||
$this->postJson('api/upload-china-bankslip',[
|
||||
'china_bank_slips' => $china_bank_slips_path,
|
||||
'actual_transfer_amount' => '12',
|
||||
'details' => "123123",
|
||||
'date' => "123123"
|
||||
]);
|
||||
|
||||
if(!file_exists($china_bank_slips_path) || $size_in_kb > 3072){
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
else{
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user