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

Bank Integration API Tests

This commit is contained in:
David Bomba 2022-08-11 17:05:33 +10:00
parent 90557a3083
commit d50220c387
10 changed files with 382 additions and 2 deletions

View File

@ -0,0 +1,36 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Factory;
use App\Models\BankTransaction;
use Illuminate\Support\Str;
class BankTransactionFactory
{
public static function create(int $company_id, int $user_id, int $account_id) :BankTransaction
{
$bank_transaction = new BankTransaction;
$bank_transaction->account_id = $account_id;
$bank_transaction->user_id = $user_id;
$bank_transaction->company_id = $company_id;
$bank_transaction->amount = 0;
$bank_transaction->currency_code = '';
$bank_transaction->account_type = '';
$bank_transaction->category_type = '';
$bank_transaction->date = now()->format('Y-m-d');
$bank_transaction->description = '';
$bank_transaction->is_matched = 0;
return $bank_transaction;
}
}

View File

@ -24,12 +24,14 @@ use App\Models\BankIntegration;
use App\Repositories\BankIntegrationRepository; use App\Repositories\BankIntegrationRepository;
use App\Services\Bank\BankService; use App\Services\Bank\BankService;
use App\Transformers\BankIntegrationTransformer; use App\Transformers\BankIntegrationTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class BankIntegrationController extends BaseController class BankIntegrationController extends BaseController
{ {
use MakesHash;
protected $entity_type = BankIntegration::class; protected $entity_type = BankIntegration::class;
protected $entity_transformer = BankIntegrationTransformer::class; protected $entity_transformer = BankIntegrationTransformer::class;
@ -418,6 +420,80 @@ class BankIntegrationController extends BaseController
return $this->itemResponse($bank_integration->fresh()); return $this->itemResponse($bank_integration->fresh());
} }
/**
* Perform bulk actions on the list view.
*
* @return Collection
*
* @OA\Post(
* path="/api/v1/invoices/bulk",
* operationId="bulkInvoices",
* tags={"invoices"},
* summary="Performs bulk actions on an array of invoices",
* description="",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="User credentials",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="The Bulk Action response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function bulk()
{
$action = request()->input('action');
if(!in_array($action, ['archive', 'restore', 'delete']))
return response()->json(['message' => 'Unsupported action.'], 400);
$ids = request()->input('ids');
$bank_integrations = BankIntegration::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
$bank_integrations->each(function ($bank_integration, $key) use ($action) {
if (auth()->user()->can('edit', $bank_integration)) {
$this->bank_integration_repo->{$action}($bank_integration);
}
});
/* Need to understand which permission are required for the given bulk action ie. view / edit */
return $this->listResponse(BankIntegration::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());
}
/** /**
* Return the remote list of accounts stored on the third part provider. * Return the remote list of accounts stored on the third part provider.
* *

View File

@ -0,0 +1,31 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Policies;
use App\Models\User;
/**
* Class BankTransactionPolicy.
*/
class BankTransactionPolicy extends EntityPolicy
{
/**
* Checks if the user has create permissions.
*
* @param User $user
* @return bool
*/
public function create(User $user) : bool
{
return $user->isAdmin();
}
}

View File

@ -13,6 +13,8 @@ namespace App\Providers;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Bank; use App\Models\Bank;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Models\Client; use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
@ -42,6 +44,7 @@ use App\Models\Vendor;
use App\Models\Webhook; use App\Models\Webhook;
use App\Policies\ActivityPolicy; use App\Policies\ActivityPolicy;
use App\Policies\BankIntegrationPolicy; use App\Policies\BankIntegrationPolicy;
use App\Policies\BankTransactionPolicy;
use App\Policies\ClientPolicy; use App\Policies\ClientPolicy;
use App\Policies\CompanyGatewayPolicy; use App\Policies\CompanyGatewayPolicy;
use App\Policies\CompanyPolicy; use App\Policies\CompanyPolicy;
@ -81,7 +84,8 @@ class AuthServiceProvider extends ServiceProvider
*/ */
protected $policies = [ protected $policies = [
Activity::class => ActivityPolicy::class, Activity::class => ActivityPolicy::class,
Bank::class => BankIntegrationPolicy::class, BankIntegration::class => BankIntegrationPolicy::class,
BankTransaction::class => BankTransactionPolicy::class,
Client::class => ClientPolicy::class, Client::class => ClientPolicy::class,
Company::class => CompanyPolicy::class, Company::class => CompanyPolicy::class,
CompanyToken::class => CompanyTokenPolicy::class, CompanyToken::class => CompanyTokenPolicy::class,

View File

@ -0,0 +1,41 @@
<?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 Database\Factories;
use App\Models\Account;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class BankIntegrationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'provider_name' => $this->faker->company(),
'provider_id' => 1,
'bank_account_name' => $this->faker->catchPhrase(),
'bank_account_id' => 1,
'bank_account_number' => $this->faker->randomNumber(9, true),
'bank_account_status' => 'active',
'bank_account_type' => 'creditCard',
'balance' => $this->faker->randomFloat(2, 10, 10000),
'currency' => 'USD',
'nickname' => $this->faker->word(),
'is_deleted' => false,
];
}
}

View File

@ -0,0 +1,40 @@
<?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 Database\Factories;
use App\Models\Account;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class BankTransactionFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'transaction_id' => $this->faker->randomNumber(9, true) ,
'amount' => $this->faker->randomFloat(2,10,10000) ,
'currency_code' => 'USD',
'account_type' => 'creditCard',
'category_id' => 1,
'category_type' => 'Random' ,
'date' => $this->faker->date('Y-m-d') ,
'bank_account_id' => 1 ,
'description' =>$this->faker->words(5, true) ,
'is_matched'=> false
];
}
}

View File

@ -63,7 +63,13 @@ return new class extends Migration
$table->date('date'); $table->date('date');
$table->unsignedBigInteger('bank_account_id'); $table->unsignedBigInteger('bank_account_id');
$table->text('description'); $table->text('description');
$table->unsignedInteger('invoice_id')->nullable();
$table->unsignedInteger('expense_id')->nullable();
$table->boolean('is_matched')->default(0);
$table->timestamps(6);
$table->softDeletes('deleted_at', 6);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade'); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');

View File

@ -110,6 +110,7 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
Route::post('bank_integrations/refresh_accounts', [BankIntegrationController::class, 'refreshAccounts'])->name('bank_integrations.refresh_accounts'); Route::post('bank_integrations/refresh_accounts', [BankIntegrationController::class, 'refreshAccounts'])->name('bank_integrations.refresh_accounts');
Route::post('bank_integrations/transactions', [BankIntegrationController::class, 'getTransactions'])->name('bank_integrations.transactions'); Route::post('bank_integrations/transactions', [BankIntegrationController::class, 'getTransactions'])->name('bank_integrations.transactions');
Route::post('bank_integrations/remove_account/{acc_id}', [BankIntegrationController::class, 'removeAccount'])->name('bank_integrations.remove_account'); Route::post('bank_integrations/remove_account/{acc_id}', [BankIntegrationController::class, 'removeAccount'])->name('bank_integrations.remove_account');
Route::post('bank_integrations/bulk', [BankIntegrationController::class, 'bulk'])->name('bank_integrations.bulk');
Route::post('check_subdomain', [SubdomainController::class, 'index'])->name('check_subdomain'); Route::post('check_subdomain', [SubdomainController::class, 'index'])->name('check_subdomain');
Route::get('ping', [PingController::class, 'index'])->name('ping'); Route::get('ping', [PingController::class, 'index'])->name('ping');

View File

@ -0,0 +1,102 @@
<?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\Feature;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Http\Controllers\BankIntegrationController
*/
class BankIntegrationApiTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
protected function setUp() :void
{
parent::setUp();
$this->makeTestData();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
}
public function testBankIntegrationGet()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/bank_integrations/'.$this->encodePrimaryKey($this->bank_integration->id));
$response->assertStatus(200);
}
public function testBankIntegrationArchived()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->bank_integration->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/bank_integrations/bulk?action=archive', $data);
$arr = $response->json();
$this->assertNotNull($arr['data'][0]['archived_at']);
}
public function testBankIntegrationRestored()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->bank_integration->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/bank_integrations/bulk?action=restore', $data);
$arr = $response->json();
$this->assertEquals(0, $arr['data'][0]['archived_at']);
}
public function testBankIntegrationDeleted()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->bank_integration->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/bank_integrations/bulk?action=delete', $data);
$arr = $response->json();
$this->assertTrue($arr['data'][0]['is_deleted']);
}
}

View File

@ -24,6 +24,8 @@ use App\Factory\PurchaseOrderFactory;
use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Company\CreateCompanyTaskStatuses; use App\Jobs\Company\CreateCompanyTaskStatuses;
use App\Models\Account; use App\Models\Account;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Company; use App\Models\Company;
@ -141,6 +143,16 @@ trait MockAccountData
*/ */
public $cu; public $cu;
/**
* @var
*/
public $bank_integration;
/**
* @var
*/
public $bank_transaction;
/** /**
* @var * @var
*/ */
@ -524,6 +536,37 @@ trait MockAccountData
'credit_id' => $this->credit->id, 'credit_id' => $this->credit->id,
]); ]);
$this->bank_integration = BankIntegration::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
'account_id' => $this->account->id,
]);
$this->bank_transaction = BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
]);
$invitations = CreditInvitation::whereCompanyId($this->credit->company_id) $invitations = CreditInvitation::whereCompanyId($this->credit->company_id)
->whereCreditId($this->credit->id); ->whereCreditId($this->credit->id);