2020-04-09 12:48:04 +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
|
|
|
|
*/
|
2020-04-09 12:48:04 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2020-07-02 04:12:42 +02:00
|
|
|
use App\Http\Middleware\PasswordProtection;
|
|
|
|
use App\Models\CompanyToken;
|
2020-04-09 12:48:04 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2020-07-02 04:12:42 +02:00
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2020-04-09 12:48:04 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Http\Controllers\TokenController
|
|
|
|
*/
|
|
|
|
class CompanyTokenApiTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockAccountData;
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
2020-07-02 04:12:42 +02:00
|
|
|
ThrottleRequests::class,
|
2020-04-09 12:48:04 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompanyTokenList()
|
|
|
|
{
|
2020-07-02 04:12:42 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2020-04-09 12:48:04 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-07-02 04:12:42 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2020-04-09 12:48:04 +02:00
|
|
|
])->get('/api/v1/tokens');
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompanyTokenPost()
|
|
|
|
{
|
2020-07-02 04:12:42 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2020-04-09 12:48:04 +02:00
|
|
|
$data = [
|
|
|
|
'name' => $this->faker->firstName,
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-07-02 04:12:42 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2020-09-06 11:38:10 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-04-09 12:48:04 +02:00
|
|
|
])->post('/api/v1/tokens', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompanyTokenPut()
|
|
|
|
{
|
2020-07-02 04:12:42 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token = CompanyToken::whereCompanyId($this->company->id)->first();
|
|
|
|
|
|
|
|
$data = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'name' => 'newname',
|
2020-04-09 12:48:04 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-07-02 04:12:42 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2020-09-06 11:38:10 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-04-09 12:48:04 +02:00
|
|
|
])->put('/api/v1/tokens/'.$this->encodePrimaryKey($company_token->id), $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals('newname', $arr['data']['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompanyTokenGet()
|
|
|
|
{
|
2020-07-02 04:12:42 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token = CompanyToken::whereCompanyId($this->company->id)->first();
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-07-02 04:12:42 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2020-09-06 11:38:10 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-04-09 12:48:04 +02:00
|
|
|
])->get('/api/v1/tokens/'.$this->encodePrimaryKey($company_token->id));
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompanyTokenNotArchived()
|
|
|
|
{
|
2020-07-02 04:12:42 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token = CompanyToken::whereCompanyId($this->company->id)->first();
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-07-02 04:12:42 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2020-09-06 11:38:10 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-04-09 12:48:04 +02:00
|
|
|
])->get('/api/v1/tokens/'.$this->encodePrimaryKey($company_token->id));
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $arr['data']['archived_at']);
|
|
|
|
}
|
|
|
|
}
|