2019-06-17 01:58:33 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
namespace Tests\Feature;
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
|
|
|
use App\Factory\CompanyUserFactory;
|
|
|
|
use App\Http\Middleware\PasswordProtection;
|
2023-05-09 05:37:53 +02:00
|
|
|
use App\Models\Account;
|
2019-06-17 01:58:33 +02:00
|
|
|
use App\Models\Company;
|
2023-05-09 05:37:53 +02:00
|
|
|
use App\Models\CompanyToken;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Models\CompanyUser;
|
|
|
|
use App\Models\User;
|
2019-06-17 01:58:33 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2019-06-17 01:58:33 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2021-03-03 08:22:14 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
2019-06-17 01:58:33 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* @test
|
2019-06-17 01:58:33 +02:00
|
|
|
* @covers App\Http\Controllers\UserController
|
2020-09-06 11:38:10 +02:00
|
|
|
*/
|
2019-06-17 01:58:33 +02:00
|
|
|
class UserTest extends TestCase
|
|
|
|
{
|
|
|
|
use MockAccountData;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2021-03-03 08:22:14 +01:00
|
|
|
private $default_email = 'attach@gmail.com';
|
|
|
|
|
2023-08-12 04:40:41 +02:00
|
|
|
public $faker;
|
|
|
|
|
2024-08-22 08:57:52 +02:00
|
|
|
protected function setUp(): void
|
2019-06-17 01:58:33 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
// Session::start();
|
2019-06-17 01:58:33 +02:00
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->makeTestData();
|
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
// Model::reguard();
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2023-08-12 04:40:41 +02:00
|
|
|
// $this->withoutExceptionHandling();
|
2021-03-03 08:22:14 +01:00
|
|
|
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class,
|
|
|
|
PasswordProtection::class
|
|
|
|
);
|
2019-06-17 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
2023-08-12 04:46:46 +02:00
|
|
|
private function mockAccount()
|
2023-05-09 05:01:27 +02:00
|
|
|
{
|
|
|
|
|
2023-08-12 04:40:41 +02:00
|
|
|
$account = Account::factory()->create([
|
|
|
|
'hosted_client_count' => 1000,
|
|
|
|
'hosted_company_count' => 1000,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$account->num_users = 3;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
$user = User::factory()->create([
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
'confirmation_code' => 'xyz123',
|
|
|
|
'email' => $this->faker->unique()->safeEmail(),
|
2023-08-12 04:46:46 +02:00
|
|
|
'password' => \Illuminate\Support\Facades\Hash::make('ALongAndBriliantPassword'),
|
2023-08-12 04:40:41 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->client_online_payment_notification = false;
|
|
|
|
$settings->client_manual_payment_notification = false;
|
|
|
|
|
|
|
|
$company = Company::factory()->create([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'settings' => $settings,
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$cu = CompanyUserFactory::create($user->id, $company->id, $account->id);
|
|
|
|
$cu->is_owner = true;
|
|
|
|
$cu->is_admin = true;
|
|
|
|
$cu->is_locked = false;
|
|
|
|
$cu->save();
|
|
|
|
|
|
|
|
$token = \Illuminate\Support\Str::random(64);
|
|
|
|
|
|
|
|
$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 = 'test token';
|
|
|
|
$company_token->token = $token;
|
|
|
|
$company_token->is_system = true;
|
2023-08-12 04:46:46 +02:00
|
|
|
$company_token->save();
|
|
|
|
|
|
|
|
return $company_token;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-01 12:52:26 +02:00
|
|
|
public function testUserLocale()
|
|
|
|
{
|
|
|
|
$this->user->language_id = "13";
|
|
|
|
$this->user->save();
|
|
|
|
|
|
|
|
$this->assertEquals("fr_CA", $this->user->getLocale());
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/statics');
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-12 04:46:46 +02:00
|
|
|
public function testUserResponse()
|
|
|
|
{
|
|
|
|
$company_token = $this->mockAccount();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
|
|
|
'email' => 'normal_user@gmail.com',
|
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => true,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice',
|
|
|
|
],
|
|
|
|
'phone' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->post('/api/v1/users?include=company_user', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$user = $response->json();
|
|
|
|
$user_id = $user['data']['id'];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->get('/api/v1/users', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertCount(2, $arr['data']);
|
|
|
|
|
2023-08-12 08:21:06 +02:00
|
|
|
//archive the user we just created:
|
2023-08-12 04:46:46 +02:00
|
|
|
|
2023-08-12 08:21:06 +02:00
|
|
|
$data = [
|
|
|
|
'action' => 'archive',
|
|
|
|
'ids' => [$user_id],
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->postJson('/api/v1/users/bulk', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$this->assertCount(1, $response->json()['data']);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->get("/api/v1/users?without={$company_token->user->hashed_id}");
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$this->assertCount(1, $response->json()['data']);
|
2023-08-12 04:46:46 +02:00
|
|
|
|
2023-08-12 08:21:06 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2023-11-02 02:06:34 +01:00
|
|
|
])->get("/api/v1/users?status=active&without={$company_token->user->hashed_id}");
|
2023-08-12 04:46:46 +02:00
|
|
|
|
2023-08-12 08:21:06 +02:00
|
|
|
$response->assertStatus(200);
|
|
|
|
$this->assertCount(0, $response->json()['data']);
|
2023-08-12 04:46:46 +02:00
|
|
|
|
2023-08-12 08:21:06 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2023-11-02 02:06:34 +01:00
|
|
|
])->get("/api/v1/users?status=archived&without={$company_token->user->hashed_id}");
|
2023-08-12 08:21:06 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$this->assertCount(1, $response->json()['data']);
|
2024-08-22 08:57:52 +02:00
|
|
|
|
2023-08-12 08:21:06 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2023-11-02 02:06:34 +01:00
|
|
|
])->get("/api/v1/users?status=deleted&without={$company_token->user->hashed_id}");
|
2023-08-12 04:46:46 +02:00
|
|
|
|
2023-08-12 08:21:06 +02:00
|
|
|
$response->assertStatus(200);
|
|
|
|
$this->assertCount(0, $response->json()['data']);
|
2023-08-12 04:40:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserAttemptingtToDeleteThemselves()
|
|
|
|
{
|
2023-05-09 05:37:53 +02:00
|
|
|
|
|
|
|
$account = Account::factory()->create([
|
|
|
|
'hosted_client_count' => 1000,
|
|
|
|
'hosted_company_count' => 1000,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$account->num_users = 3;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
$user = User::factory()->create([
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
'confirmation_code' => 'xyz123',
|
|
|
|
'email' => $this->faker->unique()->safeEmail(),
|
2023-08-12 04:40:41 +02:00
|
|
|
'password' => \Illuminate\Support\Facades\Hash::make('ALongAndBriliantPassword'),
|
2023-05-09 05:37:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->client_online_payment_notification = false;
|
|
|
|
$settings->client_manual_payment_notification = false;
|
|
|
|
|
|
|
|
$company = Company::factory()->create([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'settings' => $settings,
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$cu = CompanyUserFactory::create($user->id, $company->id, $account->id);
|
|
|
|
$cu->is_owner = true;
|
|
|
|
$cu->is_admin = true;
|
|
|
|
$cu->is_locked = false;
|
|
|
|
$cu->save();
|
|
|
|
|
|
|
|
$token = \Illuminate\Support\Str::random(64);
|
|
|
|
|
|
|
|
$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 = 'test token';
|
|
|
|
$company_token->token = $token;
|
|
|
|
$company_token->is_system = true;
|
2023-08-12 04:40:41 +02:00
|
|
|
$company_token->save();
|
2023-05-09 05:37:53 +02:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'ids' => [$user->hashed_id],
|
2023-08-12 04:40:41 +02:00
|
|
|
];
|
2023-05-09 05:01:27 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2023-05-09 05:37:53 +02:00
|
|
|
'X-API-TOKEN' => $token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2023-08-12 04:40:41 +02:00
|
|
|
])->postJson('/api/v1/users/bulk?action=delete', $data);
|
|
|
|
|
2023-05-09 05:01:27 +02:00
|
|
|
|
2023-08-12 04:40:41 +02:00
|
|
|
$response->assertStatus(401);
|
2023-05-09 05:01:27 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-05 03:18:10 +02:00
|
|
|
public function testDisconnectUserOauthMailer()
|
|
|
|
{
|
2023-11-26 08:41:42 +01:00
|
|
|
$user =
|
2023-04-05 03:18:10 +02:00
|
|
|
User::factory()->create([
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
'email' => $this->faker->safeEmail(),
|
|
|
|
'oauth_user_id' => '123456789',
|
|
|
|
'oauth_provider_id' => '123456789',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post("/api/v1/users/{$user->hashed_id}/disconnect_mailer");
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$user->fresh();
|
|
|
|
|
|
|
|
$this->assertNull($user->oauth_user_token);
|
|
|
|
$this->assertNull($user->oauth_user_refresh_token);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-19 00:31:48 +01:00
|
|
|
public function testUserFiltersWith()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->get('/api/v1/users?with='.$this->user->hashed_id);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
2019-06-17 01:58:33 +02:00
|
|
|
public function testUserList()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'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
|
|
|
|
2022-11-07 11:00:21 +01:00
|
|
|
public function testValidationRulesPhoneIsNull()
|
|
|
|
{
|
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
|
|
|
'email' => 'bob1@good.ole.boys.com',
|
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice',
|
|
|
|
],
|
|
|
|
'phone' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2024-08-22 08:45:06 +02:00
|
|
|
])->postJson('/api/v1/users?include=company_user', $data);
|
2022-11-07 11:00:21 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidationRulesPhoneIsBlankString()
|
|
|
|
{
|
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
|
|
|
'email' => 'bob1@good.ole.boys.com',
|
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice',
|
|
|
|
],
|
|
|
|
'phone' => "",
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2024-08-22 08:45:06 +02:00
|
|
|
])->postJson('/api/v1/users?include=company_user', $data);
|
2022-11-07 11:00:21 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$user_id = $this->decodePrimaryKey($arr['data']['id']);
|
|
|
|
$user = User::find($user_id);
|
|
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'first_name' => 'hey',
|
|
|
|
'last_name' => 'you',
|
|
|
|
'email' => 'bob1@good.ole.boys.com',
|
|
|
|
'company_user' => [
|
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice',
|
2023-08-04 10:13:26 +02:00
|
|
|
'notifications' => '',
|
2022-11-07 11:00:21 +01:00
|
|
|
],
|
|
|
|
'phone' => "",
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
|
|
|
])->putJson('/api/v1/users/'.$user->hashed_id.'?include=company_user', $data);
|
|
|
|
}
|
|
|
|
|
2019-11-22 22:10:53 +01:00
|
|
|
public function testUserStore()
|
|
|
|
{
|
2020-06-27 06:09:16 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
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' => [
|
2022-06-21 11:57:17 +02:00
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice',
|
2023-08-04 10:13:26 +02:00
|
|
|
'notifications' => '',
|
2022-06-21 11:57:17 +02:00
|
|
|
],
|
2019-11-22 22:10:53 +01:00
|
|
|
];
|
2019-06-17 01:58:33 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2024-08-22 08:45:06 +02:00
|
|
|
])->postJson('/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
|
|
|
|
2021-05-28 11:39:22 +02:00
|
|
|
public function testUserAttachAndDetach()
|
|
|
|
{
|
2021-05-29 22:50:55 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
2019-11-29 12:15:50 +01:00
|
|
|
|
2021-05-29 22:50:55 +02:00
|
|
|
$data = [
|
|
|
|
'first_name' => 'Test',
|
|
|
|
'last_name' => 'Palloni',
|
|
|
|
'email' => $this->default_email,
|
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-28 11:39:22 +02:00
|
|
|
$response = false;
|
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
$response = $this->withHeaders([
|
2021-05-28 11:39:22 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2024-08-22 08:45:06 +02:00
|
|
|
])->postJson('/api/v1/users?include=company_user', $data);
|
2024-08-22 08:57:52 +02:00
|
|
|
|
2021-05-28 11:39:22 +02:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
2021-05-29 22:50:55 +02:00
|
|
|
$arr = $response->json();
|
|
|
|
|
2021-05-28 11:39:22 +02:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2021-05-29 22:50:55 +02:00
|
|
|
])->delete('/api/v1/users/'.$arr['data']['id'].'/detach_from_company?include=company_user');
|
2021-05-28 11:39:22 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
2021-05-29 22:50:55 +02:00
|
|
|
$user_id = $this->decodePrimaryKey($arr['data']['id']);
|
2021-05-28 11:39:22 +02:00
|
|
|
|
2021-05-29 22:50:55 +02:00
|
|
|
$cu = CompanyUser::whereUserId($user_id)->whereCompanyId($this->company->id)->first();
|
|
|
|
$ct = CompanyToken::whereUserId($user_id)->whereCompanyId($this->company->id)->first();
|
|
|
|
$user = User::find($user_id);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-28 11:39:22 +02:00
|
|
|
$this->assertNull($cu);
|
|
|
|
$this->assertNull($ct);
|
|
|
|
$this->assertNotNull($user);
|
|
|
|
}
|
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 */
|
2020-10-01 12:49:47 +02:00
|
|
|
$company2 = Company::factory()->create([
|
2019-12-04 02:06:14 +01:00
|
|
|
'account_id' => $this->account->id,
|
|
|
|
]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2024-08-22 08:57:52 +02:00
|
|
|
$company_token = new CompanyToken();
|
2020-04-09 12:48:04 +02:00
|
|
|
$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);
|
2021-11-09 23:11:56 +01:00
|
|
|
$company_token->is_system = true;
|
2020-04-09 12:48:04 +02:00
|
|
|
$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*/
|
2021-05-29 22:50:55 +02:00
|
|
|
$data = [
|
|
|
|
'first_name' => 'Test',
|
|
|
|
'last_name' => 'Palloni',
|
|
|
|
'email' => $this->default_email,
|
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-04 02:06:14 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-04-09 12:48:04 +02:00
|
|
|
'X-API-TOKEN' => $company_token->token,
|
2024-08-22 08:45:06 +02:00
|
|
|
])->postJson('/api/v1/users?include=company_user', $data);
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
2020-11-13 11:42:06 +01:00
|
|
|
// $this->assertNotNull($new_user->company_user);
|
|
|
|
// $this->assertEquals($new_user->company_user->company_id, $company2->id);
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
/*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' => [
|
2022-06-21 11:57:17 +02:00
|
|
|
'is_admin' => false,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_client,create_invoice',
|
|
|
|
],
|
2019-12-04 02:06:14 +01:00
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
2024-08-22 08:45:06 +02:00
|
|
|
])->postJson('/api/v1/users?include=company_user', $data);
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
$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' => [
|
2022-06-21 11:57:17 +02:00
|
|
|
'is_admin' => true,
|
|
|
|
'is_owner' => false,
|
|
|
|
'permissions' => 'create_invoice,create_invoice',
|
|
|
|
],
|
2019-12-04 02:06:14 +01:00
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $company_token->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2024-08-22 08:45:06 +02:00
|
|
|
])->putJson('/api/v1/users/'.$this->encodePrimaryKey($user->id).'?include=company_user', $data);
|
2019-12-04 02:06:14 +01:00
|
|
|
|
|
|
|
$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
|
|
|
}
|