1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/tests/Feature/ClientTest.php

178 lines
4.8 KiB
PHP
Raw Normal View History

2019-03-26 06:00:15 +01:00
<?php
namespace Tests\Feature;
use App\Models\Account;
2019-03-27 05:50:13 +01:00
use App\Models\Company;
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;
use Illuminate\Support\Facades\Session;
use Tests\TestCase;
class ClientTest extends TestCase
{
2019-03-27 07:22:27 +01:00
use MakesHash;
2019-03-26 06:00:15 +01:00
public function setUp()
{
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
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,
'email' => $this->faker->unique()->safeEmail,
'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', $data);
2019-03-26 12:31:07 +01:00
2019-03-26 22:17:28 +01:00
$response->assertStatus(200)
->assertJson([
'first_name' => $data['first_name'],
]);
2019-03-26 12:31:07 +01:00
2019-03-27 05:50:13 +01:00
$acc = $response->json();
$account = Account::find($acc['id']);
2019-03-27 07:22:27 +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,
'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', $data);
$acc = $response->json();
$account = Account::find($acc['id']);
2019-03-27 05:50:13 +01:00
$token = $account->default_company->tokens()->first()->token;
2019-03-26 06:00:15 +01:00
2019-03-27 07:22:27 +01:00
$company = $account->default_company;
$company_user = $company->company_users()->first();
$user = User::find($company_user->user_id);
$this->assertTrue($user->isAdmin());
2019-03-27 07:22:27 +01:00
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([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,10)->create([
'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));
$response->assertStatus(302);
$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)
->assertJson([
'name' => 'A Funky Name'
]);
$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'])
->assertJson([
'name' => 'New Client'
]);
$response->assertStatus(200);
2019-03-27 09:38:01 +01:00
}
2019-03-26 06:00:15 +01:00
}