2019-06-17 01:58:33 +02:00
|
|
|
<?php
|
|
|
|
|
2019-12-04 02:06:14 +01:00
|
|
|
namespace Feature;
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2019-12-04 02:06:14 +01:00
|
|
|
use App\Factory\CompanyUserFactory;
|
2019-12-14 06:49:48 +01:00
|
|
|
use App\Factory\UserFactory;
|
2020-06-27 06:09:16 +02:00
|
|
|
use App\Http\Middleware\PasswordProtection;
|
2019-06-17 01:58:33 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Activity;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyLedger;
|
2019-11-30 03:42:44 +01:00
|
|
|
use App\Models\CompanyToken;
|
|
|
|
use App\Models\CompanyUser;
|
2019-06-17 01:58:33 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-12-14 06:49:48 +01:00
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2019-06-17 01:58:33 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Http\Controllers\UserController
|
|
|
|
*/
|
|
|
|
class UserTest extends TestCase
|
|
|
|
{
|
|
|
|
use MockAccountData;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->makeTestData();
|
|
|
|
|
2019-06-17 01:58:33 +02:00
|
|
|
Model::reguard();
|
|
|
|
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class,
|
|
|
|
PasswordProtection::class
|
|
|
|
);
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-06-17 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserList()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-11-22 22:10:53 +01:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-01-09 21:15:10 +01:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2019-12-30 22:59:12 +01:00
|
|
|
])->get('/api/v1/users');
|
2019-06-17 01:58:33 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
2019-11-22 22:10:53 +01:00
|
|
|
}
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2019-11-22 22:10:53 +01:00
|
|
|
public function testUserStore()
|
|
|
|
{
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2019-11-22 22:10:53 +01:00
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
2020-06-27 02:20:27 +02:00
|
|
|
'email' => 'bob1@good.ole.boys.com',
|
2019-11-22 22:10:53 +01:00
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice'
|
|
|
|
],
|
|
|
|
];
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-06-17 01:58:33 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-11-22 22:10:53 +01:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-06-27 02:05:31 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2019-12-30 22:59:12 +01:00
|
|
|
])->post('/api/v1/users?include=company_user', $data);
|
2019-06-17 01:58:33 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
2019-11-22 22:10:53 +01:00
|
|
|
$arr = $response->json();
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2019-12-04 02:06:14 +01:00
|
|
|
$this->assertNotNull($arr['data']['company_user']);
|
2019-11-22 22:10:53 +01:00
|
|
|
}
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2019-11-29 12:15:50 +01:00
|
|
|
public function testUserAttachAndDetach()
|
|
|
|
{
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2020-03-24 14:25:20 +01:00
|
|
|
$user = UserFactory::create($this->account->id);
|
2019-11-29 12:15:50 +01:00
|
|
|
$user->first_name = 'Test';
|
|
|
|
$user->last_name = 'Palloni';
|
|
|
|
$user->save();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-11-29 12:15:50 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-06-27 04:35:11 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2019-12-30 22:59:12 +01:00
|
|
|
])->post('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/attach_to_company?include=company_user');
|
2019-11-29 12:15:50 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$this->assertNotNull($user->company_user);
|
2019-11-30 03:42:44 +01:00
|
|
|
$this->assertEquals($user->company_user->company_id, $this->company->id);
|
|
|
|
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-11-30 03:42:44 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2019-12-30 22:59:12 +01:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->delete('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/detach_from_company?include=company_user');
|
2019-11-30 03:42:44 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
|
|
|
|
$cu = CompanyUser::whereUserId($user->id)->whereCompanyId($this->company->id)->first();
|
|
|
|
$ct = CompanyToken::whereUserId($user->id)->whereCompanyId($this->company->id)->first();
|
|
|
|
|
|
|
|
$this->assertNull($cu);
|
|
|
|
$this->assertNull($ct);
|
|
|
|
$this->assertNotNull($user);
|
2019-11-29 12:15:50 +01:00
|
|
|
}
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
public function testAttachUserToMultipleCompanies()
|
|
|
|
{
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
/* Create New Company */
|
|
|
|
$company2 = factory(\App\Models\Company::class)->create([
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
]);
|
2020-04-09 12:48:04 +02:00
|
|
|
|
|
|
|
$company_token = new CompanyToken;
|
|
|
|
$company_token->user_id = $this->user->id;
|
|
|
|
$company_token->company_id = $company2->id;
|
|
|
|
$company_token->account_id = $this->account->id;
|
|
|
|
$company_token->name = 'test token';
|
|
|
|
$company_token->token = \Illuminate\Support\Str::random(64);
|
|
|
|
$company_token->save();
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*Manually link this user to the company*/
|
|
|
|
$cu = CompanyUserFactory::create($this->user->id, $company2->id, $this->account->id);
|
|
|
|
$cu->is_owner = true;
|
|
|
|
$cu->is_admin = true;
|
|
|
|
$cu->save();
|
|
|
|
|
|
|
|
/*Create New Blank User and Attach to Company 2*/
|
2020-03-24 14:25:20 +01:00
|
|
|
$new_user = UserFactory::create($this->account->id);
|
2019-12-04 02:06:14 +01:00
|
|
|
$new_user->first_name = 'Test';
|
|
|
|
$new_user->last_name = 'Palloni';
|
|
|
|
$new_user->save();
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-04-09 12:48:04 +02:00
|
|
|
'X-API-TOKEN' => $company_token->token,
|
2019-12-04 02:06:14 +01:00
|
|
|
])->post('/api/v1/users/'.$this->encodePrimaryKey($new_user->id).'/attach_to_company?include=company_user');
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$this->assertNotNull($new_user->company_user);
|
|
|
|
$this->assertEquals($new_user->company_user->company_id, $company2->id);
|
|
|
|
|
|
|
|
|
|
|
|
/*Create brand new user manually with company_user object and attach to a different company*/
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
|
|
|
'email' => 'bob@good.ole.boys.co2.com',
|
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-12-04 02:06:14 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-04-09 12:48:04 +02:00
|
|
|
'X-API-TOKEN' => $company_token->token,
|
2019-12-04 02:06:14 +01:00
|
|
|
])->post('/api/v1/users?include=company_user', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertNotNull($arr['data']['company_user']);
|
|
|
|
$this->assertFalse($arr['data']['company_user']['is_admin']);
|
|
|
|
$this->assertFalse($arr['data']['company_user']['is_owner']);
|
2020-03-09 11:47:17 +01:00
|
|
|
$this->assertEquals($arr['data']['company_user']['permissions'], 'create_client,create_invoice');
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
$user = User::whereEmail('bob@good.ole.boys.co2.com')->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($user);
|
|
|
|
|
|
|
|
$cu = CompanyUser::whereUserId($user->id)->whereCompanyId($company2->id)->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($cu);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*Update the user permissions of this user*/
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'Captain',
|
|
|
|
'last_name' => 'Morgain',
|
|
|
|
'email' => 'bob@good.ole.boys.co2.com',
|
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => true,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_invoice,create_invoice'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-12-04 02:06:14 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-04-09 12:48:04 +02:00
|
|
|
'X-API-TOKEN' => $company_token->token,
|
2019-12-30 22:59:12 +01:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2019-12-04 02:06:14 +01:00
|
|
|
])->put('/api/v1/users/'.$this->encodePrimaryKey($user->id).'?include=company_user', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertNotNull($arr['data']['company_user']);
|
|
|
|
$this->assertTrue($arr['data']['company_user']['is_admin']);
|
|
|
|
$this->assertFalse($arr['data']['company_user']['is_owner']);
|
|
|
|
$this->assertEquals($arr['data']['company_user']['permissions'], 'create_invoice,create_invoice');
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|