1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/tests/Feature/ClientTest.php

610 lines
19 KiB
PHP
Raw Normal View History

2019-03-26 06:00:15 +01:00
<?php
namespace Tests\Feature;
use App\DataMapper\CompanySettings;
2019-04-24 12:01:40 +02:00
use App\DataMapper\DefaultSettings;
2019-03-26 06:00:15 +01:00
use App\Models\Account;
2019-04-24 02:22:02 +02:00
use App\Models\Client;
use App\Models\ClientContact;
2019-03-27 05:50:13 +01:00
use App\Models\Company;
use App\Models\CompanyToken;
2019-03-27 07:22:27 +01:00
use App\Models\User;
use App\Utils\Traits\MakesHash;
2019-03-26 06:00:15 +01:00
use Faker\Factory;
2019-03-26 22:17:28 +01:00
use Illuminate\Database\Eloquent\Model;
2019-03-26 06:00:15 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Request;
2019-03-28 10:05:13 +01:00
use Illuminate\Support\Facades\Log;
2019-03-26 06:00:15 +01:00
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
2019-03-26 06:00:15 +01:00
use Tests\TestCase;
use Illuminate\Routing\Middleware\ThrottleRequests;
2019-03-26 06:00:15 +01:00
2019-04-20 01:02:49 +02:00
/**
* @test
* @covers App\Http\Controllers\ClientController
*/
2019-03-26 06:00:15 +01:00
class ClientTest extends TestCase
{
2019-03-27 07:22:27 +01:00
use MakesHash;
2019-04-24 12:01:40 +02:00
use DatabaseTransactions;
2019-03-26 06:00:15 +01:00
2019-04-24 12:01:40 +02:00
public function setUp() :void
2019-03-26 06:00:15 +01:00
{
parent::setUp();
2019-03-27 05:50:13 +01:00
2019-03-26 06:00:15 +01:00
Session::start();
2019-03-26 22:17:28 +01:00
$this->faker = \Faker\Factory::create();
2019-03-27 05:50:13 +01:00
2019-03-26 22:17:28 +01:00
Model::reguard();
2019-03-27 05:50:13 +01:00
$this->withoutExceptionHandling();
Client::reguard();
ClientContact::reguard();
$this->withoutMiddleware(
ThrottleRequests::class
);
2019-03-26 06:00:15 +01:00
}
2019-03-27 05:50:13 +01:00
public function testClientList()
2019-03-26 12:31:07 +01:00
{
2019-03-27 05:50:13 +01:00
2019-03-26 22:17:28 +01:00
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
2019-06-24 13:05:47 +02:00
'name' => $this->faker->company,
'email' => $this->faker->unique()->safeEmail,
2019-03-26 22:17:28 +01:00
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
2019-03-27 05:50:13 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup?include=account', $data);
2019-03-27 05:50:13 +01:00
2019-03-26 12:31:07 +01:00
2019-04-18 13:57:22 +02:00
$response->assertStatus(200);
2019-03-26 12:31:07 +01:00
2019-03-27 05:50:13 +01:00
$acc = $response->json();
$account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
2019-03-27 05:50:13 +01:00
2019-03-28 10:05:13 +01:00
$token = $account->default_company->tokens->first()->token;
2019-03-27 05:50:13 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/clients');
$response->assertStatus(200);
2019-03-26 12:31:07 +01:00
}
2019-03-26 06:00:15 +01:00
2019-03-27 09:38:01 +01:00
/*
* @covers ClientController
*/
public function testClientRestEndPoints()
2019-03-26 06:00:15 +01:00
{
2019-03-27 07:22:27 +01:00
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
2019-06-24 13:05:47 +02:00
'name' => $this->faker->company,
'email' => $this->faker->unique()->safeEmail,
2019-03-27 07:22:27 +01:00
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup?include=account', $data);
2019-03-27 07:22:27 +01:00
$acc = $response->json();
$account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
2019-03-27 07:22:27 +01:00
$company_token = $account->default_company->tokens()->first();
2019-04-18 13:57:22 +02:00
$token = $company_token->token;
$company = $company_token->company;
$user = $company_token->user;
2019-03-27 07:22:27 +01:00
//$company_user = $company->company_users()->first();
2019-03-27 07:22:27 +01:00
//$user = User::find($company_user->user_id);
$this->assertNotNull($company_token);
$this->assertNotNull($token);
$this->assertNotNull($user);
$this->assertNotNull($company);
//$this->assertNotNull($user->token->company);
2019-04-16 07:28:30 +02:00
factory(\App\Models\Client::class, 3)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
2019-03-27 07:22:27 +01:00
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,2)->create([
2019-03-27 07:22:27 +01:00
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id
]);
});
2019-03-27 05:50:13 +01:00
$client = $account->default_company->clients()->first();
2019-03-27 09:38:01 +01:00
$client->load('contacts');
2019-03-27 05:50:13 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
2019-03-27 07:22:27 +01:00
])->get('/api/v1/clients/'.$this->encodePrimaryKey($client->id));
2019-04-03 02:09:22 +02:00
$response->assertStatus(200);
2019-03-27 07:22:27 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/clients/'.$this->encodePrimaryKey($client->id).'/edit');
2019-03-27 05:50:13 +01:00
$response->assertStatus(200);
2019-03-27 09:38:01 +01:00
$client_update = [
'name' => 'A Funky Name'
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($client->id), $client_update)
2019-04-03 04:34:28 +02:00
->assertStatus(200);
2019-03-27 09:38:01 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->delete('/api/v1/clients/'.$this->encodePrimaryKey($client->id));
$response->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', ['name' => 'New Client'])
2019-04-03 04:34:28 +02:00
->assertStatus(200);
$response->assertStatus(200);
$client->is_deleted = true;
$client->save();
$client_update = [
'name' => 'Double Funk'
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($client->id), $client_update)
->assertStatus(400);
2019-03-27 09:38:01 +01:00
}
2019-03-26 06:00:15 +01:00
2019-04-24 02:22:02 +02:00
public function testDefaultTimeZoneFromClientModel()
{
2019-04-24 12:01:40 +02:00
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
]);
2019-04-24 12:01:40 +02:00
$account->default_company_id = $company->id;
$account->save();
$user = factory(\App\Models\User::class)->create([
2019-04-26 12:51:02 +02:00
// 'account_id' => $account->id,
2019-04-24 12:01:40 +02:00
'confirmation_code' => $this->createDbHash(config('database.default'))
]);
$userPermissions = collect([
'view_invoice',
'view_client',
'edit_client',
'edit_invoice',
'create_invoice',
'create_client'
]);
$userSettings = DefaultSettings::userSettings();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'notifications' => CompanySettings::notificationDefaults(),
2019-04-24 12:01:40 +02:00
'permissions' => $userPermissions->toJson(),
'settings' => json_encode($userSettings),
'is_locked' => 0,
]);
2019-04-24 02:22:02 +02:00
factory(\App\Models\Client::class, 3)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1,
2019-04-24 02:22:02 +02:00
]);
factory(\App\Models\ClientContact::class,2)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id
]);
});
2019-04-24 12:01:40 +02:00
$client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
$this->assertNotNull($client);
2019-04-24 02:22:02 +02:00
/* Make sure we have a valid settings object*/
$this->assertEquals($client->getSetting('timezone_id'), 1);
2019-04-24 02:22:02 +02:00
/* Make sure we are harvesting valid data */
$this->assertEquals($client->timezone()->name, 'Pacific/Midway');
/* Make sure NULL settings return the correct count (0) instead of throwing an exception*/
$this->assertEquals($client->contacts->count(), 3);
2019-04-24 02:22:02 +02:00
}
public function testCreatingClientAndContacts()
{
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
]);
$account->default_company_id = $company->id;
$account->save();
$user = factory(\App\Models\User::class)->create([
// 'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default'))
]);
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => '',
'settings' => '',
'is_locked' => 0,
]);
$ct = CompanyToken::create([
'account_id' => $account->id,
'company_id' => $company->id,
'user_id' => $user->id,
'token' => \Illuminate\Support\Str::random(64),
'name' => $user->first_name. ' '. $user->last_name,
]);
$token = $ct->token;
$data = [
'name' => 'A loyal Client',
'contacts' => [
['email' => $this->faker->unique()->safeEmail]
]
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', $data)
->assertStatus(200);
// $arr = $response->json();
$data = [
'name' => 'A loyal Client',
'contacts' => [
[
'email' => $this->faker->unique()->safeEmail,
'password' => '*****',
]
]
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', $data)
->assertStatus(200);
$data = [
'name' => 'A loyal Client',
'contacts' => [
[
'email' => $this->faker->unique()->safeEmail,
'password' => '1'
]
]
];
$response = null;
try{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
//\Log::error($message);
$this->assertNotNull($message);
}
$data = [
'name' => 'A loyal Client',
'contacts' => [
[
'email' => $this->faker->unique()->safeEmail,
'password' => '1Qajsj...33'
],
]
];
$response = null;
try{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
////\Log::error($message);
//$this->assertNotNull($message);
}
$response->assertStatus(200);
$data = [
'name' => 'A loyal Client',
'contacts' => [
[
'email' => $this->faker->unique()->safeEmail,
'password' => '1Qajsj...33'
],
[
'email' => $this->faker->unique()->safeEmail,
'password' => '1234AAAAAaaaaa'
],
]
];
$response = null;
try{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
//\Log::error($message);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
$client_id = $arr['data']['id'];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/' . $client_id, $data)->assertStatus(200);
$arr = $response->json();
//\Log::error($arr);
$safe_email = $this->faker->unique()->safeEmail;
$data = [
'name' => 'A loyal Client',
'contacts' => [
[
'email' => $safe_email,
'password' => ''
],
]
];
$response = null;
try{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
//\Log::error($message);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$contact = $client->contacts()->whereEmail($safe_email)->first();
$this->assertEquals(0, strlen($contact->password));
$safe_email = $this->faker->unique()->safeEmail;
$data = [
'name' => 'A loyal Client',
'contacts' => [
[
'email' => $safe_email,
'password' => 'AFancyDancy191$Password'
],
]
];
$response = null;
try{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post('/api/v1/clients/', $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
//\Log::error($message);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$contact = $client->contacts()->whereEmail($safe_email)->first();
$this->assertGreaterThan(1, strlen($contact->password));
$password = $contact->password;
$data = [
'name' => 'A Stary eyed client',
'contacts' => [
[
'id' => $contact->hashed_id,
'email' => $safe_email,
'password' => '*****'
],
]
];
$response = null;
try{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/' . $client->hashed_id, $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
//\Log::error($message);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$client->fresh();
$contact = $client->contacts()->whereEmail($safe_email)->first();
$this->assertEquals($password, $contact->password);
}
/** @test */
// public function testMassivelyCreatingClients()
// {
// $data = [
// 'first_name' => $this->faker->firstName,
// 'last_name' => $this->faker->lastName,
// 'name' => $this->faker->company,
// 'email' => $this->faker->unique()->safeEmail,
// 'password' => 'ALongAndBrilliantPassword123',
// '_token' => csrf_token(),
// 'privacy_policy' => 1,
// 'terms_of_service' => 1
// ];
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// ])->post('/api/v1/signup?include=account', $data);
// $response->assertStatus(200);
// $acc = $response->json();
// $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
// $token = $account->default_company->tokens->first()->token;
// $body = [
// 'action' => 'create',
// 'clients' => [
// ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-1.com'],
// ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-2.com'],
// ],
// ];
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-API-TOKEN' => $token,
// ])->post(route('clients.bulk'), $body);
// $response->assertStatus(200);
// $first_record = Client::where('website', 'my-awesome-website-1.com')->first();
// $second_record = Client::where('website', 'my-awesome-website-2.com')->first();
// $this->assertNotNull($first_record);
// $this->assertNotNull($second_record);
// }
2019-03-26 06:00:15 +01:00
}