1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Tests for document.is_public validation

This commit is contained in:
David Bomba 2023-08-20 14:05:26 +10:00
parent da05b44ac3
commit 07d71b43fe
24 changed files with 201 additions and 48 deletions

View File

@ -228,7 +228,7 @@ class ClientController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $client); $this->saveDocuments($request->file('documents'), $client, $request->input('is_public', true));
} }
return $this->itemResponse($client->fresh()); return $this->itemResponse($client->fresh());

View File

@ -34,7 +34,7 @@ class UploadController extends Controller
/** @var \App\Models\ClientContact $client_contact **/ /** @var \App\Models\ClientContact $client_contact **/
$client_contact = auth()->user(); $client_contact = auth()->user();
$this->saveDocuments($request->getFile(), $client_contact->client, true); $this->saveDocuments($request->getFile(), $client_contact->client, $request->input('is_public', true));
return response([], 200); return response([], 200);
} }

View File

@ -424,7 +424,7 @@ class CompanyController extends BaseController
$company = $this->company_repo->save($request->all(), $company); $company = $this->company_repo->save($request->all(), $company);
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $company, false); $this->saveDocuments($request->input('documents'), $company, $request->input('is_public', true));
} }
if($request->has('e_invoice_certificate') && !is_null($request->file("e_invoice_certificate"))){ if($request->has('e_invoice_certificate') && !is_null($request->file("e_invoice_certificate"))){
@ -616,7 +616,7 @@ class CompanyController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $company); $this->saveDocuments($request->file('documents'), $company, $request->input('is_public', true));
} }
return $this->itemResponse($company->fresh()); return $this->itemResponse($company->fresh());

View File

@ -776,7 +776,7 @@ class CreditController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $credit); $this->saveDocuments($request->file('documents'), $credit, $request->input('is_public', true));
} }
return $this->itemResponse($credit->fresh()); return $this->itemResponse($credit->fresh());

View File

@ -564,7 +564,7 @@ class ExpenseController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $expense); $this->saveDocuments($request->file('documents'), $expense, $request->input('is_public', true));
} }
return $this->itemResponse($expense->fresh()); return $this->itemResponse($expense->fresh());

View File

@ -144,7 +144,7 @@ class GroupSettingController extends BaseController
$this->uploadLogo($request->file('company_logo'), $group_setting->company, $group_setting); $this->uploadLogo($request->file('company_logo'), $group_setting->company, $group_setting);
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $group_setting, false); $this->saveDocuments($request->input('documents'), $group_setting, $request->input('is_public', true));
} }
return $this->itemResponse($group_setting); return $this->itemResponse($group_setting);
@ -217,7 +217,7 @@ class GroupSettingController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $group_setting); $this->saveDocuments($request->file('documents'), $group_setting, $request->input('is_public', true));
} }
return $this->itemResponse($group_setting->fresh()); return $this->itemResponse($group_setting->fresh());

View File

@ -977,7 +977,7 @@ class InvoiceController extends BaseController
} }
if ($request->has('file')) { if ($request->has('file')) {
$this->saveDocuments($request->file('documents'), $invoice, $request->input('is_public', true)); $this->saveDocuments($request->file('file'), $invoice, $request->input('is_public', true));
} }
return $this->itemResponse($invoice->fresh()); return $this->itemResponse($invoice->fresh());

View File

@ -749,7 +749,7 @@ class PaymentController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $payment); $this->saveDocuments($request->file('documents'), $payment, $request->input('is_public', true));
} }
return $this->itemResponse($payment->fresh()); return $this->itemResponse($payment->fresh());

View File

@ -541,7 +541,7 @@ class ProductController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $product); $this->saveDocuments($request->file('documents'), $product, $request->input('is_public', true));
} }
return $this->itemResponse($product->fresh()); return $this->itemResponse($product->fresh());

View File

@ -264,7 +264,7 @@ class ProjectController extends BaseController
$project->saveQuietly(); $project->saveQuietly();
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $project); $this->saveDocuments($request->input('documents'), $project, $request->input('is_public', true));
} }
event('eloquent.updated: App\Models\Project', $project); event('eloquent.updated: App\Models\Project', $project);
@ -373,7 +373,7 @@ class ProjectController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $project); $this->saveDocuments($request->input('documents'), $project, $request->input('is_public', true));
} }
event('eloquent.created: App\Models\Project', $project); event('eloquent.created: App\Models\Project', $project);
@ -565,7 +565,7 @@ class ProjectController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $project); $this->saveDocuments($request->file('documents'), $project, $request->input('is_public', true));
} }
return $this->itemResponse($project->fresh()); return $this->itemResponse($project->fresh());

View File

@ -751,7 +751,7 @@ class PurchaseOrderController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $purchase_order); $this->saveDocuments($request->file('documents'), $purchase_order, $request->input('is_public', true));
} }
return $this->itemResponse($purchase_order->fresh()); return $this->itemResponse($purchase_order->fresh());

View File

@ -905,7 +905,7 @@ class QuoteController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $quote); $this->saveDocuments($request->file('documents'), $quote, $request->input('is_public', true));
} }
return $this->itemResponse($quote->fresh()); return $this->itemResponse($quote->fresh());

View File

@ -609,7 +609,7 @@ class RecurringExpenseController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $recurring_expense); $this->saveDocuments($request->file('documents'), $recurring_expense, $request->input('is_public', true));
} }
return $this->itemResponse($recurring_expense->fresh()); return $this->itemResponse($recurring_expense->fresh());

View File

@ -550,7 +550,7 @@ class RecurringInvoiceController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $recurring_invoice); $this->saveDocuments($request->file('documents'), $recurring_invoice, $request->input('is_public', true));
} }
return $this->itemResponse($recurring_invoice->fresh()); return $this->itemResponse($recurring_invoice->fresh());

View File

@ -582,7 +582,7 @@ class TaskController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $task); $this->saveDocuments($request->file('documents'), $task, $request->input('is_public', true));
} }
return $this->itemResponse($task->fresh()); return $this->itemResponse($task->fresh());

View File

@ -568,7 +568,7 @@ class VendorController extends BaseController
} }
if ($request->has('documents')) { if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $vendor); $this->saveDocuments($request->file('documents'), $vendor, $request->input('is_public', true));
} }
return $this->itemResponse($vendor->fresh()); return $this->itemResponse($vendor->fresh());

View File

@ -33,7 +33,7 @@ class UploadController extends Controller
*/ */
public function upload(StoreUploadRequest $request, PurchaseOrder $purchase_order) public function upload(StoreUploadRequest $request, PurchaseOrder $purchase_order)
{ {
$this->saveDocuments($request->getFile(), $purchase_order, true); $this->saveDocuments($request->getFile(), $purchase_order, $request->input('is_public', true));
return response([], 200); return response([], 200);
} }

View File

@ -31,7 +31,10 @@ class UpdateClientRequest extends Request
*/ */
public function authorize() : bool public function authorize() : bool
{ {
return auth()->user()->can('edit', $this->client); /** @var \App\Models\User $user */
$user = auth()->user();
return $user->can('edit', $this->client);
} }
public function rules() public function rules()

View File

@ -23,12 +23,16 @@ class StoreDocumentRequest extends Request
*/ */
public function authorize() : bool public function authorize() : bool
{ {
return auth()->user()->can('create', Document::class); /** @var \App\Models\User $user */
$user = auth()->user();
return $user->can('create', Document::class);
} }
public function rules() public function rules()
{ {
return [ return [
'is_public' => 'sometimes|boolean',
]; ];
} }
@ -36,6 +40,21 @@ class StoreDocumentRequest extends Request
{ {
$input = $this->all(); $input = $this->all();
if(isset($input['is_public']))
$input['is_public'] = $this->toBoolean($input['is_public']);
$this->replace($input); $this->replace($input);
} }
/**
* Convert to boolean
*
* @param $bool
* @return bool
*/
private function toBoolean($bool): bool
{
return filter_var($bool, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
} }

View File

@ -34,14 +34,30 @@ class UpdateDocumentRequest extends Request
public function rules() public function rules()
{ {
return [ return [
'name' => 'sometimes' 'name' => 'sometimes',
'is_public' => 'sometimes|boolean',
]; ];
} }
public function prepareForValidation() public function prepareForValidation()
{ {
$input = $this->all(); $input = $this->all();
if(isset($input['is_public']))
$input['is_public'] = $this->toBoolean($input['is_public']);
$this->replace($input); $this->replace($input);
} }
/**
* Convert to boolean
*
* @param $bool
* @return bool
*/
private function toBoolean($bool): bool
{
return filter_var($bool, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
} }

View File

@ -25,6 +25,7 @@ class UploadInvoiceRequest extends Request
{ {
/** @var \App\Models\User $user */ /** @var \App\Models\User $user */
$user = auth()->user(); $user = auth()->user();
return $user->can('edit', $this->invoice); return $user->can('edit', $this->invoice);
} }
@ -51,24 +52,5 @@ class UploadInvoiceRequest extends Request
public function prepareForValidation() public function prepareForValidation()
{ {
//tests to see if upload via binary data works.
// if(request()->getContent())
// {
// // $file = new UploadedFile(request()->getContent(), request()->header('filename'));
// $file = new UploadedFile(request()->getContent(), 'something.png');
// // request()->files->set('documents', $file);
// $this->files->add(['file' => $file]);
// // Merge it in request also (As I found this is not needed in every case)
// $this->merge(['file' => $file]);
// }
} }
} }

View File

@ -77,6 +77,16 @@ class Document extends BaseModel
'name', 'name',
]; ];
/**
* @var array<string>
*/
protected $casts = [
'is_public' => 'bool',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
/** /**
* @var array * @var array
*/ */

View File

@ -25,7 +25,7 @@ class DocumentFactory extends Factory
return [ return [
'is_default' => true, 'is_default' => true,
'is_public' => true, 'is_public' => true,
'name' => true, 'name' => $this->faker->word().".png",
]; ];
} }
} }

View File

@ -11,12 +11,13 @@
namespace Tests\Feature; namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Document;
use Tests\MockAccountData;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;
use Tests\MockAccountData; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
/** /**
* @test * @test
@ -28,6 +29,8 @@ class DocumentsApiTest extends TestCase
use DatabaseTransactions; use DatabaseTransactions;
use MockAccountData; use MockAccountData;
protected $faker;
protected function setUp() :void protected function setUp() :void
{ {
parent::setUp(); parent::setUp();
@ -41,6 +44,126 @@ class DocumentsApiTest extends TestCase
Model::reguard(); Model::reguard();
} }
public function testIsPublicTypesForDocumentRequest()
{
$d = Document::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
]);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get("/api/v1/documents/{$d->hashed_id}");
$response->assertStatus(200);
$update = [
'is_public' => false,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertFalse($arr['data']['is_public']);
$update = [
'is_public' => true,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertTrue($arr['data']['is_public']);
$update = [
'is_public' => 'true',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertTrue($arr['data']['is_public']);
$update = [
'is_public' => '1',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertTrue($arr['data']['is_public']);
$update = [
'is_public' => 1,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertTrue($arr['data']['is_public']);
$update = [
'is_public' => 'false',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertFalse($arr['data']['is_public']);
$update = [
'is_public' => '0',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertFalse($arr['data']['is_public']);
$update = [
'is_public' => 0,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/documents/{$d->hashed_id}", $update);
$response->assertStatus(200);
$arr = $response->json();
$this->assertFalse($arr['data']['is_public']);
}
public function testClientDocuments() public function testClientDocuments()
{ {
$response = $this->withHeaders([ $response = $this->withHeaders([