diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 251b17dbb410..a9636f3aa1a4 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -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'; } } diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index dcfe13ff8cfe..4286345a3a7b 100644 --- a/app/Http/Requests/Client/UpdateClientRequest.php +++ b/app/Http/Requests/Client/UpdateClientRequest.php @@ -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; diff --git a/app/Models/Presenters/ClientPresenter.php b/app/Models/Presenters/ClientPresenter.php index 8e32801a492d..f11ebe7ef4cb 100644 --- a/app/Models/Presenters/ClientPresenter.php +++ b/app/Models/Presenters/ClientPresenter.php @@ -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() diff --git a/tests/Feature/ClientApiTest.php b/tests/Feature/ClientApiTest.php new file mode 100644 index 000000000000..5370ba9e46a1 --- /dev/null +++ b/tests/Feature/ClientApiTest.php @@ -0,0 +1,93 @@ +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); + + } + +} \ No newline at end of file diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 2c20009bceb9..6c133cb510bb 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -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();