1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/tests/Feature/Shop/ShopInvoiceTest.php

190 lines
5.2 KiB
PHP
Raw Normal View History

2020-07-28 14:05:17 +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-07-28 14:05:17 +02:00
namespace Tests\Feature\Shop;
2020-10-01 13:34:05 +02:00
use App\Models\Product;
2020-07-28 14:05:17 +02:00
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\Middleware\ThrottleRequests;
2020-07-29 00:07:58 +02:00
use Illuminate\Validation\ValidationException;
2020-07-28 14:05:17 +02:00
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Http\Controllers\Shop\InvoiceController
*/
class ShopInvoiceTest extends TestCase
{
use MakesHash;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
$this->withoutExceptionHandling();
}
2020-07-29 00:07:58 +02:00
public function testTokenSuccess()
2020-07-28 14:05:17 +02:00
{
2020-07-29 00:07:58 +02:00
$this->company->enable_shop_api = true;
$this->company->save();
$response = null;
2020-07-28 14:05:17 +02:00
2020-07-29 00:07:58 +02:00
try {
$response = $this->withHeaders([
2020-07-28 14:05:17 +02:00
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
2020-07-29 00:07:58 +02:00
])->get('api/v1/shop/products');
} catch (ValidationException $e) {
2020-07-29 00:07:58 +02:00
$this->assertNotNull($message);
}
2020-07-28 14:05:17 +02:00
$response->assertStatus(200);
}
2020-07-29 00:07:58 +02:00
public function testTokenFailure()
2020-07-28 14:05:17 +02:00
{
2020-07-29 00:07:58 +02:00
$this->company->enable_shop_api = true;
$this->company->save();
2020-07-28 14:05:17 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
2020-07-28 14:05:17 +02:00
])->get('/api/v1/products');
$response->assertStatus(403);
$arr = $response->json();
}
2020-07-29 00:07:58 +02:00
public function testCompanyEnableShopApiBooleanWorks()
{
try {
$response = $this->withHeaders([
2020-07-29 00:07:58 +02:00
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
2020-07-29 00:07:58 +02:00
])->get('api/v1/shop/products');
} catch (ValidationException $e) {
2020-07-29 00:07:58 +02:00
$this->assertNotNull($message);
}
$response->assertStatus(403);
}
2020-07-28 14:05:17 +02:00
public function testGetByProductKey()
{
2020-07-29 00:07:58 +02:00
$this->company->enable_shop_api = true;
$this->company->save();
2020-10-15 23:45:46 +02:00
Product::truncate();
2020-10-01 13:34:05 +02:00
$product = Product::factory()->create([
2020-07-28 14:05:17 +02:00
'user_id' => $this->user->id,
'company_id' => $this->company->id,
]);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
2020-07-28 14:05:17 +02:00
])->get('/api/v1/shop/product/'.$product->product_key);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($product->hashed_id, $arr['data']['id']);
}
public function testGetByClientByContactKey()
{
2020-07-29 00:07:58 +02:00
$this->company->enable_shop_api = true;
$this->company->save();
2020-07-28 14:05:17 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
2020-07-28 14:05:17 +02:00
])->get('/api/v1/shop/client/'.$this->client->contacts->first()->contact_key);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($this->client->hashed_id, $arr['data']['id']);
}
public function testCreateClientOnShopRoute()
{
$this->company->enable_shop_api = true;
$this->company->save();
$data = [
'name' => 'ShopClient',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
])->post('/api/v1/shop/clients/', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals('ShopClient', $arr['data']['name']);
}
public function testCreateInvoiceOnShopRoute()
{
$this->company->enable_shop_api = true;
$this->company->save();
$data = [
'name' => 'ShopClient',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
])->post('/api/v1/shop/clients/', $data);
$response->assertStatus(200);
$arr = $response->json();
$client_hashed_id = $arr['data']['id'];
$invoice_data = [
'client_id' => $client_hashed_id,
'po_number' => 'shop_order',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key,
])->post('/api/v1/shop/invoices/', $invoice_data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals('shop_order', $arr['data']['po_number']);
}
2020-07-28 14:05:17 +02:00
}