2018-10-18 07:04:36 +02: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) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-05-14 06:05:05 +02:00
|
|
|
namespace Tests\Integration;
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
use App\Factory\CompanyUserFactory;
|
2018-10-18 07:04:36 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2018-10-22 14:04:37 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Company;
|
2019-12-08 11:28:52 +01:00
|
|
|
use App\Models\CompanyToken;
|
2020-11-13 11:42:06 +01:00
|
|
|
use App\Models\CompanyUser;
|
2018-10-18 07:04:36 +02:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Libraries\MultiDB
|
|
|
|
*
|
|
|
|
* Proves that we can reliably switch database connections at runtime
|
|
|
|
*/
|
|
|
|
class MultiDBUserTest extends TestCase
|
|
|
|
{
|
2019-04-24 12:01:40 +02:00
|
|
|
public function setUp() :void
|
2018-10-18 07:04:36 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2018-10-18 07:04:36 +02:00
|
|
|
$this->markTestSkipped('Multi DB not enabled - skipping');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
User::unguard();
|
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$ac = Account::factory()->make();
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2019-07-31 12:37:55 +02:00
|
|
|
$ac->setHidden(['hashed_id']);
|
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
$account = Account::on('db-ninja-01')->create($ac->toArray());
|
|
|
|
$account2 = Account::on('db-ninja-02')->create($ac->toArray());
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$company = Company::factory()->make([
|
2018-10-22 14:04:37 +02:00
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$company2 = Company::factory()->make([
|
2018-10-22 14:04:37 +02:00
|
|
|
'account_id' => $account2->id,
|
|
|
|
]);
|
|
|
|
|
2019-07-31 12:37:55 +02:00
|
|
|
$company->setHidden(['settings', 'settings_object', 'hashed_id']);
|
|
|
|
$company2->setHidden(['settings', 'settings_object', 'hashed_id']);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
$coco = Company::on('db-ninja-01')->create($company->toArray());
|
|
|
|
|
2020-11-13 11:42:06 +01:00
|
|
|
$coco2 = Company::on('db-ninja-02')->create($company2->toArray());
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
$user = [
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account->id,
|
2018-10-22 14:04:37 +02:00
|
|
|
'first_name' => 'user_db_1',
|
|
|
|
'last_name' => 'user_db_1-s',
|
|
|
|
'phone' => '55555',
|
2018-10-18 07:04:36 +02:00
|
|
|
'email_verified_at' => now(),
|
2020-01-09 21:15:10 +01:00
|
|
|
'password' => Hash::make('ALongAndBriliantPassword'), // secret
|
2019-09-26 15:00:51 +02:00
|
|
|
'remember_token' => \Illuminate\Support\Str::random(10),
|
2018-10-22 14:04:37 +02:00
|
|
|
'email' => 'db1@example.com',
|
|
|
|
'oauth_user_id' => '123',
|
2019-04-26 12:51:02 +02:00
|
|
|
// 'account_id' => $account->id,
|
2018-10-18 07:04:36 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$user2 = [
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account2->id,
|
2018-10-18 07:04:36 +02:00
|
|
|
'first_name' => 'user_db_2',
|
|
|
|
'last_name' => 'user_db_2-s',
|
|
|
|
'phone' => '55555',
|
|
|
|
'email_verified_at' => now(),
|
2019-12-30 22:59:12 +01:00
|
|
|
'password' => 'ALongAndBriliantPassword', // secret
|
2019-09-26 15:00:51 +02:00
|
|
|
'remember_token' => \Illuminate\Support\Str::random(10),
|
2018-10-18 07:04:36 +02:00
|
|
|
'email' => 'db2@example.com',
|
2018-10-19 05:45:55 +02:00
|
|
|
'oauth_user_id' => 'abc',
|
2019-04-26 12:51:02 +02:00
|
|
|
// 'account_id' => $account2->id,
|
2018-10-19 05:45:55 +02:00
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
];
|
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
$user = User::on('db-ninja-01')->create($user);
|
|
|
|
|
2020-11-13 11:42:06 +01:00
|
|
|
// $cu = CompanyUserFactory::create($user->id, $coco->id, $account->id);
|
|
|
|
// $cu->is_owner = true;
|
|
|
|
// $cu->is_admin = true;
|
|
|
|
// $cu->setConnection('db-ninja-01');
|
|
|
|
// $cu->save();
|
|
|
|
|
|
|
|
CompanyUser::on('db-ninja-01')->create([
|
|
|
|
'company_id' => $coco->id,
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'is_owner' => true,
|
|
|
|
'is_admin' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$user2 = User::on('db-ninja-02')->create($user2);
|
|
|
|
|
|
|
|
CompanyUser::on('db-ninja-02')->create([
|
|
|
|
'company_id' => $coco2->id,
|
|
|
|
'account_id' => $account2->id,
|
|
|
|
'user_id' => $user2->id,
|
|
|
|
'is_owner' => true,
|
|
|
|
'is_admin' => true,
|
|
|
|
]);
|
2019-12-08 11:28:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
$this->token = \Illuminate\Support\Str::random(40);
|
|
|
|
|
|
|
|
$this->company_token = CompanyToken::on('db-ninja-01')->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'company_id' => $coco->id,
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'name' => 'test token',
|
|
|
|
'token' => $this->token,
|
|
|
|
]);
|
|
|
|
|
|
|
|
User::unguard(false);
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_oauth_user_db2_exists()
|
|
|
|
{
|
2018-10-18 12:47:55 +02:00
|
|
|
$user = MultiDB::hasUser(['email' => 'db2@example.com', 'oauth_user_id' => 'abc']);
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals($user->email, 'db2@example.com');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_oauth_user_db1_exists()
|
|
|
|
{
|
2018-10-18 12:47:55 +02:00
|
|
|
$user = MultiDB::hasUser(['email' => 'db1@example.com', 'oauth_user_id' => '123']);
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals($user->email, 'db1@example.com');
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:47:55 +02:00
|
|
|
public function test_check_user_exists()
|
|
|
|
{
|
|
|
|
$this->assertTrue(MultiDB::checkUserEmailExists('db1@example.com'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_check_user_does_not_exist()
|
|
|
|
{
|
|
|
|
$this->assertFalse(MultiDB::checkUserEmailExists('bademail@example.com'));
|
|
|
|
}
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
public function test_check_that_set_db_by_email_works()
|
|
|
|
{
|
|
|
|
$this->assertTrue(MultiDB::userFindAndSetDb('db1@example.com'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_check_that_set_db_by_email_works_db_2()
|
|
|
|
{
|
|
|
|
$this->assertTrue(MultiDB::userFindAndSetDb('db2@example.com'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_check_that_set_db_by_email_works_db_3()
|
|
|
|
{
|
|
|
|
$this->assertFalse(MultiDB::userFindAndSetDb('bademail@example.com'));
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:47:55 +02:00
|
|
|
/*
|
|
|
|
* This is what you do when you demand 100% code coverage :/
|
|
|
|
*/
|
|
|
|
public function test_set_db_invokes()
|
|
|
|
{
|
2018-10-24 12:24:09 +02:00
|
|
|
$this->expectNotToPerformAssertions(MultiDB::setDB('db-ninja-01'));
|
2018-10-18 12:47:55 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
public function test_cross_db_user_linking_fails_appropriately()
|
|
|
|
{
|
|
|
|
|
|
|
|
//$this->withoutExceptionHandling();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
|
|
|
'email' => 'db2@example.com',
|
|
|
|
'company_user' => [
|
2019-12-30 22:59:12 +01:00
|
|
|
'is_admin' => true,
|
2019-12-08 11:28:52 +01:00
|
|
|
'is_owner' => false,
|
2020-09-06 11:38:10 +02:00
|
|
|
'permissions' => 'create_client,create_invoice',
|
2019-12-08 11:28:52 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
try {
|
2019-12-08 11:28:52 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2019-12-30 22:59:12 +01:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2019-12-08 11:28:52 +01:00
|
|
|
])->post('/api/v1/users?include=company_user', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($response) {
|
2019-12-08 11:28:52 +01:00
|
|
|
$response->assertStatus(302);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_cross_db_user_linking_succeeds_appropriately()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
|
|
|
'email' => 'db1@example.com',
|
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
2020-09-06 11:38:10 +02:00
|
|
|
'permissions' => 'create_client,create_invoice',
|
2019-12-08 11:28:52 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
try {
|
2019-12-08 11:28:52 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-08-19 03:49:33 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->post('/api/v1/users?include=company_user', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
\Log::error('in the validator');
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
\Log::error($message);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($response) {
|
2019-12-08 11:28:52 +01:00
|
|
|
$response->assertStatus(200);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
}
|
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public function tearDown() :void
|
2018-10-18 07:04:36 +02:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
DB::connection('db-ninja-01')->table('users')->delete();
|
|
|
|
DB::connection('db-ninja-02')->table('users')->delete();
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
}
|