From 1c3f0c590cdb86f9ef75af381140c690c610792a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 28 Mar 2019 08:32:50 +1100 Subject: [PATCH] More tests for Client REST api endpoints --- app/Http/Controllers/ClientController.php | 7 ++----- app/Http/Requests/Client/CreateClientRequest.php | 2 +- app/Http/Requests/Client/EditClientRequest.php | 2 +- app/Http/Requests/Client/ShowClientRequest.php | 2 +- app/Http/Requests/Client/StoreClientRequest.php | 2 +- app/Http/ViewComposers/HeaderComposer.php | 4 +--- app/Models/User.php | 6 ++---- tests/Feature/ClientTest.php | 14 ++++++++++++++ 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 71e6c13597..10d429186f 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -19,7 +19,6 @@ use App\Models\Currency; use App\Models\Size; use App\Repositories\ClientRepository; use App\Utils\Traits\MakesHash; -use App\Utils\Traits\UserSessionAttributes; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; @@ -30,7 +29,6 @@ use Illuminate\Support\Facades\Cache; */ class ClientController extends Controller { - use UserSessionAttributes; use MakesHash; /** @@ -74,7 +72,6 @@ class ClientController extends Controller ]) ]; - //return response()->json($data); return redirect()->route('clients.edit', ['id' => $this->encodePrimarykey($client->id)]); } @@ -121,7 +118,7 @@ class ClientController extends Controller */ public function create(CreateClientRequest $request) { - $client = ClientFactory::create(auth()->user()->company(), auth()->user()->id); + $client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id); $data = [ 'client' => $client, @@ -141,7 +138,7 @@ class ClientController extends Controller public function store(StoreClientRequest $request) { - $client = StoreClient::dispatchNow($request, new Client); + $client = StoreClient::dispatchNow($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id)); $client->load('contacts', 'primary_contact'); diff --git a/app/Http/Requests/Client/CreateClientRequest.php b/app/Http/Requests/Client/CreateClientRequest.php index 669ace6725..4c4c11715b 100644 --- a/app/Http/Requests/Client/CreateClientRequest.php +++ b/app/Http/Requests/Client/CreateClientRequest.php @@ -15,7 +15,7 @@ class CreateClientRequest extends Request public function authorize() : bool { - return $this->user()->can('create', Client::Class); + return auth()->user()->can('create', Client::Class); } } \ No newline at end of file diff --git a/app/Http/Requests/Client/EditClientRequest.php b/app/Http/Requests/Client/EditClientRequest.php index f4c98a506f..e339a9c835 100644 --- a/app/Http/Requests/Client/EditClientRequest.php +++ b/app/Http/Requests/Client/EditClientRequest.php @@ -15,7 +15,7 @@ class EditClientRequest extends Request public function authorize() { - return $this->user()->can('edit', $this->client); + return auth()->user()->can('edit', $this->client); } public function sanitize() diff --git a/app/Http/Requests/Client/ShowClientRequest.php b/app/Http/Requests/Client/ShowClientRequest.php index baa6609325..72e9b964e2 100644 --- a/app/Http/Requests/Client/ShowClientRequest.php +++ b/app/Http/Requests/Client/ShowClientRequest.php @@ -15,7 +15,7 @@ class ShowClientRequest extends Request public function authorize() : bool { - return $this->user()->can('view', $this->client); + return auth()->user()->can('view', $this->client); } } \ No newline at end of file diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 4ad75f3629..fe40385ebe 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -15,7 +15,7 @@ class StoreClientRequest extends Request public function authorize() : bool { - return $this->user()->can('create', Client::class); + return auth()->user()->can('create', Client::class); } public function rules() diff --git a/app/Http/ViewComposers/HeaderComposer.php b/app/Http/ViewComposers/HeaderComposer.php index 9472db8e70..7772802938 100644 --- a/app/Http/ViewComposers/HeaderComposer.php +++ b/app/Http/ViewComposers/HeaderComposer.php @@ -2,7 +2,6 @@ namespace App\Http\ViewComposers; -use App\Utils\Traits\UserSessionAttributes; use Illuminate\View\View; /** @@ -11,7 +10,6 @@ use Illuminate\View\View; */ class HeaderComposer { - use UserSessionAttributes; /** * Bind data to the view. @@ -40,7 +38,7 @@ class HeaderComposer }); $data['companies'] = $companies->reject(function ($company){ - return $company->id == auth()->user()->company->id; + return $company->id == auth()->user()->company()->id; }); return $data; diff --git a/app/Models/User.php b/app/Models/User.php index bb9ac9e0d2..3037f2b67f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -114,10 +114,8 @@ class User extends Authenticatable implements MustVerifyEmail */ public function user_company() { - $ct = CompanyToken::whereToken(request()->header('X-API-TOKEN'))->first(); - return $ct->company; - //return $this->user_companies->where('company_id', $this->getCurrentCompanyId())->first(); + return $this->user_companies->where('company_id', $this->getCurrentCompanyId())->first(); } @@ -169,7 +167,7 @@ class User extends Authenticatable implements MustVerifyEmail public function isAdmin() : bool { - return (bool) $this->company()->is_admin; + return (bool) $this->user_company()->is_admin; } diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index 046f8b83d9..0e10a1132b 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -103,6 +103,8 @@ class ClientTest extends TestCase $user = User::find($company_user->user_id); + $this->assertTrue($user->isAdmin()); + factory(\App\Models\Client::class, 20)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){ factory(\App\Models\ClientContact::class,1)->create([ @@ -157,6 +159,18 @@ class ClientTest extends TestCase $response->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token, + ])->post('/api/v1/clients/', ['name' => 'New Client']) + ->assertJson([ + 'name' => 'New Client' + ]); + + + $response->assertStatus(200); + }