Tests for system log routes

This commit is contained in:
David Bomba 2020-08-24 20:28:47 +10:00
parent d7dd544271
commit 4c3f466d58
4 changed files with 159 additions and 5 deletions

View File

@ -8,7 +8,7 @@ use App\Transformers\SystemLogTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
class SystemLogController extends Controller
class SystemLogController extends BaseController
{
use MakesHash;

View File

@ -11,10 +11,26 @@
namespace App\Models;
use App\Models\Filterable;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class SystemLog extends Model
{
use Filterable;
use SoftDeletes;
use MakesHash;
protected $casts = [
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'log' => 'array'
];
protected $dateFormat = 'Y-m-d H:i:s.u';
/* Category IDs */
const CATEGORY_GATEWAY_RESPONSE = 1;
const CATEGORY_MAIL = 2;
@ -51,10 +67,6 @@ class SystemLog extends Model
'type_id',
];
protected $casts = [
'log' => 'array'
];
public function resolveRouteBinding($value)
{
if (is_numeric($value)) {
@ -64,4 +76,14 @@ class SystemLog extends Model
return $this
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
/*
V2 type of scope
*/
public function scopeCompany($query)
{
$query->where('company_id', auth()->user()->companyId());
return $query;
}
}

View File

@ -31,6 +31,11 @@ class AddIsPublicToDocumentsTable extends Migration
$table->text('meta')->nullable();
});
Schema::table('system_logs', function (Blueprint $table) {
$table->softDeletes('deleted_at', 6);
});
}
/**

View File

@ -0,0 +1,127 @@
<?php
namespace Tests\Feature;
use App\DataMapper\DefaultSettings;
use App\Models\Account;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\SystemLog;
use App\Models\User;
use App\Utils\Traits\MakesHash;
use Faker\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Http\Controllers\SystemLogController
*/
class SystemLogApiTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
$this->makeTestData();
}
public function testSystemLogRoutes()
{
$sl = [
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'user_id' => $this->client->user_id,
'log' => 'thelog',
'category_id' => 1,
'event_id' => 1,
'type_id' => 1,
];
SystemLog::create($sl);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->get('/api/v1/system_logs');
$response->assertStatus(200);
$arr = $response->json();
$this->assertTrue(count($arr['data']) >=1);
$hashed_id = $arr['data'][0]['id'];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->get('/api/v1/system_logs/' . $hashed_id);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($hashed_id, $arr['data']['id']);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->put('/api/v1/system_logs/' . $hashed_id, $sl)->assertStatus(400);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->delete('/api/v1/system_logs/' . $hashed_id)->assertStatus(400);
}
public function testStoreRouteFails()
{
$sl = [
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'user_id' => $this->client->user_id,
'log' => 'thelog',
'category_id' => 1,
'event_id' => 1,
'type_id' => 1,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->post('/api/v1/system_logs', $sl)->assertStatus(400);
}
public function testCreateRouteFails()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->get('/api/v1/system_logs/create')->assertStatus(400);
}
}