1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/tests/Feature/ClientTest.php

524 lines
16 KiB
PHP
Raw Normal View History

2019-03-26 06:00:15 +01:00
<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
2019-03-26 06:00:15 +01:00
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 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\Routing\Middleware\ThrottleRequests;
2019-03-26 06:00:15 +01:00
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
2019-03-26 06:00:15 +01:00
use Tests\TestCase;
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;
use MockAccountData;
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
);
$this->makeTestData();
2019-03-26 06:00:15 +01:00
}
public function testStoreClientUsingCountryCode()
2019-03-26 12:31:07 +01:00
{
$data = [
'name' => 'Country Code Name',
'country_code' => 'US',
'currency_code' => 'USD',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$this->assertEquals(840, $client->country_id);
$this->assertEquals(1, $client->settings->currency_id);
$data = [
'name' => 'Country Code Name',
'country_code' => 'USA',
'currency_code' => 'USD',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
2019-03-26 22:17:28 +01:00
$this->assertEquals(840, $client->country_id);
$this->assertEquals(1, $client->settings->currency_id);
2019-03-27 05:50:13 +01:00
$data = [
'name' => 'Country Code Name',
'country_code' => 'AU',
'currency_code' => 'AUD',
];
2019-03-26 12:31:07 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
2019-03-26 12:31:07 +01:00
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
2019-03-27 05:50:13 +01:00
$this->assertEquals(36, $client->country_id);
$this->assertEquals(12, $client->settings->currency_id);
}
public function testClientList()
{
2019-03-27 05:50:13 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2019-03-27 05:50:13 +01:00
])->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
{
2020-10-01 12:49:47 +02:00
Client::factory()->count(3)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {
ClientContact::factory()->create([
'user_id' => $this->user->id,
2019-03-27 07:22:27 +01:00
'client_id' => $c->id,
'company_id' => $this->company->id,
'is_primary' => 1,
2019-03-27 07:22:27 +01:00
]);
2020-10-01 12:49:47 +02:00
ClientContact::factory()->count(2)->create([
'user_id' => $this->user->id,
2019-03-27 07:22:27 +01:00
'client_id' => $c->id,
'company_id' => $this->company->id,
2019-03-27 07:22:27 +01:00
]);
});
2019-03-27 05:50:13 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id));
2019-03-27 07:22:27 +01:00
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' => $this->token,
])->get('/api/v1/clients/'.$this->encodePrimaryKey($this->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',
2019-03-27 09:38:01 +01:00
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($this->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' => $this->token,
])->delete('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id));
2019-03-27 09:38:01 +01:00
$response->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', ['name' => 'New Client'])
2019-04-03 04:34:28 +02:00
->assertStatus(200);
$response->assertStatus(200);
$this->client->is_deleted = true;
$this->client->save();
$client_update = [
'name' => 'Double Funk',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id), $client_update)
->assertStatus(400);
}
public function testDefaultTimeZoneFromClientModel()
{
2020-10-01 12:49:47 +02:00
$account = Account::factory()->create();
$company = Company::factory()->create([
2019-04-24 12:01:40 +02:00
'account_id' => $account->id,
]);
2019-04-24 12:01:40 +02:00
$account->default_company_id = $company->id;
$account->save();
2020-10-01 12:49:47 +02:00
$user = User::factory()->create([
'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default')),
'email' => 'whiz@gmail.com',
2019-04-24 12:01:40 +02:00
]);
$userPermissions = collect([
'view_invoice',
'view_client',
'edit_client',
'edit_invoice',
'create_invoice',
'create_client',
2019-04-24 12:01:40 +02:00
]);
$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
2020-10-01 12:49:47 +02:00
Client::factory()->count(3)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) {
ClientContact::factory()->create([
2019-04-24 02:22:02 +02:00
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1,
2019-04-24 02:22:02 +02:00
]);
2020-10-01 12:49:47 +02:00
ClientContact::factory()->count(2)->create([
2019-04-24 02:22:02 +02:00
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
2019-04-24 02:22:02 +02:00
]);
});
2019-04-24 02:22:02 +02:00
$this->client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
2019-04-24 02:22:02 +02:00
$this->assertNotNull($this->client);
2019-04-24 02:22:02 +02:00
/* Make sure we have a valid settings object*/
$this->assertEquals($this->client->getSetting('timezone_id'), 1);
/* Make sure we are harvesting valid data */
$this->assertEquals($this->client->timezone()->name, 'Pacific/Midway');
2019-04-24 02:22:02 +02:00
/* Make sure NULL settings return the correct count (0) instead of throwing an exception*/
$this->assertEquals($this->client->contacts->count(), 3);
}
public function testCreatingClientAndContacts()
{
2020-10-01 12:49:47 +02:00
$account = Account::factory()->create();
$company = Company::factory()->create([
'account_id' => $account->id,
]);
$account->default_company_id = $company->id;
$account->save();
2020-10-01 12:49:47 +02:00
$user = User::factory()->create([
'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default')),
'email' => 'whiz@gmail.com',
]);
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => '',
'settings' => '',
'is_locked' => 0,
]);
$company_token = new CompanyToken;
$company_token->user_id = $user->id;
$company_token->company_id = $company->id;
$company_token->account_id = $account->id;
$company_token->name = $user->first_name.' '.$user->last_name;
$company_token->token = Str::random(64);
$company_token->save();
$this->token = $company_token->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' => $this->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' => $this->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' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$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' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
}
$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' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
$this->client_id = $arr['data']['id'];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/clients/'.$this->client_id, $data)->assertStatus(200);
$arr = $response->json();
$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' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
$this->client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$contact = $this->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' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
$this->client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$contact = $this->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' => $this->token,
])->put('/api/v1/clients/'.$this->client->hashed_id, $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$arr = $response->json();
2020-10-22 12:14:14 +02:00
$this->client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$this->client->fresh();
$contact = $this->client->contacts()->whereEmail($safe_email)->first();
$this->assertEquals($password, $contact->password);
}
2019-03-26 06:00:15 +01:00
}