mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
API Tests for clients
This commit is contained in:
parent
7a32986cf0
commit
0c5202ea8c
@ -44,7 +44,8 @@ class StoreClientRequest extends Request
|
||||
{
|
||||
|
||||
for ($i = 0; $i < count($contacts); $i++) {
|
||||
$rules['contacts.' . $i . '.email'] = 'required|email|unique:client_contacts,email,' . isset($contacts[$i]['id']);
|
||||
//$rules['contacts.' . $i . '.email'] = 'required|email|unique:client_contacts,email,' . isset($contacts[$i]['id']);
|
||||
$rules['contacts.' . $i . '.email'] = 'email';
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,7 +44,8 @@ class UpdateClientRequest extends Request
|
||||
if(is_array($contacts))
|
||||
{
|
||||
for ($i = 0; $i < count($contacts); $i++) {
|
||||
$rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,' . $contacts[$i]['id'];
|
||||
//$rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,' . $contacts[$i]['id'];
|
||||
$rules['contacts.' . $i . '.email'] = 'email';
|
||||
}
|
||||
}
|
||||
return $rules;
|
||||
|
@ -23,7 +23,14 @@ class ClientPresenter extends EntityPresenter
|
||||
*/
|
||||
public function name()
|
||||
{
|
||||
return $this->entity->name ?: $this->entity->primary_contact->first()->first_name . ' '. $this->entity->primary_contact->first()->last_name;
|
||||
$contact = $this->entity->primary_contact->first();
|
||||
|
||||
$contact_name = 'No Contact Set';
|
||||
|
||||
if($contact)
|
||||
$contact_name = $contact->first_name. ' '. $contact->last_name;
|
||||
|
||||
return $this->entity->name ?: $contact_name;
|
||||
}
|
||||
|
||||
public function primary_contact_name()
|
||||
|
93
tests/Feature/ClientApiTest.php
Normal file
93
tests/Feature/ClientApiTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\DataMapper\DefaultSettings;
|
||||
use App\Models\Account;
|
||||
use App\Models\Client;
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Faker\Factory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
class ClientApiTest extends TestCase
|
||||
{
|
||||
use MakesHash;
|
||||
use DatabaseTransactions;
|
||||
use MockAccountData;
|
||||
|
||||
public function setUp() :void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->makeTestData();
|
||||
|
||||
Session::start();
|
||||
|
||||
$this->faker = \Faker\Factory::create();
|
||||
|
||||
Model::reguard();
|
||||
}
|
||||
|
||||
|
||||
public function testClientPost()
|
||||
{
|
||||
$data = [
|
||||
'name' => $this->faker->firstName,
|
||||
];
|
||||
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token
|
||||
])->post('/api/v1/clients', $data);
|
||||
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function testClientPut()
|
||||
{
|
||||
$data = [
|
||||
'name' => $this->faker->firstName,
|
||||
'id_number' => 'Coolio',
|
||||
];
|
||||
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token
|
||||
])->put('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id), $data);
|
||||
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function testClientGet()
|
||||
{
|
||||
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token
|
||||
])->get('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id));
|
||||
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -13,6 +13,7 @@ namespace Tests;
|
||||
|
||||
use App\DataMapper\ClientSettings;
|
||||
use App\DataMapper\CompanySettings;
|
||||
use App\DataMapper\DefaultSettings;
|
||||
use App\Factory\ClientFactory;
|
||||
use App\Factory\InvoiceFactory;
|
||||
use App\Factory\InvoiceItemFactory;
|
||||
@ -21,6 +22,7 @@ use App\Helpers\Invoice\InvoiceCalc;
|
||||
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
||||
use App\Models\Client;
|
||||
use App\Models\CompanyGateway;
|
||||
use App\Models\CompanyToken;
|
||||
use App\Models\Credit;
|
||||
use App\Models\GroupSetting;
|
||||
use App\Models\Invoice;
|
||||
@ -49,14 +51,14 @@ trait MockAccountData
|
||||
|
||||
public $client;
|
||||
|
||||
|
||||
public $token;
|
||||
|
||||
public function makeTestData()
|
||||
{
|
||||
$this->account = factory(\App\Models\Account::class)->create();
|
||||
$this->company = factory(\App\Models\Company::class)->create([
|
||||
'account_id' => $this->account->id,
|
||||
'domain' => 'ninja.test',
|
||||
'domain' => 'ninja.test:8000',
|
||||
]);
|
||||
|
||||
$this->account->default_company_id = $this->company->id;
|
||||
@ -67,6 +69,25 @@ trait MockAccountData
|
||||
'confirmation_code' => $this->createDbHash(config('database.default'))
|
||||
]);
|
||||
|
||||
$this->token = \Illuminate\Support\Str::random(64);
|
||||
|
||||
$company_token = CompanyToken::create([
|
||||
'user_id' => $this->user->id,
|
||||
'company_id' => $this->company->id,
|
||||
'account_id' => $this->account->id,
|
||||
'name' => 'test token',
|
||||
'token' => $this->token,
|
||||
]);
|
||||
|
||||
$this->user->companies()->attach($this->company->id, [
|
||||
'account_id' => $this->account->id,
|
||||
'is_owner' => 1,
|
||||
'is_admin' => 1,
|
||||
'is_locked' => 0,
|
||||
'permissions' => json_encode([]),
|
||||
'settings' => json_encode(DefaultSettings::userSettings()),
|
||||
]);
|
||||
|
||||
$this->client = ClientFactory::create($this->company->id, $this->user->id);
|
||||
$this->client->save();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user