mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 03:24:35 -04:00
Fixes for document public/private setting
This commit is contained in:
parent
0457090226
commit
eb33ee7d78
@ -30,11 +30,22 @@ class UploadTaskRequest extends Request
|
|||||||
|
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
$rules = [
|
$rules = [];
|
||||||
'documents' => 'bail|sometimes|file|mimes:csv,png,ai,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx|max:2000000',
|
|
||||||
'is_public' => 'sometimes|boolean',
|
|
||||||
];
|
|
||||||
|
|
||||||
|
if ($this->file('documents') && is_array($this->file('documents'))) {
|
||||||
|
$rules['documents.*'] = $this->file_validation;
|
||||||
|
} elseif ($this->file('documents')) {
|
||||||
|
$rules['documents'] = $this->file_validation;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->file('file') && is_array($this->file('file'))) {
|
||||||
|
$rules['file.*'] = $this->file_validation;
|
||||||
|
} elseif ($this->file('file')) {
|
||||||
|
$rules['file'] = $this->file_validation;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rules['is_public'] = 'sometimes|boolean';
|
||||||
|
|
||||||
return $rules;
|
return $rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
151
tests/Integration/FileUploadValidationTest.php
Normal file
151
tests/Integration/FileUploadValidationTest.php
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Integration;
|
||||||
|
|
||||||
|
use App\Models\Company;
|
||||||
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Tests\MockAccountData;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
class FileUploadValidationTest extends TestCase
|
||||||
|
{
|
||||||
|
use MockAccountData;
|
||||||
|
use DatabaseTransactions;
|
||||||
|
use MakesHash;
|
||||||
|
|
||||||
|
protected function setUp() :void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->makeTestData();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIteratingThroughAllEntities()
|
||||||
|
{
|
||||||
|
|
||||||
|
Storage::fake('local');
|
||||||
|
|
||||||
|
$file = UploadedFile::fake()->image('avatar.jpg');
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'documents' => [$file],
|
||||||
|
'is_public' => false,
|
||||||
|
'_method' => 'PUT',
|
||||||
|
];
|
||||||
|
|
||||||
|
$entities = [
|
||||||
|
'invoice' => 'invoices',
|
||||||
|
'quote' => 'quotes',
|
||||||
|
'payment' => 'payments',
|
||||||
|
'credit' => 'credits',
|
||||||
|
'expense' => 'expenses',
|
||||||
|
'project' => 'projects',
|
||||||
|
'task' => 'tasks',
|
||||||
|
'vendor' => 'vendors',
|
||||||
|
'product' => 'products',
|
||||||
|
'client' => 'clients',
|
||||||
|
'recurring_invoice' => 'recurring_invoices',
|
||||||
|
'recurring_expense' => 'recurring_expenses',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach($entities as $key => $value) {
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson("/api/v1/{$value}/{$this->{$key}->hashed_id}/upload", $data);
|
||||||
|
|
||||||
|
$acc = $response->json();
|
||||||
|
$response->assertStatus(200);
|
||||||
|
|
||||||
|
$this->assertCount(1, $acc['data']['documents']);
|
||||||
|
$this->assertFalse($acc['data']['documents'][0]['is_public']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFileUploadIsPublicSetsAppropriately()
|
||||||
|
{
|
||||||
|
Storage::fake('local');
|
||||||
|
|
||||||
|
$file = UploadedFile::fake()->image('avatar.jpg');
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'documents' => [$file],
|
||||||
|
'is_public' => false,
|
||||||
|
'_method' => 'PUT',
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson("/api/v1/invoices/{$this->invoice->hashed_id}/upload", $data);
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$acc = $response->json();
|
||||||
|
|
||||||
|
$this->assertCount(1, $acc['data']['documents']);
|
||||||
|
$this->assertFalse($acc['data']['documents'][0]['is_public']);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'documents' => [$file],
|
||||||
|
'is_public' => true,
|
||||||
|
'_method' => 'PUT',
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson("/api/v1/invoices/{$this->invoice->hashed_id}/upload", $data);
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$acc = $response->json();
|
||||||
|
|
||||||
|
$this->assertCount(2, $acc['data']['documents']);
|
||||||
|
$this->assertTrue($acc['data']['documents'][1]['is_public']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMultiFileUploadIsPublicSetsAppropriately()
|
||||||
|
{
|
||||||
|
Storage::fake('local');
|
||||||
|
|
||||||
|
$file = UploadedFile::fake()->image('avatar.jpg');
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'documents' => [$file, $file],
|
||||||
|
'is_public' => false,
|
||||||
|
'_method' => 'PUT',
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson("/api/v1/invoices/{$this->invoice->hashed_id}/upload", $data);
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$acc = $response->json();
|
||||||
|
|
||||||
|
$this->assertCount(2, $acc['data']['documents']);
|
||||||
|
$this->assertFalse($acc['data']['documents'][0]['is_public']);
|
||||||
|
$this->assertFalse($acc['data']['documents'][1]['is_public']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -186,6 +186,10 @@ trait MockAccountData
|
|||||||
|
|
||||||
public $contact;
|
public $contact;
|
||||||
|
|
||||||
|
public $product;
|
||||||
|
|
||||||
|
public $recurring_invoice;
|
||||||
|
|
||||||
public function makeTestData()
|
public function makeTestData()
|
||||||
{
|
{
|
||||||
config(['database.default' => config('ninja.db.default')]);
|
config(['database.default' => config('ninja.db.default')]);
|
||||||
@ -371,6 +375,17 @@ trait MockAccountData
|
|||||||
'client_id' => $this->client->id,
|
'client_id' => $this->client->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$this->product = Product::factory()->create([
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->recurring_invoice = RecurringInvoice::factory()->create([
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'client_id' => $this->client->id,
|
||||||
|
]);
|
||||||
|
|
||||||
$this->expense = Expense::factory()->create([
|
$this->expense = Expense::factory()->create([
|
||||||
'user_id' => $user_id,
|
'user_id' => $user_id,
|
||||||
'company_id' => $this->company->id,
|
'company_id' => $this->company->id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user